Repository navigation
Expand file tree
/
Copy pathinstall.py
More file actions
291 lines (234 loc) · 9.44 KB
/
Copy pathinstall.py
File metadata and controls
291 lines (234 loc) · 9.44 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
#! /usr/bin/python3.6
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/local/bin:/usr/bin:/bin','UMASK': '0002',}
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)
connread = conn.getresponse().read()
print(connread)
return json.loads(connread)
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, cwd=None, env=CMD_ENV):
"""runs a command, returns output"""
logging.info(f'Running: {cmd}')
try:
result = subprocess.check_output(shlex.split(cmd), cwd=cwd, 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 Node.js web app 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 Node.js 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"]}'
CMD_ENV['HOME'] = f'/home/{appinfo["osuser_name"]}/'
# make myproject/index.js
cmd = f'mkdir -p {appdir}/myproject'
doit = run_command(cmd)
NEWLINE = '\\n'
appjs = textwrap.dedent(f'''\
const http = require('http');
const hostname = '127.0.0.1';
const port = {appinfo["port"]};
const server = http.createServer((req, res) => {{
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World from Node.js{NEWLINE}');
}});
server.listen(port, hostname, () => {{
console.log(`Server running at http://${{hostname}}:${{port}}/`);
}});''')
create_file(f'{appdir}/myproject/index.js', appjs, perms=0o600)
# make myproject/index.js
pkgjson = textwrap.dedent(f'''\
{{
"name": "myproject",
"version": "1.0.0",
"description": "Hello world",
"main": "index.js",
"scripts": {{
"start": "node index.js"
}}
}}''')
create_file(f'{appdir}/myproject/package.json', pkgjson, perms=0o600)
# start script
start_script = textwrap.dedent(f'''\
#!/bin/bash
APPNAME={appinfo["name"]}
# set node version via scl
source scl_source enable nodejs20
NODE=$( which node )
NPM=$( which npm )
# set your project info here
PROJECT=myproject
STARTCMD="$NPM start"
APPDIR=$HOME/apps/$APPNAME
LOGDIR=$HOME/logs/apps/$APPNAME
TMPDIR=$APPDIR/tmp
PROJECTDIR=$APPDIR/$PROJECT
PIDFILE=$TMPDIR/node.pid
mkdir -p $APPDIR/tmp
if [ -e "$PIDFILE" ] && (pgrep -F $PIDFILE &> /dev/null); then
echo "$APPNAME already running."
exit 99
fi
/usr/sbin/daemonize -c $PROJECTDIR -a -e $LOGDIR/error.log -o $LOGDIR/console.log -p $PIDFILE $STARTCMD
echo "Started $APPNAME."
''')
create_file(f'{appdir}/start', start_script, perms=0o700)
# stop script
stop_script = textwrap.dedent(f'''\
#!/bin/bash
APPNAME={appinfo["name"]}
PIDFILE="$HOME/apps/$APPNAME/tmp/node.pid"
if [ ! -e "$PIDFILE" ]; then
echo "$PIDFILE missing, maybe $APPNAME is already stopped?"
exit 99
fi
if [ -e "$PIDFILE" ] && (pgrep -F $PIDFILE &> /dev/null); then
pkill -g $(cat $PIDFILE)
sleep 3
fi
if [ -e "$PIDFILE" ] && (pgrep -F $PIDFILE &> /dev/null); then
echo "$APPNAME did not stop, killing it."
sleep 3
pkill -9 -g $(cat $PIDFILE)
fi
rm -f $PIDFILE
echo "Stopped $APPNAME."
''')
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 Node.js README
## Controlling your app
Start your app by running:
{appdir}/start
Stop your app by running:
{appdir}/stop
## Installing modules
If you want to install Node modules in your app directory:
cd {appdir}
npm install modulename
''')
create_file(f'{appdir}/README', readme)
# start it
cmd = f'{appdir}/start'
startit = run_command(cmd)
# finished, push a notice
msg = f'See README in app directory for more info.'
payload = json.dumps([{'id': args.app_uuid}])
finished=api.post('/app/installed/', payload)
logging.info(f'Completed installation of Node.js app {args.app_name}')
if __name__ == '__main__':
main()