"""从155模拟PMS的配置只读导出源表;独立容器不含交易凭证。""" import gzip import json import shlex import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent WRAPPER = r''' import json, os, subprocess, sys request = json.load(sys.stdin) info = json.loads(subprocess.check_output(['docker','inspect','pms-web']))[0] values = dict(x.split('=',1) for x in info['Config']['Env'] if '=' in x) if values.get('PMS_TABLE_PREFIX','') or values.get('PMS_STACK_PREFIX',''): raise SystemExit('实例范围不符合155模拟PMS,停止') if info['Config']['Image'].startswith('pms-real'): raise SystemExit('实际镜像不符合模拟范围,停止') env = dict(os.environ) args = ['docker','run','--rm','-i','--name','pms-heat-research', '--label','pms.role=offline-research','--cpus','0.5','--memory','1g', '--pids-limit','64','--read-only','--entrypoint','python'] for key in ['PROXY_DB_URL','DB_MYSQL_URL']: if not values.get(key): raise SystemExit('源连接配置缺失') env[key] = values[key] args += ['--env',key] args += ['--env','PMS_TABLE_PREFIX=',info['Image'],'-',request['mode']] print(json.dumps({'host': '155', 'prefix': '', 'source_image': info['Config']['Image'], 'image_id': info['Image'], 'role': 'readonly-research'}),file=sys.stderr) p = subprocess.run(args,input=request['code'].encode(),env=env) raise SystemExit(p.returncode) ''' def main(): mode = sys.argv[1] if mode != 'probe' and not (len(mode) == 6 and mode.isdigit()): raise ValueError('仅允许probe或月份') raw = ROOT / 'raw' raw.mkdir(exist_ok=True) path = raw / (mode + '.json.gz') if path.exists(): json.load(gzip.open(path)) print(mode, '已有缓存,保留') return common = (ROOT/'readonly_support.py').read_text() code = common + '\n' + (ROOT/'remote.py').read_text() with (raw/(mode+'.stderr')).open('wb') as log: p = subprocess.run(['ssh','-o','BatchMode=yes','-o','ConnectTimeout=8', '-p','2280','factor@192.168.16.155', 'python3 -c '+shlex.quote(WRAPPER)], input=json.dumps({'mode':mode,'code':code}).encode(), stdout=subprocess.PIPE,stderr=log,timeout=170) if p.returncode: raise RuntimeError(f'只读导出失败,退出码{p.returncode};未回显异常正文') out=json.loads(gzip.decompress(p.stdout)) path.write_bytes(p.stdout) if mode == 'probe': print(json.dumps(out,ensure_ascii=False,indent=2)) else: print(mode, 'rows',len(out['data']['rows']), 'bytes',len(p.stdout),flush=True) if __name__ == '__main__': main()