forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Salt-based supportconfig. Re-implementation of Support Config Tool in Salt.
- Loading branch information
1 parent
a8d3c06
commit 86b2b46
Showing
17 changed files
with
1,738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# coding=utf-8 | ||
""" | ||
Get default scenario of the support. | ||
""" | ||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
import logging | ||
import os | ||
|
||
import jinja2 | ||
import salt.exceptions | ||
import yaml | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _render_profile(path, caller, runner): | ||
""" | ||
Render profile as Jinja2. | ||
:param path: | ||
:return: | ||
""" | ||
env = jinja2.Environment( | ||
loader=jinja2.FileSystemLoader(os.path.dirname(path)), trim_blocks=False | ||
) | ||
return ( | ||
env.get_template(os.path.basename(path)) | ||
.render(salt=caller, runners=runner) | ||
.strip() | ||
) | ||
|
||
|
||
def get_profile(profile, caller, runner): | ||
""" | ||
Get profile. | ||
:param profile: | ||
:return: | ||
""" | ||
profiles = profile.split(",") | ||
data = {} | ||
for profile in profiles: | ||
if os.path.basename(profile) == profile: | ||
profile = profile.split(".")[0] # Trim extension if someone added it | ||
profile_path = os.path.join( | ||
os.path.dirname(__file__), "profiles", profile + ".yml" | ||
) | ||
else: | ||
profile_path = profile | ||
if os.path.exists(profile_path): | ||
try: | ||
rendered_template = _render_profile(profile_path, caller, runner) | ||
log.trace( | ||
"\n%(d)s\n%(t)s\n%(d)s\n", {"d": "-" * 80, "t": rendered_template} | ||
) | ||
data.update(yaml.load(rendered_template)) | ||
except Exception as ex: | ||
log.debug(ex, exc_info=True) | ||
raise salt.exceptions.SaltException( | ||
"Rendering profile failed: {}".format(ex) | ||
) | ||
else: | ||
raise salt.exceptions.SaltException( | ||
'Profile "{}" is not found.'.format(profile) | ||
) | ||
|
||
return data | ||
|
||
|
||
def get_profiles(config): | ||
""" | ||
Get available profiles. | ||
:return: | ||
""" | ||
profiles = [] | ||
for profile_name in os.listdir(os.path.join(os.path.dirname(__file__), "profiles")): | ||
if profile_name.endswith(".yml"): | ||
profiles.append(profile_name.split(".")[0]) | ||
|
||
return sorted(profiles) |
Oops, something went wrong.