This repository was archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfabfile.py
More file actions
100 lines (85 loc) · 2.89 KB
/
fabfile.py
File metadata and controls
100 lines (85 loc) · 2.89 KB
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
# Fabric deploy/rollback handling of OASIS website.
from fabric.api import run, env, cd, prompt, local, put
import datetime
import os
import glob
NOW = datetime.datetime.now()
NOW_STR = NOW.strftime('%Y-%m-%d-%H%M')
MAX_AGE = datetime.timedelta(days=10)
TARGET_DIR = '/home/groups/oasis'
DIST_DIR = os.path.join(os.getcwd(), 'dist')
HTDOCS_DN=os.path.join(TARGET_DIR, 'htdocs')
env.hosts = ['ssh.ocamlcore.org:522']
env.use_ssh_config = True
def releases(local=False):
"""List releases and their date."""
dn = TARGET_DIR
if local:
dn = DIST_DIR
glb = os.path.join(dn, 'htdocs-*.tar.gz')
releases = []
if local:
releases = glob.glob(glb)
else:
releases = run('ls -1 %s' % glb).split()
date_releases = []
for release in releases:
date = datetime.datetime.strptime(os.path.basename(release),
'htdocs-%Y-%m-%d-%H%M.tar.gz')
date_releases.append((date, release))
return date_releases
def cleanup():
"""Remove old releases."""
for (date, release) in releases():
if NOW - date > MAX_AGE:
run ('rm "%s"' % release)
for (date, release) in releases(local=True):
if NOW - date > MAX_AGE:
local ('rm "%s"' % release)
def check_short_release_names(short_names, choice):
if choice in short_names:
return choice
else:
raise Error('"%s" is not a release.' % choice)
def deploy_release(release_fn):
run('rm -R "%s"' % HTDOCS_DN)
with cd(TARGET_DIR):
run('tar xzf "%s"' % release_fn)
run('chmod -R g+w,o+rX htdocs')
def deploy():
release_fn=os.path.join(TARGET_DIR, 'htdocs-' + NOW_STR + '.tar.gz')
release_local_fn=os.path.join(DIST_DIR, os.path.basename(release_fn))
# Check we don't already have this version.
run('! test -e "%s"' % release_fn)
# Build local tarball.
if os.path.exists('tmp'):
local('rm -Rf tmp')
local('mkdir tmp')
if not os.path.exists(os.path.dirname(release_local_fn)):
local('mkdir "%s"' % os.path.dirname(release_local_fn))
local('cp -pR html tmp/htdocs')
local('tar czf "%s" -C tmp htdocs' % release_local_fn)
local('rm -Rf tmp')
# Send data and deploy.
put(release_local_fn, release_fn)
deploy_release(release_fn)
cleanup()
def rollback():
all_releases = sorted(releases(), cmp=lambda (d1, _1),(d2, _2): cmp(d2, d1))
if len(all_releases) > 1:
print "Available releases:"
short_names = dict()
latest = None
for _, release in all_releases:
basename = os.path.basename(release).replace('.tar.gz','')
if not latest:
latest = basename
short_names[basename] = release
print basename
rollback_to_release = prompt("Rollback to which release?",
default=latest,
validate=lambda r: check_short_release_names(short_names, r))
rollback_release_fn = short_names[rollback_to_release]
deploy_release(rollback_release_fn)
else:
print "No old releases."