Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Forked from whiteinge/saltshell.py
Created September 7, 2013 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SEJeff/6472812 to your computer and use it in GitHub Desktop.
Save SEJeff/6472812 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
Drop a user into a Python shell preconfigured with a Salt environment
'''
import atexit
import atexit
import os
import pprint
import readline
import rlcompleter
import sys
from code import InteractiveConsole
import jinja2
import salt.client
import salt.config
import salt.loader
import salt.pillar
import salt.runner
HISTFILE="{HOME}/.salt_history".format(**os.environ)
# Read the existing history if there is one
if os.path.exists(HISTFILE):
readline.read_history_file(HISTFILE)
# Set maximum number of items that will be written to the history file
readline.set_history_length(300)
def savehist():
readline.write_history_file(HISTFILE)
atexit.register(savehist)
# Create the Salt __opts__ variable
__opts__ = salt.config.client_config(
os.environ.get('SALT_MINION_CONFIG', '/etc/salt/minion'))
# Populate grains if it hasn't been done already
if not 'grains' in __opts__ or not __opts__['grains']:
__opts__['grains'] = salt.loader.grains(__opts__)
# Populate template variables
__salt__ = salt.loader.minion_mods(__opts__)
__grains__ = __opts__['grains']
__pillar__ = salt.pillar.get_pillar(
__opts__,
__grains__,
__opts__['id'],
__opts__.get('environment'),
).compile_pillar()
# Instantiate LocalClient and RunnerClient
SLC = salt.client.LocalClient()
SRUN = salt.runner.Runner(__opts__)
JINJA = lambda x, **y: jinja2.Template(x).render(
grains=__grains__,
salt=__salt__,
opts=__opts__,
pillar=__pillar__,
**y)
WELCOME = '''\
Welcome to the Salt repl
'''
atexit.register(lambda: sys.stdout.write('''\
Byeee.
'''))
SALTREPL = InteractiveConsole(locals=locals())
SALTREPL.interact(banner=WELCOME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment