Qualify tenant-scoped bootstrap rotation and fresh Windows scripting
This commit is contained in:
+59
-26
@@ -7,7 +7,6 @@ import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def sha(data):
|
||||
@@ -29,6 +28,39 @@ def write_atomic(path, data, mode=0o644):
|
||||
tmp.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def pack_web(dist):
|
||||
if not (dist / 'index.html').is_file():
|
||||
raise ValueError('A built WebUI directory with index.html is required')
|
||||
files = {}
|
||||
for file in dist.rglob('*'):
|
||||
if file.is_symlink():
|
||||
raise ValueError('Web build contains a symlink')
|
||||
if file.is_file():
|
||||
files[file.relative_to(dist).as_posix()] = base64.b64encode(file.read_bytes()).decode()
|
||||
return files
|
||||
|
||||
|
||||
def stage_web(dist, files):
|
||||
if 'index.html' not in files:
|
||||
raise ValueError('Web build has no index.html')
|
||||
for name, encoded in files.items():
|
||||
path = dist / name
|
||||
if Path(name).is_absolute() or '..' in Path(name).parts or path.resolve() != path:
|
||||
raise ValueError('Invalid WebUI asset path')
|
||||
if name != 'index.html':
|
||||
write_atomic(path, base64.b64decode(encoded, validate=True))
|
||||
return base64.b64decode(files['index.html'], validate=True)
|
||||
|
||||
|
||||
def publish_index(root, dist, index):
|
||||
write_atomic(dist / 'index.html', index)
|
||||
actual = subprocess.check_output(['curl','--noproxy','*','--max-time','10','--cacert',
|
||||
str(root/'devstate/api-pki/ca.pem'),'--silent','--fail','https://localhost:18443/'])
|
||||
if actual != index:
|
||||
raise RuntimeError('Served index differs')
|
||||
return sha(actual)
|
||||
|
||||
|
||||
def web_files(revision):
|
||||
"""Build the exact Git revision using the local locked frontend dependencies."""
|
||||
import tempfile
|
||||
@@ -40,16 +72,14 @@ def web_files(revision):
|
||||
subprocess.run(['tar','-xf',archive,'-C',work],check=True)
|
||||
(work/'web/node_modules').symlink_to(root/'web/node_modules',target_is_directory=True)
|
||||
subprocess.run(['npm','run','build'],cwd=work/'web',check=True)
|
||||
dist = work/'web/dist'
|
||||
return {str(file.relative_to(dist)):base64.b64encode(file.read_bytes()).decode()
|
||||
for file in dist.rglob('*') if file.is_file()}
|
||||
return pack_web(work / 'web/dist')
|
||||
|
||||
|
||||
def activate(payload):
|
||||
import fcntl
|
||||
import time
|
||||
revision = payload['revision']
|
||||
if not re.fullmatch('[0-9a-f]{40}', revision):
|
||||
if not payload['web_only'] and not re.fullmatch('[0-9a-f]{40}', revision):
|
||||
raise ValueError('Invalid Git revision')
|
||||
def run(*command):
|
||||
return subprocess.check_output(list(command),text=True).strip()
|
||||
@@ -58,10 +88,19 @@ def activate(payload):
|
||||
root = Path('/opt/rust-discovery')
|
||||
work = root/'devstate/mvp-deploy'
|
||||
work.mkdir(exist_ok=True,mode=0o700)
|
||||
role_dir = work/revision
|
||||
role_dir.mkdir(exist_ok=True,mode=0o700)
|
||||
with (work/'deployment.lock').open('a') as lock:
|
||||
fcntl.flock(lock,fcntl.LOCK_EX)
|
||||
active = inspect('api')
|
||||
dist = next(Path(m['Source']) for m in active['Mounts'] if m['Destination'] == '/web')
|
||||
if dist != root / 'web/dist' or active['Config']['Labels']['com.docker.compose.project'] != 'rust-discovery-server':
|
||||
raise RuntimeError('Unexpected development WebUI deployment')
|
||||
index = stage_web(dist, payload['web'])
|
||||
if payload['web_only']:
|
||||
print(json.dumps(dict(revision=revision, web_index_sha256=publish_index(root, dist, index),
|
||||
api_container=active['Id'], web_only=True)))
|
||||
return
|
||||
role_dir = work / revision
|
||||
role_dir.mkdir(exist_ok=True, mode=0o700)
|
||||
matches = list((Path.home()/'rmm-dev-build/server-arm64/artifacts'/revision).glob('*/manifest.json'))
|
||||
if len(matches) != 1:
|
||||
raise RuntimeError('Expected one ARM64 build manifest for this revision')
|
||||
@@ -70,7 +109,6 @@ def activate(payload):
|
||||
if (manifest['revision'] != revision or manifest['target'] != 'aarch64-unknown-linux-gnu'
|
||||
or sha(binary) != manifest['sha256']['rmm-server'] or sha(matches[0].read_bytes()) != matches[0].parent.name):
|
||||
raise RuntimeError('Server build digest mismatch')
|
||||
active = inspect('api')
|
||||
if inspect('gateway')['Image'] != active['Image']:
|
||||
raise RuntimeError('API and Gateway have different base images')
|
||||
labels = active['Config']['Labels']
|
||||
@@ -111,13 +149,6 @@ def activate(payload):
|
||||
raise RuntimeError('Dev admin/gateway boundary changed')
|
||||
if config['postgres'].get('ports'):
|
||||
raise RuntimeError('Unexpected public database')
|
||||
dist=next(Path(m['Source']) for m in active['Mounts'] if m['Destination']=='/web')
|
||||
assets={name:base64.b64decode(data,validate=True) for name,data in payload['web'].items()}
|
||||
if 'index.html' not in assets or any(Path(n).is_absolute() or '..' in Path(n).parts for n in assets):
|
||||
raise ValueError('Invalid WebUI asset set')
|
||||
for name,data in assets.items():
|
||||
if name!='index.html':
|
||||
write_atomic(dist/name,data)
|
||||
run(*compose,'run','--no-deps','-T','--rm','migrate')
|
||||
sql = ['docker','exec','rust-discovery-server-postgres-1','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c']
|
||||
recorded = dict(line.split('|') for line in run(*sql,"SELECT version,encode(checksum,'hex') FROM _sqlx_migrations WHERE success").splitlines())
|
||||
@@ -136,12 +167,7 @@ def activate(payload):
|
||||
live=run('docker','exec','rust-discovery-server-'+role+'-1','sha256sum','/usr/local/bin/rmm-server').split()[0]
|
||||
if live!=sha(binary):
|
||||
raise RuntimeError(role+' running digest differs')
|
||||
write_atomic(dist/'index.html',assets['index.html'])
|
||||
index=subprocess.check_output(['curl','--noproxy','*','--cacert',str(root/'devstate/api-pki/ca.pem'),
|
||||
'--silent','--fail','https://localhost:18443/'])
|
||||
if index!=assets['index.html']:
|
||||
raise RuntimeError('Served index differs')
|
||||
print(json.dumps(dict(revision=revision,server_sha256=sha(binary),web_index_sha256=sha(index),
|
||||
print(json.dumps(dict(revision=revision,server_sha256=sha(binary),web_index_sha256=publish_index(root, dist, index),
|
||||
image=inspect('api')['Image'],api_health='ok',gateway_health='ok')))
|
||||
|
||||
|
||||
@@ -150,13 +176,20 @@ def main():
|
||||
import inspect
|
||||
import shlex
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--ref',required=True)
|
||||
parser.add_argument('--ref', help='Exact Git revision for the server and WebUI')
|
||||
parser.add_argument('--web-only', action='store_true', help='Only activate built web assets')
|
||||
parser.add_argument('--web-dist', type=Path, help='Use an existing WebUI build with --web-only')
|
||||
parser.add_argument('--overlay',action='append',default=[],help='Deploy and use this compose overlay from the selected Git revision')
|
||||
args=parser.parse_args()
|
||||
revision=subprocess.check_output(['git','rev-parse','--verify','--end-of-options',args.ref+'^{commit}'],text=True).strip()
|
||||
web=web_files(revision)
|
||||
if args.web_dist and (not args.web_only or args.ref) or args.web_only and args.overlay:
|
||||
parser.error('Choose --web-only with either --web-dist or --ref and no compose overlay')
|
||||
if not args.web_dist and not args.ref:
|
||||
parser.error('--ref or --web-only --web-dist is required')
|
||||
revision = None if args.web_dist else subprocess.check_output(
|
||||
['git','rev-parse','--verify','--end-of-options',args.ref+'^{commit}'],text=True).strip()
|
||||
web = pack_web(args.web_dist.resolve(strict=True)) if args.web_dist else web_files(revision)
|
||||
source=Path(inspect.getfile(activate)).read_text().rsplit('\nif __name__ ==', 1)[0]
|
||||
names=subprocess.check_output(['git','ls-tree','--name-only','-r',revision,'migrations'],text=True).splitlines()
|
||||
names=subprocess.check_output(['git','ls-tree','--name-only','-r',revision,'migrations'],text=True).splitlines() if not args.web_only else []
|
||||
migrations={str(int(Path(n).name.split('_',1)[0])):hashlib.sha384(subprocess.check_output(['git','show',revision+':'+n])).hexdigest()
|
||||
for n in names if n.endswith('.sql')}
|
||||
overlays = {}
|
||||
@@ -165,7 +198,7 @@ def main():
|
||||
parser.error('Expected a server overlay filename from deploy/')
|
||||
raw = subprocess.check_output(['git', 'show', revision + ':deploy/' + name])
|
||||
overlays[name] = base64.b64encode(raw).decode()
|
||||
payload=json.dumps(dict(revision=revision,web=web,overlays=overlays,migrations=migrations))
|
||||
payload=json.dumps(dict(revision=revision,web=web,web_only=args.web_only,overlays=overlays,migrations=migrations))
|
||||
cmd=source+'\nactivate(json.loads('+repr(payload)+'))\n'
|
||||
subprocess.run(ssh_prefix('server')+[shlex.join(['python3','-I','-'])],input=cmd,text=True,check=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user