Skip to content

Commit

Permalink
Port saltstack#49480 to master
Browse files Browse the repository at this point in the history
Salt-based supportconfig. Re-implementation of Support Config Tool in
Salt.
  • Loading branch information
Bo Maryniuk authored and DmitryKuzmenko committed Apr 28, 2020
1 parent a8d3c06 commit 86b2b46
Show file tree
Hide file tree
Showing 17 changed files with 1,738 additions and 0 deletions.
81 changes: 81 additions & 0 deletions salt/cli/support/__init__.py
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)
Loading

0 comments on commit 86b2b46

Please sign in to comment.