Пример #1
0
def DisableUpgradeRedirection(unused_ref, args):
    """Disables upgrade redirection for the active project."""
    project = GetProject(args)
    messages = ar_requests.GetMessages()
    con = console_attr.GetConsoleAttr()

    log.status.Print("Disabling upgrade redirection...\n")
    if not CheckRedirectionPermission(project):
        return None

    # If the current state is finalized, then disabling is not possible
    log.status.Print("Checking current redirection status...\n")
    settings = ar_requests.GetProjectSettings(GetProject(args))
    current_status = settings.legacyRedirectionState

    if (current_status ==
            messages.ProjectSettings.LegacyRedirectionStateValueValuesEnum.
            REDIRECTION_FROM_GCR_IO_FINALIZED):
        log.status.Print(
            con.Colorize("FAIL:", "red") + " Redirection has already "
            "been finalized for project {}. Disabling redirection is not possible "
            "once it has been finalized.".format(project))
        return None

    update = console_io.PromptContinue(
        "This action will disable the redirection of Container Registry traffic to Artifact Registry for project {}"
        .format(project),
        default=False)
    if not update:
        log.status.Print("No changes made.")
        return None
    return ar_requests.DisableUpgradeRedirection(project)
def FinalizeUpgradeRedirection(unused_ref, args):
    """Finalizes upgrade redirection for the active project."""
    con = console_attr.GetConsoleAttr()
    project = GetProject(args)
    messages = ar_requests.GetMessages()

    log.status.Print("Finalizing upgrade redirection...\n")
    if not CheckRedirectionPermission(project):
        return None

    # Check the current status is enabled first.
    log.status.Print("Checking current redirection status...\n")
    settings = ar_requests.GetProjectSettings(GetProject(args))
    current_status = settings.legacyRedirectionState
    if (current_status ==
            messages.ProjectSettings.LegacyRedirectionStateValueValuesEnum.
            REDIRECTION_FROM_GCR_IO_FINALIZED):
        log.status.Print(
            "Redirection is already finalized for project {}.".format(project))
        return None
    if (current_status !=
            messages.ProjectSettings.LegacyRedirectionStateValueValuesEnum.
            REDIRECTION_FROM_GCR_IO_ENABLED):
        log.status.Print(
            "Redirection is not currently enabled for project {}.".format(
                project))
        log.status.Print(
            "Enable redirection using 'gcloud artifacts settings enable-upgrade-redirection'"
        )
        return None

    # Run the same report as enabling in case new repos have been added since.
    failed_repos = GetRedirectionEnablementReport(project)
    if failed_repos:
        log.status.Print(
            con.Colorize("FAIL: ", "red") +
            "Redirection cannot be enabled until all Container Registry repos have equivalent Artifact Registry repositories to redirect to:"
        )
        return None

    update = console_io.PromptContinue(
        "This action permanently redirects gcr.io traffic to Artifact Registry "
        "for project {}.".format(con.Emphasize(project, bold=True)),
        default=False)
    if not update:
        log.status.Print("No changes made.")
        return None
    return ar_requests.FinalizeUpgradeRedirection(GetProject(args))
Пример #3
0
def IssueGetProjectSettingsRequest(unused_ref, args):
    return ar_requests.GetProjectSettings(GetProject(args))
def EnableUpgradeRedirection(unused_ref, args):
    """Enables upgrade redirection for the active project."""
    project = GetProject(args)
    con = console_attr.GetConsoleAttr()
    dry_run = args.dry_run

    log.status.Print("Performing redirection enablement checks...\n")

    if not CheckRedirectionPermission(project):
        return None

    messages = ar_requests.GetMessages()
    settings = ar_requests.GetProjectSettings(project)
    current_status = settings.legacyRedirectionState
    if (current_status
            == messages.ProjectSettings.LegacyRedirectionStateValueValuesEnum.
            REDIRECTION_FROM_GCR_IO_ENABLED
            or current_status == messages.ProjectSettings):
        log.status.Print(
            "Redirection is already enabled for project {}.".format(project))
    elif (current_status ==
          messages.ProjectSettings.LegacyRedirectionStateValueValuesEnum.
          REDIRECTION_FROM_GCR_IO_FINALIZED):
        log.status.Print(
            "Redirection is already enabled (and finalized) for project {}.".
            format(project))
        return None

    failed_repos = GetRedirectionEnablementReport(project)

    if failed_repos:
        log.status.Print(
            con.Colorize("WARNING: ", "yellow") +
            "Not all Container Registry repositories have Artifact Registry repositories created to handle redirected requests."
        )

        for failed_repo in failed_repos:
            log.status.Print(
                "\nContainer Registry repository '{}' should redirect to "
                "an Artifact Registry Docker repository in location '{}' named '{}'. "
                "This repository can be created with:\n".format(
                    con.Emphasize(failed_repo["repository"], bold=True),
                    con.Emphasize(failed_repo["location"], bold=True),
                    con.Emphasize(failed_repo["repository"], bold=True)))

            log.status.Print(
                con.Emphasize(
                    "gcloud artifacts repositories create {} --location={} --project={} --repository-format=DOCKER"
                    .format(failed_repo["repository"], failed_repo["location"],
                            project)))

        log.status.Print(
            "\nIf Artifact Registry repositories are not created, the following Container Registry repositories will no"
            " longer serve requests:")
        for failed_repo in failed_repos:
            log.status.Print(
                con.Emphasize(failed_repo["repository"], bold=True))

    else:
        log.status.Print(
            con.Colorize("OK: ", "green") +
            "All Container Registry repositories have equivalent Artifact Registry "
            "repostories.\n")

    if dry_run:
        log.status.Print("Dry run enabled, no changes made.")
        return None

    update = console_io.PromptContinue(
        "\nThis action will redirect all Container Registry traffic to Artifact Registry for project {}."
        " After enabling redirection, you can route traffic back to Container Registry if needed, "
        " or you can finalize redirection to make redirection permanent.".
        format(project),
        default=False)
    if not update:
        log.status.Print("No changes made.")
        return None

    return ar_requests.EnableUpgradeRedirection(GetProject(args))