forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·91 lines (80 loc) · 2.14 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
# see issue: https://gitlab.com/saltstack/open/salt-pkg/-/issues/19
import _pyio
import contextlib
import multiprocessing
import os
import pathlib
import sys
import salt.scripts
import salt.utils.platform
import tiamatpip.cli
import tiamatpip.configure
import tiamatpip.utils
AVAIL = (
"minion",
"master",
"call",
"api",
"cloud",
"cp",
"extend",
"key",
"proxy",
"pip",
"run",
"shell",
"spm",
"ssh",
"support",
"syndic",
)
if "TIAMAT_PIP_PYPATH" in os.environ:
PIP_PATH = pathlib.Path(os.environ["TIAMAT_PIP_PYPATH"]).resolve()
elif not sys.platform.startswith("win"):
PIP_PATH = pathlib.Path(f"{os.sep}opt", "saltstack", "salt", "pypath")
else:
PIP_PATH = pathlib.Path(os.getenv("LocalAppData"), "salt", "pypath")
with contextlib.suppress(PermissionError):
PIP_PATH.mkdir(mode=0o755, parents=True, exist_ok=True)
tiamatpip.configure.set_user_base_path(PIP_PATH)
def py_shell():
if not sys.platform.startswith("win"):
# optional, will allow Up/Down/History in the console
import readline
import code
variables = globals().copy()
variables.update(locals())
shell = code.InteractiveConsole(variables)
shell.interact()
def redirect(argv):
"""
Change the args and redirect to another salt script
"""
if len(argv) < 2:
msg = "Must pass in a salt command, available commands are:"
for cmd in AVAIL:
msg += f"\n{cmd}"
print(msg, file=sys.stderr, flush=True)
sys.exit(1)
cmd = sys.argv[1]
if cmd == "shell":
py_shell()
return
if tiamatpip.cli.should_redirect_argv(argv):
tiamatpip.cli.process_pip_argv(argv)
return
if cmd not in AVAIL:
# Fall back to the salt command
args = ["salt"]
s_fun = salt.scripts.salt_main
else:
args = [f"salt-{cmd}"]
sys.argv.pop(1)
s_fun = getattr(salt.scripts, f"salt_{cmd}")
args.extend(argv[1:])
with tiamatpip.utils.patched_sys_argv(args):
s_fun()
if __name__ == "__main__":
multiprocessing.freeze_support()
redirect(sys.argv)