-
Notifications
You must be signed in to change notification settings - Fork 9
/
fabfile.py
199 lines (152 loc) Β· 5.1 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
195
196
197
198
199
import json
from datetime import datetime
from fabric.api import *
from fabric.operations import require
from fabric.context_managers import settings
from fabric.utils import fastprint
from fabvenv import virtualenv
from unipath import Path
SETTINGS_FILE_PATH = Path(__file__).ancestor(1).child('project_settings.json')
with open(SETTINGS_FILE_PATH, 'r') as f:
# Load settings.
project_settings = json.loads(f.read())
env.prompts = {
'Type \'yes\' to continue, or \'no\' to cancel: ': 'yes'
}
env.use_ssh_config = True
def set_stage(stage_name='development'):
stages = project_settings['stages'].keys()
if stage_name not in stages:
raise KeyError('Stage name "{0}" is not a valid stage ({1})'.format(
stage_name,
','.join(stages))
)
env.stage = stage_name
@task
def stable():
set_stage('stable')
set_project_settings()
@task
def development():
set_stage('development')
set_project_settings()
def set_project_settings():
stage_settings = project_settings['stages'][env.stage]
if not all(project_settings.items()):
raise KeyError('Missing values in project settings.')
env.settings = stage_settings
@task
def deploy(debug='no'):
"""
Deploys project to previously set stage.
"""
require('stage', provided_by=(stable, development))
require('settings', provided_by=(stable, development))
# Set env.
env.user = env.settings['user']
env.host_string = env.settings['host']
if debug == 'no':
_hide = ('stderr', 'stdout', 'warnings', 'running')
else:
_hide = ()
with hide(*_hide):
with lcd(project_settings['local']['code_src_directory']):
push_repository()
with cd(env.settings['code_src_directory']):
pull_repository()
with virtualenv(env.settings['venv_directory']):
with cd(env.settings['code_src_directory']):
install_requirements()
if project_settings.get('git_submodules', False):
pull_repository_submodules()
migrate_models()
if project_settings.get('scss', False):
compile_scss()
collect_static()
restart_application()
@task
def new_release(version, debug='no'):
"""
GitFlow new release
"""
require('stage', provided_by=(stable, development))
require('settings', provided_by=(stable, development))
# Set env.
env.user = env.settings['user']
env.host_string = env.settings['host']
if debug == 'no':
_hide = ('stderr', 'stdout', 'warnings', 'running')
else:
_hide = ()
with hide(*_hide):
local('git flow release start %s' % (version,), capture=True)
local('git commit -am \'Bumped version to %s\'' % (version,))
local('git flow release finish -m %s -p %s' % (version, version), capture=True)
def print_status(description):
def print_status_decorator(fn):
def print_status_wrapper():
now = datetime.now().strftime('%H:%M:%S')
fastprint('({time}) {description}{suffix}'.format(
time=now,
description=description.capitalize(),
suffix='...\n')
)
fn()
now = datetime.now().strftime('%H:%M:%S')
fastprint('({time}) {description}{suffix}'.format(
time=now,
description='...finished ' + description,
suffix='.\n')
)
return print_status_wrapper
return print_status_decorator
@print_status('pulling repository')
def pull_repository():
command = 'git pull origin {}'.format(
env.settings.get('git_branch')
)
run(command)
@print_status('pulling submodules repository')
def pull_repository_submodules():
command = 'git submodule update'
run(command)
@print_status('pushing repository')
def push_repository():
command = 'git push --all'
local(command)
command = 'git push --tags'
local(command)
@print_status('collecting static files')
def collect_static():
run('python manage.py collectstatic')
@print_status('compile scss files')
def compile_scss():
run('python manage.py compilescss')
@print_status('installing requirements')
def install_requirements():
with cd(env.settings['code_src_directory']):
run('pip install -r {0}'.format(env.settings['requirements_file']))
@print_status('migrating models')
def migrate_models():
run('python manage.py migrate')
@print_status('restarting application')
def restart_application():
with settings(warn_only=True):
restart_command = env.settings['restart_command']
result = run(restart_command)
if result.failed:
abort('Could not restart application.')
# noinspection PyShadowingBuiltins
@task
def help():
message = '''
Remote updating application with fabric.
Usage example:
Deploy to development server:
fab development deploy
Deploy to development server with debug:
fab stable deploy:debug=yes
Deploy to production server:
fab stable deploy
'''
fastprint(message)