86 lines
6.0 KiB
Python
86 lines
6.0 KiB
Python
#!/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')}
|
|
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 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
|
|
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 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()
|