Files
zyt/identity-admin/.test-runtime/production-x/prepare_remote.py
T
2026-11-11 14:22:02 +08:00

64 lines
3.7 KiB
Python

import os,json,hashlib,tarfile,sqlite3,subprocess,shutil,pwd,grp
ROOT='/www/wwwroot/x.zhenyangtang.com.cn'
RELEASE='group-20261104-59244e027c01'
def digest(p):
with open(p,'rb') as f:return hashlib.sha256(f.read()).hexdigest()
def extract(archive,target):
os.makedirs(target,exist_ok=True)
with tarfile.open(archive) as tar:
for item in tar.getmembers():
path=os.path.realpath(os.path.join(target,item.name))
assert path.startswith(os.path.realpath(target)+'/') and item.isfile()
os.makedirs(os.path.dirname(path),exist_ok=True)
with tar.extractfile(item) as src,open(path,'wb') as dst:shutil.copyfileobj(src,dst)
os.chmod(path,0o644)
assert digest(ROOT+'/incoming/identity-group-release.tgz')=='59244e027c0198ff3716792d23ff380effed1e1814d74dbde93d118510b3f0ec'
target=ROOT+'/releases/'+RELEASE
assert not os.path.exists(target)
extract(ROOT+'/incoming/identity-group-release.tgz',target)
with open(ROOT+'/incoming/release-manifest.json') as f:manifest=json.load(f)
for item in manifest:assert digest(target+'/'+item['path'])==item['sha256'],item['path']
extract(ROOT+'/incoming/original-source-overlay.tgz',ROOT+'/incoming/source-overlay')
with open(ROOT+'/incoming/source-overlay/overlay-manifest.json') as f:overlay=json.load(f)
source='/www/wwwroot/zyt/server'
assert os.path.isfile(source+'/app/adminapi/service/iam/v2/IamV2Client.php')
assert os.path.isfile(source+'/app/adminapi/service/iam/v2/IamV2Source.php')
for item in overlay['files']:
path=source+'/'+item['path']; expected=item['production_sha256']
assert (digest(path) if os.path.exists(path) else None)==expected,'concurrent source change: '+item['path']
assert digest(ROOT+'/incoming/source-overlay/'+item['path'])==item['overlay_sha256']
result=subprocess.run(['/www/server/php/82/bin/php','-l',ROOT+'/incoming/source-overlay/'+item['path']],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
assert result.returncode==0,'PHP lint failed: '+item['path']
www=pwd.getpwnam('www').pw_uid; group=grp.getgrnam('www').gr_gid
shared=ROOT+'/shared';os.chown(shared,www,group);os.chmod(shared,0o750)
for name in ('runtime',):
os.makedirs(shared+'/'+name,mode=0o700,exist_ok=True);os.chown(shared+'/'+name,www,group)
assert not os.path.exists(shared+'/group.sqlite')
shutil.copyfile(ROOT+'/incoming/group.sqlite',shared+'/group.sqlite');os.chmod(shared+'/group.sqlite',0o600);os.chown(shared+'/group.sqlite',www,group)
with sqlite3.connect(shared+'/group.sqlite') as db:
assert db.execute('PRAGMA integrity_check').fetchone()[0]=='ok'
assert db.execute("SELECT value FROM group_meta WHERE name='source_key'").fetchone()[0]==hashlib.sha256(b'http://127.0.0.1:8082/adminapi').hexdigest()
subjects=db.execute('SELECT COUNT(*) FROM subject').fetchone()[0]
config='''APP_DEBUG=false
IAM_DATA_MODE=shared
IAM_SOURCE_API_URL=http://127.0.0.1:8082/adminapi
IAM_SOURCE_LOGIN_URL=https://admin.zhenyangtang.com.cn/admin/login
IAM_RUNTIME_PATH=/www/wwwroot/x.zhenyangtang.com.cn/shared/runtime
IAM_SESSION_TTL=28800
IAM_CORS_ORIGINS=https://x.zhenyangtang.com.cn,https://admin.zhenyangtang.com.cn
IAM_GROUP_ENABLED=1
IAM_GROUP_OWNER_IDS=1
IAM_GROUP_NAME=甄养堂集团
IAM_GROUP_DB_PATH=/www/wwwroot/x.zhenyangtang.com.cn/shared/group.sqlite
IAM_HOSPITAL_BASE_URL=https://admin.zhenyangtang.com.cn/admin
IAM_KEFU_BASE_URL=
IAM_KEFU_COMPANY_CODE=
IAM_KEFU_METADATA_API_URL=
IAM_KEFU_METADATA_KEY_FILE=
'''
with open(shared+'/group.env','w') as f:f.write(config)
os.chmod(shared+'/group.env',0o600);os.chown(shared+'/group.env',www,group)
os.symlink(shared+'/group.env',target+'/server/.env')
os.symlink('releases/'+RELEASE,ROOT+'/current')
print(json.dumps({'ok':True,'release':RELEASE,'verified_files':len(manifest),'source_overlay_validated':len(overlay['files']),'group_subjects':subjects,'source_binding_changed':False,'kf_deployed':False}))