Skip to content

Instantly share code, notes, and snippets.

@stefanv
Last active July 3, 2021 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanv/87e79ad0563c5ed759512cfcf94c3dea to your computer and use it in GitHub Desktop.
Save stefanv/87e79ad0563c5ed759512cfcf94c3dea to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import time
import os
from datetime import datetime
# Check after every X minutes
delay = 60
upgrade_during_consensus = False
# Image name
image = 'quay.io/team-helium/validator:latest-validator-amd64'
def run(cmd, check=True):
try:
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=check)
except subprocess.CalledProcessError:
print(f'Error: could not execute `{" ".join(cmd)}`')
return ''
out = p.stdout.decode('utf-8')
try:
return out.split('\n')[0]
except:
return ''
def pull_image():
run(['docker', 'pull', image])
def running_version():
container = run(['docker', 'ps', '-q'])
if not container:
return ''
else:
return run(['docker', 'inspect', "--format='{{.Image}}'", container])
def downloaded_version():
return run(['docker', 'inspect', "--format='{{index .Id}}'", image])
def in_consensus():
try:
status = bool(run(['docker', 'exec', 'validator', 'miner', 'info', 'in_consensus'], check=False))
except:
return False
def timestamp():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
while True:
pull_image()
dv = downloaded_version()
rv = running_version()
if not (rv == dv):
if upgrade_during_consensus or (not in_consensus()):
print(f'[{timestamp()}] Launching new Docker container {dv}')
run(['docker', 'stop', 'validator'], check=False)
run(['docker', 'rm', 'validator'], check=False)
run(['docker', 'run', '-d',
'--restart', 'always',
'--publish', '2154:2154/tcp',
'--publish', '8080:8080/tcp',
'--name', 'validator',
'--mount',
os.path.expandvars('type=bind,source=$HOME/validator_data,target=/var/data'),
image])
print(f'[{timestamp()}] Pruning old images')
run(['docker', 'image', 'prune', '-f'], check=False)
else:
print(f'[{timestamp()}] Currently in consensus group; holding off on upgrade')
print(f'[{timestamp()}] Waiting {delay} minutes')
time.sleep(delay * 60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment