-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
76 lines (64 loc) · 2.08 KB
/
deploy.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
import subprocess
import os
import sys
# What is the site name?
if len(sys.argv) < 2:
print("What is the site name?")
sys.exit()
sitename = sys.argv[1]
# Remove contents of remote source directory
subprocess.call(
"ssh %s 'rm -r ~/%s/source/*'" % (sitename, sitename), shell=True
)
# What branch are we on?
branch = subprocess.check_output("git branch", shell=True).decode()
branch = [line for line in branch.split("\n") if "*" in line][0]
branch = branch.split()[1]
# What files is git tracking?
tracked_files = subprocess.check_output(
"git ls-tree %s --name-only" % branch, shell=True
).decode()
tracked_files = filter(bool, tracked_files.split("\n"))
# Push source code to remote
for tracked_file in tracked_files:
subprocess.call(
"scp -r ./%s %s:~/%s/source/" % (tracked_file, sitename, sitename), shell=True
)
# Turn off Debug
subprocess.call(
"ssh %s 'sed -i s/\"DEBUG = True\"/\"DEBUG = False\"/g ~/%s/source/cdtool/settings.py'" % (sitename, sitename),
shell=True
)
# Add allowed hosts
subprocess.call(
"ssh %s 'sed -i s/\"ALLOWED_HOSTS = \[\]\"/\"ALLOWED_HOSTS = \[£'%s£', £'www.%s£'\]\"/g ~/%s/source/cdtool/settings.py'" % (sitename, sitename, sitename, sitename),
shell=True
)
subprocess.call(
"ssh %s 'sed -i s/£/\\\"/g ~/%s/source/cdtool/settings.py'" % (sitename, sitename),
shell=True
)
# Upload the secret settings
subprocess.call(
"scp -r ./cdtool/secrets.py %s:~/%s/source/cdtool/secrets.py" % (sitename, sitename), shell=True
)
# Switch to postgres database remotely
subprocess.call(
"ssh %s 'sed -i s/\": local_db\"/\": live_db\"/g ~/%s/source/cdtool/secrets.py'" % (sitename, sitename),
shell=True
)
# Install pip packages
subprocess.call(
"ssh %s '~/%s/env/bin/pip install -r ~/%s/source/requirements.txt'" % (sitename, sitename, sitename),
shell=True
)
# Apply migrations
subprocess.call(
"ssh %s '~/%s/env/bin/python ~/%s/source/manage.py migrate'" % (sitename, sitename, sitename),
shell=True
)
# Deploy static files
subprocess.call(
"ssh %s 'cd ~/%s/source && ../env/bin/python manage.py collectstatic --noinput'" % (sitename, sitename),
shell=True
)