This repository was archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.py
83 lines (55 loc) · 1.83 KB
/
app.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
78
79
80
81
82
83
import re
from fabric.api import abort, sudo, task, run
@task
def restart(app):
"""Restart a particular app"""
_service(app, 'restart')
@task
def reload(app):
"""Reload a particular app.
Unlike `restart`, this will tell the app to reload itself gracefully; for
apps running under unicornherder, this will spin up a new process, then
stop the old one after a short overlap period.
"""
_service(app, 'reload')
@task
def stop(app):
"""Stop a particular app"""
_service(app, 'stop')
@task
def start(app):
"""Start a particular app"""
_service(app, 'start')
@task
def status(app):
"""Check status of a particular app"""
_service(app, 'status')
def _service(app, command):
sudo('service {} {}'.format(app, command))
@task
def respawn_large_unicorns(app):
"""Gracefully kill the unicorn worker using the most RAM for a particular app"""
master_pid = run('cat /var/run/{}/app.pid'.format(app))
if not re.match('^unicorn master', run('cat /proc/{}/cmdline'.format(master_pid))):
abort('App not running unicorn')
sudo("kill -QUIT $(ps -o rss,pid --ppid %s --sort +rss | tail -n1 | awk '{print $2}')" % master_pid)
def env_filename(app, name):
return '/etc/govuk/{app}/env.d/{name}'.format(app=app, name=name)
@task
def setenv(app, name, value):
"""
Set an environment variable for an application.
Note: this does not restart the application or any relevant other services.
"""
sudo('echo -n \'{value}\' > {filename}'.format(
value=value, filename=env_filename(app, name)
))
@task
def rmenv(app, name):
"""
Remove an environment variable for an application.
Note: this does not restart the application or any relevant other services.
"""
sudo('rm {filename}'.format(
filename=env_filename(app, name)
))