-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
181 lines (127 loc) · 5.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
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
import json
from fabric.api import *
from deployer.configuration import Configuration
from deployer.helpers import mkdir, rmdir
from deployer.standard_packages import package_list
import os
from StringIO import StringIO
site_settings = {
"settings_module": 'openads.settings',
"settings_local": 'openads/local_settings.py',
"application_name": 'openads',
"git_location": "https://github.com/wdq/OpenAds.git",
"git_branch": "master",
# Defaults
"static_dir": "all_static",
"media_dir": "media",
"requirements_file": 'requirements.txt',
}
def load_configuration():
with open('hosts_data.json', 'r') as f:
configuration_file = ''.join(f.readlines())
conf = json.JSONDecoder().decode(configuration_file)
print "Loaded server config for {}".format(conf['server_login'])
env.hosts.append(conf['server_login'])
env.hosts_data = Configuration(conf, site_settings)
load_configuration()
def install_requirements():
sudo('sudo apt-get -y update')
sudo('sudo apt-get -y upgrade')
sudo('sudo apt-get install -y {}'.format(package_list()))
def create_folders():
mkdir(env.hosts_data.base_path())
mkdir(env.hosts_data.log_path())
def create_virtual_environment():
with cd(env.hosts_data.base_path()):
run('virtualenv {}'.format(env.hosts_data.virtualenv_path()))
with prefix("source {}".format(env.hosts_data.virtualenv_activate_path())):
run('pip install --upgrade distribute')
run('pip install -r {}'.format(env.hosts_data.requirements_path()))
if env.hosts_data.is_mysql():
run('pip install mysql-python')
run('pip install gunicorn')
def create_local_settings():
rmdir(env.hosts_data.local_settings_path())
put(StringIO(env.hosts_data.local_settings()), env.hosts_data.local_settings_path())
def create_gunicorn_config():
rmdir(env.hosts_data.gunicorn_config_path())
put(StringIO(env.hosts_data.gunicorn_config()), env.hosts_data.gunicorn_config_path())
sudo('chmod u+x {}'.format(env.hosts_data.gunicorn_config_path()))
def create_demo_superuser():
with cd(env.hosts_data.app_path()):
with prefix("source {}".format(env.hosts_data.virtualenv_activate_path())):
commands = [
"echo \"from django.contrib.auth.models import User;",
"User.objects.create_superuser('admin', '[email protected]', 'pass')\" | python manage.py shell"
]
run(' '.join(commands))
def create_gunicorn_supervisor():
rmdir(env.hosts_data.gunicorn_supervisor_config_path())
put(
StringIO(env.hosts_data.gunicorn_supervisor_config()),
env.hosts_data.gunicorn_supervisor_config_path(),
use_sudo=True
)
sudo('sudo supervisorctl reread')
sudo('sudo supervisorctl update')
def create_nginx_config():
put(StringIO(env.hosts_data.nginx_config()), env.hosts_data.nginx_available_path(), use_sudo=True)
sudo('ln -s {} {}'.format(env.hosts_data.nginx_available_path(), env.hosts_data.nginx_enabled_path()))
sudo('sudo service nginx restart')
def delete_nginx_config():
rmdir(env.hosts_data.nginx_enabled_path(), sudo_access=True)
rmdir(env.hosts_data.nginx_available_path(), sudo_access=True)
sudo('sudo service nginx restart')
def migrate_database():
with cd(env.hosts_data.app_path()):
with prefix("source {}".format(env.hosts_data.virtualenv_activate_path())):
run("python manage.py syncdb --noinput")
run("python manage.py migrate --noinput")
def delete_folders():
rmdir(env.hosts_data.base_path())
def delete_gunicorn_supervisor():
server_stop()
rmdir(env.hosts_data.gunicorn_supervisor_config_path(), sudo_access=True)
sudo('sudo supervisorctl reread')
sudo('sudo supervisorctl update')
def make_deploy():
# Install all the packages
install_requirements()
# Create the folders for the site
create_folders()
# Create the git
run(env.hosts_data.git_clone_command())
with cd(env.hosts_data.app_path()):
run(env.hosts_data.git_checkout_command())
# Create the virtual environment
create_virtual_environment()
# Upload the local settings
create_local_settings()
# Create database
migrate_database()
create_demo_superuser()
# Create the gunicorn environment
create_gunicorn_config()
create_gunicorn_supervisor()
# Create nginx configs
create_nginx_config()
def server_status():
sudo('sudo supervisorctl status {}'.format(env.hosts_data.application_name()))
def server_stop():
sudo('sudo supervisorctl stop {}'.format(env.hosts_data.application_name()))
def server_start():
sudo('sudo supervisorctl start {}'.format(env.hosts_data.application_name()))
def server_restart():
sudo('sudo supervisorctl restart {}'.format(env.hosts_data.application_name()))
def destroy_deploy():
delete_folders()
delete_gunicorn_supervisor()
delete_nginx_config()
def update_deploy():
server_stop()
with cd(env.hosts_data.app_path()):
with prefix("source {}".format(env.hosts_data.virtualenv_activate_path())):
run('git pull')
run('pip install -r {}'.format(env.hosts_data.requirements_path()))
migrate_database()
server_start()