salt/tests/saltsh.py

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

135 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
'''
Welcome to the Salt repl which exposes the execution environment of a minion in
a pre-configured Python shell
__opts__, __salt__, __grains__, and __pillar__ are available.
Jinja can be tested with full access to the above structures in the usual way:
JINJA("""\\
I am {{ salt['cmd.run']('whoami') }}.
{% if otherstuff %}
Some other stuff here
{% endif %}
""", otherstuff=True)
A history file is maintained in ~/.saltsh_history.
completion behavior can be customized via the ~/.inputrc file.
'''
2015-01-07 01:15:56 +00:00
# pylint: disable=file-perms
2013-12-11 10:43:51 +00:00
2020-04-02 20:10:20 -05:00
import atexit
2021-04-01 16:29:01 -07:00
import builtins
import os
import pprint # pylint: disable=unused-import
import readline
import sys
from code import InteractiveConsole
2013-12-11 10:43:51 +00:00
import jinja2
import salt.client
import salt.config
import salt.loader
import salt.output
import salt.pillar
import salt.runner
# pylint: disable=unused-import
2014-11-22 10:44:46 +00:00
# These are imported to be available in the spawned shell
import salt.utils.yaml
2020-04-02 20:10:20 -05:00
# pylint: enable=unused-import
2013-12-11 10:43:51 +00:00
HISTFILE = "{HOME}/.saltsh_history".format(**os.environ)
2013-12-11 09:05:36 +00:00
def savehist():
2020-04-02 20:10:20 -05:00
"""
2013-12-11 10:43:51 +00:00
Save the history file
2020-04-02 20:10:20 -05:00
"""
readline.write_history_file(HISTFILE)
2013-12-11 09:05:36 +00:00
def get_salt_vars():
2020-04-02 20:10:20 -05:00
"""
Return all the Salt-usual double-under data structures for a minion
2020-04-02 20:10:20 -05:00
"""
# Create the Salt __opts__ variable
__opts__ = salt.config.client_config(
os.environ.get("SALT_MINION_CONFIG", "/etc/salt/minion")
2020-04-02 20:10:20 -05:00
)
# Populate grains if it hasn't been done already
if "grains" not in __opts__ or not __opts__["grains"]:
__opts__["grains"] = salt.loader.grains(__opts__)
# file_roots and pillar_roots should be set in the minion config
if "file_client" not in __opts__ or not __opts__["file_client"]:
__opts__["file_client"] = "local"
2014-08-07 14:08:50 -04:00
# ensure we have a minion id
if "id" not in __opts__ or not __opts__["id"]:
__opts__["id"] = "saltsh_mid"
# Populate template variables
__salt__ = salt.loader.minion_mods(__opts__)
__grains__ = __opts__["grains"]
2014-08-07 14:08:50 -04:00
if __opts__["file_client"] == "local":
__pillar__ = salt.pillar.get_pillar(
2021-08-03 07:25:24 +01:00
__opts__,
__grains__,
__opts__.get("id"),
__opts__.get("saltenv"),
2014-08-07 14:08:50 -04:00
).compile_pillar()
else:
__pillar__ = {}
2019-12-04 15:02:23 +00:00
# pylint: disable=invalid-name,unused-variable,possibly-unused-variable
def JINJA(x, **y):
return jinja2.Template(x).render(
grains=__grains__, salt=__salt__, opts=__opts__, pillar=__pillar__, **y
)
# pylint: enable=invalid-name,unused-variable,possibly-unused-variable
return locals()
2013-12-11 09:05:36 +00:00
def main():
2020-04-02 20:10:20 -05:00
"""
2013-12-11 10:43:51 +00:00
The main entry point
2020-04-02 20:10:20 -05:00
"""
salt_vars = get_salt_vars()
def salt_outputter(value):
2020-04-02 20:10:20 -05:00
"""
Use Salt's outputters to print values to the shell
2020-04-02 20:10:20 -05:00
"""
if value is not None:
2014-11-22 10:44:46 +00:00
builtins._ = value
salt.output.display_output(value, "", salt_vars["__opts__"])
sys.displayhook = salt_outputter
# Set maximum number of items that will be written to the history file
readline.set_history_length(300)
if os.path.exists(HISTFILE):
readline.read_history_file(HISTFILE)
atexit.register(savehist)
2013-12-11 10:43:51 +00:00
atexit.register(lambda: sys.stdout.write("Salt you later!\n"))
saltrepl = InteractiveConsole(locals=salt_vars)
saltrepl.interact(banner=__doc__)
2013-12-11 09:05:36 +00:00
if __name__ == "__main__":
main()