新增:Linux Docker 部署与自动回收、备份及容量监测
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""统一 Docker Compose 入口;密码只从本机私有文件加载,不进入源码或命令参数。"""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import secrets
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SECRET_NAMES = {'mysql_root_password': 'QUALITY_SECRET_MYSQL_ROOT',
|
||||
'mysql_password': 'QUALITY_SECRET_MYSQL_APP', 'admin_password': 'QUALITY_SECRET_ADMIN'}
|
||||
|
||||
|
||||
def initialize(root=ROOT):
|
||||
base = Path(root) / '.runtime/docker'
|
||||
directory = base / 'secrets'
|
||||
if base.is_symlink() or directory.is_symlink(): raise ValueError('Unsafe private settings path')
|
||||
directory.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
base.chmod(0o700); directory.chmod(0o700)
|
||||
for name in SECRET_NAMES:
|
||||
path = directory / name
|
||||
if path.is_symlink(): raise ValueError('Unsafe secret file')
|
||||
if path.exists(): continue
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream: stream.write(secrets.token_urlsafe(32) + '\n')
|
||||
config = base / 'config.env'
|
||||
if not config.exists():
|
||||
shutil.copyfile(Path(root) / 'deploy/docker/.env.example', config); config.chmod(0o600)
|
||||
return base
|
||||
|
||||
|
||||
def environment(root=ROOT):
|
||||
env = os.environ.copy(); directory = Path(root) / '.runtime/docker/secrets'
|
||||
for name, variable in SECRET_NAMES.items():
|
||||
path = directory / name
|
||||
if path.is_symlink() or not path.is_file() or path.stat().st_mode & 0o077:
|
||||
raise ValueError('Missing or unsafe secret file; run init first')
|
||||
value = path.read_text().strip()
|
||||
if not re.fullmatch(r'[A-Za-z0-9_-]{24,128}', value): raise ValueError('Invalid secret file')
|
||||
env[variable] = value
|
||||
return env
|
||||
|
||||
|
||||
def command(action):
|
||||
actions = {'config': ['config', '--quiet'], 'build': ['build', '--pull'], 'up': ['up', '-d', '--wait', '--wait-timeout', '1200'],
|
||||
'stop': ['stop'], 'status': ['ps'], 'logs': ['logs', '--tail', '100'],
|
||||
'backup': ['exec', '-T', 'app', '/opt/nao/bin/python', 'scripts/backup.py', 'create-and-verify'],
|
||||
'review-retention': ['exec', '-T', 'app', '/opt/nao/bin/python', 'deploy/docker/app.py', 'review-retention']}
|
||||
return ['docker', 'compose', '-f', str(ROOT / 'deploy/docker/compose.yaml'),
|
||||
'--env-file', str(ROOT / '.runtime/docker/config.env'), *actions[action]]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=['init', 'config', 'build', 'up', 'stop', 'status', 'logs', 'backup', 'review-retention'])
|
||||
args = parser.parse_args()
|
||||
if args.action == 'init':
|
||||
base = initialize(); print('Private settings prepared: ' + str(base)); return
|
||||
if not shutil.which('docker'): parser.error('Docker Engine and Compose V2 are required')
|
||||
subprocess.run(command(args.action), cwd=ROOT, env=environment(), check=True)
|
||||
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
Reference in New Issue
Block a user