def import_image(image_names: List[str], namespace: str, all_images: bool, check_s2i_thoth: bool) -> None: """Import Thoth image into an OpenShift deployment.""" if all_images and image_names: _LOGGER.error( "Cannot import all images if one or multiple image names were explicitly provided" ) sys.exit(1) if not (all_images or image_names): _LOGGER.error( "No image to import provided, state the image name explicitly or use --all for importing all Thoth images" ) sys.exit(1) if not (image_names or all_images): image_names = [ click.prompt("Choose Thoth image to be imported", default=_DEFAULT_S2I_THOTH) ] if image_names and check_s2i_thoth: thoth_images = get_thoth_s2i_images() error = False for image_name in image_names: parts = image_name.rsplit(":", maxsplit=1) if len(parts) == 2: # Discard the image tag. image_name = parts[0] if image_name not in thoth_images: _LOGGER.error( "Image %r not found in Thoth's images, available are: %s", image_name, thoth_images, ) error = True if error: sys.exit(1) if not image_names: image_names = get_thoth_s2i_images() for image_name in image_names: _LOGGER.info("Importing image %r to namespace %r", image_name, namespace) import_thoth_s2i_image(namespace, image_name)
def patch( path: str, s2i_thoth: str, tag: str, from_image_stream_tag: str, check_s2i_thoth: bool = True, insert_env_vars: bool = True, ) -> None: """Patch local templates to use Thoth. Adjust templates stored on the filesystem to use Thoth's s2i. """ if check_s2i_thoth: thoth_images = get_thoth_s2i_images() if s2i_thoth not in thoth_images: _LOGGER.error( "Image %r not found in Thoth's images, available are: %r", s2i_thoth, thoth_images, ) sys.exit(1) _LOGGER.info("Patching files in %r to use %r", path, s2i_thoth) build_configs = BuildConfig.load_all(path, skip_errors=True) if not build_configs: _LOGGER.error("No BuildConfig found in %r", path) sys.exit(1) from_image_stream_tag_re = re.compile(from_image_stream_tag) image_stream_name = s2i_thoth.rsplit("/", maxsplit=1)[-1] changed_build_configs = change_image_stream( build_configs, f"{image_stream_name}:{tag}", lambda bc: bool( from_image_stream_tag_re.fullmatch(bc.get_image_stream_tag())), ) if insert_env_vars: _LOGGER.warning( "Formatting of templates might change when inserting Thoth and Thamos specific environment variables" ) for build_config in changed_build_configs.values(): build_config.insert_thoth_env_vars() build_config.save2file() _LOGGER.info("Patching done \\o/, total BuildConfigs patched: %d", len(changed_build_configs)) if len(changed_build_configs) > 0: _LOGGER.warning( "Don't forget to create an image stream with image %r and tag %r", s2i_thoth, tag, )
def images(output_format: str) -> None: """Show available Thoth images.""" thoth_images = get_thoth_s2i_images() if output_format == "json": click.echo(json.dumps({"s2i_thoth": thoth_images}, indent=2)) elif output_format == "yaml": click.echo(yaml.safe_dump({"s2i_thoth": thoth_images})) elif output_format == "pretty": for image in thoth_images: click.echo(f"{_MARK_OK} {image}") elif output_format == "text": click.echo("\n".join(thoth_images)) else: raise NotImplementedError(f"Unknown output format {output_format}")
def migrate( namespace: str, s2i_thoth: str, tag: str, from_image_stream_tag: str, selector: Optional[str] = None, do_import_image: bool = True, dry_run: bool = False, trigger_build: bool = False, check_s2i_thoth: bool = True, insert_env_vars: bool = True, ) -> None: """Migrate an existing OpenShift application to use Thoth. Adjust an existing OpenShift deployment to use Thoth by adjusting OpenShift's BuildConfigs. """ s2i_thoth = s2i_thoth or click.prompt("Choose Thoth image to be imported", default=_DEFAULT_S2I_THOTH) if check_s2i_thoth: thoth_images = get_thoth_s2i_images() if s2i_thoth not in thoth_images: _LOGGER.error( "Image %r not found in Thoth's images, available are: %r", s2i_thoth, thoth_images, ) sys.exit(1) from_image_stream_tag_re = re.compile(from_image_stream_tag) image_stream_name = s2i_thoth.rsplit("/", maxsplit=1)[-1] with tempfile.NamedTemporaryFile() as temp_file: oc_get_bc(namespace=namespace, selector=selector, path=temp_file.name) build_configs = BuildConfig.load_all(path=temp_file.name, skip_errors=True) changed_build_configs = change_image_stream( build_configs, f"{image_stream_name}:{tag}", lambda bc: bool( from_image_stream_tag_re.fullmatch(bc.get_image_stream_tag())), ) if insert_env_vars: for build_config in changed_build_configs.values(): build_config.insert_thoth_env_vars() if dry_run: if trigger_build: _LOGGER.warning("Dry run will not trigger build") if do_import_image: _LOGGER.warning( "Dry run will not import image into OpenShift's registry") for build_config in changed_build_configs.values(): click.echo("--") click.echo(build_config.to_yaml()) return for build_config in changed_build_configs.values(): build_config.apply() if trigger_build: for build_config in changed_build_configs.values(): build_config.trigger_build(only_if_no_config_change=True) if len(changed_build_configs) > 0 and do_import_image: import_thoth_s2i_image(namespace, s2i_thoth)