golden hour
/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils
⬆️ Go Up
Upload
File/Folder
Size
Actions
__init__.py
237 B
Del
OK
__pycache__
-
Del
OK
aggregation.py
5.17 KB
Del
OK
ansible.py
1.48 KB
Del
OK
args.py
18.33 KB
Del
OK
asynchronous.py
4.06 KB
Del
OK
atomicfile.py
5.33 KB
Del
OK
aws.py
20.37 KB
Del
OK
azurearm.py
11.42 KB
Del
OK
beacons.py
517 B
Del
OK
boto3mod.py
8.36 KB
Del
OK
boto_elb_tag.py
3.02 KB
Del
OK
botomod.py
7.98 KB
Del
OK
cache.py
11.49 KB
Del
OK
channel.py
489 B
Del
OK
cloud.py
116.3 KB
Del
OK
color.py
2.72 KB
Del
OK
compat.py
1.89 KB
Del
OK
configcomparer.py
3.88 KB
Del
OK
configparser.py
10.82 KB
Del
OK
context.py
6.8 KB
Del
OK
crypt.py
5 KB
Del
OK
ctx.py
1.42 KB
Del
OK
data.py
53.03 KB
Del
OK
dateutils.py
2.3 KB
Del
OK
debug.py
4.19 KB
Del
OK
decorators
-
Del
OK
dictdiffer.py
16.48 KB
Del
OK
dicttrim.py
3.9 KB
Del
OK
dictupdate.py
11.33 KB
Del
OK
dns.py
35.21 KB
Del
OK
doc.py
2.25 KB
Del
OK
dockermod
-
Del
OK
entrypoints.py
1.83 KB
Del
OK
environment.py
2.2 KB
Del
OK
error.py
1.18 KB
Del
OK
etcd_util.py
33.25 KB
Del
OK
event.py
52.45 KB
Del
OK
extend.py
8.87 KB
Del
OK
extmods.py
6.04 KB
Del
OK
filebuffer.py
3.15 KB
Del
OK
files.py
27.94 KB
Del
OK
find.py
22.08 KB
Del
OK
fsutils.py
3.29 KB
Del
OK
functools.py
6.02 KB
Del
OK
gitfs.py
130.41 KB
Del
OK
github.py
1.52 KB
Del
OK
gzip_util.py
2.86 KB
Del
OK
hashutils.py
5.91 KB
Del
OK
http.py
33.9 KB
Del
OK
iam.py
1.22 KB
Del
OK
icinga2.py
754 B
Del
OK
idem.py
1.22 KB
Del
OK
immutabletypes.py
2.46 KB
Del
OK
itertools.py
2.36 KB
Del
OK
jid.py
3 KB
Del
OK
jinja.py
33.92 KB
Del
OK
job.py
6.89 KB
Del
OK
json.py
3.78 KB
Del
OK
kickstart.py
41.04 KB
Del
OK
kinds.py
493 B
Del
OK
lazy.py
3.06 KB
Del
OK
listdiffer.py
10.9 KB
Del
OK
locales.py
2.06 KB
Del
OK
mac_utils.py
14.01 KB
Del
OK
mako.py
3.97 KB
Del
OK
master.py
29.7 KB
Del
OK
mattermost.py
1.77 KB
Del
OK
memcached.py
3.56 KB
Del
OK
migrations.py
1.46 KB
Del
OK
mine.py
3.68 KB
Del
OK
minion.py
4.13 KB
Del
OK
minions.py
43.38 KB
Del
OK
mount.py
1.15 KB
Del
OK
msazure.py
5.36 KB
Del
OK
msgpack.py
4.69 KB
Del
OK
nacl.py
13.65 KB
Del
OK
namecheap.py
4.32 KB
Del
OK
napalm.py
23.22 KB
Del
OK
nb_popen.py
7.24 KB
Del
OK
network.py
73.86 KB
Del
OK
nxos.py
12.94 KB
Del
OK
nxos_api.py
4 KB
Del
OK
odict.py
13.21 KB
Del
OK
openstack
-
Del
OK
oset.py
6.41 KB
Del
OK
pagerduty.py
3.03 KB
Del
OK
parsers.py
122.69 KB
Del
OK
path.py
11.24 KB
Del
OK
pbm.py
9.81 KB
Del
OK
pkg
-
Del
OK
platform.py
5.84 KB
Del
OK
powershell.py
4.15 KB
Del
OK
preseed.py
2.64 KB
Del
OK
process.py
40.76 KB
Del
OK
profile.py
3.21 KB
Del
OK
proxy.py
331 B
Del
OK
psutil_compat.py
3.63 KB
Del
OK
pushover.py
4.51 KB
Del
OK
pycrypto.py
5.41 KB
Del
OK
pydsl.py
13.74 KB
Del
OK
pyobjects.py
10.75 KB
Del
OK
reactor.py
18.99 KB
Del
OK
reclass.py
752 B
Del
OK
roster_matcher.py
3.55 KB
Del
OK
rsax931.py
8.42 KB
Del
OK
s3.py
8.78 KB
Del
OK
saltclass.py
14.27 KB
Del
OK
sanitizers.py
2.51 KB
Del
OK
schedule.py
71.81 KB
Del
OK
schema.py
54.26 KB
Del
OK
sdb.py
4.04 KB
Del
OK
slack.py
3.58 KB
Del
OK
smb.py
11.16 KB
Del
OK
smtp.py
3.27 KB
Del
OK
ssdp.py
14.75 KB
Del
OK
ssh.py
769 B
Del
OK
state.py
8.43 KB
Del
OK
stringio.py
355 B
Del
OK
stringutils.py
16.95 KB
Del
OK
systemd.py
5.51 KB
Del
OK
templates.py
24.03 KB
Del
OK
textformat.py
5.03 KB
Del
OK
thin.py
31.91 KB
Del
OK
timed_subprocess.py
4.06 KB
Del
OK
timeout.py
1.53 KB
Del
OK
timeutil.py
2.4 KB
Del
OK
url.py
5 KB
Del
OK
user.py
11.86 KB
Del
OK
validate
-
Del
OK
value.py
247 B
Del
OK
vault.py
21.74 KB
Del
OK
verify.py
25.34 KB
Del
OK
versions.py
17.17 KB
Del
OK
virt.py
3.24 KB
Del
OK
virtualbox.py
22.43 KB
Del
OK
vmware.py
129.74 KB
Del
OK
vsan.py
17.18 KB
Del
OK
vt.py
31.47 KB
Del
OK
vt_helper.py
4.4 KB
Del
OK
win_chcp.py
3.7 KB
Del
OK
win_dacl.py
95.49 KB
Del
OK
win_dotnet.py
4.74 KB
Del
OK
win_functions.py
12.69 KB
Del
OK
win_lgpo_auditpol.py
8.48 KB
Del
OK
win_lgpo_netsh.py
17.87 KB
Del
OK
win_lgpo_reg.py
16.98 KB
Del
OK
win_network.py
16.35 KB
Del
OK
win_osinfo.py
2.83 KB
Del
OK
win_pdh.py
13.85 KB
Del
OK
win_reg.py
30.82 KB
Del
OK
win_runas.py
10.53 KB
Del
OK
win_service.py
5.2 KB
Del
OK
win_system.py
14.47 KB
Del
OK
win_update.py
40.36 KB
Del
OK
winapi.py
818 B
Del
OK
x509.py
73.19 KB
Del
OK
xdg.py
316 B
Del
OK
xmlutil.py
13.91 KB
Del
OK
yaml.py
349 B
Del
OK
yamldumper.py
3.37 KB
Del
OK
yamlencoding.py
1.55 KB
Del
OK
yamllint.py
1.61 KB
Del
OK
yamlloader.py
6.04 KB
Del
OK
yamlloader_old.py
8.15 KB
Del
OK
yast.py
619 B
Del
OK
zeromq.py
1.74 KB
Del
OK
zfs.py
19.15 KB
Del
OK
Edit: pydsl.py
""" :maintainer: Jack Kuan <kjkuan@gmail.com> :maturity: new :platform: all A Python DSL for generating Salt's highstate data structure. This module is intended to be used with the `pydsl` renderer, but can also be used on its own. Here's what you can do with Salt PyDSL:: # Example translated from the online salt tutorial apache = state('apache') apache.pkg.installed() apache.service.running() \\ .watch(pkg='apache', file='/etc/httpd/conf/httpd.conf', user='apache') if __grains__['os'] == 'RedHat': apache.pkg.installed(name='httpd') apache.service.running(name='httpd') apache.group.present(gid=87).require(apache.pkg) apache.user.present(uid=87, gid=87, home='/var/www/html', shell='/bin/nologin') \\ .require(apache.group) state('/etc/httpd/conf/httpd.conf').file.managed( source='salt://apache/httpd.conf', user='root', group='root', mode=644) Example with ``include`` and ``extend``, translated from the online salt tutorial:: include('http', 'ssh') extend( state('apache').file( name='/etc/httpd/conf/httpd.conf', source='salt://http/httpd2.conf' ), state('ssh-server').service.watch(file='/etc/ssh/banner') ) state('/etc/ssh/banner').file.managed(source='salt://ssh/banner') Example of a ``cmd`` state calling a python function:: def hello(s): s = "hello world, %s" % s return dict(result=True, changes=dict(changed=True, output=s)) state('hello').cmd.call(hello, 'pydsl!') """ # Implementation note: # - There's a bit of terminology mix-up here: # - what I called a state or state declaration here is actually # an ID declaration. # - what I called a module or a state module actually corresponds # to a state declaration. # - and a state function is a function declaration. # TODOs: # # - support exclude declarations # # - allow this: # state('X').cmd.run.cwd = '/' # assert state('X').cmd.run.cwd == '/' # # - make it possible to remove: # - state declarations # - state module declarations # - state func and args # from uuid import uuid4 as _uuid from salt.state import HighState from salt.utils.odict import OrderedDict REQUISITES = set( "listen require watch prereq use listen_in require_in watch_in prereq_in use_in" " onchanges onfail".split() ) class PyDslError(Exception): pass class Options(dict): def __getattr__(self, name): return self.get(name) SLS_MATCHES = None class Sls: def __init__(self, sls, saltenv, rendered_sls): self.name = sls self.saltenv = saltenv self.includes = [] self.included_highstate = HighState.get_active().building_highstate self.extends = [] self.decls = [] self.options = Options() self.funcs = [] # track the ordering of state func declarations self.rendered_sls = rendered_sls # a set of names of rendered sls modules if not HighState.get_active(): raise PyDslError("PyDSL only works with a running high state!") @classmethod def get_all_decls(cls): return HighState.get_active()._pydsl_all_decls @classmethod def get_render_stack(cls): return HighState.get_active()._pydsl_render_stack def set(self, **options): self.options.update(options) def include(self, *sls_names, **kws): if "env" in kws: # "env" is not supported; Use "saltenv". kws.pop("env") saltenv = kws.get("saltenv", self.saltenv) if kws.get("delayed", False): for incl in sls_names: self.includes.append((saltenv, incl)) return HIGHSTATE = HighState.get_active() global SLS_MATCHES if SLS_MATCHES is None: SLS_MATCHES = HIGHSTATE.top_matches(HIGHSTATE.get_top()) highstate = self.included_highstate slsmods = [] # a list of pydsl sls modules rendered. for sls in sls_names: r_env = "{}:{}".format(saltenv, sls) if r_env not in self.rendered_sls: self.rendered_sls.add( sls ) # needed in case the starting sls uses the pydsl renderer. histates, errors = HIGHSTATE.render_state( sls, saltenv, self.rendered_sls, SLS_MATCHES ) HIGHSTATE.merge_included_states(highstate, histates, errors) if errors: raise PyDslError("\n".join(errors)) HIGHSTATE.clean_duplicate_extends(highstate) state_id = "_slsmod_{}".format(sls) if state_id not in highstate: slsmods.append(None) else: for arg in highstate[state_id]["stateconf"]: if isinstance(arg, dict) and next(iter(arg)) == "slsmod": slsmods.append(arg["slsmod"]) break if not slsmods: return None return slsmods[0] if len(slsmods) == 1 else slsmods def extend(self, *state_funcs): if self.options.ordered or self.last_func(): raise PyDslError("Cannot extend() after the ordered option was turned on!") for f in state_funcs: state_id = f.mod._state_id self.extends.append(self.get_all_decls().pop(state_id)) i = len(self.decls) for decl in reversed(self.decls): i -= 1 if decl._id == state_id: del self.decls[i] break def state(self, id=None): if not id: id = ".{}".format(_uuid()) # adds a leading dot to make use of stateconf's namespace feature. try: return self.get_all_decls()[id] except KeyError: self.get_all_decls()[id] = s = StateDeclaration(id) self.decls.append(s) return s def last_func(self): return self.funcs[-1] if self.funcs else None def track_func(self, statefunc): self.funcs.append(statefunc) def to_highstate(self, slsmod): # generate a state that uses the stateconf.set state, which # is a no-op state, to hold a reference to the sls module # containing the DSL statements. This is to prevent the module # from being GC'ed, so that objects defined in it will be # available while salt is executing the states. slsmod_id = "_slsmod_" + self.name self.state(slsmod_id).stateconf.set(slsmod=slsmod) del self.get_all_decls()[slsmod_id] highstate = OrderedDict() if self.includes: highstate["include"] = [{t[0]: t[1]} for t in self.includes] if self.extends: highstate["extend"] = extend = OrderedDict() for ext in self.extends: extend[ext._id] = ext._repr(context="extend") for decl in self.decls: highstate[decl._id] = decl._repr() if self.included_highstate: errors = [] HighState.get_active().merge_included_states( highstate, self.included_highstate, errors ) if errors: raise PyDslError("\n".join(errors)) return highstate def load_highstate(self, highstate): for sid, decl in highstate.items(): s = self.state(sid) for modname, args in decl.items(): if "." in modname: modname, funcname = modname.rsplit(".", 1) else: funcname = next(x for x in args if isinstance(x, str)) args.remove(funcname) mod = getattr(s, modname) named_args = {} for x in args: if isinstance(x, dict): k, v = next(iter(x.items())) named_args[k] = v mod(funcname, **named_args) class StateDeclaration: def __init__(self, id): self._id = id self._mods = [] def __getattr__(self, name): for m in self._mods: if m._name == name: return m m = StateModule(name, self._id) self._mods.append(m) return m __getitem__ = __getattr__ def __str__(self): return self._id def __iter__(self): return iter(self._mods) def _repr(self, context=None): return OrderedDict(m._repr(context) for m in self) def __call__(self, check=True): sls = Sls.get_render_stack()[-1] if self._id in sls.get_all_decls(): last_func = sls.last_func() if last_func and self._mods[-1]._func is not last_func: raise PyDslError( "Cannot run state({}: {}) that is required by a runtime " "state({}: {}), at compile time.".format( self._mods[-1]._name, self._id, last_func.mod, last_func.mod._state_id, ) ) sls.get_all_decls().pop(self._id) sls.decls.remove(self) self._mods[0]._func._remove_auto_require() for m in self._mods: try: sls.funcs.remove(m._func) except ValueError: pass result = HighState.get_active().state.functions["state.high"]( {self._id: self._repr()} ) if not isinstance(result, dict): # A list is an error raise PyDslError( "An error occurred while running highstate: {}".format( "; ".join(result) ) ) result = sorted(result.items(), key=lambda t: t[1]["__run_num__"]) if check: for k, v in result: if not v["result"]: import pprint raise PyDslError( "Failed executing low state at compile time:\n{}".format( pprint.pformat({k: v}) ) ) return result class StateModule: def __init__(self, name, parent_decl): self._state_id = parent_decl self._name = name self._func = None def __getattr__(self, name): if self._func: if name == self._func.name: return self._func else: if name not in REQUISITES: if self._func.name: raise PyDslError( "Multiple state functions({}) not allowed in a " "state module({})!".format(name, self._name) ) self._func.name = name return self._func return getattr(self._func, name) if name in REQUISITES: self._func = f = StateFunction(None, self) return getattr(f, name) else: self._func = f = StateFunction(name, self) return f def __call__(self, _fname, *args, **kws): return getattr(self, _fname).configure(args, kws) def __str__(self): return self._name def _repr(self, context=None): return (self._name, self._func._repr(context)) def _generate_requsite_method(t): def req(self, *args, **kws): for mod in args: self.reference(t, mod, None) for mod_ref in kws.items(): self.reference(t, *mod_ref) return self return req class StateFunction: def __init__(self, name, parent_mod): self.mod = parent_mod self.name = name self.args = [] # track the position of the auto-added require for easy # removal if run at compile time. self.require_index = None sls = Sls.get_render_stack()[-1] if sls.options.ordered: last_f = sls.last_func() if last_f: self.require(last_f.mod) self.require_index = len(self.args) - 1 sls.track_func(self) def _remove_auto_require(self): if self.require_index is not None: del self.args[self.require_index] self.require_index = None def __call__(self, *args, **kws): self.configure(args, kws) return self def _repr(self, context=None): if not self.name and context != "extend": raise PyDslError( "No state function specified for module: {}".format(self.mod._name) ) if not self.name and context == "extend": return self.args return [self.name] + self.args def configure(self, args, kws): args = list(args) if args: first = args[0] if ( self.mod._name == "cmd" and self.name in ("call", "wait_call") and callable(first) ): args[0] = first.__name__ kws = dict(func=first, args=args[1:], kws=kws) del args[1:] args[0] = dict(name=args[0]) for k, v in kws.items(): args.append({k: v}) self.args.extend(args) return self def reference(self, req_type, mod, ref): if isinstance(mod, StateModule): ref = mod._state_id elif not (mod and ref): raise PyDslError( "Invalid a requisite reference declaration! {}: {}".format(mod, ref) ) self.args.append({req_type: [{str(mod): str(ref)}]}) ns = locals() for req_type in REQUISITES: ns[req_type] = _generate_requsite_method(req_type) del ns del req_type
Save