from __future__ import absolute_import import os from salt.exceptions import CommandNotFoundError def __virtual__(): return True def _get_circusctl_bin(bin_env): ''' Return circusctl command to call, either from a virtualenv, an argument passed in, or from the global modules options ''' cmd = 'circusctl' if not bin_env: which_result = __salt__['cmd.which_bin']([cmd]) if which_result is None: raise CommandNotFoundError( 'Could not find a `{0}` binary'.format(cmd) ) return which_result # try to get binary from env if os.path.isdir(bin_env): cmd_bin = os.path.join(bin_env, 'bin', cmd) if os.path.isfile(cmd_bin): return cmd_bin raise CommandNotFoundError('Could not find a `{0}` binary'.format(cmd)) return bin_env def _ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env): ''' Return the command list to use ''' ret = [_get_circusctl_bin(bin_env)] if endpoint is not None: ret += ['--endpoint', endpoint] if json: ret.append('--json') if prettify: ret.append('--prettify') if ssh is not None: ret += ['--ssh', ssh] if ssh_keyfile is not None: ret += ['--ssh_keyfile', ssh_keyfile] if timeout is not None: ret += ['--timeout', timeout] ret.append(cmd) if args is not None: ret += args return ' ' .join(ret) def _get_return(ret): if ret['retcode'] == 0: return ret['stdout'] else: return ret['stderr'] def _run_ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env): ret = __salt__['cmd.run_all']( _ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env), python_shell=False ) return _get_return(ret) def _signal_watcher(signal, name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env): args = [] if name is not None: args.append(name) if waiting: args.append('--waiting') return _run_ctl_cmd(signal, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def start(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _signal_watcher('start', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def stop(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _signal_watcher('stop', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def restart(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _signal_watcher('restart', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def reloadconfig(waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _signal_watcher('reloadconfig', None, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def status(name=None, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _signal_watcher('status', name, False, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env) def dstats(endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None): return _run_ctl_cmd('dstats', None, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)