-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
187 lines (164 loc) · 4.04 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
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
from fabric.api import *
env.user = "ubuntu"
env.use_ssh_config = True
env.note = ""
env.newrelic_application_id = ""
"""
Environments
"""
def production():
"""
Work on production environment
"""
env.requireNote = True;
env.settings = 'production'
env.hosts = [
'prod-api'
]
env.newrelic_application_id = "3874131"
def integration():
"""
Work on staging environment
"""
env.requireNote = False;
env.settings = 'integration'
env.hosts = [
'int-api'
]
def staging():
"""
Work on staging environment
"""
env.requireNote = False;
env.settings = 'staging'
env.hosts = [
'stage-api'
]
"""
Branches
"""
def stable():
"""
Work on stable branch.
"""
env.branch = 'stable'
def master():
"""
Work on development branch.
"""
env.branch = 'master'
def branch(branch_name):
"""
Work on any specified branch.
"""
env.branch = branch_name
"""
Commands - setup
"""
def setup():
"""
Install and start the server.
"""
require('settings', provided_by=[production, integration, staging])
require('branch', provided_by=[stable, master, branch])
clone_repo()
checkout_latest()
install_requirements()
boot()
def clone_repo():
"""
Do initial clone of the git repository.
"""
run('git clone https://github.com/CodeNow/api-server.git')
def checkout_latest():
"""
Pull the latest code on the specified branch.
"""
with cd('api-server'):
run('git config --global credential.helper cache')
run('git fetch --all')
run('git reset --hard origin/%(branch)s' % env)
def install_requirements():
"""
Install the required packages using npm.
"""
with cd('api-server'):
sudo('cp ./scripts/%(settings)s_api-server.conf /etc/init/api-server.conf' % env)
sudo('cp ./scripts/%(settings)s_cleanup.conf /etc/init/cleanup.conf' % env)
run('npm install')
def validateNote(input):
"""
ensures note is not empty
"""
if(bool(not input or input.isspace())):
raise Exception('release note is REQUIRED. just jot down what is in this release alright')
if ";" in input:
raise Exception('can not use ; in note')
return input
def addNote():
"""
add note to deployment
"""
if(env.requireNote):
prompt("add release note: ", "note", validate=validateNote)
def track_deployment():
"""
Update deployments for tracking
"""
if env.newrelic_application_id:
with cd('api-server'):
branch = run('git rev-parse --abbrev-ref HEAD')
commit = run('git rev-parse HEAD');
author = env.author
note = env.note
cmd = 'curl -H "x-api-key:b04ef0fa7d124e606c0c480ac9207a85b78787bda4bfead" \
-d "deployment[application_id]=' + env.newrelic_application_id+'\" \
-d "deployment[description]=branch:'+branch+'" \
-d "deployment[revision]=' + commit + '" \
-d "deployment[changelog]=' + note + '" \
-d "deployment[user]=' + author + '" \
https://api.newrelic.com/deployments.xml'
run(cmd)
"""
Commands - deployment
"""
def deploy():
"""
Deploy the latest version of the site to the server.
"""
require('settings', provided_by=[production, integration, staging])
require('branch', provided_by=[stable, master, branch])
prompt("your name please: ", "author")
addNote()
checkout_latest()
install_requirements()
track_deployment()
restart_service()
def restart_service():
"""
Restart the server.
"""
sudo('service api-server restart')
sudo('service cleanup restart')
"""
Commands - rollback
"""
def rollback(commit_id):
"""
Rolls back to specified git commit hash or tag.
There is NO guarantee we have committed a valid dataset for an arbitrary
commit hash.
"""
require('settings', provided_by=[production, integration, staging])
require('branch', provided_by=[stable, master, branch])
checkout_latest()
git_reset(commit_id)
install_requirements()
restart_service()
def git_reset(commit_id):
"""
Reset the git repository to an arbitrary commit hash or tag.
"""
env.commit_id = commit_id
run("cd api-server; git reset --hard %(commit_id)s" % env)