2020-03-07 04:15:19 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import base
|
|
|
|
import json
|
|
|
|
import docker_api
|
2020-11-10 10:33:09 +01:00
|
|
|
from testutils import DOCKER_USER, DOCKER_PWD
|
2020-03-07 04:15:19 +01:00
|
|
|
|
|
|
|
def load_bundle(service_image, invocation_image):
|
|
|
|
bundle_file = "./tests/apitests/python/bundle_data/bundle.json"
|
|
|
|
bundle_tmpl_file = "./tests/apitests/python/bundle_data/bundle.json.tmpl"
|
|
|
|
with open(bundle_tmpl_file,'r') as load_f:
|
|
|
|
load_dict = json.load(load_f)
|
|
|
|
load_dict["images"]["hello"]["image"] = service_image
|
|
|
|
load_dict["invocationImages"][0]["image"] = invocation_image
|
|
|
|
bundle_str = json.dumps(load_dict)
|
|
|
|
with open(bundle_file,'w') as dump_f:
|
|
|
|
dump_f.write(bundle_str)
|
|
|
|
dump_f.close()
|
|
|
|
return bundle_file
|
|
|
|
|
|
|
|
def cnab_fixup_bundle(bundle_file, target, auto_update_bundle = True):
|
|
|
|
fixed_bundle_file = "./tests/apitests/python/bundle_data/fixed-bundle.json"
|
2021-03-17 15:33:02 +01:00
|
|
|
command = ["cnab-to-oci", "--log-level", "debug", "fixup", bundle_file, "--target", target, "--bundle", fixed_bundle_file]
|
2020-03-07 04:15:19 +01:00
|
|
|
if auto_update_bundle == True:
|
|
|
|
command.append("--auto-update-bundle")
|
|
|
|
#fixed_bundle_file = bundle_file
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Command: ", command)
|
2020-03-07 04:15:19 +01:00
|
|
|
ret = base.run_command(command)
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Command return: ", ret)
|
2020-03-07 04:15:19 +01:00
|
|
|
return fixed_bundle_file
|
|
|
|
|
|
|
|
def cnab_push_bundle(bundle_file, target):
|
|
|
|
command = ["cnab-to-oci", "push", bundle_file, "--target", target, "--auto-update-bundle"]
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Command: ", command)
|
2020-03-07 04:15:19 +01:00
|
|
|
ret = base.run_command(command)
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Command return: ", ret)
|
2020-03-07 04:15:19 +01:00
|
|
|
for line in ret.split("\n"):
|
|
|
|
line = line.replace('\"', '')
|
|
|
|
if line.find('sha256') >= 0:
|
|
|
|
return line[-71:]
|
|
|
|
raise Exception(r"Fail to get sha256 in returned data: {}".format(ret))
|
|
|
|
|
|
|
|
def push_cnab_bundle(harbor_server, user, password, service_image, invocation_image, target, auto_update_bundle = True):
|
2020-04-22 10:44:10 +02:00
|
|
|
docker_api.docker_info_display()
|
2020-11-10 10:33:09 +01:00
|
|
|
|
|
|
|
#Add docker login command to avoid pull request access rate elimitation by docker hub
|
|
|
|
docker_api.docker_login_cmd("", DOCKER_USER, DOCKER_PWD, enable_manifest = False)
|
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
docker_api.docker_login_cmd(harbor_server, user, password, enable_manifest = False)
|
2020-03-07 04:15:19 +01:00
|
|
|
bundle_file = load_bundle(service_image, invocation_image)
|
|
|
|
fixed_bundle_file = cnab_fixup_bundle(bundle_file, target, auto_update_bundle = auto_update_bundle)
|
|
|
|
sha256 = cnab_push_bundle(fixed_bundle_file, target)
|
|
|
|
return sha256
|