-
Notifications
You must be signed in to change notification settings - Fork 0
/
fab_services.py
77 lines (62 loc) · 1.75 KB
/
fab_services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
This recipe will help you to manage your services. You can specify service by
name or by index.
usage:
fab -f fab_services.py -H <hostname> status:1
fab -f fab_services.py -H <hostname> status:nginx
fab -f fab_services.py -H <hostname> status:asdf --> will show you help
"""
from fabric.api import env, task, sudo
SERVICES = {
'nginx': '/etc/init.d/nginx %s',
'postgresql': '/etc/init.d/postgresql %s',
'redis': '/etc/init.d/redis-server %s',
'supervisor': '/etc/init.d/supervisor %s',
'gunicorn': 'supervisorctl %s gunicorn',
# other services
}
SERVICES_LIST = ['nginx',
'mongo',
'postgresql',
'redis']
def _clean(service):
try:
# -1 --> we use one based indexes in console
service = SERVICES_LIST[int(service) - 1]
except (ValueError, IndexError):
pass
if service not in SERVICES:
print "Bad argument. Should be one of " + str(SERVICES.keys())
print "You can use int for service number: "
for i in enumerate(SERVICES_LIST):
print "%s) %s" % (i[0] + 1, i[1])
exit()
return service
@task
def stop(service):
"""
Stop service execution
"""
service = _clean(service)
sudo(SERVICES[service] % ("stop",))
@task
def start(service):
"""
Start service
"""
service = _clean(service)
sudo(SERVICES[service] % ("start",))
@task
def restart(service):
"""
Restart service
"""
service = _clean(service)
sudo(SERVICES[service] % ("restart",))
@task
def status(service):
"""
Check service execution status
"""
service = _clean(service)
sudo(SERVICES[service] % ("status",))