Geen omschrijving

circusd.py 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import absolute_import
  2. import os
  3. from salt.exceptions import CommandNotFoundError
  4. def __virtual__():
  5. return True
  6. def _get_circusctl_bin(bin_env):
  7. '''
  8. Return circusctl command to call, either from a virtualenv, an argument
  9. passed in, or from the global modules options
  10. '''
  11. cmd = 'circusctl'
  12. if not bin_env:
  13. which_result = __salt__['cmd.which_bin']([cmd])
  14. if which_result is None:
  15. raise CommandNotFoundError(
  16. 'Could not find a `{0}` binary'.format(cmd)
  17. )
  18. return which_result
  19. # try to get binary from env
  20. if os.path.isdir(bin_env):
  21. cmd_bin = os.path.join(bin_env, 'bin', cmd)
  22. if os.path.isfile(cmd_bin):
  23. return cmd_bin
  24. raise CommandNotFoundError('Could not find a `{0}` binary'.format(cmd))
  25. return bin_env
  26. def _ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env):
  27. '''
  28. Return the command list to use
  29. '''
  30. ret = [_get_circusctl_bin(bin_env)]
  31. if endpoint is not None:
  32. ret += ['--endpoint', endpoint]
  33. if json:
  34. ret.append('--json')
  35. if prettify:
  36. ret.append('--prettify')
  37. if ssh is not None:
  38. ret += ['--ssh', ssh]
  39. if ssh_keyfile is not None:
  40. ret += ['--ssh_keyfile', ssh_keyfile]
  41. if timeout is not None:
  42. ret += ['--timeout', timeout]
  43. ret.append(cmd)
  44. if args is not None:
  45. ret += args
  46. return ' ' .join(ret)
  47. def _get_return(ret):
  48. if ret['retcode'] == 0:
  49. return ret['stdout']
  50. else:
  51. return ret['stderr']
  52. def _run_ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env):
  53. ret = __salt__['cmd.run_all'](
  54. _ctl_cmd(cmd, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env),
  55. python_shell=False
  56. )
  57. return _get_return(ret)
  58. def _signal_watcher(signal, name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env):
  59. args = []
  60. if name is not None:
  61. args.append(name)
  62. if waiting:
  63. args.append('--waiting')
  64. return _run_ctl_cmd(signal, args, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  65. def start(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  66. return _signal_watcher('start', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  67. def stop(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  68. return _signal_watcher('stop', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  69. def restart(name=None, waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  70. return _signal_watcher('restart', name, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  71. def reloadconfig(waiting=True, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  72. return _signal_watcher('reloadconfig', None, waiting, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  73. def status(name=None, endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  74. return _signal_watcher('status', name, False, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)
  75. def dstats(endpoint=None, json=False, prettify=False, ssh=None, ssh_keyfile=None, timeout=None, bin_env=None):
  76. return _run_ctl_cmd('dstats', None, endpoint, json, prettify, ssh, ssh_keyfile, timeout, bin_env)