-
Notifications
You must be signed in to change notification settings - Fork 65
/
fabfile.py
93 lines (71 loc) · 2.63 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
import time
from fabric.api import run, execute, env, sudo
from fabric.colors import red, green, blue
from fabric.operations import local
environment = "production"
env.use_ssh_config = True
env.hosts = ["congress-api@congress"]
branch = "master"
repo = "git://github.com/sunlightlabs/congress.git"
home = "/projects/congress-api"
shared_path = "%s/congress/shared" % home
versions_path = "%s/congress/versions" % home
version_path = "%s/%s" % (versions_path, time.strftime("%Y%m%d%H%M%S"))
current_path = "%s/congress/current" % home
# how many old releases to be kept at deploy-time
keep = 10
## can be run only as part of deploy
def checkout():
run('git clone -q -b %s %s %s' % (branch, repo, version_path))
def links():
run("ln -sf %s/config.yml %s/config/config.yml" % (shared_path, version_path))
run("ln -sf %s/mongoid.yml %s/config/mongoid.yml" % (shared_path, version_path))
run("ln -sf %s/newrelic.yml %s/config/newrelic.yml" % (shared_path, version_path))
run("ln -sf %s/config.ru %s/config.ru" % (shared_path, version_path))
run("ln -sf %s/unicorn.rb %s/unicorn.rb" % (shared_path, version_path))
run("ln -sf %s/config/cron/scripts/* %s/cron" % (version_path, shared_path))
run("ln -sf %s/data %s/data" % (home, version_path))
def dependencies():
run("cd %s && bundle install --local" % version_path)
run("workon congress && cd %s && pip install -r tasks/requirements.txt" % version_path)
def create_indexes():
run("cd %s && rake create_indexes" % version_path)
def make_current():
run('rm -f %s && ln -s %s %s' % (current_path, version_path, current_path))
def cleanup():
versions = run("ls -x %s" % versions_path).split()
# destroy all but the most recent X
destroy = versions[:-keep]
for version in destroy:
command = "rm -rf %s/%s" % (versions_path, version)
run(command)
## can be run on their own
def set_crontab():
run("cd %s && rake set_crontab environment=%s current_path=%s" % (current_path, environment, current_path))
def disable_crontab():
run("cd %s && rake disable_crontab" % current_path)
def start():
execute(restart)
def stop():
#run("sudo stop congress")
run("kill `cat %s/unicorn.pid`" % shared_path)
def restart():
sudo("%s/bin/restart-sudo.sh" % home, shell=False)
# run("kill -USR2 `cat %s/unicorn.pid`" % shared_path)
def deploy():
execute(checkout)
execute(links)
execute(dependencies)
execute(create_indexes)
execute(make_current)
execute(set_crontab)
execute(restart)
execute(cleanup)
def deploy_cold():
execute(checkout)
execute(links)
execute(dependencies)
execute(create_indexes)
execute(make_current)
execute(set_crontab)
execute(start)