/
fabfile.py
174 lines (138 loc) · 4.91 KB
/
fabfile.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from fabric.api import cd
from fabric.api import env
from fabric.api import hide
from fabric.api import lcd
from fabric.api import local
from fabric.api import put
from fabric.api import run
from fabric.api import sudo
# apt-get install sudo
# #adduser <username> sudo
# echo '<username> ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers
# fab -H <host> -u <username> <command>
# once: os_upgrade debian install config restart
# next: update reload
NAME = 'mysite'
ETC_PATH = '/usr/local/etc/' + NAME
LIB_PATH = '/usr/local/lib/' + NAME
USER = env['user']
PY2 = False
def update():
name = get_name()
filename = name + '.tar.gz'
with lcd('dist'), cd(LIB_PATH):
put(filename, env.cwd)
run('rm -rf ' + name)
run('tar xzf ' + filename)
run('rm ' + filename)
with cd(name):
if PY2:
run('python2 -m virtualenv env')
else:
run('python3 -m venv env')
run('env/bin/pip install -e . pylibmc')
sudo('cp content/maintenance.html /usr/share/nginx/www')
run('rm -f current && ln -s %s current' % name)
def config():
with cd('%s/etc/' % CURRENT_PATH):
run('cp *.conf *.ini %s' % ETC_PATH)
# sysctl
with cd('%s/etc/' % CURRENT_PATH):
run('cp *.conf *.ini %s' % ETC_PATH)
with cd('/etc/sysctl.d'):
sudo('rm -f boc.conf')
sudo('ln -s %s/sysctl.conf boc.conf' % ETC_PATH)
sudo('sysctl -f boc.conf')
# nginx
with cd('/etc/nginx/sites-available'):
sudo('rm -f %s.conf' % NAME)
sudo('ln -s %s/nginx.conf %s.conf' % (ETC_PATH, NAME))
with cd('/etc/nginx/sites-enabled'):
sudo('rm -f %s.conf' % NAME)
sudo('ln -s /etc/nginx/sites-available/%s.conf' % NAME)
sudo('rm -f /etc/nginx/sites-enabled/default')
sudo('/usr/sbin/nginx -s quit && sleep 5')
sudo('/etc/init.d/uwsgi stop')
sudo('/etc/init.d/nginx start')
# memcached
sudo('/etc/init.d/memcached stop')
with cd('/etc'):
sudo('rm -f memcached.conf')
sudo('ln -s %s/memcached.conf' % ETC_PATH)
sudo('/etc/init.d/memcached start')
# uwsgi
with cd('/etc/uwsgi/apps-available'):
sudo('rm -f %s.ini' % NAME)
sudo('ln -s %s/config.ini %s.ini' % (ETC_PATH, NAME))
with cd('/etc/uwsgi/apps-enabled'):
sudo('rm -f %s.ini' % NAME)
sudo('ln -s /etc/uwsgi/apps-available/%s.ini' % NAME)
sudo('/etc/init.d/uwsgi start')
def install():
sudo('mkdir -p %s %s' % (ETC_PATH, LIB_PATH))
sudo('chown -R %s %s %s' % (USER, ETC_PATH, LIB_PATH))
update()
def uninstall():
sudo('rm -rf ' + LIB_PATH)
def purge():
uninstall()
sudo('rm -rf ' + ETC_PATH)
def reload():
sudo('/etc/init.d/uwsgi reload')
def start():
sudo('/etc/init.d/memcached start')
sudo('/etc/init.d/uwsgi start')
def stop():
sudo('/usr/sbin/nginx -s quit && sleep 5')
sudo('/etc/init.d/nginx stop')
sudo('/etc/init.d/uwsgi stop')
sudo('/etc/init.d/nginx start')
sudo('/etc/init.d/memcached stop')
def restart():
stop()
start()
def flush_cache():
sudo('echo flush_all | socat unix-connect:/var/tmp/memcached-%s.sock -'
% NAME)
def cache_stats():
with hide('output', 'running'):
stats = sudo('echo stats | socat unix-connect:'
'/var/tmp/memcached-%s.sock -' % NAME)
items = dict((r[1], r[2].strip()) for r in [i.split(' ')
for i in stats.split('\n')[:-1]])
c = float(items['cmd_get'])
h = c and 100.0 * float(items['get_hits']) / c or 0.0
print('Cache: %.2f%% hit rate, %.2fM gets, %.2fG read, %.2f days' %
(h, c / 1e6,
float(items['bytes_read']) / pow(1024, 3),
float(items['uptime']) / 86400.0))
def debian():
sudo('apt-get -dqq update')
packages = 'build-essential ntpdate gettext libgmp3-dev nginx-full ' + \
'uwsgi libmemcached-dev libz-dev memcached mailutils socat '
if PY2:
packages += 'python2.7 python2.7-dev python-virtualenv ' + \
'uwsgi-plugin-python'
else:
packages += 'python3 python3-dev python3-venv uwsgi-plugin-python3'
sudo('apt-get --no-install-recommends -yq install ' + packages)
sudo('/etc/init.d/uwsgi start')
sudo('/etc/init.d/nginx start')
sudo('/etc/init.d/memcached start')
sudo('apt-get -q clean')
sudo('ntpdate pool.ntp.org')
def os_upgrade():
sudo('apt-get -dqq update')
sudo('apt-get -yq upgrade')
sudo('apt-get -q clean; apt-get -q autoclean; apt-get -q autoremove')
# region: internal details
def get_name():
revision = local("git rev-list --count HEAD", capture=True)
assert int(revision) >= 0
name = local('python setup.py --fullname', capture=True)
return name + '.' + revision
CURRENT_PATH = LIB_PATH + '/current'
env.colorize_errors = True
env.key_filename = '~/.ssh/id_rsa'
env.ssh_config_path = '~/.ssh/config'
env.use_ssh_config = True