ops: install narrowly scoped bounded disk maintenance via CI
Owned disk maintenance only / maintenance (push) Successful in 11s
Owned disk maintenance only / maintenance (push) Successful in 11s
This commit is contained in:
1 parent
dc34c57a0c
commit
6dec2c1469
4 files changed
+115
No files matched your search
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Owned opt-in test expiry, conservative unused cache, and shared build guard."""
|
||||
import pathlib,os,time,shutil,fcntl,subprocess,json,sys
|
||||
BASE=pathlib.Path('/opt/services/canina/.managed-disk')
|
||||
RESERVE=20*1024**3
|
||||
|
||||
def retire(root,now=None):
|
||||
root=pathlib.Path(root);now=time.time() if now is None else now
|
||||
if root.is_symlink() or root.resolve()!=root:raise RuntimeError('unsafe root')
|
||||
removed=[]
|
||||
for p in sorted(root.iterdir()):
|
||||
marker=p/'.disposable-test'
|
||||
if p.is_symlink() or not p.is_dir() or p.resolve()!=p:continue
|
||||
if not marker.is_file() or marker.is_symlink() or marker.read_text()!='canina-managed-test-v1\n':continue
|
||||
if now-p.stat().st_mtime<7*86400:continue
|
||||
# Unknown/source-bearing contents are never eligible; symlinks are never followed.
|
||||
if any(x.name in {'.git','.env','node_modules','vendor'} or x.is_symlink() or os.path.ismount(x) for x in p.rglob('*')):continue
|
||||
users=[]
|
||||
for proc in pathlib.Path('/proc').glob('[0-9]*'):
|
||||
for q in [proc/'cwd',proc/'root',*(proc/'fd').glob('*')]:
|
||||
try:
|
||||
v=os.readlink(q)
|
||||
if v==str(p) or v.startswith(str(p)+'/'):users.append(v)
|
||||
except OSError:pass
|
||||
if users:continue
|
||||
shutil.rmtree(p);removed.append(p.name)
|
||||
return removed
|
||||
|
||||
def busy():
|
||||
for p in pathlib.Path('/proc').glob('[0-9]*'):
|
||||
try:
|
||||
args=(p/'cmdline').read_bytes().replace(b'\0',b' ').decode(errors='replace')
|
||||
if any(x in args for x in ['docker build','compose build','next build','npm ci','npm install','composer install','rclone rcat']):return True
|
||||
except OSError:continue
|
||||
return False
|
||||
|
||||
def main():
|
||||
if BASE.is_symlink() or BASE.resolve()!=BASE:raise SystemExit('unsafe state root')
|
||||
with (BASE/'heavy.lock').open('a') as lock:
|
||||
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
if sys.argv[1:2]==['build']:
|
||||
free=shutil.disk_usage(BASE).free
|
||||
if free<RESERVE:raise SystemExit('Insufficient available capacity: 20GiB required')
|
||||
if not sys.argv[2:]:raise SystemExit('command required')
|
||||
raise SystemExit(subprocess.call(sys.argv[2:]))
|
||||
if busy():raise SystemExit('Active heavy work: retention deferred')
|
||||
before=shutil.disk_usage(BASE).free
|
||||
removed=retire(BASE/'tests')
|
||||
p=subprocess.run(['docker','builder','prune','--force','--filter','until=24h'],capture_output=True,text=True,timeout=300,check=True)
|
||||
print(json.dumps({'removed_tests':removed,'docker_cache_result':p.stdout.strip(),'available_before':before,'available_after':shutil.disk_usage(BASE).free}))
|
||||
if __name__=='__main__':main()
|
||||
Reference in new issue
Block a user