2018-03-19 03:26:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# coding: utf-8
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
import argparse
|
2019-08-21 12:27:21 +00:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
2022-12-20 08:48:18 +00:00
|
|
|
import os
|
2018-03-19 03:26:51 +00:00
|
|
|
import sys
|
2022-12-20 08:48:18 +00:00
|
|
|
import time
|
|
|
|
|
2021-12-14 09:03:08 +00:00
|
|
|
import django
|
|
|
|
from django.core import management
|
|
|
|
from django.db.utils import OperationalError
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
2021-12-14 09:03:08 +00:00
|
|
|
APP_DIR = os.path.join(BASE_DIR, 'apps')
|
|
|
|
|
|
|
|
os.chdir(APP_DIR)
|
|
|
|
sys.path.insert(0, APP_DIR)
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jumpserver.settings")
|
|
|
|
django.setup()
|
2018-03-19 03:26:51 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2019-02-22 08:14:50 +00:00
|
|
|
try:
|
2021-12-14 09:03:08 +00:00
|
|
|
from jumpserver import const
|
2022-12-20 08:48:18 +00:00
|
|
|
|
2019-03-27 09:46:35 +00:00
|
|
|
__version__ = const.VERSION
|
2019-02-22 08:14:50 +00:00
|
|
|
except ImportError as e:
|
2019-12-05 07:09:25 +00:00
|
|
|
print("Not found __version__: {}".format(e))
|
|
|
|
print("Python is: ")
|
2021-03-14 21:15:48 +00:00
|
|
|
logging.info(sys.executable)
|
2019-02-22 08:14:50 +00:00
|
|
|
__version__ = 'Unknown'
|
2019-12-05 07:09:25 +00:00
|
|
|
sys.exit(1)
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
try:
|
2021-12-14 09:03:08 +00:00
|
|
|
from jumpserver.const import CONFIG
|
2022-05-19 16:10:22 +00:00
|
|
|
from common.utils.file import download_file
|
2022-10-18 09:47:13 +00:00
|
|
|
from common.utils import make_dirs
|
2019-02-22 08:33:27 +00:00
|
|
|
except ImportError as e:
|
2019-12-05 07:09:25 +00:00
|
|
|
print("Import error: {}".format(e))
|
|
|
|
print("Could not find config file, `cp config_example.yml config.yml`")
|
2018-03-19 03:26:51 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
os.environ["PYTHONIOENCODING"] = "UTF-8"
|
2021-08-06 11:16:18 +00:00
|
|
|
|
2019-10-08 10:17:48 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s %(message)s', level=logging.INFO,
|
|
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
|
|
)
|
2018-03-19 03:26:51 +00:00
|
|
|
|
2019-10-08 10:17:48 +00:00
|
|
|
logger = logging.getLogger()
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
try:
|
2022-10-18 09:47:13 +00:00
|
|
|
make_dirs(os.path.join(BASE_DIR, "data", "static"))
|
|
|
|
make_dirs(os.path.join(BASE_DIR, "data", "media"))
|
2018-03-19 03:26:51 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
2018-10-24 02:53:00 +00:00
|
|
|
def check_database_connection():
|
|
|
|
for i in range(60):
|
2021-12-14 09:03:08 +00:00
|
|
|
logging.info(f"Check database connection: {i}")
|
|
|
|
try:
|
|
|
|
management.call_command('check', '--database', 'default')
|
2019-10-08 10:17:48 +00:00
|
|
|
logging.info("Database connect success")
|
2018-10-24 02:53:00 +00:00
|
|
|
return
|
2021-12-14 09:03:08 +00:00
|
|
|
except OperationalError:
|
|
|
|
logging.info('Database not setup, retry')
|
2022-03-02 12:48:43 +00:00
|
|
|
except Exception as exc:
|
|
|
|
logging.error('Unexpect error occur: {}'.format(str(exc)))
|
2018-10-24 02:53:00 +00:00
|
|
|
time.sleep(1)
|
2021-08-10 06:43:54 +00:00
|
|
|
logging.error("Connection database failed, exit")
|
2018-10-24 02:53:00 +00:00
|
|
|
sys.exit(10)
|
|
|
|
|
2018-03-19 03:26:51 +00:00
|
|
|
|
2021-04-27 06:21:48 +00:00
|
|
|
def expire_caches():
|
2021-12-14 09:03:08 +00:00
|
|
|
try:
|
|
|
|
management.call_command('expire_caches')
|
|
|
|
except:
|
|
|
|
pass
|
2021-04-27 06:21:48 +00:00
|
|
|
|
|
|
|
|
2020-12-03 06:03:42 +00:00
|
|
|
def perform_db_migrate():
|
2019-10-08 10:17:48 +00:00
|
|
|
logging.info("Check database structure change ...")
|
|
|
|
logging.info("Migrate model change to database ...")
|
2021-12-14 09:03:08 +00:00
|
|
|
try:
|
|
|
|
management.call_command('migrate')
|
|
|
|
except Exception:
|
2022-01-10 08:52:44 +00:00
|
|
|
logging.error('Perform migrate failed, exit', exc_info=True)
|
2021-12-14 09:03:08 +00:00
|
|
|
sys.exit(11)
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def collect_static():
|
2019-10-08 10:17:48 +00:00
|
|
|
logging.info("Collect static files")
|
2021-12-14 09:03:08 +00:00
|
|
|
try:
|
|
|
|
management.call_command('collectstatic', '--no-input', '-c', verbosity=0, interactive=False)
|
|
|
|
logging.info("Collect static files done")
|
|
|
|
except:
|
|
|
|
pass
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
|
2022-03-02 12:48:43 +00:00
|
|
|
def compile_i18n_file():
|
2021-09-23 02:26:39 +00:00
|
|
|
django_mo_file = os.path.join(BASE_DIR, 'apps', 'locale', 'zh', 'LC_MESSAGES', 'django.mo')
|
|
|
|
if os.path.exists(django_mo_file):
|
|
|
|
return
|
|
|
|
os.chdir(os.path.join(BASE_DIR, 'apps'))
|
2021-12-14 09:03:08 +00:00
|
|
|
management.call_command('compilemessages', verbosity=0, interactive=False)
|
2021-09-23 02:26:39 +00:00
|
|
|
logging.info("Compile i18n files done")
|
|
|
|
|
|
|
|
|
2022-05-19 16:10:22 +00:00
|
|
|
def download_ip_db():
|
|
|
|
db_base_dir = os.path.join(APP_DIR, 'common', 'utils', 'ip')
|
|
|
|
db_path_url_mapper = {
|
|
|
|
('geoip', 'GeoLite2-City.mmdb'): 'https://jms-pkg.oss-cn-beijing.aliyuncs.com/ip/GeoLite2-City.mmdb',
|
|
|
|
('ipip', 'ipipfree.ipdb'): 'https://jms-pkg.oss-cn-beijing.aliyuncs.com/ip/ipipfree.ipdb'
|
|
|
|
}
|
|
|
|
for p, src in db_path_url_mapper.items():
|
|
|
|
path = os.path.join(db_base_dir, *p)
|
|
|
|
if os.path.isfile(path) and os.path.getsize(path) > 1000:
|
|
|
|
continue
|
|
|
|
print("Download ip db: {}".format(path))
|
|
|
|
download_file(src, path)
|
|
|
|
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
def install_builtin_applets():
|
|
|
|
logging.info("Install builtin applets")
|
|
|
|
try:
|
2023-01-19 05:44:42 +00:00
|
|
|
management.call_command('install_builtin_applets', verbosity=0)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("Install builtin applets err: {}".format(e))
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
|
|
|
2021-08-10 06:43:54 +00:00
|
|
|
def upgrade_db():
|
|
|
|
collect_static()
|
|
|
|
perform_db_migrate()
|
|
|
|
|
|
|
|
|
2018-03-19 03:26:51 +00:00
|
|
|
def prepare():
|
2018-10-24 02:53:00 +00:00
|
|
|
check_database_connection()
|
2020-12-07 11:11:32 +00:00
|
|
|
upgrade_db()
|
2021-04-27 06:21:48 +00:00
|
|
|
expire_caches()
|
2022-05-19 16:10:22 +00:00
|
|
|
download_ip_db()
|
2022-12-20 08:48:18 +00:00
|
|
|
install_builtin_applets()
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
|
2021-12-14 09:03:08 +00:00
|
|
|
def start_services():
|
|
|
|
services = args.services if isinstance(args.services, list) else [args.services]
|
|
|
|
if action == 'start' and {'all', 'web'} & set(services):
|
|
|
|
prepare()
|
|
|
|
|
2021-12-14 09:41:53 +00:00
|
|
|
start_args = []
|
2021-12-14 09:03:08 +00:00
|
|
|
if args.daemon:
|
2021-12-14 09:41:53 +00:00
|
|
|
start_args.append('--daemon')
|
2021-12-14 09:03:08 +00:00
|
|
|
if args.force:
|
2021-12-14 09:41:53 +00:00
|
|
|
start_args.append('--force')
|
2023-07-19 09:12:44 +00:00
|
|
|
if args.worker:
|
|
|
|
start_args.extend(['--worker', str(args.worker)])
|
|
|
|
else:
|
|
|
|
worker = os.environ.get('CORE_WORKER')
|
|
|
|
if isinstance(worker, str) and worker.isdigit():
|
|
|
|
start_args.extend(['--worker', worker])
|
2021-12-14 09:03:08 +00:00
|
|
|
|
|
|
|
try:
|
2021-12-14 09:41:53 +00:00
|
|
|
management.call_command(action, *services, *start_args)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info('Cancel ...')
|
|
|
|
time.sleep(2)
|
2022-03-02 12:48:43 +00:00
|
|
|
except Exception as exc:
|
|
|
|
logging.error("Start service error {}: {}".format(services, exc))
|
2021-12-14 09:03:08 +00:00
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
2018-03-19 03:26:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="""
|
|
|
|
Jumpserver service control tools;
|
2018-03-25 13:47:29 +00:00
|
|
|
|
|
|
|
Example: \r\n
|
|
|
|
|
2018-03-19 03:26:51 +00:00
|
|
|
%(prog)s start all -d;
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'action', type=str,
|
2021-08-11 06:26:23 +00:00
|
|
|
choices=("start", "stop", "restart", "status", "upgrade_db", "collect_static"),
|
2018-03-19 03:26:51 +00:00
|
|
|
help="Action to run"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-08-06 11:16:18 +00:00
|
|
|
"services", type=str, default='all', nargs="*",
|
2024-04-10 10:14:49 +00:00
|
|
|
choices=("all", "web", "task"),
|
2018-03-19 03:26:51 +00:00
|
|
|
help="The service to start",
|
|
|
|
)
|
2021-08-06 11:16:18 +00:00
|
|
|
parser.add_argument('-d', '--daemon', nargs="?", const=True)
|
2022-12-01 08:22:26 +00:00
|
|
|
parser.add_argument('-w', '--worker', type=int, nargs="?")
|
2021-08-06 11:16:18 +00:00
|
|
|
parser.add_argument('-f', '--force', nargs="?", const=True)
|
2018-03-19 03:26:51 +00:00
|
|
|
|
2021-08-06 11:16:18 +00:00
|
|
|
args = parser.parse_args()
|
2018-03-19 03:26:51 +00:00
|
|
|
|
|
|
|
action = args.action
|
2021-08-06 11:16:18 +00:00
|
|
|
if action == "upgrade_db":
|
2020-12-05 05:19:44 +00:00
|
|
|
upgrade_db()
|
2021-08-11 06:26:23 +00:00
|
|
|
elif action == "collect_static":
|
|
|
|
collect_static()
|
2018-03-19 03:26:51 +00:00
|
|
|
else:
|
2021-12-14 09:03:08 +00:00
|
|
|
start_services()
|