Repository navigation
Expand file tree
/
Copy pathinstall.py
More file actions
320 lines (256 loc) · 11.2 KB
/
Copy pathinstall.py
File metadata and controls
320 lines (256 loc) · 11.2 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#! /bin/python3
import argparse
import sys
import logging
import os
import http.client
import json
import textwrap
import secrets
import string
import subprocess
import shlex
import random
from urllib.parse import urlparse
API_HOST = os.environ.get('API_URL').strip('https://').strip('http://')
API_BASE_URI = '/api/v1'
CMD_ENV = {
'PATH': '/usr/sqlite330/bin:/usr/local/bin:/usr/bin:/bin',
'UMASK': '0002',
'LD_LIBRARY_PATH': '/usr/sqlite330/lib',
}
class OpalstackAPITool():
"""simple wrapper for http.client get and post"""
def __init__(self, host, base_uri, authtoken, user, password):
self.host = host
self.base_uri = base_uri
# if there is no auth token, then try to log in with provided credentials
if not authtoken:
endpoint = self.base_uri + '/login/'
payload = json.dumps({
'username': user,
'password': password
})
conn = http.client.HTTPSConnection(self.host)
conn.request('POST', endpoint, payload,
headers={'Content-type': 'application/json'})
result = json.loads(conn.getresponse().read())
if not result.get('token'):
logging.warn('Invalid username or password and no auth token provided, exiting.')
sys.exit()
else:
authtoken = result['token']
self.headers = {
'Content-type': 'application/json',
'Authorization': f'Token {authtoken}'
}
def get(self, endpoint):
"""GETs an API endpoint"""
endpoint = self.base_uri + endpoint
conn = http.client.HTTPSConnection(self.host)
conn.request('GET', endpoint, headers=self.headers)
return json.loads(conn.getresponse().read())
def post(self, endpoint, payload):
"""POSTs data to an API endpoint"""
endpoint = self.base_uri + endpoint
conn = http.client.HTTPSConnection(self.host)
conn.request('POST', endpoint, payload, headers=self.headers)
return json.loads(conn.getresponse().read())
def create_file(path, contents, writemode='w', perms=0o600):
"""make a file, perms are passed as octal"""
with open(path, writemode) as f:
f.write(contents)
os.chmod(path, perms)
logging.info(f'Created file {path} with permissions {oct(perms)}')
def download(url, localfile, writemode='wb', perms=0o600):
"""save a remote file, perms are passed as octal"""
logging.info(f'Downloading {url} as {localfile} with permissions {oct(perms)}')
u = urlparse(url)
if u.scheme == 'http':
conn = http.client.HTTPConnection(u.netloc)
else:
conn = http.client.HTTPSConnection(u.netloc)
conn.request('GET', u.path)
r = conn.getresponse()
with open(localfile, writemode) as f:
while True:
data = r.read(4096)
if data:
f.write(data)
else:
break
os.chmod(localfile, perms)
logging.info(f'Downloaded {url} as {localfile} with permissions {oct(perms)}')
def gen_password(length=20):
"""makes a random password"""
chars = string.ascii_letters + string.digits
return ''.join(secrets.choice(chars) for i in range(length))
def run_command(cmd, env=CMD_ENV):
"""runs a command, returns output"""
logging.info(f'Running: {cmd}')
try:
result = subprocess.check_output(shlex.split(cmd), env=env)
except subprocess.CalledProcessError as e:
logging.debug(e.output)
return result
def add_cronjob(cronjob):
"""appends a cron job to the user's crontab"""
homedir = os.path.expanduser('~')
tmpname = f'{homedir}/.tmp{gen_password()}'
tmp = open(tmpname, 'w')
subprocess.run('crontab -l'.split(),stdout=tmp)
tmp.write(f'{cronjob}\n')
tmp.close()
cmd = f'crontab {tmpname}'
doit = run_command(cmd)
cmd = run_command(f'rm -f {tmpname}')
logging.info(f'Added cron job: {cronjob}')
def main():
"""run it"""
# grab args from cmd or env
parser = argparse.ArgumentParser(
description='Installs Django on Opalstack account')
parser.add_argument('-i', dest='app_uuid', help='UUID of the base app',
default=os.environ.get('UUID'))
parser.add_argument('-n', dest='app_name', help='name of the base app',
default=os.environ.get('APPNAME'))
parser.add_argument('-t', dest='opal_token', help='API auth token',
default=os.environ.get('OPAL_TOKEN'))
parser.add_argument('-u', dest='opal_user', help='Opalstack account name',
default=os.environ.get('OPAL_USER'))
parser.add_argument('-p', dest='opal_password', help='Opalstack account password',
default=os.environ.get('OPAL_PASS'))
args = parser.parse_args()
# init logging
logging.basicConfig(level=logging.INFO,
format='[%(asctime)s] %(levelname)s: %(message)s')
# go!
logging.info(f'Started installation of Django app {args.app_name}')
api = OpalstackAPITool(API_HOST, API_BASE_URI, args.opal_token, args.opal_user, args.opal_password)
appinfo = api.get(f'/app/read/{args.app_uuid}')
appdir = f'/home/{appinfo["osuser_name"]}/apps/{appinfo["name"]}'
# create tmp dir
os.mkdir(f'{appdir}/tmp', 0o700)
logging.info(f'Created directory {appdir}/tmp')
CMD_ENV['TMPDIR'] = f'{appdir}/tmp'
# create virtualenv
python_executable_path = run_command('which python3.13').decode('utf-8').strip()
cmd = f'{python_executable_path} -m venv {appdir}/env'
doit = run_command(cmd)
logging.info(f'Created virtualenv at {appdir}/env')
# install uwsgi
cmd = f'scl enable devtoolset-11 -- {appdir}/env/bin/pip install uwsgi'
doit = run_command(cmd)
perms = run_command(f'chmod 700 {appdir}/env/bin/uwsgi')
logging.info('Installed latest uWSGI into virtualenv')
# install django
cmd = f'scl enable devtoolset-11 -- {appdir}/env/bin/pip install django==6.0.9'
doit = run_command(cmd)
logging.info('Installed latest Django into virtualenv')
# create project dir
os.mkdir(f'{appdir}/myproject', 0o700)
logging.info(f'Created Django project directory {appdir}/myproject')
# run startproject with dir option
cmd = f'{appdir}/env/bin/django-admin startproject myproject {appdir}/myproject'
doit = run_command(cmd)
logging.info(f'Populated Django project directory {appdir}/myproject')
# django config
# set ALLOWED_HOSTS
cmd = f'''sed -r -i "s/^ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['\*'\]/" {appdir}/myproject/myproject/settings.py'''
doit = run_command(cmd)
logging.info(f'Wrote initial Django config to {appdir}/myproject/myproject/settings.py')
# uwsgi config
uwsgi_conf = textwrap.dedent(f'''\
[uwsgi]
master = True
http-socket = 127.0.0.1:{appinfo["port"]}
env = LD_LIBRARY_PATH=/usr/sqlite330/lib
virtualenv = {appdir}/env/
daemonize = /home/{appinfo["osuser_name"]}/logs/apps/{appinfo["name"]}/uwsgi.log
pidfile = {appdir}/tmp/uwsgi.pid
workers = 2
threads = 2
# adjust the following to point to your project
python-path = {appdir}/myproject
wsgi-file = {appdir}/myproject/myproject/wsgi.py
touch-reload = {appdir}/myproject/myproject/wsgi.py
''')
create_file(f'{appdir}/uwsgi.ini', uwsgi_conf, perms=0o600)
# start script
start_script = textwrap.dedent(f'''\
#!/bin/bash
export TMPDIR={appdir}/tmp
export LD_LIBRARY_PATH=/usr/sqlite330/lib
mkdir -p {appdir}/tmp
PIDFILE="{appdir}/tmp/uwsgi.pid"
if [ -e "$PIDFILE" ] && (pgrep -u {appinfo["osuser_name"]} | grep -x -f $PIDFILE &> /dev/null); then
echo "uWSGI for {appinfo["name"]} already running."
exit 99
fi
{appdir}/env/bin/uwsgi --ini {appdir}/uwsgi.ini
echo "Started uWSGI for {appinfo["name"]}."
''')
create_file(f'{appdir}/start', start_script, perms=0o700)
# stop script
stop_script = textwrap.dedent(f'''\
#!/bin/bash
PIDFILE="{appdir}/tmp/uwsgi.pid"
if [ ! -e "$PIDFILE" ]; then
echo "$PIDFILE missing, maybe uWSGI is already stopped?"
exit 99
fi
PID=$(cat $PIDFILE)
if [ -e "$PIDFILE" ] && (pgrep -u {appinfo["osuser_name"]} | grep -x -f $PIDFILE &> /dev/null); then
{appdir}/env/bin/uwsgi --stop $PIDFILE
sleep 3
fi
if [ -e "$PIDFILE" ] && (pgrep -u {appinfo["osuser_name"]} | grep -x -f $PIDFILE &> /dev/null); then
echo "uWSGI did not stop, killing it."
sleep 3
kill -9 $PID
fi
rm -f $PIDFILE
echo "Stopped."
''')
create_file(f'{appdir}/stop', stop_script, perms=0o700)
# cron
m = random.randint(0,9)
croncmd = f'0{m},1{m},2{m},3{m},4{m},5{m} * * * * {appdir}/start > /dev/null 2>&1'
cronjob = add_cronjob(croncmd)
# make README
readme = textwrap.dedent(f'''\
# Opalstack Django README
## Post-install steps
Please take the following steps before you begin to use your Django
installation:
1. Connect your Django application to a site route in the control panel.
2. Edit {appdir}/myproject/myproject/settings.py to set ALLOWED_HOSTS
to include your site's domains. Example:
ALLOWED_HOSTS = ['domain.com', 'www.domain.com']
3. Run the following commands to restart your Django instance:
{appdir}/stop
{appdir}/start
## Using your own project
If you want to serve your own Django project from this instance:
1. Upload your project directory to {appdir}
2. Activate the app's environment:
source {appdir}/env/bin/activate
3. Install your project's Python dependencies with pip.
4. Edit {appdir}/uwsgi.ini to point `wsgi-file` and `touch-reload` at your project's WSGI handler
5. Run the following commands to restart your Django instance:
{appdir}/stop
{appdir}/start
## More info
See https://docs.opalstack.com/topic-guides/django/ for more information.
''')
create_file(f'{appdir}/README', readme)
# start it
cmd = f'{appdir}/start'
startit = run_command(cmd)
# finished, push a notice with credentials
msg = f'See README in app directory for final steps.'
payload = json.dumps([{'id': args.app_uuid }])
finished=api.post('/app/installed/', payload)
logging.info(f'Completed installation of Django app {args.app_name}')
if __name__ == '__main__':
main()