#!/usr/bin/env python3 """Conservative CI retention: current mount users win; dirty/unpublished trees stay. Default is dry-run. No volumes, images, secrets or arbitrary workspaces are swept. """ import argparse,fcntl,json,os,pathlib,re,shutil,subprocess,time BASE=pathlib.Path('/opt/services/canina') def command(args): return subprocess.check_output(args,text=True,stderr=subprocess.PIPE).strip() def git(path,*args):return command(['git','-C',str(path),*args]) def eligible(path,root,protected,*,clean,pushed): path=pathlib.Path(path);root=pathlib.Path(root) if path.is_symlink() or root.is_symlink() or path.parent!=root or path.resolve()!=path or root.resolve()!=root:return False if not re.fullmatch('[0-9a-f]{40}',path.name):return False return clean and pushed and not any(str(path)==p or str(path).startswith(p.rstrip('/')+'/') or p.startswith(str(path)+'/') for p in protected) def dependencies(): ids=command(['docker','ps','-aq']).split() containers=json.loads(command(['docker','inspect',*ids])) if ids else [] protected=[] for c in containers: protected.extend(m['Source'] for m in c['Mounts'] if m.get('Source','').startswith('/')) labels=c['Config'].get('Labels') or {} for key in ['com.docker.compose.project.working_dir','com.docker.compose.project.config_files']: protected.extend(p for p in labels.get(key,'').split(',') if p.startswith('/')) for pid in pathlib.Path('/proc').iterdir(): if not pid.name.isdigit():continue for p in [pid/'cwd',*(pid/'fd').glob('*')]: try: target=os.readlink(p) if target.startswith(str(BASE)+'/'):protected.append(target) except (OSError,PermissionError):pass protected.extend(line.split()[4].replace('\\040',' ') for line in pathlib.Path('/proc/self/mountinfo').read_text().splitlines() if line.split()[4].startswith(str(BASE)+'/')) 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') root=BASE/('releases' if args.environment=='stage' else 'production-releases') if root.is_symlink() or root.resolve()!=root:raise SystemExit('Unsafe release root') lock=os.open(str(BASE/'release-retention.lock'),os.O_CREAT|os.O_RDWR|os.O_NOFOLLOW,0o600) fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) protected,containers=dependencies() current=root/args.current_sha if git(current,'rev-parse','HEAD')!=args.current_sha:raise SystemExit('Current release identity mismatch') # Fail closed if Docker does not identify the accepted SHA as a current user. if not any(p==str(current) or p.startswith(str(current)+'/') for p in protected):raise SystemExit('Current SHA is not active') protected.append(str(current)) report={'time':time.time(),'apply':args.apply,'current':str(current),'containers':containers,'protected_paths':protected,'entries':[]} for path in sorted(root.iterdir()): e={'path':str(path),'removed':False};report['entries'].append(e) try: if not eligible(path,root,protected,clean=True,pushed=True):e['reason']='active-or-unsafe';continue e.update(inspect(path)) 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=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) if archive.resolve()!=archive:raise RuntimeError('Unsafe runtime archive') os.chmod(archive,0o700) for name in ignored_paths: destination=archive/name if destination.exists() or destination.is_symlink():raise RuntimeError('Runtime archive collision') shutil.copyfile(path/name,destination);os.chmod(destination,0o600) common=pathlib.Path(e['common']) if common!=path/'.git': command(['git','--git-dir',str(common),'worktree','remove',str(path)]) else: if len(git(path,'worktree','list','--porcelain').split('worktree '))-1!=1:raise RuntimeError('Has child worktrees') if not shutil.rmtree.avoids_symlink_attacks:raise RuntimeError('No safe rmtree') shutil.rmtree(path) e['removed']=True except (subprocess.CalledProcessError,RuntimeError,OSError) as ex:e['reason']='inspection-failed:'+type(ex).__name__ print(json.dumps(report,indent=2)) if __name__=='__main__':main()