def upload_uniq_image(osp_connection: Connection, cloud: str, cluster_name: str, images_dir: str, installer: str): """Function uploads unique image to the cluster, instead of making shared one""" inst_url, version = get_url(installer) image_name = f"osia-{cluster_name}-{version}" image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2") image_file = None if image_path.exists(): logging.info("Found image at %s", image_path.name) image_file = image_path.as_posix() else: logging.info("Starting download of image %s", inst_url) image_file = download_image(inst_url, image_path.as_posix()) logging.info("Starting upload of image into openstack") osp_connection.create_image(image_name, filename=image_file, container_format="bare", disk_format="qcow2", wait=True, osia_clusters=cluster_name, visibility='private') logging.info("Upload finished") image = osp_connection.image.find_image(image_name) logging.info("Image uploaded as %s", image.name) with open(Path(cluster_name).joinpath("fips.json"), "w") as fips: obj = {'cloud': cloud, 'fips': [], 'image': image_name} json.dump(obj, fips) return image.name
def resolve_image(osp_connection: Connection, cloud: str, cluster_name: str, images_dir: str, installer: str, error: Optional[Exception]): """Function searches for image in openstack and creates it if it doesn't exist""" inst_url, version = get_url(installer) image_name = f"osia-rhcos-{version}" image = osp_connection.image.find_image(image_name, ignore_missing=True) if image is None: image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2") image_file = None if image_path.exists(): logging.info("Found image at %s", image_path.name) image_file = image_path.as_posix() else: logging.info("Starting download of image %s", inst_url) image_file = download_image(inst_url, image_path.as_posix()) logging.info("Starting upload of image into openstack") osp_connection.create_image(image_name, filename=image_file, container_format="bare", disk_format="qcow2", wait=True, osia_clusters=cluster_name, visibility='private') logging.info("Upload finished") image = osp_connection.image.find_image(image_name) logging.info("Image uploaded as %s", image.name) else: logging.info("Reusing found image in openstack %s", image.name) try: add_cluster(osp_connection, image, cluster_name) except SDKException as err: if error is not None: raise ImageException("Couldn't add cluster to image") from err logging.warning( "Image disappeared while metadata were written, trying again") logging.debug("Openstack error: %s", err) return resolve_image(osp_connection, cloud, cluster_name, images_dir, installer, err) with open(Path(cluster_name).joinpath("fips.json"), "w") as fips: obj = {'cloud': cloud, 'fips': [], 'image': image_name} json.dump(obj, fips) return image.name