fix(ops): retire exact CI-generated historical release deltas safely
Deploy Canina / deploy (push) Successful in 6m12s
Deploy Canina / deploy (push) Successful in 6m12s
This commit is contained in:
1 parent
c733a806bd
commit
1e27387016
2 files changed
+44
-8
No files matched your search
@@ -32,6 +32,29 @@ def dependencies():
|
||||
return sorted(set(protected)),[{'name':c['Name'],'mounts':c['Mounts'],'labels':{k:v for k,v in (c['Config'].get('Labels') or {}).items() if k.startswith('com.docker.compose.')}} for c in containers]
|
||||
def inspect(path):
|
||||
return {'head':git(path,'rev-parse','HEAD'),'branch':subprocess.run(['git','-C',str(path),'symbolic-ref','--short','-q','HEAD'],text=True,capture_output=True).stdout.strip(),'upstream':subprocess.run(['git','-C',str(path),'rev-parse','--abbrev-ref','@{upstream}'],text=True,capture_output=True).stdout.strip(),'status':git(path,'status','--porcelain=v1','--untracked-files=all'),'unpushed':git(path,'rev-list','HEAD','--not','--remotes'),'common':git(path,'rev-parse','--path-format=absolute','--git-common-dir')}
|
||||
RUNTIME_NAMES={'.support-chat.env','.revalidation.env','.stage-base.json','.stage-compose.json','.stage-runtime.json','.stage-runtime-merged.json','.prod-compose.json','.prod-rollback.json','.prod-snapshot.json','.prod-schema-evidence.json'}
|
||||
def disposable_state(path,environment):
|
||||
# Compare complete bytes against HEAD plus the exact tracked CI transforms.
|
||||
if git(path,'diff','--cached','--name-only'):raise RuntimeError('Staged source preserved')
|
||||
for name in git(path,'diff','--name-only').splitlines():
|
||||
if name not in {'.dockerignore','nginx.conf'}:raise RuntimeError('Dirty source preserved')
|
||||
original=subprocess.check_output(['git','-C',str(path),'show','HEAD:'+name])
|
||||
expected=original
|
||||
if name=='nginx.conf':
|
||||
values={'__BACKEND_HOST__':'backend_stage' if environment=='stage' else 'backend_prod','__APP_HOST__':'stage.canina.ir' if environment=='stage' else 'canina.ir','__ADMIN_HOST__':'stageadmin.canina.ir' if environment=='stage' else 'admin.canina.ir'}
|
||||
for key,value in values.items():expected=expected.replace(key.encode(),value.encode())
|
||||
else:expected+=b'\n.support-chat.env\n.revalidation.env\n.stage-*.json\n' if environment=='stage' else b'\n.prod-*.json\n.*.env\n'
|
||||
if (path/name).is_symlink() or (path/name).read_bytes()!=expected:raise RuntimeError('Unknown render delta preserved')
|
||||
others=set(git(path,'ls-files','--others','--exclude-standard').splitlines()+git(path,'ls-files','--others','--ignored','--exclude-standard').splitlines())
|
||||
private=[]
|
||||
for name in sorted(others):
|
||||
p=path/name
|
||||
if p.is_symlink() or not p.is_file():raise RuntimeError('Unknown runtime type')
|
||||
if name in RUNTIME_NAMES:private.append(name)
|
||||
elif re.fullmatch(r'scripts/__pycache__/[a-zA-Z0-9_-]+\.cpython-312\.pyc',name) and (path/'scripts'/(p.name.split('.cpython-')[0]+'.py')).is_file():pass
|
||||
else:raise RuntimeError('Unknown untracked/ignored file preserved')
|
||||
return private
|
||||
|
||||
def main():
|
||||
ap=argparse.ArgumentParser();ap.add_argument('--apply',action='store_true');ap.add_argument('--current-sha',required=True);ap.add_argument('--environment',choices=['stage','production'],required=True);args=ap.parse_args()
|
||||
if not re.fullmatch('[0-9a-f]{40}',args.current_sha):raise SystemExit('Invalid current SHA')
|
||||
@@ -51,18 +74,18 @@ def main():
|
||||
try:
|
||||
if not eligible(path,root,protected,clean=True,pushed=True):e['reason']='active-or-unsafe';continue
|
||||
e.update(inspect(path))
|
||||
if not eligible(path,root,protected,clean=e['status']=='',pushed=e['unpushed']==''):e['reason']='dirty-or-unpushed';continue
|
||||
# Never remove ignored runtime secrets/config even from an otherwise clean tree.
|
||||
ignored=git(path,'ls-files','--others','--ignored','--exclude-standard')
|
||||
runtime_names={'.support-chat.env','.revalidation.env','.stage-compose.json','.stage-runtime.json','.stage-runtime-merged.json','.prod-compose.json','.prod-rollback.json','.prod-snapshot.json'}
|
||||
ignored_paths=ignored.splitlines()
|
||||
if any(name not in runtime_names or (path/name).is_symlink() or not (path/name).is_file() for name in ignored_paths):
|
||||
e['reason']='unknown-ignored-files-preserved';continue
|
||||
if e['unpushed']:e['reason']='unpushed';continue
|
||||
ignored_paths=disposable_state(path,args.environment)
|
||||
# Verify against the authoritative repository, not stale local remote refs.
|
||||
branch='develop' if args.environment=='stage' else 'main'
|
||||
git(path,'fetch','--quiet','origin',branch)
|
||||
git(path,'merge-base','--is-ancestor','HEAD','FETCH_HEAD')
|
||||
e['preserved_runtime_files']=ignored_paths
|
||||
e['reason']='eligible'
|
||||
if args.apply:
|
||||
latest,_=dependencies()
|
||||
if not eligible(path,root,latest,clean=git(path,'status','--porcelain=v1','--untracked-files=all')=='',pushed=git(path,'rev-list','HEAD','--not','--remotes')==''):raise RuntimeError('Dependency/status changed')
|
||||
if not eligible(path,root,latest,clean=True,pushed=git(path,'rev-list','HEAD','--not','--remotes')=='') or disposable_state(path,args.environment)!=ignored_paths:raise RuntimeError('Dependency/status changed')
|
||||
if pathlib.Path(e['common'])!=path/'.git' and e['status']:raise RuntimeError('Dirty linked worktree preserved')
|
||||
if ignored_paths:
|
||||
archive=BASE/'.retained-runtime'/args.environment/path.name
|
||||
archive.mkdir(parents=True,mode=0o700,exist_ok=True)
|
||||
|
||||
@@ -24,6 +24,19 @@ class RetentionTests(unittest.TestCase):
|
||||
self.assertEqual(r.inspect(p)['unpushed'],'')
|
||||
(p/'untracked').write_text('preserve')
|
||||
self.assertIn('untracked',r.inspect(p)['status'])
|
||||
def test_only_exact_generated_changes_are_disposable(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p=pathlib.Path(d)
|
||||
def g(*a):return subprocess.check_output(['git','-C',d,*a],stderr=subprocess.DEVNULL,text=True)
|
||||
g('init');g('config','user.email','test@example.invalid');g('config','user.name','Test')
|
||||
(p/'nginx.conf').write_text('proxy __BACKEND_HOST__ __APP_HOST__ __ADMIN_HOST__');(p/'.dockerignore').write_text('original\n');g('add','.');g('commit','-m','fixture')
|
||||
(p/'nginx.conf').write_text('proxy backend_stage stage.canina.ir stageadmin.canina.ir');(p/'.dockerignore').write_text('original\n\n.support-chat.env\n.revalidation.env\n.stage-*.json\n')
|
||||
(p/'.revalidation.env').write_text('private')
|
||||
self.assertEqual(r.disposable_state(p,'stage'),['.revalidation.env'])
|
||||
(p/'nginx.conf').write_text('uncommitted source')
|
||||
with self.assertRaises(RuntimeError):r.disposable_state(p,'stage')
|
||||
g('checkout','--','nginx.conf');(p/'unknown.env').write_text('unknown')
|
||||
with self.assertRaises(RuntimeError):r.disposable_state(p,'stage')
|
||||
def test_disk_pressure_thresholds(self):
|
||||
spec=importlib.util.spec_from_file_location('watch',ROOT/'disk-watch.py');w=importlib.util.module_from_spec(spec);spec.loader.exec_module(w)
|
||||
self.assertTrue(w.pressure(100*1024**3,9*1024**3))
|
||||
|
||||
Reference in new issue
Block a user