52 lines
3.4 KiB
Python
52 lines
3.4 KiB
Python
import pathlib,os,json,time,fcntl,importlib.util,uuid,sys
|
|
TARGET='3558647e8d82092cd0e31eefbe8b9e5e9eacf573';OLD='96f510965f70581a4b41dd94463f598b77dd7cf5'
|
|
operation=pathlib.Path('/var/lib/investmoz-deploy/offers-exact-3558647e-20261114')
|
|
spec=importlib.util.spec_from_file_location('investmoz_existing_pipeline','/var/www/deploy/investmoz-pipeline.py');module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
|
|
config=json.loads(pathlib.Path('/etc/investmoz-deploy/delivery.json').read_text());assert config['target_url']=='https://teste-investidor.tempo.co.mz' and config['branch']=='main'
|
|
runner=module.Delivery(config);state=pathlib.Path(config['state_root'])
|
|
def status(value,**extra):
|
|
(operation/'status.json').write_text(json.dumps({'state':value,'target':TARGET,'previous':OLD,'lock_held':True,**extra}))
|
|
with (state/'delivery.lock').open('a') as lock:
|
|
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
try:
|
|
assert json.loads((state/'installed.json').read_text())['commit']==OLD
|
|
assert not list((state/'requests').glob('*.json'))
|
|
status('reserved_waiting_backup_and_fast_forward',installation_started=False)
|
|
deadline=time.monotonic()+1200
|
|
while not (operation/'go.json').exists():
|
|
if (operation/'abort.json').exists():raise RuntimeError('Authorised operation cancelled before installation')
|
|
if time.monotonic()>deadline:raise RuntimeError('Reservation timed out without starting installation')
|
|
time.sleep(1)
|
|
proof=json.loads((operation/'go.json').read_text());assert proof['target']==TARGET and proof['backup_restore_verified'] is True
|
|
original_latest=runner.latest
|
|
def pinned_latest():
|
|
actual=original_latest()
|
|
if actual!=TARGET:raise RuntimeError('MAIN_REVISION_MISMATCH: refusing every other revision')
|
|
return actual
|
|
runner.latest=pinned_latest
|
|
assert pinned_latest()==TARGET
|
|
runner.git('merge-base','--is-ancestor',OLD,TARGET)
|
|
assert all(x.stem==TARGET for x in (state/'requests').glob('*.json'))
|
|
with (operation/'build.log').open('wb') as log:
|
|
runner.log=log;status('installing_exact_revision',installation_started=True)
|
|
result=runner.deliver(TARGET);runner.log=None
|
|
assert result.get('commit')==TARGET and result.get('status') in ['installed','already_installed']
|
|
assert json.loads((state/'installed.json').read_text())['commit']==TARGET
|
|
(operation/'executor-result.json').write_text(json.dumps(result,indent=2))
|
|
status('installed_waiting_postverification',installation_started=True,installed_revision=TARGET)
|
|
deadline=time.monotonic()+1800
|
|
while not (operation/'finish.json').exists():
|
|
if (operation/'abort.json').exists():raise RuntimeError('Postverification requires recovery; watcher remains paused')
|
|
if time.monotonic()>deadline:raise RuntimeError('Postverification timed out; watcher remains paused')
|
|
time.sleep(1)
|
|
assert json.loads((operation/'finish.json').read_text())['postverified'] is True
|
|
assert pinned_latest()==TARGET
|
|
requests=list((state/'requests').glob('*.json'));assert all(x.stem==TARGET for x in requests)
|
|
for item in requests:item.rename(state/'processed'/(item.stem+'-exact-offers-'+uuid.uuid4().hex[:8]+'.json'))
|
|
status('verified_complete',installation_started=True,installed_revision=TARGET)
|
|
except Exception as error:
|
|
runner.log=None
|
|
status('stopped',error_type=type(error).__name__,error=str(error),installed_revision=json.loads((state/'installed.json').read_text()).get('commit'))
|
|
raise
|
|
finally:fcntl.flock(lock,fcntl.LOCK_UN)
|