salt/run.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3 KiB
Python
Raw Normal View History

2020-06-12 21:24:20 -06:00
#!/usr/bin/env python3
# see issue: https://gitlab.com/saltstack/open/salt-pkg/-/issues/19
import contextlib
2020-06-12 21:24:20 -06:00
import multiprocessing
import os
import pathlib
2020-06-12 21:24:20 -06:00
import sys
import _pyio
import tiamatpip.cli
import tiamatpip.configure
import tiamatpip.utils
2020-06-12 21:24:20 -06:00
import salt.scripts
import salt.utils.platform
2020-06-12 21:24:20 -06:00
AVAIL = (
"minion",
"master",
"call",
"api",
"cloud",
"cp",
"extend",
"key",
"proxy",
"pip",
"run",
"shell",
"spm",
"ssh",
"support",
"syndic",
Merge freeze into master (#62438) * fixes saltstack/salt#62372 unable to use random shuffle and sample functions as Jinja filters * move random_shuffle and random_sample logic to utils * static seed in tests seems to have shifted * static seed in tests require hash module * Change Tiamat to onedir in release notes * Reinstate known issues * Update release notes with onedir package support policy * need to check the version of Netmiko python library and then import the exceptions from different locations depending on the result. * Adding changelog. * swap out if...else for double try...except. * Remove extra fix we don't need anymore * [Docs] include onedir system python note * Update all platforms to use pycparser 2.21 or greater for Py 3.9 or higher, fixes fips fault with openssl v3.x * Remove the PyObjC dependency Signed-off-by: Pedro Algarvio <palgarvio@vmware.com> * Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime * Document usage of bundled Python runtime for Client API * Use explicit locals for custom script execution, handle exception in similar fashion as Python * Remove old __file__ replacement * Apply suggestions from code review Co-authored-by: Pedro Algarvio <pedro@algarvio.me> Co-authored-by: nicholasmhughes <nicholasmhughes@gmail.com> Co-authored-by: Alyssa Rock <alyssa.rock@gmail.com> Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com> Co-authored-by: Twangboy <leesh@vmware.com> Co-authored-by: David Murphy < dmurphy@saltstack.com> Co-authored-by: Pedro Algarvio <palgarvio@vmware.com> Co-authored-by: Lukas Raska <lukas@raska.me> Co-authored-by: Pedro Algarvio <pedro@algarvio.me>
2022-08-08 11:27:10 -06:00
"python",
2020-06-12 21:24:20 -06:00
)
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()
2020-06-12 21:24:20 -06:00
Merge freeze into master (#62438) * fixes saltstack/salt#62372 unable to use random shuffle and sample functions as Jinja filters * move random_shuffle and random_sample logic to utils * static seed in tests seems to have shifted * static seed in tests require hash module * Change Tiamat to onedir in release notes * Reinstate known issues * Update release notes with onedir package support policy * need to check the version of Netmiko python library and then import the exceptions from different locations depending on the result. * Adding changelog. * swap out if...else for double try...except. * Remove extra fix we don't need anymore * [Docs] include onedir system python note * Update all platforms to use pycparser 2.21 or greater for Py 3.9 or higher, fixes fips fault with openssl v3.x * Remove the PyObjC dependency Signed-off-by: Pedro Algarvio <palgarvio@vmware.com> * Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime * Document usage of bundled Python runtime for Client API * Use explicit locals for custom script execution, handle exception in similar fashion as Python * Remove old __file__ replacement * Apply suggestions from code review Co-authored-by: Pedro Algarvio <pedro@algarvio.me> Co-authored-by: nicholasmhughes <nicholasmhughes@gmail.com> Co-authored-by: Alyssa Rock <alyssa.rock@gmail.com> Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com> Co-authored-by: Twangboy <leesh@vmware.com> Co-authored-by: David Murphy < dmurphy@saltstack.com> Co-authored-by: Pedro Algarvio <palgarvio@vmware.com> Co-authored-by: Lukas Raska <lukas@raska.me> Co-authored-by: Pedro Algarvio <pedro@algarvio.me>
2022-08-08 11:27:10 -06:00
def python_runtime():
2022-08-11 11:14:17 +02:00
import traceback
Merge freeze into master (#62438) * fixes saltstack/salt#62372 unable to use random shuffle and sample functions as Jinja filters * move random_shuffle and random_sample logic to utils * static seed in tests seems to have shifted * static seed in tests require hash module * Change Tiamat to onedir in release notes * Reinstate known issues * Update release notes with onedir package support policy * need to check the version of Netmiko python library and then import the exceptions from different locations depending on the result. * Adding changelog. * swap out if...else for double try...except. * Remove extra fix we don't need anymore * [Docs] include onedir system python note * Update all platforms to use pycparser 2.21 or greater for Py 3.9 or higher, fixes fips fault with openssl v3.x * Remove the PyObjC dependency Signed-off-by: Pedro Algarvio <palgarvio@vmware.com> * Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime * Document usage of bundled Python runtime for Client API * Use explicit locals for custom script execution, handle exception in similar fashion as Python * Remove old __file__ replacement * Apply suggestions from code review Co-authored-by: Pedro Algarvio <pedro@algarvio.me> Co-authored-by: nicholasmhughes <nicholasmhughes@gmail.com> Co-authored-by: Alyssa Rock <alyssa.rock@gmail.com> Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com> Co-authored-by: Twangboy <leesh@vmware.com> Co-authored-by: David Murphy < dmurphy@saltstack.com> Co-authored-by: Pedro Algarvio <palgarvio@vmware.com> Co-authored-by: Lukas Raska <lukas@raska.me> Co-authored-by: Pedro Algarvio <pedro@algarvio.me>
2022-08-08 11:27:10 -06:00
# extract the absolute script path to alter sys.path and specific dunder variables
script = pathlib.Path(sys.argv[2]).expanduser().resolve()
sys.path.insert(0, str(script.parent))
# update passed args so they don't start with "<binary> python"
sys.argv[:] = sys.argv[2:]
exec_locals = {"__name__": "__main__", "__file__": str(script), "__doc__": None}
with open(script, encoding="utf-8") as rfh:
try:
exec(rfh.read(), exec_locals)
except Exception:
traceback.print_exc()
sys.exit(1)
def redirect(argv):
2020-06-12 21:24:20 -06:00
"""
Change the args and redirect to another salt script
"""
if len(argv) < 2:
2020-06-12 21:24:20 -06:00
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)
2020-06-12 21:24:20 -06:00
sys.exit(1)
cmd = sys.argv[1]
if cmd == "shell":
py_shell()
return
Merge freeze into master (#62438) * fixes saltstack/salt#62372 unable to use random shuffle and sample functions as Jinja filters * move random_shuffle and random_sample logic to utils * static seed in tests seems to have shifted * static seed in tests require hash module * Change Tiamat to onedir in release notes * Reinstate known issues * Update release notes with onedir package support policy * need to check the version of Netmiko python library and then import the exceptions from different locations depending on the result. * Adding changelog. * swap out if...else for double try...except. * Remove extra fix we don't need anymore * [Docs] include onedir system python note * Update all platforms to use pycparser 2.21 or greater for Py 3.9 or higher, fixes fips fault with openssl v3.x * Remove the PyObjC dependency Signed-off-by: Pedro Algarvio <palgarvio@vmware.com> * Add "<tiamat> python" subcommand to allow execution or arbitrary scripts via bundled Python runtime * Document usage of bundled Python runtime for Client API * Use explicit locals for custom script execution, handle exception in similar fashion as Python * Remove old __file__ replacement * Apply suggestions from code review Co-authored-by: Pedro Algarvio <pedro@algarvio.me> Co-authored-by: nicholasmhughes <nicholasmhughes@gmail.com> Co-authored-by: Alyssa Rock <alyssa.rock@gmail.com> Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com> Co-authored-by: Twangboy <leesh@vmware.com> Co-authored-by: David Murphy < dmurphy@saltstack.com> Co-authored-by: Pedro Algarvio <palgarvio@vmware.com> Co-authored-by: Lukas Raska <lukas@raska.me> Co-authored-by: Pedro Algarvio <pedro@algarvio.me>
2022-08-08 11:27:10 -06:00
if cmd == "python":
if len(argv) < 3:
msg = "Must pass script location to this command"
print(msg, file=sys.stderr, flush=True)
sys.exit(1)
python_runtime()
return
if tiamatpip.cli.should_redirect_argv(argv):
tiamatpip.cli.process_pip_argv(argv)
return
if cmd not in AVAIL:
2020-06-12 21:24:20 -06:00
# Fall back to the salt command
args = ["salt"]
2020-06-12 21:24:20 -06:00
s_fun = salt.scripts.salt_main
else:
args = [f"salt-{cmd}"]
2020-06-12 21:24:20 -06:00
sys.argv.pop(1)
s_fun = getattr(salt.scripts, f"salt_{cmd}")
args.extend(argv[1:])
with tiamatpip.utils.patched_sys_argv(args):
2021-07-27 13:11:18 -06:00
s_fun()
2020-06-12 21:24:20 -06:00
if __name__ == "__main__":
2022-06-06 11:59:52 -06:00
multiprocessing.freeze_support()
redirect(sys.argv)