forked from swirlai/swirl-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswirl.py
executable file
·572 lines (469 loc) · 16.9 KB
/
swirl.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
'''
@author: Sid Probstein
@contact: [email protected]
@version: Swirl 1.x
'''
import re
import argparse
from asyncio.subprocess import STDOUT
import sys
import os
import subprocess
from subprocess import SubprocessError
import json
import time
import signal
from datetime import datetime
from swirl_server import settings
module_name = 'swirl.py'
from swirl.banner import SWIRL_BANNER, bcolors, SWIRL_VERSION
from swirl.utils import get_page_fetcher_or_none, is_running_celery_redis
from swirl.services import SWIRL_SERVICES_DEBUG, SWIRL_SERVICES_DEBUG_DICT, SERVICES, SERVICES_DICT
SWIRL_CORE_SERVICES = ['django', 'celery-worker']
SWIRL_VERSION_CHECK_URL = 'http://updatecheck.swirl.today/'
COMMAND_LIST = [ 'help', 'start', 'debug', 'start_sleep', 'stop', 'restart', 'migrate', 'setup', 'status', 'watch', 'logs' ]
def get_swirl_version():
"""
Fetch the current version of swirl and if it fails for any reason, return the current
version instead.
"""
version = SWIRL_VERSION
url = SWIRL_VERSION_CHECK_URL
try:
page = get_page_fetcher_or_none(url=url).get_page()
version_text = page.get_text_strip_html()
match = re.search(r'(\d+\.\d+(?:\.\d+){0,2})', version_text)
if match:
version = match.group(1)
except Exception as err:
print('Error while checking version; startup continuing')
finally:
return version.strip()
def service_is_retired(service_name):
ret = False
try:
for service in SERVICES:
if service['name'] == service_name:
if service['retired']:
print(f"{service_name} is retired, ignoring\n", end='')
ret = True
except Exception as err:
print(f"{err} checking retired service")
finally:
return ret
def check_pid(pid):
proc = subprocess.run(['ps','-p',str(pid)], capture_output=True)
result = proc.stdout.decode('UTF-8')
return str(pid) in result
def show_pids(pid_string):
proc = subprocess.run(['ps','-p', pid_string[:-1]], capture_output=True)
result = proc.stdout.decode('UTF-8')
return result
##################################################
def load_swirl_file():
if os.path.exists('.swirl'):
try:
swirl_file = open('./.swirl', 'r')
dict_pid = json.load(swirl_file)
swirl_file.close()
except OSError as err:
print(f"Error: {err}")
return False
return dict_pid
else:
return {}
def write_swirl_file(dict_pid):
try:
swirl_file = open('./.swirl', 'w')
result = swirl_file.write(json.dumps(dict_pid))
swirl_file.close()
except OSError as err:
print(f"Error: {err}")
return False
return True
##################################################
def launch(name, path):
# prepare the path
path_list = path.split()
# create the log file
try:
f = open(f'./logs/{name}.log', 'ab')
except OSError as err:
print(f"Error: {err} creating: ./logs/{name}.log")
return -1
try:
process = subprocess.Popen(path_list, stdout=f, stderr=subprocess.STDOUT)
except Exception as err: # Broad exception okay here.
print(f"Error: {err} creating process: {' '.join(path_list)}")
return -1
# do not close this file
if process.returncode == None:
return process.pid
else:
return -1
##################################################
def start(service_list):
dict_pid = {}
# check if .swirl exists
dict_pid = load_swirl_file()
if dict_pid:
for service_name in dict_pid:
if service_name in service_list:
print(f"Error: {service_name} appears to be running, please check .swirl or remove it; exiting...")
return False
# end for
# items in service_list are NOT in dict_pid, so continue and start them
# end if
if not os.path.exists('./logs'):
print("Warning: logs directory does not exist, creating it")
os.mkdir('./logs')
if not is_running_celery_redis():
print(f"Error: Celery requires redis, settings.CELERY_BROKER_URL:{settings.CELERY_BROKER_URL}\n"
f"settings.CELERY_RESULT_BACKEND:{settings.CELERY_RESULT_BACKEND} but it does not appear to be running,\n"
"please consult the admin guide at https://docs.swirlaiconnect.com/Admin-Guide.html.")
return False
# start service_list
pids = ""
flag = False
for service_name in service_list:
if service_name in SERVICES_DICT:
if service_is_retired(service_name=service_name):
continue
print(f"Start: {service_name} -> {SERVICES_DICT[service_name]} ... ", end='')
result = launch(service_name, SERVICES_DICT[service_name])
time.sleep(5)
if result > 0:
print(f'Ok, pid: {result}')
dict_pid[service_name] = result
pids = pids + str(result) + ','
else:
print(f"Error: {result}, check logs for output")
flag = True
# end if
else:
print(f"Warning: unknown service: {service_name}, ignoring\n")
# end if
# end for
if len(dict_pid) > 0:
# something was started, write the file
print("Updating .swirl... ", end='')
write_swirl_file(dict_pid)
print("Ok")
print()
print(show_pids(pids))
if flag:
return False
try:
sw_version = get_swirl_version()
if sw_version != SWIRL_VERSION:
print(f"You're using version {SWIRL_VERSION} of Swirl, and version {sw_version} is available.")
else:
print(f"You're using version {SWIRL_VERSION} of Swirl, the current version.")
except Exception as err:
print(f"INFO {err} getting version, continuing start")
return True
##################################################
def start_sleep (service_list):
status = start(service_list)
return status
##################################################
def debug(service_list):
pass
##################################################
def watch(service_list):
while 1:
try:
print(datetime.now())
print()
x = status(service_list)
time.sleep(60)
except KeyboardInterrupt:
break
return True
##################################################
def logs(service_list):
print("tail -f logs/*.log - hit ^C to stop:")
try:
p = subprocess.Popen(['tail','-f','logs/django.log','logs/celery-worker.log'], stdout=subprocess.PIPE)
while p.poll() is None:
l = p.stdout.readline()
print(l.decode("utf-8").replace('\n',''))
p.kill()
return True
except KeyboardInterrupt:
p.kill()
return True
##################################################
def status(service_list):
# check if .swirl exists
dict_pid = load_swirl_file()
if not dict_pid:
print(f"Swirl does not appear to be running - .swirl not found")
return True
pid_string = ""
for service_name in dict_pid:
if service_name in service_list:
if service_is_retired(service_name=service_name):
continue
pid_string = pid_string + str(dict_pid[service_name]) + ','
print(f"Service: {service_name}...", end='')
if check_pid(dict_pid[service_name]):
print(f"RUNNING, pid:{dict_pid[service_name]}")
else:
print(f"UNKNOWN, pid:{dict_pid[service_name]} not found")
# end if
# end if
# end for
print()
print(show_pids(pid_string))
return True
##################################################
def migrate(service_list):
print("Checking Migrations:")
any_change = False
proc = subprocess.run(['python','manage.py','makemigrations'], capture_output=True)
if proc.returncode != 0:
print(f"Error: {proc.stderr.decode('UTF-8')}")
return False
result = proc.stdout.decode('UTF-8')
if not 'No changes detected' in result:
any_change = True
else:
print(result)
proc = subprocess.run(['python','manage.py','makemigrations','swirl'], capture_output=True)
if proc.returncode != 0:
print(f"Error: {proc.stderr.decode('UTF-8')}")
return False
result = proc.stdout.decode('UTF-8')
if not 'No changes detected' in result:
any_change = True
else:
print(result)
print()
print("Migrating:")
print()
proc = subprocess.run(['python','manage.py','migrate'], capture_output=True)
if proc.returncode != 0:
print(f"Error: {proc.stderr.decode('UTF-8')}")
return False
result = proc.stdout.decode('UTF-8')
print(result)
return True
##################################################
def stop(service_list):
dict_pid = {}
# check if .swirl exists
dict_pid = load_swirl_file()
if not dict_pid:
print(f"Error: .swirl not found, exiting")
return False
# stop service_list
pids = ""
stopped_names = []
flag = False
for service_name in dict_pid:
# if in .swirl
if service_name in service_list:
if service_is_retired(service_name=service_name):
continue
# if specified as argument to command
if service_name == SERVICES[0]['name']:
# except for the first listed service
continue
print(f"Stop: {service_name}, pid: {dict_pid[service_name]}... ", end='')
pid = int(dict_pid[service_name])
try:
os.kill(pid, 0) # if the pid doesn't exist, this will throw an exception
os.kill(pid, signal.SIGTERM)
except OSError as err:
print(f"Error: {err}")
flag = True
time.sleep(10)
# confirm
if check_pid(pid):
print(f"Error: still running!")
flag = True
else:
print(f"Ok")
stopped_names.append(service_name)
pids = pids + str(pid) + ','
# end if
# end if
# end for
# shut down the first service, last - IF specified
if SERVICES[0]['name'] in dict_pid:
if SERVICES[0]['name'] in service_list:
print(f"Stop: {SERVICES[0]['name']}, pid_group: {dict_pid[SERVICES[0]['name']]}... ", end='')
pid = int(dict_pid[SERVICES[0]['name']])
try:
pgrp = os.getpgid(pid)
os.killpg(pgrp, signal.SIGTERM)
# os.kill(pid, 0) # if the pid doesn't exist, this will throw an exception
# os.kill(pid, signal.SIGTERM)
except OSError as err:
print(f"Error: {err}")
flag = True
time.sleep(10)
# confirm
if check_pid(pid):
print(f"Error: still running!")
flag = True
else:
print(f"Ok")
stopped_names.append(SERVICES[0]['name'])
pids = pids + str(pid) + ','
# end if
# end if
print()
print(show_pids(pids))
for service_name in stopped_names:
del dict_pid[service_name]
if dict_pid:
# print("Removing stopped services from .swirl... ", end='')
write_swirl_file(dict_pid)
# print("Ok")
if flag:
print("Warning: at least one error occured, check .swirl for remaining pids")
return False
if not dict_pid:
# print("All services stopped, removing .swirl... ", end='')
try:
os.remove('.swirl')
except OSError as err:
print(f"Error: {err}")
return False
# print("Ok")
# end if
return True
##################################################
def restart(service_list):
print(f"Restart: {', '.join(service_list)}")
result = stop(service_list)
if result:
time.sleep(5)
result = start(service_list)
else:
print(f"Error stopping: {' '.join(service_list)}")
return result
# end if
if not result:
print(f"Error starting: {' '.join(service_list)}")
return result
##################################################
def help(service_list):
# Filter out the retired services
active_services = [service for service in service_list if not SERVICES[service].get('retired', False)]
print("Usage: python swirl.py <command> [<service-list>]\n")
# Now use the active_services list to print the available services
print(f"Available commands: {', '.join(COMMAND_LIST)}")
print(f"Available services: {', '.join(active_services)}, core ({', '.join(SWIRL_CORE_SERVICES)})\n")
print("The start, status, stop and restart commands default to all Swirl services.")
print("Most optionally accept one or more Swirl service names, separated by spaces.")
print()
result = True
##################################################
def setup(service_list):
print("Setting Up Swirl:")
result = migrate(service_list)
if not result:
print(f"Error: error during migration, check console output")
return False
if not os.path.exists(os.getcwd() + '/static'):
# collect statics
print()
print(f"Collecting Statics:")
print()
proc = subprocess.run(['python','manage.py','collectstatic'], capture_output=True)
if proc.returncode != 0:
print(f"Error: {proc.stderr.decode('UTF-8')}")
return False
result = proc.stdout.decode('UTF-8')
print(result)
if 'static files copied' in result:
print("Ok")
return True
else:
return False
else:
return True
##################################################
##################################################
def main(argv):
global SERVICES
global SERVICES_DICT
print(f"{SWIRL_BANNER}")
print()
parser = argparse.ArgumentParser(description="Manage the Swirl server")
parser.add_argument('command', nargs='+', help="Specify 'help' to get a list of available commands")
parser.add_argument('-d', '--debug', action="store_true", help="start Swirl in debug mode")
args = parser.parse_args()
if args.debug:
SERVICES = SWIRL_SERVICES_DEBUG
SERVICES_DICT = SWIRL_SERVICES_DEBUG_DICT
# check to see that we are in a directory with swirl under it, and manage.py in it
dir = os.getcwd()
if not os.path.exists(dir + '/swirl'):
print(f"{bcolors.FAIL}Error: swirl subdirectory is missing, are you sure '{dir}' is the right directory?{bcolors.ENDC}")
if not os.path.exists(dir + '/manage.py'):
print(f"{bcolors.FAIL}Error: manage.py is missing, are you sure '{dir}' is the right directory?{bcolors.ENDC}")
if not args.command[0] in COMMAND_LIST:
print(f"Unknown command: '{args.command[0]}'")
return 0
else:
service_list = args.command[1:]
if service_list == [] or service_list[0].lower() == 'all':
service_list = []
for service in SERVICES:
# only add default services, since none was specified
if 'default' in service:
if service['default']:
service_list.append(service['name'])
elif service_list[0].lower() == 'core':
service_list = SWIRL_CORE_SERVICES
else:
for service in service_list:
if not service in SERVICES_DICT:
print(f"{bcolors.WARNING}Unknown service: {service}{bcolors.ENDC}")
print(f"Available services: ", end='')
for swirl_service in SERVICES:
if not swirl_service['retired']:
print(f"{swirl_service['name']} ", end='')
print()
return False
# end if
# end for
# run the command
command = args.command[0]
result = COMMAND_DISPATCH.get(command)(service_list=service_list)
# end if
if result == False:
print(f"{bcolors.FAIL}Command {args.command[0]} reported an error{bcolors.ENDC}")
return 1
else:
print(f"{bcolors.OKGREEN}Command successful!{bcolors.ENDC}")
if args.command[0] == 'start_sleep':
while 1:
try:
time.sleep(1)
except KeyboardInterrupt:
return 0
else:
return 0
# end if
COMMAND_DISPATCH = {
'help': help,
'start': start,
'debug': debug,
'start_sleep': start_sleep,
'stop' : stop,
'restart': restart,
'migrate': migrate,
'setup' : setup,
'status': status,
'watch':watch,
'logs': logs
}
#############################################
if __name__ == "__main__":
main(sys.argv)
# end