def docker_image(repo_name, tag):
image_name = repo_name + ‘:’ + tag
client = docker.from_env()
client.login(docker_username, docker_password)
latest_image_pulled = client.images.pull(image_name)
cmd_copy = ‘docker inspect -f “{{json .Config.Labels }}” ’ + image_name + ’ |json_pp’
fl = os.popen(cmd_copy)
label_portion = fl.read()
print (’ \nLabel portion extracted from payload is:’, label_portion)
label_dict_persentation = json.loads(label_portion)
label_last_image_pulled = label_dict_persentation[‘git-sha’]
print (’ \nValue of the git-sha- Label of the last image pulled from dockerhub is :’, label_last_image_pulled)
return image_name, label_last_image_pulled
def balena_push(image_name, app_name):
balena = Balena()
auth_token = balena.auth.authenticate(username = balena_username, password = balena_password)
balena_login = subprocess.run([‘balena’, ‘login’, ‘–token’, auth_token], capture_output=True, text=True, check=True)
print(’ \nbalena_login_returncode is :’, balena_login.returncode)
balena_deploy = subprocess.run([‘balena’, ‘deploy’, app_name, image_name], capture_output=True, text=True, check=True)
print(’ \nbalena_deploy_returncode is: ', balena_deploy.returncode)
return (balena_deploy.returncode)
def main():
image_name, label_last_image_pulled = docker_image(repo_name, tag)
print(' \nIn the def main() the label passed from docker_image def is: ', label_last_image_pulled)
with open ('timestamp.txt', 'r+') as f_last_pushed:
f_last_pushed_content = f_last_pushed.readline()
print(' \nLabel inside the file indicates the last image pushed to the local device is: ',f_last_pushed_content, end='')
if f_last_pushed_content != label_last_image_pulled:
deploy_result = balena_push(image_name, app_name)
if int(deploy_result) == 0:
f_last_pushed.seek(0)
f_last_pushed.write(label_last_image_pulled)
else:
print(' \nBalena Deployment was unsuccessful')
else:
print(' \nLocal device is up to date, no new images since last image:', label_last_image_pulled)