-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
167 lines (131 loc) · 3.98 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
# coding: utf-8
from __future__ import unicode_literals
import sys
import os
import time
from decouple import config
from fabric.api import local, env, task, puts, run, cd, prefix, sudo
from fabric.operations import get
env.shell = "/bin/bash -l -i -c"
env.hosts = ['[email protected]']
env.password = config('ENV_PASSWORD', default=None)
PROJECT = 'logd'
PROJECT_ROOT = ''
APP_FOLDER = ''
APP_ROOT = PROJECT_ROOT + APP_FOLDER
FRONT_FOLDER = '_front'
FRONT_ROOT = os.path.join(APP_ROOT, FRONT_FOLDER)
DOWNLOAD_CACHE_DIR = '$HOME/.cache/pip'
VIRTUALENV = ''
MANAGE = 'python manage.py {cmd} --settings=conf.settings'
ENVIRONMENT = ''
def _set_project(environment):
global PROJECT_ROOT
global VIRTUALENV
global APP_ROOT
global ENVIRONMENT
global FRONT_ROOT
ENVIRONMENT = environment
PROJECT_ROOT = '~/web/{PROJECT}_{ENVIRONMENT}'.format(
ENVIRONMENT=environment, PROJECT=PROJECT)
VIRTUALENV = '{PROJECT}_{ENVIRONMENT}'.format(
ENVIRONMENT=environment, PROJECT=PROJECT)
APP_ROOT = os.path.join(PROJECT_ROOT, APP_FOLDER)
FRONT_ROOT = os.path.join(APP_ROOT, FRONT_FOLDER)
@task
def dev():
_set_project('dev')
@task
def prod():
_set_project('prod')
@task
def deploy(restart='yes', requirements='yes', migration='yes', before_migrate=None):
if not PROJECT_ROOT:
puts('Please choose your environment: dev or prod')
sys.exit()
push()
run('date')
update_files()
if requirements == 'yes':
update_requirements()
if migration == 'yes':
migrate()
# front_update()
collect_static()
if restart == 'yes':
restart_service(VIRTUALENV)
@task
def backup_db(db_name=''):
if not PROJECT_ROOT:
puts('Please choose your environment: dev or prod')
sys.exit()
db_name = VIRTUALENV if not db_name else db_name
filename = "backups/backup__{db_name}__{timestamp}.tar.backup".format(
db_name=db_name,
timestamp=time.strftime('%Y-%m-%d-%H-%M-%S'),
)
with cd(APP_ROOT):
run('mkdir -p backups')
sudo('chown postgres backups')
pwd = run('pwd')
sudo(
'pg_dump -Ft {db_name} >{filename}'.format(
db_name=db_name, filename=filename),
user='postgres',
)
remote_file = os.path.join(pwd, filename)
get(remote_file, filename, use_sudo=True)
@task
def push():
puts('push local changes')
local('git push')
@task
def update_files():
"only update files"
puts('updating project files')
with cd(APP_ROOT):
run('git reset --hard')
run('git pull')
@task
def update_requirements(name='prod'):
puts('updating requirements')
with prefix("workon {}".format(VIRTUALENV)):
with cd(APP_ROOT):
run(
'pip install '
'-r requirements/{name}.txt'.format(
download_cache=DOWNLOAD_CACHE_DIR,
PROJECT=PROJECT,
name=name,
)
)
@task
def front_update():
puts('updating front assets')
with cd(FRONT_ROOT):
run('npm update')
run('bower update')
run('gulp build')
@task
def migrate(database=''):
puts('running migrations {}'.format(database))
cmd = 'migrate' if not database else 'migrate --database {}'.format(database)
with prefix("workon {}".format(VIRTUALENV)):
with cd(APP_ROOT):
run(MANAGE.format(cmd=cmd))
@task
def collect_static():
puts('collecting static')
with prefix("workon {}".format(VIRTUALENV)):
with cd(APP_ROOT):
run(MANAGE.format(cmd='collectstatic --noinput'))
@task
def restart_service(services='uwsgi'):
# sudo('/usr/sbin/service nginx restart')
service_list = [services, ] if isinstance(services, basestring) else services
for service in service_list:
puts('restarting {}'.format(service))
sudo('sudo service {service} restart'.format(service=service))
@task
def list_vars():
run('set')