-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfabfile.py
130 lines (100 loc) · 3.27 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
import time
import getpass
from fabric.api import *
from fabric.contrib.console import confirm
import subprocess
class Site(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def run(self, cmd):
with cd(self.dir):
sudo(cmd, user=self.user_id)
def backup(self):
self.run('venv/bin/python manage.py backup-to-s3')
def deploy(self):
self.git_pull()
self.update_packages()
self.run('venv/bin/python manage.py migrate')
self.run('venv/bin/python manage.py collectstatic --noinput')
self.restart()
def git_pull(self):
# .pyc files can create ghost behavior when .py files are deleted...
self.run("find . -name '*.pyc' -delete")
self.run("git fetch origin && git reset --hard origin/master")
def git_tag(self):
self.run("git tag | sort -g | tail -n 1 | sed s/$/+1/ | bc | xargs git tag")
self.run("git push --tags && git push")
def rebuild_index(self):
self.run("venv/bin/python manage.py rebuild_index")
def update_packages(self):
self.run("./venv/bin/pip install -r requirements.txt")
def restart(self):
#header("Running: Restart server script: %s" % self.gunicorn)
#run("sudo /etc/init.d/%s restart" % self.gunicorn)
self.run("touch reload")
PROD = Site(
dir='/home/wikipendium-web/wikipendium.no/',
user_id='wikipendium-web'
)
env.hosts = ['wikipendium.no']
@task
def clone_prod_data():
"""
Download production data (database and uploaded files) and insert locally
"""
env.user = prompt("Username on prod server:", default=getpass.getuser())
dump_file = str(time.time()) + ".json"
# Ignore errors on these next steps, so that we are sure we clean up no matter what
with settings(warn_only=True):
# Dump the database to a file...
PROD.run('source venv/bin/activate ' +
'&& nice python manage.py dumpdb > ' +
dump_file)
# clean password hashes
PROD.run('sed -i \'s/"password": "[^"]*"/"password": ""/\' ' + dump_file)
# Then download that file
get(PROD.dir + dump_file, dump_file)
# Replace this db with the contents of the dump
local('python manage.py restoredb < ' + dump_file)
# ... then cleanup the dump files
PROD.run('rm ' + dump_file)
local('rm ' + dump_file)
@task
def deploy():
"""
"""
# mac-only command, just for fun
try:
subprocess.call(['say', '"Ship! Ship! Ship!"'])
except:
pass
print "ship! ship! ship!"
env.user = prompt("Username on prod server:", default=getpass.getuser())
should_tag = tag()
PROD.backup()
PROD.deploy()
if should_tag:
PROD.git_tag()
def header(text):
print ("#" * 45) + "\n# %s\n" % text + ("#" * 45)
@task
def restart():
PROD.restart()
@task
def rebuild_index():
PROD.rebuild_index()
@task
def backup():
"""
Dump a full backup to S3.
"""
env.user = prompt("Username on prod server:", default=getpass.getuser())
PROD.backup()
def tag():
if not confirm("Give new tag for this deployment?"):
if confirm("Are you sure?", default=False):
return False
else:
tag()
else:
return True