2016-09-01 15:09:58 +00:00
|
|
|
#!/usr/bin/env python
|
2017-12-30 14:53:59 +00:00
|
|
|
# coding: utf-8
|
2016-09-01 15:10:28 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2017-12-25 17:54:10 +00:00
|
|
|
import threading
|
2017-12-21 18:08:29 +00:00
|
|
|
import time
|
2017-12-25 17:54:10 +00:00
|
|
|
import argparse
|
2017-12-26 16:14:46 +00:00
|
|
|
import sys
|
2016-09-01 15:10:28 +00:00
|
|
|
|
2017-12-21 18:08:29 +00:00
|
|
|
from apps import __version__
|
2016-09-01 15:10:28 +00:00
|
|
|
|
2017-12-21 18:08:29 +00:00
|
|
|
try:
|
|
|
|
from config import config as CONFIG
|
2016-09-01 15:10:28 +00:00
|
|
|
except ImportError:
|
|
|
|
CONFIG = type('_', (), {'__getattr__': None})()
|
|
|
|
|
2018-01-01 15:45:27 +00:00
|
|
|
os.environ["PYTHONIOENCODING"] = "UTF-8"
|
2016-09-01 15:10:28 +00:00
|
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
2017-12-21 18:08:29 +00:00
|
|
|
APPS_DIR = os.path.join(BASE_DIR, 'apps')
|
|
|
|
HTTP_HOST = CONFIG.HTTP_BIND_HOST or '127.0.0.1'
|
|
|
|
HTTP_PORT = CONFIG.HTTP_LISTEN_PORT or 8080
|
2017-12-22 13:42:12 +00:00
|
|
|
DEBUG = CONFIG.DEBUG
|
2017-12-21 18:08:29 +00:00
|
|
|
LOG_LEVEL = CONFIG.LOG_LEVEL
|
|
|
|
WORKERS = 4
|
2016-09-01 15:10:28 +00:00
|
|
|
|
2017-12-25 17:54:10 +00:00
|
|
|
EXIT_EVENT = threading.Event()
|
2017-12-29 15:53:45 +00:00
|
|
|
processes = {}
|
2017-12-26 16:14:46 +00:00
|
|
|
|
2017-12-25 04:22:49 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.join(BASE_DIR, "data", "static"))
|
|
|
|
os.makedirs(os.path.join(BASE_DIR, "data", "media"))
|
|
|
|
except:
|
|
|
|
pass
|
2016-09-01 15:10:28 +00:00
|
|
|
|
2017-12-25 17:54:10 +00:00
|
|
|
|
|
|
|
def make_migrations():
|
|
|
|
print("Check database change, make migrations")
|
2017-12-27 17:18:50 +00:00
|
|
|
os.chdir(os.path.join(BASE_DIR, 'apps'))
|
|
|
|
subprocess.call('python manage.py migrate', shell=True)
|
2017-12-25 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def collect_static():
|
|
|
|
print("Collect static files")
|
|
|
|
os.chdir(os.path.join(BASE_DIR, 'apps'))
|
|
|
|
subprocess.call('python manage.py collectstatic --no-input', shell=True)
|
|
|
|
|
|
|
|
|
2017-12-21 18:08:29 +00:00
|
|
|
def start_gunicorn():
|
2017-12-25 17:54:10 +00:00
|
|
|
print("- Start Gunicorn WSGI HTTP Server")
|
|
|
|
make_migrations()
|
|
|
|
collect_static()
|
2017-12-21 18:08:29 +00:00
|
|
|
os.chdir(APPS_DIR)
|
2017-12-30 12:45:54 +00:00
|
|
|
cmd = "gunicorn jumpserver.wsgi -b {}:{} -w {} ".format(
|
|
|
|
HTTP_HOST, HTTP_PORT, WORKERS
|
|
|
|
)
|
|
|
|
log_format = '%(h)s %(t)s "%(r)s" %(s)s %(b)s '
|
|
|
|
log = " --access-logfile - --access-logformat '{}' ".format(log_format)
|
|
|
|
cmd += log
|
2017-12-22 13:42:12 +00:00
|
|
|
if DEBUG:
|
|
|
|
cmd += " --reload"
|
2017-12-31 04:20:08 +00:00
|
|
|
p = subprocess.Popen(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
|
2017-12-26 16:14:46 +00:00
|
|
|
return p
|
2016-09-01 15:10:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_celery():
|
2017-12-21 18:08:29 +00:00
|
|
|
print("- Start Celery as Distributed Task Queue")
|
|
|
|
os.chdir(APPS_DIR)
|
2017-12-24 16:36:14 +00:00
|
|
|
# Todo: Must set this environment, otherwise not no ansible result return
|
2017-12-22 13:42:12 +00:00
|
|
|
os.environ.setdefault('PYTHONOPTIMIZE', '1')
|
2017-12-25 17:54:10 +00:00
|
|
|
|
2017-12-28 03:39:12 +00:00
|
|
|
cmd = """
|
2017-12-30 14:26:05 +00:00
|
|
|
export C_FORCE_ROOT=1;
|
|
|
|
celery -A common worker -l {}
|
2017-12-28 03:39:12 +00:00
|
|
|
""".format(LOG_LEVEL.lower())
|
2017-12-25 17:54:10 +00:00
|
|
|
|
2017-12-26 16:14:46 +00:00
|
|
|
p = subprocess.Popen(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
return p
|
2016-09-01 15:10:28 +00:00
|
|
|
|
|
|
|
|
2017-12-21 18:08:29 +00:00
|
|
|
def start_beat():
|
|
|
|
print("- Start Beat as Periodic Task Scheduler")
|
|
|
|
os.chdir(APPS_DIR)
|
2017-12-22 13:42:12 +00:00
|
|
|
os.environ.setdefault('PYTHONOPTIMIZE', '1')
|
2017-12-25 17:54:10 +00:00
|
|
|
os.environ.setdefault('C_FORCE_ROOT', '1')
|
2018-01-01 13:04:43 +00:00
|
|
|
pidfile = '/tmp/beat.pid'
|
2017-12-28 06:34:17 +00:00
|
|
|
if os.path.exists(pidfile):
|
2018-01-01 15:45:27 +00:00
|
|
|
print("Beat pid file `{}` exist, remove it".format(pidfile))
|
2017-12-28 06:34:17 +00:00
|
|
|
os.unlink(pidfile)
|
2018-01-01 13:04:43 +00:00
|
|
|
time.sleep(0.5)
|
2018-01-01 12:54:48 +00:00
|
|
|
|
|
|
|
if os.path.exists(pidfile):
|
2018-01-01 15:45:27 +00:00
|
|
|
print("Beat pid file `{}` exist yet, may be something wrong".format(pidfile))
|
2018-01-01 12:54:48 +00:00
|
|
|
os.unlink(pidfile)
|
2018-01-01 13:04:43 +00:00
|
|
|
time.sleep(0.5)
|
2018-01-01 12:54:48 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
scheduler = "django_celery_beat.schedulers:DatabaseScheduler"
|
2017-12-28 02:52:40 +00:00
|
|
|
options = "--pidfile {} -l {} --scheduler {} --max-interval 60".format(
|
|
|
|
pidfile, LOG_LEVEL, scheduler,
|
|
|
|
)
|
|
|
|
cmd = 'celery -A common beat {} '.format(options)
|
2017-12-26 16:14:46 +00:00
|
|
|
p = subprocess.Popen(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
return p
|
2016-09-01 15:10:28 +00:00
|
|
|
|
|
|
|
|
2017-12-25 17:54:10 +00:00
|
|
|
def start_service(services):
|
2017-12-21 18:08:29 +00:00
|
|
|
print(time.ctime())
|
|
|
|
print('Jumpserver version {}, more see https://www.jumpserver.org'.format(
|
|
|
|
__version__))
|
|
|
|
print('Quit the server with CONTROL-C.')
|
2017-12-25 17:54:10 +00:00
|
|
|
|
2017-12-26 16:14:46 +00:00
|
|
|
services_all = {
|
|
|
|
"gunicorn": start_gunicorn,
|
|
|
|
"celery": start_celery,
|
|
|
|
"beat": start_beat
|
|
|
|
}
|
|
|
|
|
|
|
|
if 'all' in services:
|
|
|
|
for name, func in services_all.items():
|
|
|
|
processes[name] = func()
|
|
|
|
else:
|
|
|
|
for name in services:
|
|
|
|
func = services_all.get(name)
|
|
|
|
processes[name] = func()
|
|
|
|
|
|
|
|
stop_event = threading.Event()
|
|
|
|
while not stop_event.is_set():
|
|
|
|
for name, proc in processes.items():
|
|
|
|
if proc.poll() is not None:
|
|
|
|
print("\n\n" + "####"*10 + " ERROR OCCUR " + "####"*10)
|
|
|
|
print("Start service {} [FAILED]".format(name))
|
|
|
|
for _, p in processes.items():
|
|
|
|
p.terminate()
|
|
|
|
stop_event.set()
|
|
|
|
print("Exited".format(name))
|
|
|
|
break
|
|
|
|
time.sleep(5)
|
2016-09-01 15:09:58 +00:00
|
|
|
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
def stop_service():
|
|
|
|
for name, proc in processes.items():
|
|
|
|
print("Stop service {}".format(name))
|
|
|
|
proc.terminate()
|
|
|
|
|
2017-12-30 14:26:05 +00:00
|
|
|
if os.path.exists("/tmp/beat.pid"):
|
|
|
|
os.unlink('/tmp/beat.pid')
|
|
|
|
|
2017-12-29 15:53:45 +00:00
|
|
|
|
2018-01-01 15:45:27 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description="Jumpserver start tools")
|
|
|
|
parser.add_argument("services", type=str, nargs='+', default="all",
|
|
|
|
choices=("all", "gunicorn", "celery", "beat"),
|
|
|
|
help="The service to start",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
start_service(args.services)
|
2016-09-01 15:09:58 +00:00
|
|
|
|