-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
176 lines (139 loc) · 5.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
import os
from fabric.state import env
from fabric.api import cd, run, sudo, settings
from fabric.contrib.files import exists, upload_template
env.hosts = ['sergey@138.68.145.206']
def _set_env():
home_path = '/var/www/'
env.user = 'sergey'
env.PROJECT_NAME = 'open_music'
env.TEMPLATES_PATH = 'deploy_templates'
env.BASE_PYTHON_PATH = '/usr/bin/python3.5'
env.REMOTE_PROJECT_PATH = os.path.join(home_path, 'src/%s/' % env.PROJECT_NAME)
env.VIRTUAL_ENV_PATH = os.path.join(
home_path,
'src/.virtualenvs/%s/' % env.PROJECT_NAME
)
env.PIP = os.path.join(env.VIRTUAL_ENV_PATH, 'bin/pip')
env.PYTHON = os.path.join(env.VIRTUAL_ENV_PATH, 'bin/python')
env.DJANGO_CONFIGURATION_NAME = 'Prod'
env.UWSGI_PROCESSES = 5
env.DOMAIN_NAME = '138.68.145.206'
env.UWSGI_MODULE = 'music_project'
env.SUDO_PERMISSION =True
env.GIT_REP_PATH ='https://github.com/seregagavrilov/open_music'
def bootstrap():
_set_env()
install_system_packages(
packages=[
'python3-dev',
'python-pip',
'nginx',
'git'
]
)
# _put_template('uwsgi.service', '/etc/systemd/system/', use_sudo=True)
create_folders()
configure_nginx()
configure_uwsgi()
update_src()
create_virtualenv()
install_libs()
restart_all()
run_management_command('collectstatic --noinput')
run_management_command('migrate')
def install_system_packages(packages, do_apt_get_update=True):
if do_apt_get_update:
sudo('apt-get update')
sudo('apt-get install %s' % (" ".join(packages)))
def configure_nginx():
_put_template('nginx.conf', '/etc/nginx/sites-available/nginx.conf', use_sudo=True)
def configure_uwsgi():
uwsgi_config_filename = 'project.ini'
uwsgi_base_config_filename = 'uwsgi.service'
_put_template(uwsgi_config_filename, '/etc/uwsgi/apps-available/', use_sudo=True)
apps_enabled_link_path = '/etc/uwsgi/apps-enabled/%s' % uwsgi_config_filename
if not exists(apps_enabled_link_path):
sudo(
'ln -s /etc/uwsgi/apps-available/%(file)s %(link)s' % {
'file': uwsgi_config_filename,
'link': apps_enabled_link_path,
}
)
_put_template(uwsgi_base_config_filename, '/etc/systemd/system/uwsgi.service', use_sudo=True)
def create_folders():
_mkdir(env.VIRTUAL_ENV_PATH, is_sudo=True)
_mkdir(os.path.join(env.REMOTE_PROJECT_PATH, 'static'), is_sudo=True)
_mkdir('/etc/uwsgi/apps-enabled/', is_sudo=True)
_mkdir('/etc/uwsgi/apps-available/', is_sudo=True)
# _mkdir('/etc/systemd/system/uwsgi.service', is_sudo=True)
def update_src():
runner = return_runner_if_is_sudo()
if not is_git_repo_exists():
clear_project_folder(True)
clone_src(True)
with cd(env.REMOTE_PROJECT_PATH):
runner('git pull')
def clone_src(is_sudo=False):
runner = sudo if is_sudo else run
with cd(env.REMOTE_PROJECT_PATH):
runner('git clone %s .' % env.GIT_REP_PATH)
def is_git_repo_exists():
return exists(os.path.join(env.REMOTE_PROJECT_PATH, '.git'))
def clear_project_folder(is_sudo):
runner = sudo if is_sudo else run
runner('rm -rf %s*' % env.REMOTE_PROJECT_PATH)
def return_runner_if_is_sudo():
return sudo if env.SUDO_PERMISSION else run
def create_virtualenv(reinstall_pip=True):
runner = return_runner_if_is_sudo()
if reinstall_pip: # http://askubuntu.com/questions/488529/
runner('%s -m venv --without-pip %s' % (env.BASE_PYTHON_PATH, env.VIRTUAL_ENV_PATH))
activate_command = 'source %s' % os.path.join(env.VIRTUAL_ENV_PATH, 'bin/activate')
runner('%s && curl https://bootstrap.pypa.io/get-pip.py | python' % activate_command)
else:
runner('%s -m venv %s' % (env.BASE_PYTHON_PATH, env.VIRTUAL_ENV_PATH))
def install_libs():
runner = return_runner_if_is_sudo()
requirements_path = os.path.join(env.REMOTE_PROJECT_PATH, 'requirements.txt')
runner('%s install -r %s' % (env.PIP, requirements_path))
# def restart_all():
# #sudo('service nginx restart')
# restart_initctl_services(['uwsgi'])
def run_management_command(command):
run('DJANGO_CONFIGURATION=%s %s %s %s' % (
env.DJANGO_CONFIGURATION_NAME,
env.PYTHON,
os.path.join(env.REMOTE_PROJECT_PATH, 'open_music', 'manage.py'),
command
))
def _put_template(template_name, destination_full_path, use_sudo=False, backup_path=None):
context = {
'uwsgi_module': env.UWSGI_MODULE,
'user': env.user,
'project_path': env.REMOTE_PROJECT_PATH,
'uwsgi_processes_amount' : env.UWSGI_PROCESSES,
'venv_path': env.VIRTUAL_ENV_PATH,
'python': env.PYTHON,
'project_name': env.PROJECT_NAME,
'configuration_name': env.DJANGO_CONFIGURATION_NAME,
'domain_name' : env.DOMAIN_NAME,
}
if backup_path:
with settings(warn_only=True):
sudo('mv %s %s' % (destination_full_path, backup_path))
upload_template(
os.path.join(env.TEMPLATES_PATH, template_name),
destination_full_path,
use_sudo=use_sudo,
context=context,
backup=False,
)
def _mkdir(path, is_sudo=False):
runner = sudo if is_sudo else run
runner('mkdir -p %s' % path)
def restart_all(services=None):
services = services or ['uwsgi', 'nginx']
sudo('systemctl daemon-reload')
for service in services:
sudo('service %s restart' % service)