forked from sunlightlabs/scout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
118 lines (87 loc) · 3.22 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
import time
from fabric.api import run, execute, env
env.use_ssh_config = True
branch = "master"
repo = "git://github.com/sunlightlabs/scout.git"
# keep 10 releases at a time on disk
keep = 10
# default to staging, override with "fab [command] --set target=production"
target = env.get('target', 'staging')
if target == "staging":
env.hosts = ["alarms@dupont"]
username = "alarms"
elif target == "production":
env.hosts = ["scout@scout"]
username = "scout"
home = "/projects/%s" % username
shared_path = "%s/shared" % home
versions_path = "%s/versions" % home
version_path = "%s/%s" % (versions_path, time.strftime("%Y%m%d%H%M%S"))
current_path = "%s/current" % home
## can be run only as part of deploy
def cleanup():
versions = run("ls -x %s" % versions_path).split()
destroy = versions[:-keep]
for version in destroy:
command = "rm -rf %s/%s" % (versions_path, version)
run(command)
def checkout():
run('git clone -q -b %s %s %s' % (branch, repo, version_path))
def links():
run("ln -s %s/system %s/public/system" % (shared_path, version_path))
run("ln -s %s/sitemap %s/public/sitemap" % (shared_path, version_path))
run("ln -s %s/config.yml %s/config/config.yml" % (shared_path, version_path))
run("ln -s %s/services.yml %s/config/services.yml" % (shared_path, version_path))
run("ln -s %s/config.ru %s/config.ru" % (shared_path, version_path))
run("ln -s %s/unicorn.rb %s/unicorn.rb" % (shared_path, version_path))
# default to a robots.txt.else rather than a robots.txt,
# so that if this fails for some reason, we end up with no robots.txt
# rather than a don't-index-anything robots.txt.
if target == "production":
robots = "production"
else:
robots = "else"
run("cp %s/public/robots.txt.%s %s/public/robots.txt" % (version_path, robots, version_path))
def dependencies():
run("cd %s && bundle install --local" % version_path)
def create_indexes():
run("cd %s && bundle exec rake create_indexes" % version_path)
def sync_assets():
run("cd %s && bundle exec rake assets:sync" % version_path)
def make_current():
run('rm -f %s && ln -s %s %s' % (current_path, version_path, current_path))
## can be run on their own
def set_crontab():
run("cd %s && bundle exec rake crontab:set environment=%s current_path=%s" % (current_path, target, current_path))
def disable_crontab():
run("cd %s && bundle exec rake crontab:disable environment=%s current_path=%s" % (current_path, target, current_path))
def start():
run("cd %s && bundle exec unicorn -D -l %s/%s.sock -c unicorn.rb" % (current_path, shared_path, username))
def stop():
run("kill `cat %s/unicorn.pid`" % shared_path)
def restart():
stop()
start()
#run("kill -USR2 `cat %s/unicorn.pid`" % shared_path)
def clear_cache():
run("cd %s && rake clear_cache" % current_path)
def deploy():
execute(checkout)
execute(links)
execute(dependencies)
execute(create_indexes)
execute(sync_assets)
execute(make_current)
execute(set_crontab)
execute(restart)
execute(cleanup)
# only difference is it uses start instead of restart
def deploy_cold():
execute(checkout)
execute(links)
execute(dependencies)
execute(create_indexes)
execute(sync_assets)
execute(make_current)
execute(set_crontab)
execute(start)