Exemple #1
0
def host_pings(host, timeout=15, ip_addr=False):
    """This ensures the given IP/hostname pings succesfully.

    :param host: A string. The IP or hostname of host.
    :param int timeout: The polling timeout in minutes.
    :param Boolean ip_addr: To return the ip address of the host

    """
    timeup = time.time() + int(timeout) * 60
    while True:
        command = subprocess.Popen('ping -c1 {0}; echo $?'.format(host),
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   shell=True)
        output = command.communicate()[0].decode()
        # Checking the return code of ping is 0
        if time.time() > timeup:
            logger.warning('The timeout for pinging the host {0} has '
                           'reached!'.format(host))
            return False
        if int(output.split()[-1]) == 0:
            if ip_addr:
                ip = output[output.find("(") + 1:output.find(")")]
                return True, ip
            return True
        else:
            time.sleep(5)
def wait_till_rhevm4_instance_status(instance_name, status, timeout=5):
    """Waits untill given RHEVM4 VM status reached.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.

    :param instance_name: A string. RHEVM Instance name to create.
    :param template_name: A string. RHEVM image name from which instance
        to be created.
    :param int timeout: The polling timeout in minutes to create rhevm
    instance.
    """
    with get_rhevm4_client().build() as rhevm_client:
        timeup = time.time() + int(timeout) * 60
        vservice = rhevm_client.system_service().vms_service()
        while True:
            if time.time() > timeup:
                logger.warning(
                    'Timeout in turning VM instance {0}.'.format(status))
                sys.exit(1)
            vm = vservice.list(search='name={}'.format(instance_name))[0]
            vm_status = vm.status.name
            logger.info('Current Status: {0}'.format(vm_status))
            if vm_status.lower() == status.lower():
                return True
            time.sleep(5)
Exemple #3
0
def satellite6_capsule_zstream_upgrade(cap_host):
    """Upgrades Capsule to its latest zStream version

    :param string cap_host: Capsule hostname onto which the capsule upgrade
    will run

    Note: For zstream upgrade both 'To' and 'From' version should be same

    FROM_VERSION
        Current satellite version which will be upgraded to latest version
    TO_VERSION
        Next satellite version to which satellite will be upgraded
    """
    logger.highlight('\n========== CAPSULE UPGRADE =================\n')
    from_version = os.environ.get('FROM_VERSION')
    to_version = os.environ.get('TO_VERSION')
    if not from_version == to_version:
        logger.warning('zStream Upgrade on Capsule cannot be performed as '
                       'FROM and TO versions are not same!')
        sys.exit(1)
    major_ver = distro_info()[1]
    if os.environ.get('CAPSULE_URL'):
        disable_old_repos('rhel-{0}-server-satellite-capsule-{1}-rpms'.format(
            major_ver, from_version))
    # Check what repos are set
    if os.environ.get("FOREMAN_MAINTAIN_CAPSULE_UPGRADE") == 'true':
        upgrade_using_foreman_maintain(sat_host=False)
    else:
        nonfm_upgrade(satellite_upgrade=False)
    # Rebooting the capsule for kernel update if any
    reboot(160)
    host_ssh_availability_check(cap_host)
    # Check if Capsule upgrade is success
    upgrade_validation()
def check_necessary_env_variables_for_upgrade(product):
    """Checks if necessary Environment Variables are provided

    :param string product: The product name to upgrade
    """
    failure = []
    # The upgrade product
    products = ['satellite', 'capsule', 'client', 'longrun', 'n-1']
    if product not in products:
        failure.append('Product name should be one of {0}.'.format(
            ', '.join(products)))
    # From which version to upgrade
    supported_vers = ['6.4', '6.3', '6.2', '6.1', '6.0']
    if os.environ.get('FROM_VERSION') not in supported_vers:
        failure.append('Wrong FROM_VERSION provided to upgrade from. '
                       'Provide one of 6.4, 6.3, 6.2, 6.1, 6.0')
    # To which version to upgrade
    if os.environ.get('TO_VERSION') not in ['6.4', '6.1', '6.2', '6.3']:
        failure.append('Wrong TO_VERSION provided to upgrade to. '
                       'Provide one of 6.4, 6.1, 6.2, 6.3')
    # Check If OS is set for creating an instance name in rhevm
    if not os.environ.get('OS'):
        failure.append('Please provide OS version as rhel7 or rhel6, '
                       'And retry !')
    if failure:
        logger.warning('Cannot Proceed Upgrade as:')
        for msg in failure:
            logger.warning(msg)
        sys.exit(1)
    return True
Exemple #5
0
def wait_till_rhevm_instance_status(instance_name, status, timeout=5):
    """Waits untill given VM status reached.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.

    :param instance_name: A string. RHEVM Instance name to create.
    :param template_name: A string. RHEVM image name from which instance
        to be created.
    :param int timeout: The polling timeout in minutes to create rhevm
    instance.
    """
    rhevm_client = get_rhevm_client()
    timeup = time.time() + int(timeout) * 60
    while True:
        if time.time() > timeup:
            logger.warning(
                'Timeout in turning VM instance {0}.'.format(status))
            sys.exit(1)
        vm_status = rhevm_client.vms.get(
            name=instance_name).get_status().get_state()
        logger.info('Current Status: {0}'.format(vm_status))
        if vm_status == status:
            return True
        time.sleep(5)
    rhevm_client.disconnect()
def host_ssh_availability_check(host, timeout=7):
    """This ensures the given host has ssh up and running..

    :param host: A string. The IP or hostname of host.
    :param int timeout: The polling timeout in minutes.

    """
    _, ip = host_pings(host, timeout=timeout, ip_addr=True)
    timeup = time.time() + int(timeout) * 60
    while True:
        command = subprocess.Popen(
            'nc -vn {0} 22 <<< \'\''.format(ip),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            shell=True
        )
        output = command.communicate()[1]
        print output
        # Checking the return code of ping is 0
        if time.time() > timeup:
            logger.warning('SSH timed out for host {0} '.format(host))
            return False
        if output.__contains__('seconds'):
            return True
        else:
            time.sleep(5)
def generate_satellite_docker_clients_on_rhevm(client_os,
                                               clients_count,
                                               custom_ak=None,
                                               org_label=None,
                                               puppet=False):
    """Generates satellite clients on docker as containers

    :param string client_os: Client OS of which client to be generated
        e.g: rhel6, rhel7
    :param string clients_count: No of clients to generate
    :param string custom_ak: Activation key name, to register clients
    :param string org_label: The organization in which the docker clients to
        created and where the custom ak is available
    :param bool puppet: Genearates puppet clients only if true

    Environment Variables:

    RHEV_SAT_HOST
        The satellite hostname for which clients to be generated and
        registered
    RHEV_CLIENT_AK
        The AK using which client will be registered to satellite
    """
    if int(clients_count) == 0:
        logger.warning(
            'Clients count to generate on Docker should be atleast 1 !')
        sys.exit(1)
    satellite_hostname = os.environ.get('RHEV_SAT_HOST')
    ak = custom_ak or os.environ.get('RHEV_CLIENT_AK_{}'.format(
        client_os.upper()))
    result = {'katello': {}, 'puppet': {}}
    puppet = bool(puppet)
    if not puppet:
        host_title = 'scenariokatelloclient{0}'.format(
            gen_string('alpha')) if custom_ak else 'dockerkatelloclient'
        image = 'upgrade:{}'
    else:
        host_title = 'scenariopuppetclient{0}'.format(
            gen_string('alpha')) if custom_ak else 'dockerpuppetclient'
        image = 'upgrade:puppet-{}'
    for count in range(int(clients_count)):
        if bz_bug_is_open('1405085'):
            time.sleep(5)
        # If custom activation key is passed, it will be used to create custom
        # docker clients for scenario tests and we will require to set distinct
        # hostname for those content hosts
        hostname = '{0}{1}{2}'.format(count, host_title, client_os)
        if org_label:
            create_command = 'docker run -d -h {0} -v /dev/log:/dev/log ' \
                '-e "SATHOST={1}" -e "AK={2}" -e "ORG={3}" {4}'.format(
                    hostname, satellite_hostname, ak, org_label,
                    image.format(client_os))
        else:
            create_command = 'docker run -d -h {0} -v /dev/log:/dev/log ' \
                '-e "SATHOST={1}" -e "AK={2}" {3}'.format(
                    hostname, satellite_hostname, ak, image.format(client_os))
        container_id = run(create_command)
        result['puppet' if puppet else 'katello'][hostname] = container_id
    return result
Exemple #8
0
def satellite6_capsule_zstream_upgrade(cap_host):
    """Upgrades Capsule to its latest zStream version

    :param string cap_host: Capsule hostname onto which the capsule upgrade
    will run

    Note: For zstream upgrade both 'To' and 'From' version should be same

    FROM_VERSION
        Current satellite version which will be upgraded to latest version
    TO_VERSION
        Next satellite version to which satellite will be upgraded
    """
    logger.highlight('\n========== CAPSULE UPGRADE =================\n')
    from_version = os.environ.get('FROM_VERSION')
    to_version = os.environ.get('TO_VERSION')
    if not from_version == to_version:
        logger.warning('zStream Upgrade on Capsule cannot be performed as '
                       'FROM and TO versions are not same!')
        sys.exit(1)
    major_ver = distro_info()[1]
    if os.environ.get('CAPSULE_URL'):
        disable_old_repos('rhel-{0}-server-satellite-capsule-{1}-rpms'.format(
            major_ver, from_version))
    # Check what repos are set
    run('yum repolist')
    if from_version == '6.0':
        # Stop katello services, except mongod
        run('for i in qpidd pulp_workers pulp_celerybeat '
            'pulp_resource_manager httpd; do service $i stop; done')
    else:
        # Stop katello services
        run('katello-service stop')
    run('yum clean all', warn_only=True)
    logger.info('Updating system and capsule packages ... ')
    preyum_time = datetime.now().replace(microsecond=0)
    update_packages(quiet=False)
    postyum_time = datetime.now().replace(microsecond=0)
    logger.highlight('Time taken for capsule packages update - {}'.format(
        str(postyum_time - preyum_time)))
    setup_capsule_firewall()
    preup_time = datetime.now().replace(microsecond=0)
    if to_version == '6.0':
        run('katello-installer --upgrade')
    elif to_version == '6.1':
        run('capsule-installer --upgrade')
    else:
        run('satellite-installer --scenario capsule --upgrade ')
    postup_time = datetime.now().replace(microsecond=0)
    logger.highlight('Time taken for Capsule Upgrade - {}'.format(
        str(postup_time - preup_time)))
    # Rebooting the capsule for kernel update if any
    reboot(160)
    host_ssh_availability_check(cap_host)
    # Check if Capsule upgrade is success
    run('katello-service status', warn_only=True)
Exemple #9
0
def get_openstack_client():
    """Creates client object instance from openstack novaclient API.
    And returns the client object for further use.

    The following environment variables affect this command:

    USERNAME
        The username of an openstack project to login.
    PASSWORD
        The password of an openstack project to login.
    AUTH_URL
        The authentication url of the project.
    PROJECT_NAME
        Project NAME of an openstack project.

    """
    username = os.environ.get('USERNAME')
    if username is None:
        logger.warning('The USERNAME environment variable should be defined')
    password = os.environ.get('OSP_PASSWORD')
    if password is None:
        logger.warning(
            'The OSP_PASSWORD environment variable should be defined')
    auth_url = os.environ.get('AUTH_URL')
    if auth_url is None:
        logger.warning('The AUTH_URL environment variable should be defined')
    project_name = os.environ.get('PROJECT_NAME')
    if project_name is None:
        logger.warning('The PROJECT_NAME environment variable should '
                       'be defined')
    domain_name = os.environ.get('DOMAIN_NAME')
    if domain_name is None:
        logger.warning('The DOMAIN_NAME environment variable should '
                       'be defined')
    data = {
        'clouds': {
            'identity_api_version': 3,
            'satellite-jenkins': {
                'auth': {
                    'username': username,
                    'password': password,
                    'project_name': project_name,
                    'auth_url': auth_url,
                    'user_domain_name': domain_name,
                    'project_domain_name': domain_name
                }
            }
        }
    }
    with open('clouds.yml', 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)
    openstack_client = shade.openstack_cloud(cloud='satellite-jenkins')
    return openstack_client
Exemple #10
0
def get_rhevm_client():
    """Creates and returns a client for rhevm.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.
    """
    username = os.environ.get('RHEV_USER')
    if username is None:
        logger.warning('The RHEV_USER environment variable should be defined.')
    password = os.environ.get('RHEV_PASSWD')
    if password is None:
        logger.warning(
            'The RHEV_PASSWD environment variable should be defined.')
    api_url = os.environ.get('RHEV_URL')
    if api_url is None:
        logger.warning('An RHEV_URL environment variable should be defined.')
    try:
        return API(url=api_url,
                   username=username,
                   password=password,
                   insecure=True)
    except errors.RequestError:
        logger.warning('Invalid Credentials provided for RHEVM.')
        sys.exit(1)
def delete_rhevm4_instance(instance_name, timeout=5):
    """Deletes RHEVM4 Instance.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.

    :param instance_name: A string. RHEVM instance name to delete.
    :param int timeout: The polling timeout in minutes to delete rhevm
    instance.
    """
    with get_rhevm4_client().build() as rhevm_client:
        vservice = rhevm_client.system_service().vms_service()
        vm = vservice.list(search='name={}'.format(instance_name))
        if not vm:
            logger.info('The instance {0} is not found '
                        'in RHEV to delete!'.format(instance_name))
        else:
            vm = vm[0]
            logger.info(
                'Deleting instance {0} from RHEVM.'.format(instance_name))
            if vm.delete_protected:
                logger.warning(
                    'The instance {0} is under delete protection and cannot '
                    'be deleted.'.format(instance_name))
                sys.exit(1)
            if vm.status.name == 'UP':
                vservice.vm_service(vm.id).shutdown()
                if wait_till_rhevm4_instance_status(instance_name, 'down'):
                    vservice.vm_service(vm.id).remove()
            elif vm.status.name == 'DOWN':
                vservice.vm_service(vm.id).remove()
            timeup = time.time() + int(timeout) * 60
            while True:
                if time.time() > timeup:
                    logger.warning(
                        'The timeout for deleting RHEVM instance has reached!')
                    sys.exit(1)
                if not vservice.list(search='name={}'.format(instance_name)):
                    logger.info(
                        'Instance {0} is now deleted from RHEVM!'.format(
                            instance_name))
                    break
Exemple #12
0
def delete_rhevm_instance(instance_name, timeout=5):
    """Deletes RHEVM Instance.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.

    :param instance_name: A string. RHEVM instance name to delete.
    :param int timeout: The polling timeout in minutes to delete rhevm
    instance.
    """
    rhevm_client = get_rhevm_client()
    vm = rhevm_client.vms.list(query='name={0}'.format(instance_name))
    if not vm:
        logger.info('The instance {0} is not found '
                    'in RHEV to delete!'.format(instance_name))
    else:
        logger.info('Deleting instance {0} from RHEVM.'.format(instance_name))
        if rhevm_client.vms.get(name=instance_name).get_delete_protected():
            logger.warning('The instance {0} is under delete protection and '
                           'cannot be deleted.'.format(instance_name))
            sys.exit(1)
        if rhevm_client.vms.get(
                name=instance_name).get_status().get_state() == 'up':
            rhevm_client.vms.get(name=instance_name).shutdown()
            if wait_till_rhevm_instance_status(instance_name, 'down'):
                rhevm_client.vms.get(name=instance_name).delete()
        elif rhevm_client.vms.get(
                name=instance_name).get_status().get_state() == 'down':
            rhevm_client.vms.get(name=instance_name).delete()
        timeup = time.time() + int(timeout) * 60
        while True:
            if time.time() > timeup:
                logger.warning(
                    'The timeout for deleting RHEVM instance has reached!')
                sys.exit(1)
            vm = rhevm_client.vms.list(query='name={0}'.format(instance_name))
            if not vm:
                logger.info('Instance {0} is now deleted from RHEVM!'.format(
                    instance_name))
                break
    rhevm_client.disconnect()
Exemple #13
0
def disable_old_repos(repo_name, timeout=1):
    """This ensures that the repo is disable and the command doesn't timeout

    :param repo_name: Repo ID of the repo to be disable
    :param int timeout: The polling timeout in minutes.
    """
    timeup = time.time() + int(timeout) * 60
    while True:
        run('subscription-manager refresh')
        repos = run('subscription-manager repos --list | grep \'Repo ID\'')
        if time.time() > timeup:
            logger.warning('There is no {0} repo to disable'.format(repo_name))
            return False
        if repos.__contains__(repo_name):
            run('subscription-manager repos --disable {0}'.format(repo_name))
            return True
        else:
            time.sleep(5)
Exemple #14
0
def docker_wait_until_repo_list(container_id, timeout=5):
    """Waits until the ak is attached and repo's are listed

    :param string container_id: ID of the docker container
    :param int timeout: The polling timeout in minutes.
    """
    timeup = time.time() + int(timeout) * 60
    while True:
        result = docker_execute_command(container_id,
                                        'yum repolist | grep repolist')
        result = int(result.splitlines()[0].split(':')[1].strip())
        if time.time() > timeup:
            logger.warning('There are no repos on {0} or timeup of {1} mins '
                           'has occured'.format(container_id, timeout))
            return False
        if result > 0:
            return True
        else:
            time.sleep(5)
Exemple #15
0
def get_sat_cap_version(product):
    """Determines and returns the installed Satellite/Capsule version on system

    :param string product: The product name as satellite/capsule
    :return string: Satellite/Capsule version
    """
    if 'sat' in product.lower():
        _6_2_VERSION_COMMAND = 'rpm -q satellite'
        _LT_6_2_VERSION_COMMAND = (
            'grep "VERSION" /usr/share/foreman/lib/satellite/version.rb')
    if 'cap' in product.lower():
        _6_2_VERSION_COMMAND = 'rpm -q satellite-capsule'
        _LT_6_2_VERSION_COMMAND = 'None'
    results = (_extract_sat_cap_version(cmd)
               for cmd in (_6_2_VERSION_COMMAND, _LT_6_2_VERSION_COMMAND))
    for version, cmd_result in results:
        if version:
            return version
    logger.warning(
        'Unable to detect installed version due to:\n{}'.format(cmd_result))
Exemple #16
0
def generate_satellite_docker_clients_on_rhevm(client_os,
                                               clients_count,
                                               custom_ak=None):
    """Generates satellite clients on docker as containers

    :param string client_os: Client OS of which client to be generated
        e.g: rhel6, rhel7
    :param string clients_count: No of clients to generate
    :param string custom_ak: Activation key name, to register clients

    Environment Variables:

    RHEV_SAT_HOST
        The satellite hostname for which clients to be generated and
        registered
    RHEV_CLIENT_AK
        The AK using which client will be registered to satellite
    """
    if int(clients_count) == 0:
        logger.warning(
            'Clients count to generate on Docker should be atleast 1 !')
        sys.exit(1)
    satellite_hostname = os.environ.get('RHEV_SAT_HOST')
    ak = custom_ak or os.environ.get('RHEV_CLIENT_AK_{}'.format(
        client_os.upper()))
    result = {}
    for count in range(int(clients_count)):
        if bz_bug_is_open('1405085'):
            time.sleep(5)
        # If custom activation key is passed, it will be used to create custom
        # docker clients for scenario tests and we will require to set distinct
        # hostname for those content hosts
        host_title = 'scenarioclient{0}'.format(
            gen_string('alpha')) if custom_ak else 'dockerclient'
        hostname = '{0}{1}{2}'.format(count, host_title, client_os)
        container_id = run(
            'docker run -d -h {0} -v /dev/log:/dev/log -e "SATHOST={1}" '
            '-e "AK={2}" upgrade:{3}'.format(hostname, satellite_hostname, ak,
                                             client_os))
        result[hostname] = container_id
    return result
Exemple #17
0
def satellite6_setup(os_version):
    """Sets up required things on upgrade running machine and on Satellite to
    perform satellite upgrade later

    :param string os_version: The OS version onto which the satellite installed
        e.g: rhel6, rhel7
    """
    # If Personal Satellite Hostname provided
    if os.environ.get('SATELLITE_HOSTNAME'):
        sat_host = os.environ.get('SATELLITE_HOSTNAME')
    # Else run upgrade on rhevm satellite
    else:
        # Get image name and Hostname from Jenkins environment
        missing_vars = [
            var for var in ('RHEV_SAT_IMAGE', 'RHEV_SAT_HOST')
            if var not in os.environ
        ]
        # Check if image name and Hostname in jenkins are set
        if missing_vars:
            logger.warning('The following environment variable(s) must be set '
                           'in jenkins environment: {0}.'.format(
                               ', '.join(missing_vars)))
            sys.exit(1)
        sat_image = os.environ.get('RHEV_SAT_IMAGE')
        sat_host = os.environ.get('RHEV_SAT_HOST')
        sat_instance = 'upgrade_satellite_auto_{0}'.format(os_version)
        execute(delete_rhevm4_instance, sat_instance)
        execute(create_rhevm4_instance, sat_instance, sat_image)
        if not host_pings(sat_host):
            sys.exit(1)
        execute(host_ssh_availability_check, sat_host)
        # start's/enables/install's ntp
        # Check that hostname and localhost resolve correctly
        execute(install_prerequisites, host=sat_host)
        # Subscribe the instance to CDN
        execute(subscribe, host=sat_host)
        execute(foreman_service_restart, host=sat_host)
    # Set satellite hostname in fabric environment
    env['satellite_host'] = sat_host
    logger.info('Satellite {} is ready for Upgrade!'.format(sat_host))
    return sat_host
Exemple #18
0
def check_necessary_env_variables_for_upgrade(product):
    """Checks if necessary Environment Variables are provided

    :param string product: The product name to upgrade
    """
    failure = []
    # The upgrade product
    products = ['satellite', 'capsule', 'client', 'longrun', 'n-1']
    if product not in products:
        failure.append('Product name should be one of {0}.'.format(
            ', '.join(products)))
    # Check If OS is set for creating an instance name in rhevm
    if not os.environ.get('OS'):
        failure.append('Please provide OS version as rhel7 or rhel6, '
                       'And retry !')
    if failure:
        logger.warning('Cannot Proceed Upgrade as:')
        for msg in failure:
            logger.warning(msg)
        sys.exit(1)
    return True
def get_rhevm4_client():
    """Creates and returns a client for rhevm4.

    The following environment variables affect this command:

    RHEV_USER
        The username of a rhevm project to login.
    RHEV_PASSWD
        The password of a rhevm project to login.
    RHEV_URL
        An url to API of rhevm project.
    """
    username = os.environ.get('RHEV_USER')
    if username is None:
        logger.warning('The RHEV_USER environment variable should be defined.')
    password = os.environ.get('RHEV_PASSWD')
    if password is None:
        logger.warning(
            'The RHEV_PASSWD environment variable should be defined.')
    api_url = os.environ.get('RHEV_URL')
    if api_url is None:
        logger.warning('An RHEV_URL environment variable should be defined.')
    builder = ConnectionBuilder(url=api_url,
                                username=username,
                                password=password,
                                insecure=True)
    conn = builder.build()
    if not conn.test():
        logger.error('Invalid Connection details are provided to RHEVM.')
        sys.exit(1)
    return builder
def satellite6_capsule_setup(sat_host, os_version, upgradable_capsule=True):
    """Setup all per-requisites for user provided capsule or auto created
    capsule on rhevm for capsule upgrade.

    :param string sat_host: Satellite hostname to which the capsule registered
    :param string os_version: The OS version onto which the capsule installed
        e.g: rhel6, rhel7
    :param bool upgradable_capsule: Whether to setup capsule to be able to
        upgrade in future
    """
    # For User Defined Capsule
    if os.environ.get('CAPSULE_HOSTNAMES'):
        cap_hosts = os.environ.get('CAPSULE_HOSTNAMES')
        if not os.environ.get('CAPSULE_AK'):
            logger.warning('CAPSULE_AK environment variable is not defined !')
            sys.exit(1)
    # Else run upgrade on rhevm capsule
    else:
        # Get image name and Hostname from Jenkins environment
        missing_vars = [
            var
            for var in ('RHEV_CAP_IMAGE', 'RHEV_CAP_HOST', 'RHEV_CAPSULE_AK')
            if var not in os.environ
        ]
        # Check if image name and Hostname in jenkins are set
        if missing_vars:
            logger.warning('The following environment variable(s) must be '
                           'set: {0}.'.format(', '.join(missing_vars)))
            sys.exit(1)
        cap_image = os.environ.get('RHEV_CAP_IMAGE')
        cap_hosts = os.environ.get('RHEV_CAP_HOST')
        cap_instance = 'upgrade_capsule_auto_{0}'.format(os_version)
        execute(delete_rhevm_instance, cap_instance)
        logger.info('Turning on Capsule Instance ....')
        execute(create_rhevm_instance, cap_instance, cap_image)
        non_responsive_host = []
        env['capsule_hosts'] = cap_hosts
        if ',' in cap_hosts:
            cap_hosts = [cap.strip() for cap in cap_hosts.split(',')]
        else:
            cap_hosts = [cap_hosts]
        for cap_host in cap_hosts:
            if not host_pings(cap_host):
                non_responsive_host.append(cap_host)
            else:
                execute(host_ssh_availability_check, cap_host)
                execute(lambda: run('katello-service restart'), host=cap_host)
        if non_responsive_host:
            logger.warning(
                str(non_responsive_host) + ' these are '
                'non-responsive hosts')
            sys.exit(1)
    copy_ssh_key(sat_host, cap_hosts)
    # Dont run capsule upgrade requirements for n-1 capsule
    if upgradable_capsule:
        execute(sync_capsule_repos_to_upgrade, cap_hosts, host=sat_host)
        for cap_host in cap_hosts:
            logger.info('Capsule {} is ready for Upgrade'.format(cap_host))
    return cap_hosts
Exemple #21
0
def get_hostname_from_ip(ip, timeout=3):
    """Retrives the hostname by logging into remote machine by IP.
    Specially for the systems who doesnt support reverse DNS.
    e.g usersys machines.

    :param ip: A string. The IP address of the remote host.
    :param int timeout: The polling timeout in minutes.

    """
    timeup = time.time() + int(timeout) * 60
    while True:
        if time.time() > timeup:
            logger.warning(
                'The timeout for getting the Hostname from IP has reached!')
            return False
        try:
            output = execute(lambda: run('hostname'), host=ip)
            logger.info('Hostname determined as: {0}'.format(output[ip]))
            break
        except:
            time.sleep(5)
    return output[ip]
Exemple #22
0
def generate_satellite_docker_clients_on_rhevm(client_os,
                                               clients_count,
                                               ak=None):
    """Generates satellite clients on docker as containers

    :param string client_os: Client OS of which client to be generated
        e.g: rhel6, rhel7
    :param string clients_count: No of clients to generate
    :param string ak: Activation key name, to register clients

    Environment Variables:

    RHEV_SAT_HOST
        The satellite hostname for which clients to be generated and
        registered
    RHEV_CLIENT_AK
        The AK using which client will be registered to satellite
    """
    if int(clients_count) == 0:
        logger.warning(
            'Clients count to generate on Docker should be atleast 1 !')
        sys.exit(1)
    satellite_hostname = os.environ.get('RHEV_SAT_HOST')
    if ak:
        ak = ak
    else:
        ak = os.environ.get('RHEV_CLIENT_AK_{}'.format(client_os.upper()))
    result = {}
    for count in range(int(clients_count)):
        if bz_bug_is_open('1405085'):
            time.sleep(5)
        hostname = '{0}dockerclient{1}'.format(count, client_os)
        container_id = run(
            'docker run -d -h {0} -v /dev/log:/dev/log -e "SATHOST={1}" '
            '-e "AK={2}" upgrade:{3}'.format(hostname, satellite_hostname, ak,
                                             client_os))
        result[hostname] = container_id
    return result
def satellite6_client_setup():
    """Sets up required things on upgrade running machine and on Client to
    perform client upgrade later

    If not personal clients, then it creates docker containers as clients on
    rhevm vm.

    Environment Variable:

    DOCKER_VM
        The Docker VM IP/Hostname on rhevm to create clients
    """
    # If User Defined Clients Hostname provided
    clients6 = os.environ.get('CLIENT6_HOSTS')
    clients7 = os.environ.get('CLIENT7_HOSTS')
    puppet_clients6 = puppet_clients7 = None
    docker_vm = os.environ.get('DOCKER_VM')
    clients_count = os.environ.get('CLIENTS_COUNT')
    from_version = os.environ.get('FROM_VERSION')
    sat_host = env.get('satellite_host')
    if clients6:
        clients6 = [client.strip() for client in str(clients6).split(',')]
        # Sync latest sat tools repo to clients if downstream
        if os.environ.get('TOOLS_URL_RHEL6'):
            logger.info('Syncing Tools repos of rhel6 in Satellite:')
            execute(sync_tools_repos_to_upgrade,
                    'rhel6',
                    clients6,
                    host=sat_host)
    if clients7:
        clients7 = [client.strip() for client in str(clients7).split(',')]
        # Sync latest sat tools repo to clients if downstream
        if os.environ.get('TOOLS_URL_RHEL7'):
            logger.info('Syncing Tools repos of rhel7 in Satellite:')
            execute(sync_tools_repos_to_upgrade,
                    'rhel7',
                    clients7,
                    host=sat_host)
    # Run upgrade on Docker Containers
    if not all([clients6, clients7]):
        if not clients_count:
            logger.warning('Clients Count is not set, please set and rerun !')
            sys.exit(1)
        elif int(clients_count) < 2:
            logger.warning('Clients Count should be atleast 2, please rerun !')
            sys.exit(1)
        # Check if the VM containing docker images is up, else turn on
        with get_rhevm4_client().build() as rhevm_client:
            instance_name = 'sat6-docker-upgrade'
            template_name = 'sat6-docker-upgrade-template'
            vm = rhevm_client.system_service().vms_service().list(
                search='name={}'.format(instance_name))
            if not vm:
                logger.info(
                    'Docker VM for generating Content Host is not created. '
                    'Creating it, please wait..')
                create_rhevm4_instance(instance_name, template_name)
                execute(manage_daemon, 'restart', 'docker', host=docker_vm)
            elif vm[0].status.name.lower() == 'down':
                logger.info('Docker VM for generating Content Host is not up. '
                            'Turning on, please wait ....')
                rhevm_client.vms.get(name=instance_name).start()
                wait_till_rhevm4_instance_status(instance_name, 'up', 5)
                execute(manage_daemon, 'restart', 'docker', host=docker_vm)
        time.sleep(5)
        logger.info('Generating {} clients on RHEL6 and RHEL7 on Docker. '
                    'Please wait .....'.format(clients_count))
        # Generate Clients on RHEL 7 and RHEL 6
        clients6 = execute(generate_satellite_docker_clients_on_rhevm,
                           'rhel6',
                           int(clients_count) / 2,
                           host=docker_vm)[docker_vm]
        clients7 = execute(generate_satellite_docker_clients_on_rhevm,
                           'rhel7',
                           int(clients_count) / 2,
                           host=docker_vm)[docker_vm]
        # Allow all puppet clients to be signed automatically
        execute(puppet_autosign_hosts, from_version, ['*'], host=sat_host)
        puppet_clients7 = execute(generate_satellite_docker_clients_on_rhevm,
                                  'rhel7',
                                  2,
                                  puppet=True,
                                  host=docker_vm)[docker_vm]
        puppet_clients6 = execute(generate_satellite_docker_clients_on_rhevm,
                                  'rhel6',
                                  2,
                                  puppet=True,
                                  host=docker_vm)[docker_vm]
        # Sync latest sat tools repo to clients if downstream
        if all([
                os.environ.get('TOOLS_URL_RHEL6'),
                os.environ.get('TOOLS_URL_RHEL7')
        ]):
            time.sleep(10)
            vers = ['6.0', '6.1']
            logger.info('Syncing Tools repos of rhel7 in Satellite..')
            if from_version in vers:
                all_clients7 = clients7.values() + puppet_clients7.values()
                all_clients6 = clients6.values() + puppet_clients6.values()
            else:
                all_clients7 = clients7.keys() + puppet_clients7.keys()
                all_clients6 = clients6.keys() + puppet_clients6.keys()
            execute(
                sync_tools_repos_to_upgrade,
                'rhel7',
                # Containers_ids are not required from sat version > 6.1 to
                # attach the subscription to client
                all_clients7,
                host=sat_host)
            time.sleep(10)
            logger.info('Syncing Tools repos of rhel6 in Satellite..')
            execute(
                sync_tools_repos_to_upgrade,
                'rhel6',
                # Containers_ids are not requied from sat version > 6.1 to
                # attach the subscriprion to client
                all_clients6,
                host=sat_host)
        # Refresh subscriptions on clients
        time.sleep(30)
        execute(refresh_subscriptions_on_docker_clients,
                clients6.values() + puppet_clients6.values(),
                host=docker_vm)
        time.sleep(30)
        execute(refresh_subscriptions_on_docker_clients,
                clients7.values() + puppet_clients7.values(),
                host=docker_vm)
        # Resetting autosign conf
        execute(puppet_autosign_hosts,
                from_version, [''],
                False,
                host=sat_host)
    logger.info('Clients are ready for Upgrade.')
    return clients6, clients7, puppet_clients7, puppet_clients6
Exemple #24
0
def sync_tools_repos_to_upgrade(client_os, hosts):
    """This syncs tools repo in Satellite server and also attaches
    the new tools repo subscription onto each client

    :param string client_os: The client OS of which tools repo to be synced
        e.g: rhel6, rhel7
    :param list hosts: The list of capsule hostnames to which new capsule
        repo subscription will be attached

    Following environment variable affects this function:

    TOOLS_URL_{client_os}
        The url of tools repo from latest satellite compose.
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'

    Personal Upgrade Env Vars:

    CLIENT_AK
        The ak_name attached to subscription of client

    Rhevm upgrade Env Vars:

    RHEV_CLIENT_AK
        The AK name used in client subscription
    """
    client_os = client_os.upper()
    tools_repo_url = os.environ.get('TOOLS_URL_{}'.format(client_os))
    if tools_repo_url is None:
        logger.warning('The Tools Repo URL for {} is not provided '
                       'to perform Client Upgrade !'.format(client_os))
        sys.exit(1)
    ak_name = os.environ.get(
        'CLIENT_AK_{}'.format(client_os),
        os.environ.get('RHEV_CLIENT_AK_{}'.format(client_os)))
    if ak_name is None:
        logger.warning('The AK details are not provided for {0} Client '
                       'upgrade!'.format(client_os))
        sys.exit(1)
    org = entities.Organization().search(
        query={'search': 'name="{}"'.format("Default Organization")})[0]
    ak = entities.ActivationKey(organization=org).search(
        query={'search': 'name={}'.format(ak_name)})[0]
    cv = ak.content_view.read()
    lenv = ak.environment.read()
    toolsproduct_name = customcontents['tools']['prod'].format(
        client_os=client_os)
    toolsrepo_name = customcontents['tools']['repo'].format(
        client_os=client_os)
    # adding sleeps in between to avoid race conditions
    tools_product = entities.Product(name=toolsproduct_name,
                                     organization=org).create()
    tools_repo = entities.Repository(name=toolsrepo_name,
                                     product=tools_product,
                                     url=tools_repo_url,
                                     organization=org,
                                     content_type='yum').create()
    entities.Repository(id=tools_repo.id).sync()
    cv.repository += [tools_repo]
    cv.update(['repository'])
    call_entity_method_with_timeout(cv.read().publish, timeout=2500)
    published_ver = entities.ContentViewVersion(
        id=max([cv_ver.id for cv_ver in cv.read().version])).read()
    published_ver.promote(data={'environment_id': lenv.id, 'force': False})
    tools_sub = entities.Subscription().search(
        query={'search': 'name={0}'.format(toolsproduct_name)})[0]
    ak.add_subscriptions(data={
        'quantity': 1,
        'subscription_id': tools_sub.id,
    })
    # Add this latest tools repo to hosts to upgrade
    sub = entities.Subscription().search(
        query={'search': 'name={0}'.format(toolsproduct_name)})[0]
    for host in hosts:
        if float(os.environ.get('FROM_VERSION')) <= 6.1:
            # If not User Hosts then, attach sub to dockered clients
            if not all([
                    os.environ.get('CLIENT6_HOSTS'),
                    os.environ.get('CLIENT7_HOSTS')
            ]):
                docker_vm = os.environ.get('DOCKER_VM')
                execute(attach_subscription_to_host_from_content_host,
                        sub.cp_id,
                        True,
                        host,
                        host=docker_vm)
            # Else, Attach subs to user hosts
            else:
                execute(attach_subscription_to_host_from_content_host,
                        sub.cp_id,
                        host=host)
        else:
            host = entities.Host().search(
                query={'search': 'name={}'.format(host)})[0]
            entities.HostSubscription(host=host).add_subscriptions(
                data={'subscriptions': [{
                    'id': sub.id,
                    'quantity': 1
                }]})
Exemple #25
0
def satellite6_upgrade(zstream=False):
    """This function is used to perform the satellite upgrade of two type based on
    their passed parameter.
    :param zstream:

    if upgrade_type==None:
        - Upgrades Satellite Server from old version to latest
            The following environment variables affect this command:

            BASE_URL
                Optional, defaults to available satellite version in CDN.
                URL for the compose repository
            FROM_VERSION
                Current satellite version which will be upgraded to latest version
            TO_VERSION
                Satellite version to upgrade to and enable repos while upgrading.
                e.g '6.1','6.2', '6.3'
            FOREMAN_MAINTAIN_SATELLITE_UPGRADE
                use foreman-maintain for satellite upgrade

    else:
        - Upgrades Satellite Server to its latest zStream version
            Note: For zstream upgrade both 'To' and 'From' version should be same

            FROM_VERSION
                Current satellite version which will be upgraded to latest version
            TO_VERSION
                Next satellite version to which satellite will be upgraded
            FOREMAN_MAINTAIN_SATELLITE_UPGRADE
                use foreman-maintain for satellite upgrade

    """
    logger.highlight('\n========== SATELLITE UPGRADE =================\n')
    to_version = os.environ.get('TO_VERSION')
    from_version = os.environ.get('FROM_VERSION')
    if zstream:
        if not from_version == to_version:
            logger.warning(
                'zStream Upgrade on Satellite cannot be performed as '
                'FROM and TO versions are not same!')
            sys.exit(1)
    base_url = None if not os.environ.get('BASE_URL') else os.environ.get(
        'BASE_URL')
    major_ver = distro_info()[1]
    disable_repo_name = ["*"]
    enable_repos_name = [
        'rhel-{0}-server-rpms'.format(major_ver),
        'rhel-server-rhscl-{0}-rpms'.format(major_ver)
    ]

    if bz_bug_is_open(1850934):
        run('echo "apache::mod::proxy::proxy_timeout: 120" >> '
            '/etc/foreman-installer/custom-hiera.yaml')

    # This statement will execute only until downstream release not become beta.
    if os.environ.get('DOWNSTREAM_FM_UPGRADE') == 'true' or \
            os.environ.get('FOREMAN_MAINTAIN_SATELLITE_UPGRADE') == 'false':
        # Following disables the old satellite repo and extra repos enabled
        # during subscribe e.g Load balancer Repo

        enable_disable_repo(disable_repo_name, enable_repos_name)
        os.environ[
            "whitelisted_param"] = ", repositories-validate, repositories-setup"
    else:
        os.environ["whitelisted_param"] = ''

    if os.environ.get('FOREMAN_MAINTAIN_SATELLITE_UPGRADE') == 'true' \
            and os.environ.get('OS') == 'rhel7':
        foreman_maintain_upgrade(base_url)
    else:
        # To install the package using foreman-maintain and it is applicable
        # above 6.7 version.
        setup_satellite_firewall()
        if not zstream:
            run('rm -rf /etc/yum.repos.d/rhel-{optional,released}.repo')
            logger.info('Updating system packages ... ')
            foreman_packages_installation_check(state="unlock")
            setup_foreman_maintain()
            update_packages(quiet=True)

        if base_url is None:
            enable_disable_repo([], [
                'rhel-{0}-server-satellite-{1}-rpms'.format(
                    major_ver, to_version)
            ])
            # Remove old custom sat repo
            repository_cleanup('sat')
        else:
            repository_setup("sat6", "satellite 6", base_url, 1, 0)
        nonfm_upgrade()
        foreman_packages_installation_check(state="lock")
    # Rebooting the satellite for kernel update if any
    reboot(180)
    host_ssh_availability_check(env.get('satellite_host'))
    # Test the Upgrade is successful
    upgrade_validation(True)
def sync_capsule_repos_to_upgrade(capsules):
    """This syncs capsule repo in Satellite server and also attaches
    the capsule repo subscription to each capsule

    :param list capsules: The list of capsule hostnames to which new capsule
    repo subscription will be attached

    Following environment variable affects this function:

    CAPSULE_URL
        The url for capsule repo from latest satellite compose.
        If not provided, capsule repo from Red Hat repositories will be enabled
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'
    TO_VERSION
        Upgradable Satellite version - To enable capsule repo
        e.g '6.1', '6.2'
    OS
        OS version to enable next version capsule repo
        e.g 'rhel7', 'rhel6'

    Personal Upgrade Env Vars:

    CAPSULE_AK
        The AK name used in capsule subscription

    Rhevm upgrade Env Vars:

    RHEV_CAPSULE_AK
        The AK name used in capsule subscription
    """
    logger.info('Syncing latest capsule repos in Satellite ...')
    capsule_repo = os.environ.get('CAPSULE_URL')
    from_version = os.environ.get('FROM_VERSION')
    to_version = os.environ.get('TO_VERSION')
    os_ver = os.environ.get('OS')[-1]
    if to_version in ['6.4', '6.3']:
        tools_repo_url = os.environ.get('TOOLS_URL_RHEL7')
    activation_key = os.environ.get(
        'CAPSULE_AK', os.environ.get('RHEV_CAPSULE_AK'))
    if activation_key is None:
        logger.warning(
            'The AK name is not provided for Capsule upgrade! Aborting...')
        sys.exit(1)
    # Set hammer configuration
    set_hammer_config()
    cv_name, env_name = hammer_determine_cv_and_env_from_ak(
        activation_key, '1')
    # Fix dead pulp tasks
    if os_ver == '6':
        run('for i in pulp_resource_manager pulp_workers pulp_celerybeat; '
            'do service $i restart; done')
    # If custom capsule repo is not given then
    # enable capsule repo from Redhat Repositories
    product_name = 'capsule6_latest' if capsule_repo \
        else 'Red Hat Satellite Capsule'
    repo_name = 'capsule6_latest_repo' if capsule_repo \
        else 'Red Hat Satellite Capsule {0} (for RHEL {1} Server) ' \
        '(RPMs)'.format(to_version, os_ver)
    try:
        if capsule_repo:
            # Check if the product of latest capsule repo is already created,
            # if not create one and attach the subscription to existing AK
            get_attribute_value(hammer(
                'product list --organization-id 1'), product_name, 'name')
            # If keyError is not thrown as if the product is created already
            logger.info(
                'The product for latest Capsule repo is already created!')
            logger.info('Attaching that product subscription to capsule ....')
        else:
            # In case of CDN Upgrade, the capsule repo has to be resynced
            # and needs to publich/promote those contents
            raise KeyError
    except KeyError:
        # If latest capsule repo is not created already(Fresh Upgrade),
        # So create new....
        if to_version in ['6.4', '6.3']:
            (
                rhscl_prd,
                rhscl_repo_name,
                rhscl_label,
                rh7server_prd,
                rh7server_repo_name,
                rh7server_label
            ) = sync_rh_repos_to_satellite()
            if tools_repo_url:
                capsule_tools = 'Capsule Tools Product'
                capsule_tools_repo = 'Capsule Tools Repo'
                hammer_product_create(capsule_tools, '1')
                time.sleep(2)
                hammer_repository_create(
                    capsule_tools_repo, '1', capsule_tools, tools_repo_url)
            else:
                tools_prd = 'Red Hat Enterprise Linux Server'
                tools_repo = 'Red Hat Satellite Tools {0} ' \
                             '(for RHEL {1} Server) (RPMs)'.format(to_version,
                                                                   os_ver
                                                                   )
                tools_label = 'rhel-{0}-server-satellite-tools-{1}-' \
                              'rpms'.format(os_ver, to_version)
                hammer_repository_set_enable(
                    tools_repo, tools_prd, '1', 'x86_64')
                time.sleep(5)
            hammer_repository_synchronize(capsule_tools_repo,
                                          '1',
                                          capsule_tools
                                          )
            hammer_content_view_add_repository(
                cv_name, '1', rhscl_prd, rhscl_repo_name)
            hammer_content_view_add_repository(
                cv_name, '1', rh7server_prd, rh7server_repo_name)
            hammer_content_view_add_repository(
                cv_name, '1', capsule_tools, capsule_tools_repo)
            hammer_activation_key_content_override(
                activation_key, rhscl_label, '1', '1')
            hammer_activation_key_content_override(
                activation_key, rh7server_label, '1', '1')
            if tools_repo_url:
                hammer_activation_key_add_subscription(
                    activation_key, '1', capsule_tools)
            else:
                hammer_activation_key_content_override(
                    activation_key, tools_label, '1', '1')
        if capsule_repo:
            hammer_product_create(product_name, '1')
            time.sleep(2)
            hammer_repository_create(
                repo_name, '1', product_name, capsule_repo)
        else:
            hammer_repository_set_enable(
                repo_name, product_name, '1', 'x86_64')
            repo_name = repo_name.replace('(', '').replace(')', '') + ' x86_64'
        hammer_repository_synchronize(repo_name, '1', product_name)
        # Add repos to CV
        hammer_content_view_add_repository(
            cv_name, '1', product_name, repo_name)
        hammer_content_view_publish(cv_name, '1')
        # Promote cv
        lc_env_id = get_attribute_value(
            hammer('lifecycle-environment list --organization-id 1 '
                   '--name {}'.format(env_name)), env_name, 'id')
        cv_version_data = hammer(
            'content-view version list --content-view {} '
            '--organization-id 1'.format(cv_name))
        latest_cv_ver = sorted([float(data['name'].split(
            '{} '.format(cv_name))[1]) for data in cv_version_data]).pop()
        cv_ver_id = get_attribute_value(cv_version_data, '{0} {1}'.format(
            cv_name, latest_cv_ver), 'id')
        hammer_content_view_promote_version(
            cv_name, cv_ver_id, lc_env_id, '1',
            False if from_version == '6.0' else True)
        if capsule_repo:
            hammer_activation_key_add_subscription(
                activation_key, '1', product_name)
        else:
            label = 'rhel-{0}-server-satellite-capsule-{1}-rpms'.format(
                os_ver, to_version)
            hammer_activation_key_content_override(
                activation_key, label, '1', '1')
    # Add this latest capsule repo to capsules to perform upgrade later
    # If downstream capsule, Update AK with latest capsule repo subscription
    if capsule_repo:
        for capsule in capsules:
            if from_version == '6.1':
                subscription_id = get_product_subscription_id(
                    '1', product_name)
                execute(
                    attach_subscription_to_host_from_content_host,
                    subscription_id,
                    host=capsule)
            else:
                attach_subscription_to_host_from_satellite(
                    '1', product_name, capsule)
    else:
        # In upgrade to CDN capsule, the subscription will be already attached
        pass
def sync_tools_repos_to_upgrade(client_os, hosts):
    """This syncs tools repo in Satellite server and also attaches
    the new tools repo subscription onto each client

    :param string client_os: The client OS of which tools repo to be synced
        e.g: rhel6, rhel7
    :param list hosts: The list of capsule hostnames to which new capsule
        repo subscription will be attached

    Following environment variable affects this function:

    TOOLS_URL_{client_os}
        The url of tools repo from latest satellite compose.
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'

    Personal Upgrade Env Vars:

    CLIENT_AK
        The ak_name attached to subscription of client

    Rhevm upgrade Env Vars:

    RHEV_CLIENT_AK
        The AK name used in client subscription
    """
    client_os = client_os.upper()
    tools_repo_url = os.environ.get('TOOLS_URL_{}'.format(client_os))
    if tools_repo_url is None:
        logger.warning('The Tools Repo URL for {} is not provided '
                       'to perform Client Upgrade !'.format(client_os))
        sys.exit(1)
    activation_key = os.environ.get(
        'CLIENT_AK_{}'.format(client_os),
        os.environ.get('RHEV_CLIENT_AK_{}'.format(client_os))
    )
    if activation_key is None:
        logger.warning('The AK details are not provided for {0} Client '
                       'upgrade!'.format(client_os))
        sys.exit(1)
    # Set hammer configuration
    set_hammer_config()
    cv_name, env_name = hammer_determine_cv_and_env_from_ak(
        activation_key, '1')
    tools_product = 'tools6_latest_{}'.format(client_os)
    tools_repo = 'tools6_latest_repo_{}'.format(client_os)
    # adding sleeps in between to avoid race conditions
    time.sleep(20)
    hammer_product_create(tools_product, '1')
    time.sleep(10)
    hammer_repository_create(tools_repo, '1', tools_product, tools_repo_url)
    time.sleep(10)
    hammer_repository_synchronize(tools_repo, '1', tools_product)
    hammer_content_view_add_repository(cv_name, '1', tools_product, tools_repo)
    hammer_content_view_publish(cv_name, '1')
    # Promote cv
    lc_env_id = get_attribute_value(
        hammer('lifecycle-environment list --organization-id 1 '
               '--name {}'.format(env_name)), env_name, 'id')
    cv_version_data = hammer(
        'content-view version list --content-view {} '
        '--organization-id 1'.format(cv_name))
    latest_cv_ver = sorted([float(data['name'].split(
        '{} '.format(cv_name))[1]) for data in cv_version_data]).pop()
    cv_ver_id = get_attribute_value(cv_version_data, '{0} {1}'.format(
        cv_name, latest_cv_ver), 'id')
    hammer_content_view_promote_version(cv_name, cv_ver_id, lc_env_id, '1')
    # Add new product subscriptions to AK
    hammer_activation_key_add_subscription(activation_key, '1', tools_product)
    # Add this latest tools repo to hosts to upgrade
    for host in hosts:
        if os.environ.get('FROM_VERSION') in ['6.0', '6.1']:
            subscription_id = get_product_subscription_id('1', tools_product)
            # If not User Hosts then, attach sub to dockered clients
            if not all([
                os.environ.get('CLIENT6_HOSTS'),
                os.environ.get('CLIENT7_HOSTS')
            ]):
                docker_vm = os.environ.get('DOCKER_VM')
                execute(
                    attach_subscription_to_host_from_content_host,
                    subscription_id,
                    True,
                    host,
                    host=docker_vm)
            # Else, Attach subs to user hosts
            else:
                execute(
                    attach_subscription_to_host_from_content_host,
                    subscription_id,
                    host=host)
        else:
            attach_subscription_to_host_from_satellite(
                '1', tools_product, host)
Exemple #28
0
def sync_capsule_repos_to_upgrade(capsules):
    """This syncs capsule repo in Satellite server and also attaches
    the capsule repo subscription to each capsule

    :param list capsules: The list of capsule hostnames to which new capsule
    repo subscription will be attached

    Following environment variable affects this function:

    CAPSULE_URL
        The url for capsule repo from latest satellite compose.
        If not provided, capsule repo from Red Hat repositories will be enabled
    TOOLS_URL_RHEL{}.format(os_ver)
        The url for capsuletools repo from latest satellite compose.
        If not provided, capsule repo from Red Hat repositories will be enabled
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'
    TO_VERSION
        Upgradable Satellite version - To enable capsule repo
        e.g '6.1', '6.2'
    OS
        OS version to enable next version capsule repo
        e.g 'rhel7', 'rhel6'

    Personal Upgrade Env Vars:

    CAPSULE_AK
        The AK name used in capsule subscription

    RHEV upgrade Env Vars:

    RHEV_CAPSULE_AK
        The AK name used in capsule subscription
    """
    to_version = os.environ.get('TO_VERSION')
    logger.info('Syncing latest capsule repos in Satellite ...')
    os_ver = os.environ.get('OS')[-1]
    capsule_repo = os.environ.get('CAPSULE_URL')
    capsuletools_url = os.environ.get('TOOLS_URL_RHEL{}'.format(os_ver))
    ak_name = os.environ.get('CAPSULE_AK', os.environ.get('RHEV_CAPSULE_AK'))
    if ak_name is None:
        logger.warning(
            'The AK name is not provided for Capsule upgrade! Aborting...')
        sys.exit(1)
    org = entities.Organization(id=1).read()
    ak = entities.ActivationKey(organization=org).search(
        query={'search': 'name={}'.format(ak_name)})[0]
    cv = ak.content_view.read()
    lenv = ak.environment.read()
    # Fix dead pulp tasks
    if os_ver == '6':
        run('for i in pulp_resource_manager pulp_workers pulp_celerybeat; '
            'do service $i restart; done')
    _sync_capsule_subscription_to_capsule_ak(ak)
    if float(to_version) >= 6.3:
        _add_additional_subscription_for_capsule(ak, capsuletools_url)
    # Publishing and promoting the CV with all newly added capsule, capsuletools, rhscl and
    # server repos combine
    call_entity_method_with_timeout(cv.read().publish, timeout=2000)
    published_ver = entities.ContentViewVersion(
        id=max([cv_ver.id for cv_ver in cv.read().version])).read()
    published_ver.promote(data={'environment_id': lenv.id, 'force': False})
    # Add capsule and tools custom prod subscription to capsules
    if capsule_repo:
        add_custom_product_subscription_to_hosts(
            customcontents['capsule']['prod'], capsules)
    if float(to_version) >= 6.3:
        if capsuletools_url:
            add_custom_product_subscription_to_hosts(
                customcontents['capsule_tools']['prod'], capsules)
def satellite6_zstream_upgrade():
    """Upgrades Satellite Server to its latest zStream version

    Note: For zstream upgrade both 'To' and 'From' version should be same

    FROM_VERSION
        Current satellite version which will be upgraded to latest version
    TO_VERSION
        Next satellite version to which satellite will be upgraded
    PERFORM_FOREMAN_MAINTAIN_UPGRADE
        use foreman-maintain for satellite upgrade
    """
    logger.highlight('\n========== SATELLITE UPGRADE =================\n')
    from_version = os.environ.get('FROM_VERSION')
    to_version = os.environ.get('TO_VERSION')
    if not from_version == to_version:
        logger.warning('zStream Upgrade on Satellite cannot be performed as '
                       'FROM and TO versions are not same!')
        sys.exit(1)
    base_url = os.environ.get('BASE_URL')
    major_ver = distro_info()[1]
    # remove once BZ 1644354 fixed
    run("yum erase ant-junit -y")
    if os.environ.get('PERFORM_FOREMAN_MAINTAIN_UPGRADE') == "true" \
            and os.environ.get('OS') == 'rhel7':
        if base_url is None:
            os.environ['DISTRIBUTION'] = "CDN"
        else:
            os.environ['DISTRIBUTION'] = "DOWNSTREAM"
        # setup foreman-maintain
        setup_foreman_maintain()
        preup_time = datetime.now().replace(microsecond=0)
        # perform upgrade using foreman-maintain
        upgrade_using_foreman_maintain()
        postup_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for Satellite Upgrade - {}'.format(
            str(postup_time - preup_time)))
    else:
        setup_satellite_firewall()
        # Following disables the old satellite repo and extra repos enabled
        # during subscribe e.g Load balancer Repo
        disable_repos('*', silent=True)
        enable_repos('rhel-{0}-server-rpms'.format(major_ver))
        enable_repos('rhel-server-rhscl-{0}-rpms'.format(major_ver))
        enable_repos('rhel-{0}-server-extras-rpms'.format(major_ver))
        # If CDN upgrade then enable satellite latest version repo
        if base_url is None:
            enable_repos('rhel-{0}-server-satellite-{1}-rpms'.format(
                major_ver, to_version))
            # Remove old custom sat repo
            for fname in os.listdir('/etc/yum.repos.d/'):
                if 'sat' in fname.lower():
                    os.remove('/etc/yum.repos.d/{}'.format(fname))
        # Else, consider this as Downstream upgrade
        else:
            # Add Sat6 repo from latest compose
            satellite_repo = StringIO()
            satellite_repo.write('[sat6]\n')
            satellite_repo.write('name=satellite 6\n')
            satellite_repo.write('baseurl={0}\n'.format(base_url))
            satellite_repo.write('enabled=1\n')
            satellite_repo.write('gpgcheck=0\n')
            put(local_path=satellite_repo,
                remote_path='/etc/yum.repos.d/sat6.repo')
            satellite_repo.close()
        # Check what repos are set
        run('yum repolist')
        # Stop katello services, except mongod
        run('katello-service stop')
        if to_version == '6.1':
            run('service-wait mongod start')
        run('yum clean all', warn_only=True)
        # Updating the packages again after setting sat6 repo
        logger.info('Updating system and satellite packages... ')
        preyum_time = datetime.now().replace(microsecond=0)
        update_packages(quiet=False)
        postyum_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for system and satellite packages update'
                         ' - {}'.format(str(postyum_time - preyum_time)))
        # Running Upgrade
        preup_time = datetime.now().replace(microsecond=0)
        if to_version == '6.1':
            run('katello-installer --upgrade')
        else:
            run('satellite-installer --scenario satellite --upgrade')
        postup_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for Satellite Upgrade - {}'.format(
            str(postup_time - preup_time)))
    # Rebooting the satellite for kernel update if any
    reboot(180)
    host_ssh_availability_check(env.get('satellite_host'))
    # Test the Upgrade is successful
    run('hammer ping', warn_only=True)
    run('katello-service status', warn_only=True)
def satellite6_upgrade():
    """Upgrades satellite from old version to latest version.

    The following environment variables affect this command:

    BASE_URL
        Optional, defaults to available satellite version in CDN.
        URL for the compose repository
    TO_VERSION
        Satellite version to upgrade to and enable repos while upgrading.
        e.g '6.1','6.2', '6.3'
    PERFORM_FOREMAN_MAINTAIN_UPGRADE
        use foreman-maintain for satellite upgrade
    """
    logger.highlight('\n========== SATELLITE UPGRADE =================\n')
    to_version = os.environ.get('TO_VERSION')
    base_url = os.environ.get('BASE_URL')
    if to_version not in ['6.1', '6.2', '6.3', '6.4', '6.5']:
        logger.warning('Wrong Satellite Version Provided to upgrade to. '
                       'Provide one of 6.1, 6.2, 6.3, 6.4, 6.5')
        sys.exit(1)
    major_ver = distro_info()[1]
    # remove once BZ 1644354 fixed
    run("yum erase ant-junit -y")
    if os.environ.get('PERFORM_FOREMAN_MAINTAIN_UPGRADE') == 'true' \
            and os.environ.get('OS') == 'rhel7':
        if base_url is None:
            os.environ['DISTRIBUTION'] = "CDN"
        else:
            os.environ['DISTRIBUTION'] = "DOWNSTREAM"
        # setup foreman-maintain
        setup_foreman_maintain()
        preup_time = datetime.now().replace(microsecond=0)
        # perform upgrade using foreman-maintain
        upgrade_using_foreman_maintain()
        postup_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for Satellite Upgrade - {}'.format(
            str(postup_time - preup_time)))
    else:
        setup_satellite_firewall()
        run('rm -rf /etc/yum.repos.d/rhel-{optional,released}.repo')
        logger.info('Updating system packages ... ')
        # setup foreman-maintain
        setup_foreman_maintain()
        update_packages(quiet=True)
        # Following disables the old satellite repo and extra repos enabled
        # during subscribe e.g Load balancer Repo
        disable_repos('*', silent=True)
        enable_repos('rhel-{0}-server-rpms'.format(major_ver))
        enable_repos('rhel-server-rhscl-{0}-rpms'.format(major_ver))
        enable_repos('rhel-{0}-server-extras-rpms'.format(major_ver))
        # If CDN upgrade then enable satellite latest version repo
        if base_url is None:
            enable_repos('rhel-{0}-server-satellite-{1}-rpms'.format(
                major_ver, to_version))
            # Remove old custom sat repo
            for fname in os.listdir('/etc/yum.repos.d/'):
                if 'sat' in fname.lower():
                    os.remove('/etc/yum.repos.d/{}'.format(fname))
        # Else, consider this as Downstream upgrade
        else:
            # Add Sat6 repo from latest compose
            satellite_repo = StringIO()
            satellite_repo.write('[sat6]\n')
            satellite_repo.write('name=satellite 6\n')
            satellite_repo.write('baseurl={0}\n'.format(base_url))
            satellite_repo.write('enabled=1\n')
            satellite_repo.write('gpgcheck=0\n')
            put(local_path=satellite_repo,
                remote_path='/etc/yum.repos.d/sat6.repo')
            satellite_repo.close()
        # Check what repos are set
        run('yum repolist')
        # Stop katello services, except mongod
        run('katello-service stop')
        if to_version == '6.1':
            run('service-wait mongod start')
        run('yum clean all', warn_only=True)
        # Updating the packages again after setting sat6 repo
        logger.info('Updating satellite packages ... ')
        preyum_time = datetime.now().replace(microsecond=0)
        update_packages(quiet=False)
        postyum_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for satellite packages update- {}'.format(
            str(postyum_time - preyum_time)))
        # Running Upgrade
        preup_time = datetime.now().replace(microsecond=0)
        if to_version == '6.1':
            run('katello-installer --upgrade')
        else:
            run('satellite-installer --scenario satellite --upgrade')
        postup_time = datetime.now().replace(microsecond=0)
        logger.highlight('Time taken for Satellite Upgrade - {}'.format(
            str(postup_time - preup_time)))
    # Rebooting the satellite for kernel update if any
    reboot(180)
    host_ssh_availability_check(env.get('satellite_host'))
    # Test the Upgrade is successful
    run('hammer ping', warn_only=True)
    run('katello-service status', warn_only=True)
    # Enable ostree feature only for rhel7 and sat6.2
    if to_version == '6.2' and major_ver == 7:
        enable_ostree(sat_version='6.2')