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