Esempio n. 1
0
File: cli.py Progetto: bissenbay/s2i
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)
Esempio n. 2
0
File: cli.py Progetto: bissenbay/s2i
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)