新增:Linux Docker 部署与自动回收、备份及容量监测
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
#!/usr/bin/env python3
|
||||
"""容器入口:固定运行包、私有配置、子进程守护和安全停止。"""
|
||||
import argparse
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path[:0] = [str(ROOT / 'scripts'), str(ROOT / 'deploy/macos')]
|
||||
|
||||
|
||||
def secret(name):
|
||||
path = Path('/run/secrets') / name
|
||||
value = path.read_text().strip()
|
||||
if not re.fullmatch(r'[A-Za-z0-9_-]{24,128}', value):
|
||||
raise ValueError('Invalid secret: ' + name)
|
||||
return value
|
||||
|
||||
|
||||
def identifier(value):
|
||||
if not re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{0,47}', value):
|
||||
raise ValueError('Invalid database identifier')
|
||||
return value
|
||||
|
||||
|
||||
def prepare(root=ROOT):
|
||||
import backup_retention as br
|
||||
root = Path(root)
|
||||
for relative in ('.runtime/retention', '.runtime/operations', '.runtime/nao-users',
|
||||
'.runtime/nao-trial', 'runtimedata/archive', 'output/backups', 'output/logs'):
|
||||
(root / relative).mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
for relative, destination in (('.runtime/mysql', '/opt/mysql'), ('.runtime/nao-trial/venv', '/opt/nao')):
|
||||
path = root / relative
|
||||
if path.is_symlink():
|
||||
if str(path.readlink()) != destination: raise ValueError('Unexpected runtime link')
|
||||
elif path.exists(): raise ValueError('Runtime path already occupied')
|
||||
else: path.symlink_to(destination, target_is_directory=True)
|
||||
value = {'mysqlHost': 'mysql', 'mysqlPort': 3306,
|
||||
'mysqlDatabase': identifier(os.environ.get('MYSQL_DATABASE', 'quality_platform')),
|
||||
'mysqlUser': identifier(os.environ.get('MYSQL_USER', 'quality_app')),
|
||||
'mysqlPassword': secret('mysql_password'), 'rootPassword': secret('mysql_root_password'),
|
||||
'adminUsername': os.environ.get('QUALITY_ADMIN_USERNAME', 'admin'),
|
||||
'adminPassword': secret('admin_password'), 'databaseReady': True, 'binaryLogEnabled': False,
|
||||
'legacyTimestampOffsetSeconds': os.environ.get('MYSQL_LEGACY_TIMESTAMP_OFFSET_SECONDS', 'UNCONFIRMED')}
|
||||
config = root / '.runtime/local-config.json'
|
||||
if config.exists():
|
||||
old = json.loads(config.read_text())
|
||||
# 更改持久数据库的密码需要专门轮换,不能在重启时默默写入不一致配置。
|
||||
for key in ('mysqlDatabase', 'mysqlUser', 'mysqlPassword', 'rootPassword'):
|
||||
if old.get(key) != value[key]: raise ValueError('Persisted database settings changed: ' + key)
|
||||
br.atomic(config, value)
|
||||
cnf = root / '.runtime/mysql-root.cnf'
|
||||
cnf.write_text('[client]\nuser=root\npassword=' + value['rootPassword'] + '\nhost=mysql\nport=3306\nprotocol=TCP\n')
|
||||
cnf.chmod(0o600)
|
||||
import operations
|
||||
policy = root / '.runtime/operations/policy.json'
|
||||
if not policy.exists(): br.atomic(policy, operations.DEFAULTS)
|
||||
return value
|
||||
|
||||
|
||||
def backend_environment(value):
|
||||
env = os.environ.copy()
|
||||
env.update({'MYSQL_URL': 'jdbc:mysql://mysql:3306/' + value['mysqlDatabase'] +
|
||||
'?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&forceConnectionTimeZoneToSession=true&useSSL=false&allowPublicKeyRetrieval=true',
|
||||
'MYSQL_USER': value['mysqlUser'], 'MYSQL_PASSWORD': value['mysqlPassword'],
|
||||
'QUALITY_BOOTSTRAP_ADMIN_USERNAME': value['adminUsername'],
|
||||
'QUALITY_BOOTSTRAP_ADMIN_PASSWORD': value['adminPassword'],
|
||||
'SERVER_ADDRESS': '0.0.0.0', 'QUALITY_PROJECT_ROOT': str(ROOT),
|
||||
'QUALITY_ARCHIVE_ROOT': str(ROOT / 'runtimedata/archive'), 'QUALITY_ALLOWED_ROOTS': '',
|
||||
'LOGGING_CONFIG': str(ROOT / 'deploy/macos/logback-quality.xml'),
|
||||
'QUALITY_LOG_DIR': str(ROOT / 'output/logs'), 'QUALITY_CONTAINER': '1'})
|
||||
return env
|
||||
|
||||
|
||||
def ready():
|
||||
try:
|
||||
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
||||
with opener.open('http://127.0.0.1:5281/actuator/health', timeout=3) as response:
|
||||
return json.load(response).get('status') == 'UP'
|
||||
except (OSError, ValueError): return False
|
||||
|
||||
|
||||
def review_retention(first=False):
|
||||
import backup_retention as br
|
||||
from retention import Database
|
||||
path = ROOT / '.runtime/retention/config.json'
|
||||
if first and path.exists(): return
|
||||
with br.lock(ROOT, timeout=0):
|
||||
db = Database(ROOT)
|
||||
try:
|
||||
if db.busy(): raise RuntimeError('Application jobs are active; review deferred')
|
||||
accepted = br.accepted_backend(ROOT)
|
||||
if not accepted: raise RuntimeError('Pinned backend not found')
|
||||
br.atomic(path, {'schemaSignature': db.schema_signature, 'acceptedJarSha256': accepted[1],
|
||||
'retirementGraceSeconds': 1800, 'intervalSeconds': 300})
|
||||
finally: db.conn.close()
|
||||
|
||||
|
||||
def serve():
|
||||
import backup_retention as br
|
||||
(ROOT / '.runtime').mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
guard = (ROOT / '.runtime/container.lock').open('a')
|
||||
fcntl.flock(guard, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
value = prepare()
|
||||
br.accepted_backend(ROOT)
|
||||
heap = os.environ.get('JAVA_MAX_HEAP', '2048m')
|
||||
if not re.fullmatch(r'[1-9][0-9]*[mMgG]', heap): raise ValueError('Invalid Java heap size')
|
||||
env = backend_environment(value)
|
||||
children = {'backend': subprocess.Popen(['java', '-Xms128m', '-Xmx' + heap, '-jar', str(ROOT / 'app/backend.jar')],
|
||||
env=env, start_new_session=True)}
|
||||
stopping = False
|
||||
def stop(signum, frame):
|
||||
nonlocal stopping
|
||||
stopping = True
|
||||
signal.signal(signal.SIGTERM, stop); signal.signal(signal.SIGINT, stop)
|
||||
try:
|
||||
while not stopping:
|
||||
if children['backend'].poll() is not None: return 1
|
||||
if ready():
|
||||
try: review_retention(first=True)
|
||||
except Exception as error: print('retention-initialization: ' + type(error).__name__, flush=True)
|
||||
for role in ('retention', 'monitor'):
|
||||
if role not in children or children[role].poll() is not None:
|
||||
children[role] = subprocess.Popen([sys.executable, str(ROOT / 'deploy/docker/worker.py'), role],
|
||||
env=env, start_new_session=True)
|
||||
time.sleep(1)
|
||||
return 0
|
||||
finally:
|
||||
for process in children.values():
|
||||
if process.poll() is None:
|
||||
try: os.killpg(process.pid, signal.SIGTERM)
|
||||
except ProcessLookupError: pass
|
||||
deadline = time.monotonic() + 270
|
||||
for process in children.values():
|
||||
try: process.wait(timeout=max(.1, deadline - time.monotonic()))
|
||||
except subprocess.TimeoutExpired:
|
||||
os.killpg(process.pid, signal.SIGKILL); process.wait()
|
||||
guard.close()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=['serve', 'health', 'review-retention'], default='serve', nargs='?')
|
||||
args = parser.parse_args()
|
||||
if args.action == 'health': return 0 if ready() else 1
|
||||
if args.action == 'review-retention': review_retention(); return 0
|
||||
return serve()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: raise SystemExit(main())
|
||||
except Exception as error:
|
||||
print('Container startup failed: ' + type(error).__name__, file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user