def clone(cwd, repository, opts=None, user=None, identity=None): ''' Clone a new repository cwd The path to the Mercurial repository repository The hg URI of the repository opts : None Any additional options to add to the command line user : None Run hg as a user other than what the minion runs as identity : None Private SSH key on the minion server for authentication (ssh://) .. versionadded:: 2015.5.0 CLI Example: .. code-block:: bash salt '*' hg.clone /path/to/repo https://bitbucket.org/birkenfeld/sphinx ''' _check_hg() cmd = ['hg', 'clone', '{0}'.format(repository), '{0}'.format(cwd)] if opts: for opt in opts.split(): cmd.append('{0}'.format(opt)) if identity: cmd.append(_ssh_flag(identity)) return __salt__['cmd.run'](cmd, runas=user, python_shell=False, use_vt=not utils.is_windows())
def clone(cwd, repository, opts=None, user=None, identity=None, https_user=None, https_pass=None): ''' Clone a new repository cwd The path to the Git repository repository The git URI of the repository opts : None Any additional options to add to the command line user : None Run git as a user other than what the minion runs as identity : None A path to a private key to use over SSH https_user : None HTTP Basic Auth username for HTTPS (only) clones .. versionadded:: 20515.5.0 https_pass : None HTTP Basic Auth password for HTTPS (only) clones .. versionadded:: 2015.5.0 CLI Example: .. code-block:: bash salt '*' git.clone /path/to/repo git://github.com/saltstack/salt.git salt '*' git.clone /path/to/repo.git\\ git://github.com/saltstack/salt.git '--bare --origin github' ''' _check_git() repository = _add_http_basic_auth(repository, https_user, https_pass) if not opts: opts = '' if utils.is_windows(): cmd = 'git clone {0} {1} {2}'.format(repository, cwd, opts) else: cmd = 'git clone {0} {1!r} {2}'.format(repository, cwd, opts) return _git_run(cmd, runas=user, identity=identity)
def key_exists(): if utils.is_windows(): permanent_hive = 'HKCU' permanent_key = 'Environment' if permanent == 'HKLM': permanent_hive = 'HKLM' permanent_key = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' out = __salt__['reg.read_value'](permanent_hive, permanent_key, key) return out['success'] is True else: return False
def _wait_caller(self, opts): ''' Returns when RAET Minion Yard is available ''' yardname = 'manor' dirpath = opts['sock_dir'] role = opts.get('id') if not role: emsg = ("Missing role required to setup RAET SaltCaller.") log.error(emsg + "\n") raise ValueError(emsg) kind = opts.get('__role') # application kind 'master', 'minion', etc if kind not in kinds.APPL_KINDS: emsg = ("Invalid application kind = '{0}' for RAET SaltCaller.".format(kind)) log.error(emsg + "\n") raise ValueError(emsg) if kind in [kinds.APPL_KIND_NAMES[kinds.applKinds.minion], kinds.APPL_KIND_NAMES[kinds.applKinds.caller], ]: lanename = "{0}_{1}".format(role, kind) else: emsg = ("Unsupported application kind '{0}' for RAET SaltCaller.".format(kind)) log.error(emsg + '\n') raise ValueError(emsg) ha, dirpath = Yard.computeHa(dirpath, lanename, yardname) if is_windows(): # RAET lanes do not use files on Windows. Need to use win32file # API to check for existence. exists = False while not exists: try: f = win32file.CreateFile( ha, win32file.GENERIC_WRITE | win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None) win32file.CloseHandle(f) exists = True except win32file.error: time.sleep(0.1) else: while not ((os.path.exists(ha) and not os.path.isfile(ha) and not os.path.isdir(ha))): time.sleep(0.1) time.sleep(0.5)
def setup_multiprocessing_logging(queue=None): ''' This code should be called from within a running multiprocessing process instance. ''' from salt.utils import is_windows global __MP_LOGGING_CONFIGURED global __MP_LOGGING_QUEUE_HANDLER if __MP_IN_MAINPROCESS is True and not is_windows(): # We're in the MainProcess, return! No multiprocessing logging setup shall happen # Windows is the exception where we want to set up multiprocessing # logging in the MainProcess. return try: logging._acquireLock() # pylint: disable=protected-access if __MP_LOGGING_CONFIGURED is True: return # Let's set it to true as fast as possible __MP_LOGGING_CONFIGURED = True if __MP_LOGGING_QUEUE_HANDLER is not None: return # The temp null and temp queue logging handlers will store messages. # Since noone will process them, memory usage will grow. If they # exist, remove them. __remove_null_logging_handler() __remove_queue_logging_handler() # Let's add a queue handler to the logging root handlers __MP_LOGGING_QUEUE_HANDLER = SaltLogQueueHandler(queue or get_multiprocessing_logging_queue()) logging.root.addHandler(__MP_LOGGING_QUEUE_HANDLER) # Set the logging root level to the lowest to get all messages logging.root.setLevel(logging.GARBAGE) logging.getLogger(__name__).debug( 'Multiprocessing queue logging configured for the process running ' 'under PID: {0}'.format(os.getpid()) ) # The above logging call will create, in some situations, a futex wait # lock condition, probably due to the multiprocessing Queue's internal # lock and semaphore mechanisms. # A small sleep will allow us not to hit that futex wait lock condition. time.sleep(0.0001) finally: logging._releaseLock() # pylint: disable=protected-access
def action(self): ''' Pull the queue for functions to execute ''' while self.fun.value: msg = self.fun.value.popleft() data = msg.get('pub') match = getattr( self.matcher.value, '{0}_match'.format( data.get('tgt_type', 'glob') ) )(data['tgt']) if not match: continue if 'user' in data: log.info( 'User {0[user]} Executing command {0[fun]} with jid ' '{0[jid]}'.format(data)) else: log.info( 'Executing command {0[fun]} with jid {0[jid]}'.format(data) ) log.debug('Command details {0}'.format(data)) if is_windows(): # SaltRaetNixJobber is not picklable. Pickling is necessary # when spawning a process in Windows. Since the process will # be spawned and joined on non-Windows platforms, instead of # this, just run the function directly and absorb any thrown # exceptions. try: self.proc_run(msg) except Exception as exc: log.error( 'Exception caught by jobber: {0}'.format(exc), exc_info=True) else: process = multiprocessing.Process( target=self.proc_run, kwargs={'msg': msg} ) process.start() process.join()
PYTHON_CMDS="python27 python2.7 python26 python2.6 python2 python" for py_cmd in $PYTHON_CMDS do if "$py_cmd" -c "import sys; sys.exit(not sys.hexversion >= 0x02060000);" >/dev/null 2>&1 then py_cmd_path=`"$py_cmd" -c 'import sys; print sys.executable;'` exec $SUDO "$py_cmd_path" -c 'exec """{{SSH_PY_CODE}}""".decode("base64")' exit 0 else continue fi done echo "ERROR: Unable to locate appropriate python command" >&2 exit $EX_PYTHON_OLD EOF'''.format( EX_THIN_PYTHON_OLD=salt.defaults.exitcodes.EX_THIN_PYTHON_OLD, ) if not is_windows(): shim_file = os.path.join(os.path.dirname(__file__), 'ssh_py_shim.py') if not os.path.exists(shim_file): # On esky builds we only have the .pyc file shim_file += "c" with salt.utils.fopen(shim_file) as ssh_py_shim: SSH_PY_SHIM = ssh_py_shim.read() log = logging.getLogger(__name__) class SSH(object): ''' Create an SSH execution system ''' def __init__(self, opts):
# -*- coding: utf-8 -*- ''' Support for the Mercurial SCM ''' # Import salt libs from salt import utils from salt.modules import state_std if utils.is_windows(): hg_binary = "hg.exe" else: hg_binary = "hg" def _check_hg(): utils.check_or_die(hg_binary) def revision(cwd, rev='tip', short=False, user=None, **kwargs): ''' Returns the long hash of a given identifier (hash, branch, tag, HEAD, etc) cwd The path to the Mercurial repository rev: tip The revision short: False Return an abbreviated commit hash
import salt.utils.jid import salt.defaults.exitcodes from salt.log import LOG_LEVELS from salt.utils import is_windows from salt.utils import print_cli from salt.utils import kinds from salt.utils import activate_profile from salt.utils import output_profile from salt.cli import daemons try: from raet import raeting, nacling from raet.lane.stacking import LaneStack from raet.lane.yarding import RemoteYard, Yard if is_windows(): import win32file except ImportError: # Don't die on missing transport libs since only one transport is required pass # Import 3rd-party libs import salt.ext.six as six # Custom exceptions from salt.exceptions import ( SaltClientError, CommandNotFoundError, CommandExecutionError, SaltInvocationError,
def pull(cwd, opts=None, user=None, identity=None, repository=None): ''' Perform a pull on the given repository cwd The path to the Mercurial repository repository : None Perform pull from the repository different from .hg/hgrc:[paths]:default opts : None Any additional options to add to the command line user : None Run hg as a user other than what the minion runs as identity : None Private SSH key on the minion server for authentication (ssh://) .. versionadded:: 2015.5.0 CLI Example: .. code-block:: bash salt '*' hg.pull /path/to/repo opts=-u ''' _check_hg() cmd = ['hg', 'pull'] if identity: cmd.append(_ssh_flag(identity)) if opts: for opt in opts.split(): cmd.append(opt) if repository is not None: cmd.append(repository) return __salt__['cmd.run'](cmd, cwd=cwd, runas=user, python_shell=False, use_vt=not utils.is_windows())
# -*- coding: utf-8 -*- ''' Support for the Mercurial SCM ''' # Import salt libs from salt import utils if utils.is_windows(): hg_binary = 'hg.exe' else: hg_binary = 'hg' def _check_hg(): utils.check_or_die(hg_binary) def revision(cwd, rev='tip', short=False, user=None): ''' Returns the long hash of a given identifier (hash, branch, tag, HEAD, etc) cwd The path to the Mercurial repository rev: tip The revision short: False Return an abbreviated commit hash
exec $SUDO "$py_cmd_path" -c \ 'import base64; exec(base64.b64decode("""{{SSH_PY_CODE}}""").decode("utf-8"))' fi exit 0 else continue fi done echo "ERROR: Unable to locate appropriate python command" >&2 exit $EX_PYTHON_INVALID EOF'''.format( EX_THIN_PYTHON_INVALID=salt.defaults.exitcodes.EX_THIN_PYTHON_INVALID, ).split('\n')]) if not is_windows(): shim_file = os.path.join(os.path.dirname(__file__), 'ssh_py_shim.py') if not os.path.exists(shim_file): # On esky builds we only have the .pyc file shim_file += "c" with salt.utils.fopen(shim_file) as ssh_py_shim: SSH_PY_SHIM = ssh_py_shim.read() log = logging.getLogger(__name__) class SSH(object): ''' Create an SSH execution system ''' def __init__(self, opts):
import salt.defaults.exitcodes from salt.log import LOG_LEVELS from salt.utils import is_windows from salt.utils import print_cli from salt.utils import kinds from salt.utils import activate_profile from salt.utils import output_profile from salt.utils.process import MultiprocessingProcess from salt.cli import daemons try: from raet import raeting, nacling from raet.lane.stacking import LaneStack from raet.lane.yarding import RemoteYard, Yard if is_windows(): import win32file except ImportError: # Don't die on missing transport libs since only one transport is required pass # Import 3rd-party libs import salt.ext.six as six # Custom exceptions from salt.exceptions import ( SaltClientError, CommandNotFoundError, CommandExecutionError, SaltInvocationError,
def setval(key, val, false_unsets=False, permanent=False): ''' Set a single salt process environment variable. Returns True on success. key The environment key to set. Must be a string. val The value to set. Must be a string or False. Refer to the 'false_unsets' parameter for behavior when set to False. false_unsets If val is False and false_unsets is True, then the key will be removed from the salt processes environment dict entirely. If val is False and false_unsets is not True, then the key's value will be set to an empty string. Default: False. permanent On Windows minions this will set the environment variable in the registry so that it is always added as a environment variable when applications open. If you want to set the variable to HKLM instead of HKCU just pass in "HKLM" for this parameter. On all other minion types this will be ignored. Note: This will only take affect on applications opened after this has been set. CLI Example: .. code-block:: bash salt '*' environ.setval foo bar salt '*' environ.setval baz val=False false_unsets=True salt '*' environ.setval baz bar permanent=True salt '*' environ.setval baz bar permanent=HKLM ''' is_windows = utils.is_windows() if is_windows: permanent_hive = 'HKCU' permanent_key = 'Environment' if permanent == 'HKLM': permanent_hive = 'HKLM' permanent_key = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' if not isinstance(key, six.string_types): log.debug( '{0}: \'key\' argument is not a string type: \'{1}\'' .format(__name__, key) ) if val is False: if false_unsets is True: try: os.environ.pop(key, None) if permanent and is_windows: __salt__['reg.delete_value'](permanent_hive, permanent_key, key) return None except Exception as exc: log.error( '{0}: Exception occurred when unsetting ' 'environ key \'{1}\': \'{2}\'' .format(__name__, key, exc) ) return False else: val = '' if isinstance(val, six.string_types): try: os.environ[key] = val if permanent and is_windows: __salt__['reg.set_value'](permanent_hive, permanent_key, key, val) return os.environ[key] except Exception as exc: log.error( '{0}: Exception occurred when setting' 'environ key \'{1}\': \'{2}\'' .format(__name__, key, exc) ) return False else: log.debug( '{0}: \'val\' argument for key \'{1}\' is not a string ' 'or False: \'{2}\'' .format(__name__, key, val) ) return False