-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
166 lines (126 loc) · 4.67 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
from fabric.api import *
import json
import os
import subprocess
# Default release is 'current'
env.release = 'current'
# Environment configuration
def wh():
"""Configuration for waffle-house"""
env.user = 'root'
env.hosts = ['waffle-house.mit.edu']
env.path = '/var/www/simadmin'
env.branch = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE).stdout.read()
def sd():
"""Configuration for simmons-dev aka waffle-house"""
wh()
# Actions
def setup():
"""Setup a fresh virtualenv, checks out latest code
and install everything we need so we are read to deploy"""
run('mkdir -p %(path)s' % env)
with cd(env.path):
# Create the virtualenv
run('virtualenv .venv')
run('mkdir releases; mkdir shared;')
_clone_repo()
_checkout_latest()
_install_requirements()
def deploy():
"""Deploys the currently checkout branch"""
_checkout_latest()
_install_requirements()
_symlink_current_release()
reload()
def _clone_repo():
with cd(env.path):
run('git clone https://github.com/ammubhave/simadmin.git repository')
def _checkout_latest():
import time
env.release = time.strftime('%Y%m%d%H%M%S')
with cd(env.path):
with cd('repository'):
run('git fetch')
run('git checkout %(branch)s' % env)
run('git submodule update --init')
run('cp -R repository releases/%(release)s' % env)
run('rm -rf releases/%(release)s/.git*' % env)
def _install_requirements():
"""Install the requirements packages using pip"""
with cd(env.path):
run('.venv/bin/pip install -r releases/%(release)s/requirements.txt' % env)
def _symlink_current_release():
with cd(env.path):
with settings(warn_only=True), cd('releases'):
run('rm previous; mv current previous;' % env)
run('ln -s %(release)s current' % env)
with settings(warn_only=True):
run('rm shared/static')
run('ln -s ../releases/%(release)s/static shared/static' % env)
run('cp releases/current/conf/simadmin.conf /etc/httpd/conf.d/simadmin.conf')
def rollback():
"""Limited rollback. Swaps between the previous and current release"""
with cd(env.path), cd('releases'):
run('mv current _previous')
run('mv previous current')
run('mv _previous previous')
run('cp current/conf/simadmin.conf /etc/httpd/conf.d/simadmin.conf')
reload()
def local_sync(name):
import time
env.path = '/var/www/web_root/' + name
env.release = time.strftime('%Y%m%d%H%M%S')
with lcd(env.path):
# print 'execuring local ls'
with lcd('repository'):
local('git pull -f')
local('git submodule update --init')
local('cp -R repository releases/%(release)s' % env)
local('rm -rf releases/%(release)s/.git*' % env)
with settings(warn_only=True), lcd('releases'):
local('rm previous; mv current previous;' % env)
local('ln -s %(release)s current' % env)
local('touch /var/www/apache_config/_reload_apache_flag')
def local_add(repo, name):
if repo.startswith("\"") and repo.endswith("\""):
repo = repo[1:-1]
# Sanity checks
if os.path.exists('/var/www/web_root/' + name):
print 'ERROR: ' + '/var/www/web_root/' + name + ' already exists.'
return
if os.path.exists('/var/www/apache_config/simadmin/meta' + name + '.json'):
print 'ERROR: ' + '/var/www/apache_config/simadmin/meta' + name + '.json already exists.'
return
# All's good, let's deploy!
with lcd('/var/www/web_root'):
local('mkdir -p ' + name)
with lcd(name):
local('git clone ' + repo + ' repository')
local('mkdir -p releases')
with open('/var/www/apache_config/simadmin/meta/' + name + '.json', 'w') as f:
f.write(json.dumps({
'path': name,
'repo': repo,
}))
local_sync(name)
def local_remove(name):
# Remove all website data and configs
local('rm /var/www/apache_config/simadmin/meta/' + name + '.json')
local('rm -rf /var/www/web_root/' + name)
# Do a apache graceful
local('touch /var/www/apache_config/_reload_apache_flag')
# Apache commands
def reload():
"""Does graceful reload of apache"""
run('apachectl configtest')
run('apachectl -k graceful')
def stop():
"""CAUTION! Stops apache"""
run('apachectl -k stop')
def start():
"""Starts apache"""
run('apachectl -k start')
def restart():
"""Restarts apache (not graceful), checks for valid config first"""
run('apachectl configtest')
run('apachectl -k restart')