-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfabfile.py
194 lines (144 loc) · 4.42 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import json
from fabric.contrib.files import sed
from fabric.context_managers import cd
from fabric.api import env, sudo, run
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Load deploy settings in deploy.json
with open(os.path.join(PROJECT_DIR, 'deploy.json')) as f:
envs = json.loads(f.read())
REPO_URL = envs['REPO_URL']
PROJECT_NAME = envs['PROJECT_NAME']
REMOTE_HOST_SSH = envs['REMOTE_HOST_SSH']
REMOTE_HOST = envs['REMOTE_HOST']
REMOTE_USER = envs['REMOTE_USER']
REMOTE_PASSWORD = envs['REMOTE_PASSWORD']
SERVER_DOMAIN = envs['SERVER_DOMAIN']
STATIC_ROOT_NAME = 'collected_static'
STATIC_URL_NAME = 'static'
env.user = REMOTE_USER
username = env.user
env.hosts = [
REMOTE_HOST_SSH,
]
env.password = REMOTE_PASSWORD
project_folder = '/home/{}/{}'.format(env.user, PROJECT_NAME)
apt_requirements = [
'git',
'python3-dev',
'python3-pip',
'build-essential',
'python3-setuptools',
'nginx',
'postgresql',
'postgresql-contrib',
'redis-server',
'libzmq-dev',
'libevent-dev',
]
def new_server():
setup()
deploy()
def setup():
_get_latest_apt()
_install_apt_requirements(apt_requirements)
def deploy():
_get_latest_source()
_install_python_packages()
_update_static_files()
_update_database()
_generate_nginx_conf()
_make_circus()
_grant_nginx()
_restart_nginx()
_start_circusd()
_autodeploy()
_createsuperuserauto()
def _get_latest_apt():
sudo('sudo apt-get update && sudo apt-get -y upgrade')
def _install_apt_requirements(apt_requirements):
reqs = ''
for req in apt_requirements:
reqs += (' ' + req)
sudo('sudo apt-get -y install {}'.format(reqs))
def _get_latest_source():
# run('git clone %s %s' % (REPO_URL, project_folder))
run('git clone %s %s -b fabric' % (REPO_URL, project_folder))
def _install_python_packages():
with cd(project_folder):
sudo('sudo pip3 install -r requirements.txt')
def _update_settings():
settings_path = project_folder + '/{}/settings.py'.format(PROJECT_NAME)
sed(settings_path, 'DEBUG = TRUE', 'DEBUG = FALSE')
sed(
settings_path,
'ALLOWED_HOSTS = .+$',
'ALLOWED_HOSTS = ["%s"]' % (REMOTE_HOST,)
)
def _update_static_files():
with cd(project_folder):
run('python3 manage.py collectstatic --noinput')
def _update_database():
with cd(project_folder):
run('make prepare-postgresql')
run('python3 manage.py migrate --noinput')
def _autodeploy():
with cd(project_folder):
run('python3 manage.py autodeploy')
def _createsuperuserauto():
with cd(project_folder):
run('python3 manage.py createsuperuserauto')
def _generate_nginx_conf():
nginx_conf = '''
server {
listen 80;
server_name %s;
charset utf-8;
client_max_body_size 20M;
location /static/ {
alias %s/collected_static/;
}
location / {
proxy_pass http://0.0.0.0:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
''' % (SERVER_DOMAIN, project_folder)
f = open(project_folder + '/coding-night-live_nginx.conf', 'w')
f.write(nginx_conf)
f.close()
def _grant_nginx():
sudo('sudo ln -s ' + project_folder + '/coding-night-live_nginx.conf /etc/nginx/sites-enabled/')
def _restart_nginx():
sudo('sudo systemctl restart nginx')
def _make_circus():
circus_conf = '''
[watcher:daphne]
cmd = daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer
working_dir = %s/
copy_env = True
user = %s
[watcher:worker]
cmd = python3 manage.py runworker
working_dir = %s/
copy_env = True
user = www-data
[watcher:redis]
cmd = redis-server
copy_env = True
user = %s
''' % (project_folder, REMOTE_USER, project_folder, REMOTE_USER)
f = open(project_folder + '/circus.ini', 'w')
f.write(circus_conf.replace(' ', ''))
f.close()
def _start_circusd():
with cd(project_folder):
sudo('sudo nohup circusd --daemon ./circus.ini')