def upgrade(ctx):
    oo_cfg = ctx.obj['oo_cfg']

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg['configuration'])
        sys.exit(1)

    # Update config to reflect the version we're targetting, we'll write
    # to disk once ansible completes successfully, not before.
    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']
    if oo_cfg.settings['variant'] == 'enterprise':
        oo_cfg.settings['variant'] = 'openshift-enterprise'
    version = find_variant(oo_cfg.settings['variant'])[1]
    oo_cfg.settings['variant_version'] = version.name
    click.echo("Openshift will be upgraded from %s %s to %s %s on the following hosts:\n" % (
        old_variant, old_version, oo_cfg.settings['variant'],
        oo_cfg.settings['variant_version']))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook()
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
            oo_cfg.settings['ansible_log_path'])
    else:
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx):
    oo_cfg = ctx.obj['oo_cfg']
    verbose = ctx.obj['verbose']

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']

    message = """
        This tool will help you upgrade your existing OpenShift installation.
"""
    click.echo(message)
    click.echo("Version {} found. Do you want to update to the latest version of {} " \
               "or migrate to the next major release?".format(old_version, old_version))
    resp = click.prompt(
        "(1) Update to latest {} (2) Migrate to next relese".format(
            old_version))

    if resp == "2":
        # TODO: Make this a lot more flexible
        new_version = "3.1"
        # Update config to reflect the version we're targetting, we'll write
        # to disk once ansible completes successfully, not before.
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'
        version = find_variant(oo_cfg.settings['variant'])[1]
        oo_cfg.settings['variant_version'] = version.name
    else:
        new_version = old_version

    click.echo(
        "Openshift will be upgraded from %s %s to %s %s on the following hosts:\n"
        % (old_variant, old_version, oo_cfg.settings['variant'],
           oo_cfg.settings['variant_version']))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(old_version, new_version,
                                                     verbose)
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
                   oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx):
    oo_cfg = ctx.obj['oo_cfg']
    verbose = ctx.obj['verbose']

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']


    message = """
        This tool will help you upgrade your existing OpenShift installation.
"""
    click.echo(message)
    click.echo("Version {} found. Do you want to update to the latest version of {} " \
               "or migrate to the next major release?".format(old_version, old_version))
    resp = click.prompt("(1) Update to latest {} (2) Migrate to next relese".format(old_version))

    if resp == "2":
        # TODO: Make this a lot more flexible
        new_version = "3.1"
        # Update config to reflect the version we're targetting, we'll write
        # to disk once ansible completes successfully, not before.
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'
        version = find_variant(oo_cfg.settings['variant'])[1]
        oo_cfg.settings['variant_version'] = version.name
    else:
        new_version = old_version

    click.echo("Openshift will be upgraded from %s %s to %s %s on the following hosts:\n" % (
        old_variant, old_version, oo_cfg.settings['variant'],
        oo_cfg.settings['variant_version']))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(old_version, new_version, verbose)
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
            oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx):
    oo_cfg = ctx.obj['oo_cfg']
    verbose = ctx.obj['verbose']

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    # Update config to reflect the version we're targetting, we'll write
    # to disk once ansible completes successfully, not before.
    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']
    if oo_cfg.settings['variant'] == 'enterprise':
        oo_cfg.settings['variant'] = 'openshift-enterprise'
    version = find_variant(oo_cfg.settings['variant'])[1]
    oo_cfg.settings['variant_version'] = version.name
    click.echo(
        "Openshift will be upgraded from %s %s to %s %s on the following hosts:\n"
        % (old_variant, old_version, oo_cfg.settings['variant'],
           oo_cfg.settings['variant_version']))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(verbose)
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
                   oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx, latest_minor, next_major):
    oo_cfg = ctx.obj['oo_cfg']
    verbose = ctx.obj['verbose']

    upgrade_mappings = {
        '3.0': {
            'minor_version': '3.0',
            'minor_playbook': 'v3_0_minor/upgrade.yml',
            'major_version': '3.1',
            'major_playbook': 'v3_0_to_v3_1/upgrade.yml',
        },
        '3.1': {
            'minor_version': '3.1',
            'minor_playbook': 'v3_1_minor/upgrade.yml',
            'major_playbook': 'v3_1_to_v3_2/upgrade.yml',
            'major_version': '3.2',
        }
    }

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']
    mapping = upgrade_mappings.get(old_version)

    message = """
        This tool will help you upgrade your existing OpenShift installation.
"""
    click.echo(message)

    if not (latest_minor or next_major):
        click.echo("Version {} found. Do you want to update to the latest version of {} " \
                   "or migrate to the next major release?".format(old_version, old_version))
        response = click.prompt("(1) Update to latest {} " \
                                "(2) Migrate to next release".format(old_version),
                                type=click.Choice(['1', '2']),)
        if response == "1":
            latest_minor = True
        if response == "2":
            next_major = True

    if next_major:
        playbook = mapping['major_playbook']
        new_version = mapping['major_version']
        # Update config to reflect the version we're targetting, we'll write
        # to disk once ansible completes successfully, not before.
        oo_cfg.settings['variant_version'] = new_version
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'

    if latest_minor:
        playbook = mapping['minor_playbook']
        new_version = mapping['minor_version']

    click.echo(
        "Openshift will be upgraded from %s %s to %s %s on the following hosts:\n"
        % (old_variant, old_version, oo_cfg.settings['variant'], new_version))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(playbook, verbose)
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
                   oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx, latest_minor, next_major):
    oo_cfg = ctx.obj['oo_cfg']

    if len(oo_cfg.deployment.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    variant = oo_cfg.settings['variant']
    if find_variant(variant)[0] is None:
        click.echo("%s is not a supported variant for upgrade." % variant)
        sys.exit(0)

    old_version = oo_cfg.settings['variant_version']

    try:
        mapping = UPGRADE_MAPPINGS[old_version]
    except KeyError:
        click.echo('No upgrades available for %s %s' % (variant, old_version))
        sys.exit(0)

    message = """
        This tool will help you upgrade your existing OpenShift installation.
        Currently running: %s %s
"""
    click.echo(message % (variant, old_version))

    # Map the dynamic upgrade options to the playbook to run for each.
    # Index offset by 1.
    # List contains tuples of booleans for (latest_minor, next_major)
    selections = []
    if not (latest_minor or next_major):
        i = 0
        if 'minor_playbook' in mapping:
            click.echo("(%s) Update to latest %s" % (i + 1, old_version))
            selections.append((True, False))
            i += 1
        if 'major_playbook' in mapping:
            click.echo("(%s) Upgrade to next release: %s" % (i + 1, mapping['major_version']))
            selections.append((False, True))
            i += 1

        response = click.prompt("\nChoose an option from above",
                                type=click.Choice(list(map(str, range(1, len(selections) + 1)))))
        latest_minor, next_major = selections[int(response) - 1]

    if next_major:
        if 'major_playbook' not in mapping:
            click.echo("No major upgrade supported for %s %s with this version "
                       "of atomic-openshift-utils." % (variant, old_version))
            sys.exit(0)
        playbook = mapping['major_playbook']
        new_version = mapping['major_version']
        # Update config to reflect the version we're targeting, we'll write
        # to disk once Ansible completes successfully, not before.
        oo_cfg.settings['variant_version'] = new_version
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'

    if latest_minor:
        if 'minor_playbook' not in mapping:
            click.echo("No minor upgrade supported for %s %s with this version "
                       "of atomic-openshift-utils." % (variant, old_version))
            sys.exit(0)
        playbook = mapping['minor_playbook']
        new_version = old_version

    click.echo("OpenShift will be upgraded from %s %s to latest %s %s on the following hosts:\n" % (
        variant, old_version, oo_cfg.settings['variant'], new_version))
    for host in oo_cfg.deployment.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        if not click.confirm("\nDo you want to proceed?"):
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(oo_cfg.deployment.hosts,
                                                     playbook,
                                                     ctx.obj['verbose'])
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
                   oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx, latest_minor, next_major):
    oo_cfg = ctx.obj['oo_cfg']

    if len(oo_cfg.deployment.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    variant = oo_cfg.settings['variant']
    if find_variant(variant)[0] is None:
        click.echo("%s is not a supported variant for upgrade." % variant)
        sys.exit(0)

    old_version = oo_cfg.settings['variant_version']
    mapping = UPGRADE_MAPPINGS.get(old_version)

    message = """
        This tool will help you upgrade your existing OpenShift installation.
        Currently running: %s %s
"""
    click.echo(message % (variant, old_version))

    # Map the dynamic upgrade options to the playbook to run for each.
    # Index offset by 1.
    # List contains tuples of booleans for (latest_minor, next_major)
    selections = []
    if not (latest_minor or next_major):
        i = 0
        if 'minor_playbook' in mapping:
            click.echo("(%s) Update to latest %s" % (i + 1, old_version))
            selections.append((True, False))
            i += 1
        if 'major_playbook' in mapping:
            click.echo("(%s) Upgrade to next release: %s" %
                       (i + 1, mapping['major_version']))
            selections.append((False, True))
            i += 1

        response = click.prompt(
            "\nChoose an option from above",
            type=click.Choice(list(map(str, range(1,
                                                  len(selections) + 1)))))
        latest_minor, next_major = selections[int(response) - 1]

    if next_major:
        if 'major_playbook' not in mapping:
            click.echo(
                "No major upgrade supported for %s %s with this version "
                "of atomic-openshift-utils." % (variant, old_version))
            sys.exit(0)
        playbook = mapping['major_playbook']
        new_version = mapping['major_version']
        # Update config to reflect the version we're targetting, we'll write
        # to disk once Ansible completes successfully, not before.
        oo_cfg.settings['variant_version'] = new_version
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'

    if latest_minor:
        if 'minor_playbook' not in mapping:
            click.echo(
                "No minor upgrade supported for %s %s with this version "
                "of atomic-openshift-utils." % (variant, old_version))
            sys.exit(0)
        playbook = mapping['minor_playbook']
        new_version = old_version

    click.echo(
        "OpenShift will be upgraded from %s %s to latest %s %s on the following hosts:\n"
        % (variant, old_version, oo_cfg.settings['variant'], new_version))
    for host in oo_cfg.deployment.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        if not click.confirm("\nDo you want to proceed?"):
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(oo_cfg.deployment.hosts,
                                                     playbook,
                                                     ctx.obj['verbose'])
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
                   oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")
def upgrade(ctx, latest_minor, next_major):
    oo_cfg = ctx.obj['oo_cfg']
    verbose = ctx.obj['verbose']

    upgrade_mappings = {
                        '3.0':{
                               'minor_version' :'3.0',
                               'minor_playbook':'v3_0_minor/upgrade.yml',
                               'major_version' :'3.1',
                               'major_playbook':'v3_0_to_v3_1/upgrade.yml',
                              },
                        '3.1':{
                               'minor_version' :'3.1',
                               'minor_playbook':'v3_1_minor/upgrade.yml',
                               'major_playbook':'v3_1_to_v3_2/upgrade.yml',
                               'major_version' :'3.2',
                            }
                       }

    if len(oo_cfg.hosts) == 0:
        click.echo("No hosts defined in: %s" % oo_cfg.config_path)
        sys.exit(1)

    old_variant = oo_cfg.settings['variant']
    old_version = oo_cfg.settings['variant_version']
    mapping = upgrade_mappings.get(old_version)

    message = """
        This tool will help you upgrade your existing OpenShift installation.
"""
    click.echo(message)

    if not (latest_minor or next_major):
        click.echo("Version {} found. Do you want to update to the latest version of {} " \
                   "or migrate to the next major release?".format(old_version, old_version))
        response = click.prompt("(1) Update to latest {} " \
                                "(2) Migrate to next release".format(old_version),
                                type=click.Choice(['1', '2']),)
        if response == "1":
            latest_minor = True
        if response == "2":
            next_major = True

    if next_major:
        playbook = mapping['major_playbook']
        new_version = mapping['major_version']
        # Update config to reflect the version we're targetting, we'll write
        # to disk once ansible completes successfully, not before.
        oo_cfg.settings['variant_version'] = new_version
        if oo_cfg.settings['variant'] == 'enterprise':
            oo_cfg.settings['variant'] = 'openshift-enterprise'

    if latest_minor:
        playbook = mapping['minor_playbook']
        new_version = mapping['minor_version']

    click.echo("Openshift will be upgraded from %s %s to %s %s on the following hosts:\n" % (
        old_variant, old_version, oo_cfg.settings['variant'], new_version))
    for host in oo_cfg.hosts:
        click.echo("  * %s" % host.connect_to)

    if not ctx.obj['unattended']:
        # Prompt interactively to confirm:
        proceed = click.confirm("\nDo you wish to proceed?")
        if not proceed:
            click.echo("Upgrade cancelled.")
            sys.exit(0)

    retcode = openshift_ansible.run_upgrade_playbook(playbook, verbose)
    if retcode > 0:
        click.echo("Errors encountered during upgrade, please check %s." %
            oo_cfg.settings['ansible_log_path'])
    else:
        oo_cfg.save_to_disk()
        click.echo("Upgrade completed! Rebooting all hosts is recommended.")