-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
180 lines (139 loc) · 5.04 KB
/
__init__.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
import glob
import os
from datetime import datetime
from subprocess import check_call
# noinspection PyPackageRequirements
from fabric.api import env, lcd, cd, run, put, settings, prefix, task, get
# noinspection PyPackageRequirements
from fabric.contrib.project import rsync_project
__author__ = 'Cedric RICARD'
__all__ = ['start', 'stop', 'empty_folder', 'deploy', 'deploy_app', 'dump_db', 'load_db',
'make_archive',
'create_superuser', 'load_initial_data',
'install_system',
]
@task
def stop():
run("supervisorctl stop %s" % env.app_name)
@task
def start():
run("supervisorctl start %s" % env.app_name)
def _run_compile(folder_list=None, clear=False, all_libraries=True):
with cd(env.app_dir):
put(os.path.join(os.path.dirname(__file__), 'compile.py'), remote_path=env.app_dir)
if clear:
run("python -O compile.py -c")
elif folder_list is not None:
run("python -O compile.py %s %s" % (
all_libraries and '-a' or '',
' '.join(map(lambda x: '"%s"' % x, folder_list))
))
run('rm compile.py')
def compile_python_files():
_run_compile(env.app_folder_list)
def clean_compiled_files():
# cleanup *.pyc / *.pyo files
_run_compile(clear=True)
def sync_sources(test_only=False):
rsync_project(
env.app_dir,
local_dir=env.local_dir + "/",
delete=True,
default_opts='-thrvz',
extra_opts='-ci --filter=". %s"' % env.rsync_filter + (test_only and " --dry-run" or ""),
)
@task
def empty_folder():
with settings(warn_only=True):
with cd(env.app_dir):
run('find . -type f -name \'*.tar.gz\' | xargs rm -rf')
run('rm -r %s' % ' '.join(map(lambda x: '"%s"' % x, env.app_folder_list)))
@task
def make_archive():
"""
Very simple task that make an archive from the application folder list and other files list
"""
fname = 'build-%s-%s.tar.gz' % (env.app_name, datetime.now().strftime("%Y-%m-%dT%H-%M-%S"))
with lcd(env.local_dir):
args = ['tar', '-czf', fname]
args.extend(env.app_folder_list)
args.extend(env.app_other_files_list)
check_call(args, cwd=env.local_dir)
print("Archive created: %s" % fname)
return fname
def untar_archive():
tar_gz_build_files = glob.glob(os.path.join(env.local_dir, 'build-%s-*.tar.gz' % env.app_name))
if not len(tar_gz_build_files):
raise IOError("Can't find Archive!")
tar_gz_build_file = tar_gz_build_files[-1]
tar_gz_file_name = tar_gz_build_file.split(os.path.sep)[-1]
with cd(env.app_dir):
put(tar_gz_build_file, env.app_dir)
run('tar -xzvf ' + tar_gz_file_name)
@task
def deploy_app(with_rsync=True):
run("mkdir -p %s" % env.app_dir)
if with_rsync:
sync_sources()
else:
empty_folder()
untar_archive()
clean_compiled_files()
# compile_python_files()
with cd(env.app_dir):
with settings(warn_only=True):
if run("test -d .env").failed:
run("virtualenv .env")
if 'static_dir' in env:
run('rm -rf %s' % env.static_dir)
with prefix('. %s/.env/bin/activate' % env.app_dir):
run('pip install -r requirements.txt --upgrade')
with cd(env.django_app_dir):
run('python manage.py migrate --noinput')
if 'static_dir' in env:
run('python manage.py collectstatic --noinput')
run('chown -R %s:%s .' % (env.app_user, env.app_user))
@task
def load_initial_data(fixture_path):
with cd(env.app_dir):
with prefix('. %s/.env/bin/activate' % env.app_dir):
with cd(env.django_app_dir):
run("python ./manage.py loaddata %s" % fixture_path)
run('python manage.py createsuperuser')
@task
def create_superuser():
with cd(env.app_dir):
with prefix('. %s/.env/bin/activate' % env.app_dir):
with cd(env.django_app_dir):
run('python manage.py createsuperuser')
def rsync_deploy():
stop()
deploy_app(with_rsync=True)
start()
@task(default=True)
def deploy():
stop()
make_archive()
deploy_app(with_rsync=False)
start()
@task
def dump_db():
with cd(env.app_dir):
with prefix('. %s/.env/bin/activate' % env.app_dir):
with cd(env.django_app_dir):
fname = 'dump-%s.json' % datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
run('python manage.py dumpdata > %s' % fname)
get(fname)
@task
def load_db(fname):
if os.path.exists(fname):
with cd(env.app_dir):
with prefix('. %s/.env/bin/activate' % env.app_dir):
with cd(env.django_app_dir):
put(fname)
run('python manage.py loaddata %s' % fname)
@task
def install_system():
with lcd(env.fab_dir):
args = ['ansible-playbook', '--ask-sudo-pass', '-i', "%s:%s," % (env.host, env.port), '%s.yml' % env.app_name]
check_call(args, cwd=env.fab_dir, shell=False)