-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.py
234 lines (210 loc) · 7.89 KB
/
tasks.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
222
223
224
225
226
227
228
229
230
231
232
233
234
"""Define invoke tasks."""
import getpass
import os
import sys
from pathlib import Path
import dj_database_url
from django.core.management.utils import get_random_secret_key
from dotenv import find_dotenv, load_dotenv
from invoke import task
BASE_DIR = os.path.dirname(__file__)
BASE_DIRNAME = os.path.dirname(BASE_DIR)
PROJECT_DIRNAME = os.path.basename(os.path.dirname(__file__))
EMPEROR_MODE = True
VASSALS = f"{BASE_DIRNAME}/vassals"
USERNAME = os.getlogin()
ENV_FILE = f"{BASE_DIR}/.env"
SECRET_KEY = get_random_secret_key()
@task
def init(c):
"""Initialize project."""
try:
VENV_ROOT = str(Path(os.getenv("VIRTUAL_ENV")).parent).replace(
"/", "\/"
) # noqa
except TypeError:
print("Activate your virtualenv and run the inv command again")
return
EMPEROR_MODE = confirm(
"Do you want to configure your uWSGI vassal in emperor mode? (no=stand-alone)"
)
if EMPEROR_MODE:
vassals = (
input(
f"We will use '{VASSALS}' as the vassal directory or specify the path: "
)
or VASSALS
)
bonjour = confirm(
"Do you want to use Bonjour for OSX (Yes) or Avahi for Linux (No)? "
)
if bonjour:
ZEROCONF = "bonjour"
ZEROOPTS = "name=%(project_name).local,cname=localhost"
else:
ZEROCONF = "avahi"
ZEROOPTS = "%(project_name).local"
python_plugin = (
input(f"Specify python plugin to configure uwsgi (default: python3): ")
or "python3"
)
database = (
input(f"We'll use '{PROJECT_DIRNAME}' as database name or specify the name: ")
or PROJECT_DIRNAME
)
username = input(f"Enter the database user name: ")
password = getpass.getpass(f"Enter the database user password: ")
print("Compiling pip file in requirements")
c.run("make pip")
print("Installing libraries in requirements")
c.run("make dev")
if not os.path.exists("static"):
print("Making static directory")
c.run("mkdir static")
if not os.path.exists("media"):
print("Making media directory")
c.run("mkdir media")
ini_dir = f"{BASE_DIR}/uwsgiconf/local"
PYVERSION = f"{sys.version_info[0]}.{sys.version_info[1]}"
WORKAREA_ROOT = BASE_DIRNAME.replace("/", "\/") # noqa
print("Generating uwsgi user file")
if EMPEROR_MODE and not os.path.exists(f"{vassals}/{PROJECT_DIRNAME}.ini"):
c.run(f"cp {ini_dir}/vassal.ini.tpl {ini_dir}/{USERNAME}.ini")
c.run(
(
f'sed -i".bak" -e "s/USERNAME/{USERNAME}/g;s/ZEROCONF/{ZEROCONF}/g;'
f's/ZEROOPTS/{ZEROOPTS}/g;" {ini_dir}/{USERNAME}.ini'
)
)
c.run(
f"ln -s "
f"{BASE_DIR}/uwsgiconf/local/{USERNAME}.ini "
f"{vassals}/{PROJECT_DIRNAME}.ini"
)
else:
c.run(f"cp {ini_dir}/standalone.ini.tpl {ini_dir}/{USERNAME}.ini")
c.run(
f'sed -i".bak" -e "s/plugin = python3/plugin = {python_plugin}/g;"'
f" {ini_dir}/{USERNAME}.ini"
)
c.run(
f'sed -i".bak" -e "s/WORKAREA_ROOT/{WORKAREA_ROOT}/g;" {ini_dir}/{USERNAME}.ini'
)
c.run(f'sed -i".bak" -e "s/PYVERSION/{PYVERSION}/g;" {ini_dir}/{USERNAME}.ini')
c.run(f'sed -i".bak" -e "s/VENV_ROOT/{VENV_ROOT}/g;" {ini_dir}/{USERNAME}.ini')
print("Create env file")
if not os.path.exists(f"{ENV_FILE}"):
c.run(f"cp {ENV_FILE}.tpl {ENV_FILE}")
c.run(
(
f'sed -i".bak" -e '
f'"s/database/{database}/g;s/password/{password}/g;'
f's/secretkey/{SECRET_KEY}/g;s/username/{username}/g"'
f" {ENV_FILE}"
)
)
print("Collect static files")
c.run("make collectstatic")
createdb(c)
print("*** Next steps ***")
print(f"a) Check the uwsgiconf/local/{USERNAME}.ini and verify the python plugin")
print("b) Check the uwsgiconf/remote/globlal.ini file and verify the python plugin")
print("c) Check the domain in uwsgiconf/remote/[alpha|beta|production].ini file")
print("d) Configure the deploy/hosts file with server data")
print("e) Configure the deploy/[alpha|beta|production].yml files with correct data")
print(f"f) Configure the file by {PROJECT_DIRNAME}/settings.py")
if EMPEROR_MODE:
c.run(f"python -m webbrowser -t http://{PROJECT_DIRNAME}.local/")
@task
def createdb(c):
"""Create database."""
if confirm(
"Attention: you are creating the PostgreSQL DB. Do you want to proceed?"
):
db_name, db_host, db_port, db_user = get_db()
c.run(
f"createdb -e -h {db_host} -p {db_port} -U {db_user} -O {db_user} {db_name}"
)
if confirm("Attention: you are applying migrations. Do you want to proceed?"):
c.run("make migrate")
@task
def dropdb(c):
"""Drop database."""
if confirm("Warning, you are deleting the db. Are you sure you want to proceed?"):
db_name, db_host, db_port, db_user = get_db()
c.run(f"dropdb -e -h {db_host} -p {db_port} -U {db_user} {db_name}")
@task
def dumpdb(c):
"""Dump database."""
db_name, db_host, db_port, db_user = get_db()
c.run(
f"pg_dump -h {db_host} -p {db_port} -U {db_user} {db_name} | "
"bzip2 -9 > deploy/dump.sql.bz2"
)
@task
def gitinit(c, git_repository_url):
"""Initialize git repository."""
c.run(f'sed -i".bak" -e "s,GIT_REPOSITORY_URL,{git_repository_url},g;" README.md')
c.run("git init")
c.run("pre-commit install")
c.run("git add -A")
c.run("git commit -m 'Initial commit'")
c.run(f"git remote add origin {git_repository_url}")
c.run("git push -u origin master")
@task
def restart(c):
"""Restart uWSGI instance."""
c.run(f"touch uwsgiconf/local/{USERNAME}.ini")
def get_db():
"""Fetch database credentials."""
load_dotenv(find_dotenv())
db_url = os.getenv("DATABASE_URL")
db_default = dj_database_url.parse(db_url)
db_name = db_default["NAME"]
db_host = db_default["HOST"]
db_port = db_default["PORT"]
db_user = db_default["USER"]
return db_name, db_host, db_port, db_user
# NOTE: originally cribbed from fab 1's contrib.console.confirm
def confirm(question, assume_yes=True):
"""
Ask user a yes/no question and return their response as a boolean.
``question`` should be a simple, grammatically complete question such as
"Do you wish to continue?", and will have a string similar to ``" [Y/n] "``
appended automatically. This function will *not* append a question mark for
you.
By default, when the user presses Enter without typing anything, "yes" is
assumed. This can be changed by specifying ``affirmative=False``.
.. note::
If the user does not supplies input that is (case-insensitively) equal
to "y", "yes", "n" or "no", they will be re-prompted until they do.
:param str question: The question part of the input.
:param bool assume_yes:
Whether to assume the affirmative answer by default. Default value:
``True``.
:returns: A `bool`.
"""
# Set up suffix
if assume_yes:
suffix = "Y/n"
else:
suffix = "y/N"
# Loop till we get something we like
# TODO: maybe don't do this? It can be annoying. Turn into 'q'-for-quit?
while True:
# TODO: ensure that this is Ctrl-C friendly, ISTR issues with
# raw_input/input on some Python versions blocking KeyboardInterrupt.
response = input(f"{question} [{suffix}] ")
response = response.lower().strip() # Normalize
# Default
if not response:
return assume_yes
# Yes
if response in ["y", "yes"]:
return True
# No
if response in ["n", "no"]:
return False
# Didn't get empty, yes or no, so complain and loop
err = "I didn't understand you. Please specify '(y)es' or '(n)o'."
print(err, file=sys.stderr)