This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
forked from getweber/weber
-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
executable file
·221 lines (177 loc) · 7.08 KB
/
manage.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#! /usr/bin/python
from __future__ import print_function
import os
import sys
import time
import random
import string
import subprocess
from _lib.bootstrapping import bootstrap_env, from_project_root, requires_env, from_env_bin
from _lib.ansible import ensure_ansible
bootstrap_env(["base"])
from _lib.params import APP_NAME
from _lib.frontend import frontend, ember
from _lib.source_package import prepare_source_package
from _lib.db import db
from _lib.celery import celery
from _lib.utils import interact
from _lib.deployment import run_gunicorn
import click
import requests
import logbook
##### ACTUAL CODE ONLY BENEATH THIS POINT ######
@click.group()
def cli():
pass
cli.add_command(run_gunicorn)
cli.add_command(db)
cli.add_command(frontend)
cli.add_command(ember)
cli.add_command(celery)
@cli.command('ensure-secret')
@click.argument("conf_file")
def ensure_secret(conf_file):
dirname = os.path.dirname(conf_file)
if not os.path.isdir(dirname):
os.makedirs(dirname)
if os.path.exists(conf_file):
return
with open(conf_file, "w") as f:
print('SECRET_KEY: "{0}"'.format(_generate_secret()), file=f)
print('SECURITY_PASSWORD_SALT: "{0}"'.format(_generate_secret()), file=f)
def _generate_secret(length=50):
return "".join([random.choice(string.ascii_letters) for i in range(length)])
@cli.command()
@click.option("--develop", is_flag=True)
@click.option("--app", is_flag=True)
def bootstrap(develop, app):
deps = ["base"]
if develop:
deps.append("develop")
if app:
deps.append("app")
bootstrap_env(deps)
click.echo(click.style("Environment up to date", fg='green'))
@cli.command()
@click.option('--livereload/--no-livereload', is_flag=True, default=True)
@click.option('-p', '--port', default=8000, envvar='TESTSERVER_PORT')
@click.option('--tmux/--no-tmux', is_flag=True, default=True)
@requires_env("app", "develop")
def testserver(tmux, livereload, port):
if tmux:
return _run_tmux_frontend(port=port)
from flask_app.app import create_app
extra_files=[
from_project_root("flask_app", "app.yml")
]
app = create_app({'DEBUG': True, 'TESTING': True, 'SECRET_KEY': 'dummy', 'SECURITY_PASSWORD_SALT': 'dummy'})
if livereload:
from livereload import Server
s = Server(app)
for filename in extra_files:
s.watch(filename)
s.watch('flask_app')
for filename in ['webapp.js', 'vendor.js', 'webapp.css']:
s.watch(os.path.join('static', 'assets', filename), delay=0.5)
logbook.StreamHandler(sys.stderr, level='DEBUG').push_application()
s.serve(port=port, liveport=35729)
else:
app.run(port=port, extra_files=extra_files)
def _run_tmux_frontend(port):
tmuxp = from_env_bin('tmuxp')
os.execve(tmuxp, [tmuxp, 'load', from_project_root('_lib', 'frontend_tmux.yml')], dict(os.environ, TESTSERVER_PORT=str(port), CONFIG_DIRECTORY=from_project_root("conf.d")))
@cli.command()
@click.option("--dest", type=click.Choice(["production", "staging", "localhost", "vagrant", "custom"]), help="Deployment target", required=True)
@click.option("-i", "--inventory", type=str, default=None, help="Path to an inventory file. Should be specified only when \"--dest custom\" is set")
@click.option("--vagrant-machine", type=str, default="", help="Vagrant machine to provision")
@click.option("--sudo/--no-sudo", default=False)
@click.option("--ask-sudo-pass/--no-ask-sudo-pass", default=False)
def deploy(dest, sudo, ask_sudo_pass, vagrant_machine, inventory):
prepare_source_package()
ansible = ensure_ansible()
if dest == "vagrant":
# Vagrant will invoke ansible
environ = os.environ.copy()
environ["PATH"] = "{}:{}".format(os.path.dirname(ansible), environ["PATH"])
# "vagrant up --provision" doesn't call provision if the virtual machine is already up,
# so we have to call vagrant provision explicitly
click.echo(click.style("Running deployment on Vagrant. This may take a while...", fg='magenta'))
subprocess.check_call('vagrant up ' + vagrant_machine, shell=True, env=environ)
subprocess.check_call('vagrant provision ' + vagrant_machine, shell=True, env=environ)
else:
if dest == "custom":
if inventory is None:
raise click.ClickException("-i/--inventory should be specified together with \"--dest custom\"")
if not os.path.exists(inventory):
raise click.ClickException("Custom inventory file {} doesn't exist".format(inventory))
else:
if inventory is not None:
raise click.ClickException("-i/--inventory should be specified only when \"--dest custom\" is specified")
inventory = from_project_root("ansible", "inventories", dest)
click.echo(click.style("Running deployment on {}. This may take a while...".format(inventory), fg='magenta'))
cmd = [ansible, "-i", inventory]
if dest in ("localhost",):
cmd.extend(["-c", "local"])
if dest == "localhost":
cmd.append("--sudo")
if sudo:
cmd.append('--sudo')
if ask_sudo_pass:
cmd.append('--ask-sudo-pass')
cmd.append(from_project_root("ansible", "site.yml"))
subprocess.check_call(cmd)
@cli.command()
def unittest():
_run_unittest()
@requires_env("app", "develop")
def _run_unittest():
subprocess.check_call(
[from_env_bin("py.test"), "tests/test_ut"], cwd=from_project_root())
@cli.command()
@click.argument('pytest_args', nargs=-1)
def pytest(pytest_args):
_run_pytest(pytest_args)
@requires_env("app", "develop")
def _run_pytest(pytest_args=()):
subprocess.check_call(
[from_env_bin("py.test")]+list(pytest_args), cwd=from_project_root())
@cli.command()
def fulltest():
_run_fulltest()
@requires_env("app", "develop")
def _run_fulltest(extra_args=()):
subprocess.check_call([from_env_bin("py.test"), "tests"]
+ list(extra_args), cwd=from_project_root())
@cli.command('travis-test')
def travis_test():
subprocess.check_call('createdb {0}'.format(APP_NAME), shell=True)
_run_unittest()
subprocess.check_call('dropdb {0}'.format(APP_NAME), shell=True)
def _wait_for_travis_availability():
click.echo(click.style("Waiting for service to become available on travis", fg='magenta'))
time.sleep(10)
for _ in range(10):
click.echo("Checking service...")
resp = requests.get("http://localhost/")
click.echo("Request returned {0}".format(resp.status_code))
if resp.status_code == 200:
break
time.sleep(5)
else:
raise RuntimeError("Web service did not become responsive")
click.echo(click.style("Service is up", fg='green'))
@cli.command()
@requires_env("app", "develop")
def shell():
from flask_app.app import create_app
from flask_app import models
app = create_app()
with app.app_context():
interact({
'db': db,
'app': app,
'models': models,
'db': models.db,
})
if __name__ == "__main__":
cli()