Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port #49480 to master #56830

Closed
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions salt/cli/support/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
Get default scenario of the support.
"""

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