This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
fabfile.py
171 lines (139 loc) · 5.57 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
from contextlib import nested
from datetime import datetime
import time
from fabric.api import run, env, task, execute, runs_once, parallel
from fabric.context_managers import cd, prefix
import requests
from webapps.lib.conf import get_deploy_config
c = get_deploy_config()
env.use_ssh_config = True
config = {
'basedir': '/opt/10gen/',
'project': 'trymongo',
'origin': 'git@github.com:10gen-labs/mongo-web-shell.git',
'credentials': '/opt/10gen/{0}-{1}/shared/config.yml'
}
env.roledefs = {
'staging': ['trymongo-staging.10gen.cc'],
'prod': ['trymongo-1.10gen.cc', 'trymongo-2.10gen.cc']
}
f_credentials = c.get('flowdock_credentials')
f_user = f_credentials.get('user')
f_pass = f_credentials.get('password')
flowdock_url = 'https://api.flowdock.com/flows/10gen/tools/messages'
datetime_format = '%Y%m%d%H%M%S'
def broadcast(msg):
if f_user and f_pass:
requests.post(flowdock_url, auth=(f_user, f_pass), data={
'event': 'message',
'content': msg
})
## Wrap with runs_once to prevent this task from running multiple times per host
@runs_once
@task
def deploy(refspec):
releasedir_map = execute(load_deploy, refspec)
execute(swap_deploy, releasedir_map)
@task
def swap_deploy(releasedir_map):
## releasedir_map is a dict that maps host to releasedir
releasedir = releasedir_map[env['host']]
role = env.roles[0]
project_user = config['project'] + '-' + role
projectdir = project_user + '/'
basedir = config['basedir'] + projectdir + 'releases/'
try:
with cd(config['basedir'] + projectdir):
run('ln -sfn {0} {1}'.format(basedir+releasedir, 'current'))
run('sudo /etc/init.d/httpd reload')
broadcast('Swap successful')
except SystemExit:
with cd(basedir):
run('mkdir -p duds')
run('mv {0} duds'.format(releasedir))
@parallel
def load_deploy(refspec):
role = env.roles[0]
project_user = config['project'] + '-' + role
projectdir = project_user + '/'
releasedir = time.strftime(datetime_format)
basedir = config['basedir'] + projectdir + 'releases/'
try:
user = run('whoami')
broadcast('{0} is deploying {1} to {2}'.format(user, refspec, env['host']))
with cd(basedir):
run('mkdir {0}'.format(releasedir))
run('git clone {0} {1}'.format(config['origin'], releasedir))
with cd(basedir + releasedir):
run('git checkout {0}'.format(refspec))
run('git submodule init')
run('git submodule update')
run('virtualenv venv')
with nested(
prefix('source venv/bin/activate'),
prefix('export PIP_DOWNLOAD_CACHE={0}shared/.pip_download_cache'.format(config['basedir'] + projectdir))
):
run('pip install --allow-all-external -r requirements.txt')
run('npm cache clean')
run('npm install --production')
run('grunt build')
run('touch webapps/server/{0}'.format(role))
# Upgrade ownership and permissions
run('chgrp -R {0} .'.format(project_user))
run('chmod -R g+w .')
broadcast('Deploy download complete')
return releasedir
except SystemExit:
with cd(basedir):
run('mkdir -p duds')
run('mv {0} duds'.format(releasedir))
broadcast(':rage3: Deploy failed')
def get_basedir(role):
projectdir = config['project'] + '-' + role + '/'
return config['basedir'] + projectdir
def get_releasedir(role):
return get_basedir(role) + 'releases/'
@task
def remove_duds():
role = env.roles[0]
releasedir = get_releasedir(role)
with cd(releasedir):
run('rm -rf duds/*')
broadcast('duds/ directory emptied!')
@task
def cleanup(keep_count=3):
role = env.roles[0]
releasedir = get_releasedir(role)
with cd(releasedir):
remove_count = run("ls -1 | grep -P '\d+' | head -n -{0} | wc -l".format(keep_count))
if remove_count:
run("ls -1 | grep -P '\d+' | head -n -{0} | xargs rm -rf".format(keep_count))
broadcast('{0} release directories removed. Thank you for taking out the trash!'.format(remove_count))
@task
def rollback():
role = env.roles[0]
basedir = get_basedir(role)
with cd(basedir):
currentdir = run("readlink current | awk -F/ '{print $NF}'")
newdir = run("ls -1 releases | grep -P '\d+' | awk '$1 < {0}' | tail -n 1".format(currentdir))
if newdir:
run("ln -sfn {0} {1}".format(basedir + 'releases/' + newdir, 'current'))
run("touch {0}".format(role))
deploy_time = datetime.strptime(newdir, datetime_format)
broadcast(":construction_worker: {0} rolled back to {1} (originally deployed @ {2})".format(env['host'], newdir, deploy_time))
else:
broadcast(":rage3: nothing to roll back to")
@task
def rollforward():
role = env.roles[0]
basedir = get_basedir(role)
with cd(basedir):
currentdir = run("readlink current | awk -F/ '{print $NF}'")
newdir = run("ls -1 releases | grep -P '\d+' | awk '$1 > {0}' | head -n 1".format(currentdir))
if newdir:
run("ln -sfn {0} {1}".format(basedir + 'releases/' + newdir, 'current'))
run("touch {0}".format(role))
deploy_time = datetime.strptime(newdir, datetime_format)
broadcast(":construction_worker: {0} rolled forward to {1} (originally deployed @ {2})".format(env['host'], newdir, deploy_time))
else:
broadcast(":rage3: nothing to roll forward to")