Example #1
0
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 get_osp_hostname(ipaddr):
    """The openstack has floating ip and we need to fetch the hostname from DNS
    :param ipaddr : IP address of the osp box
    """
    try:
        return socket.gethostbyaddr(ipaddr)[0]
    except Exception as ex:
        logger.error(ex)
Example #3
0
def validate_and_create_product_templates():
    """Task to do a sanity check on the satellite and capsule and then
    create their templates after z-stream upgrade

    Environment variables required to run upgrade on RHEVM Setup and will be
    fetched from Jenkins:
    ----------------------------------------------------------------------

    RHEV_SAT_HOST
        The rhevm satellite hostname to run upgrade on
    RHEV_CAP_HOST
        The rhevm capsule hostname to run upgrade on
    RHEV_STORAGE
        The storage domain on the rhevm used to create templates
    RHEV_SAT_IMAGE
        The satellite Image from which satellite instance will be created
    RHEV_CAP_IMAGE
        The capsule Image from which capsule instance will be created
    RHEV_SAT_INSTANCE
        The satellite instance name in rhevm of which template is to be
        created, generally the upgraded box
    RHEV_CAP_INSTANCE
        The capsule instance name in rhevm of which template is to be
        created, generally the upgraded box
    """
    # Get the instances name, specified in the jenkins job
    sat_instance = os.environ.get('RHEV_SAT_INSTANCE')
    cap_instance = os.environ.get('RHEV_CAP_INSTANCE')
    cluster = 'Default'
    storage = os.environ.get('RHEV_STORAGE')
    if sat_instance and cap_instance:
        sat_host = os.environ.get('RHEV_SAT_HOST')
        new_sat_template = os.environ.get('RHEV_SAT_IMAGE') + "_new"
        cap_host = os.environ.get('RHEV_CAP_HOST')
        new_cap_template = os.environ.get('RHEV_CAP_IMAGE') + "_new"
        if check_necessary_env_variables_for_upgrade('capsule'):
            execute(check_ntpd, host=sat_host)
            execute(katello_restart, host=sat_host)
            execute(capsule_sync, cap_host, host=sat_host)
            execute(check_ntpd, host=cap_host)
            execute(katello_restart, host=cap_host)
            try:
                create_rhevm_template(sat_instance, cluster, new_sat_template,
                                      storage)
                create_rhevm_template(cap_instance, cluster, new_cap_template,
                                      storage)
            except Exception as ex:
                logger.error('Failed to Create thread :\n%s' % str(ex))
Example #4
0
def create_rhevm4_template(host, cluster, new_template, storage):
    """ Creates RHEVM4 template from Virtual machines

    :param string host: The Virtual machine name of which, template is to be
        created.
    :param string cluster: The Cluster name of the RHEVM, in which the
        template is to be created.
    :param string new_template: The name of the template to be created.
    :param string storage: The name of the storage domain, which will be
        used to create template.
    """
    with get_rhevm4_client().build() as rhevm_client:
        storage_domain = rhevm_client.system_service().storage_domains_service(
        ).list(search='name={}'.format(storage))[0]
        cluster = rhevm_client.system_service().clusters_service().list(
            search='name={}'.format(cluster))
        vservice = rhevm_client.system_service().vms_service()
        size = storage_domain.available / 1024 / 1024 / 1024
        vm = vservice.list(search='name={}'.format(host))[0]
        if size > 300 and vm:
            try:
                vservice.vm_service(vm.id).stop()
                logger.info('Waiting for VM to reach Down status')
                wait_till_rhevm4_instance_status(host, 'down')
                logger.info('Template creation in Progress')
                templateconf = types.Template(name=new_template,
                                              vm=vm,
                                              cluster=cluster)
                tservice = rhevm_client.system_service().templates_service()
                tservice.add(template=templateconf)
                wait_till_rhevm4_instance_status(host, 'down', timeout=80)
                if tservice.list(search='name={}'.format(new_template)):
                    logger.info('{0} template is created successfully'.format(
                        new_template))
            except Exception as ex:
                logger.error('Failed to Create Template from VM:\n%s' %
                             str(ex))
        else:
            logger.error('Low Storage cannot proceed or VM not found')
            sys.exit()
Example #5
0
def create_rhevm_template(host, cluster, new_template, storage):
    """ Creates template from Virtual machines

    :param string host: The Virtual machine name of which, template is to be
        created.
    :param string cluster: The Cluster name of the RHEVM, in which the
        template is to be created.
    :param string new_template: The name of the template to be created.
    :param string storage: The name of the storage domain, which will be
        used to create template.
    """
    get_client = get_rhevm_client()
    storage_domain = get_client.storagedomains.get(name=storage)
    size = storage_domain.get_available() / 1024 / 1024 / 1024
    vm = get_client.vms.get(host)
    if size > 300 and vm:
        try:
            vm.stop()
            logger.info('Waiting for VM to reach Down status')
            wait_till_rhevm_instance_status(host, 'down')
            logger.info('Template creation in Progress')
            get_client.templates.add(
                params.Template(name=new_template,
                                vm=get_client.vms.get(host),
                                cluster=get_client.clusters.get(cluster)))
            wait_till_rhevm_instance_status(host, 'down', timeout=80)
            if get_client.templates.get(new_template):
                logger.info('{0} template is created successfully'.format(
                    new_template))
                get_client.disconnect()
        except Exception as ex:
            logger.error('Failed to Create Template from VM:\n%s' % str(ex))
            get_client.disconnect()

    else:
        get_client.disconnect()
        logger.error('Low Storage cannot proceed or VM not found')
Example #6
0
def delete_openstack_instance(instance_name):
    """Deletes openstack Instance.

    :param instance_name: A string. Openstack instance name to delete.

    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.

    """
    openstack_client = get_openstack_client()
    if openstack_client.delete_server(instance_name, timeout=300):
        logger.info('Success! The instance {0} has been deleted from '
                    'Openstack.'.format(instance_name))
    else:
        logger.error('Instance {0} not found in Openstack project.'.format(
            instance_name))
def katello_restart():
    """Restarts the katello services"""
    services = run('katello-service restart')
    if services.return_code > 0:
        logger.error('Unable to re-start the Satellite Services')
        sys.exit(1)
Example #8
0
def create_openstack_instance(instance_name,
                              image_name,
                              volume_size,
                              flavor_name=None,
                              ssh_key=None,
                              network_name=None):
    """Creates openstack Instance from Image and Assigns a floating IP
    to instance. Also It ensures that instance is ready for testing.

    :param instance_name: A string. Openstack Instance name to create.
    :param image_name: A string. Openstack image name from which instance
        to be created.
    :param volume_size: A string. Volume size to be created for osp instance
    :param flavor_name: A string. Openstack flavor_name for instance.
        e.g m1.small.
    :param ssh_key: A string. ssh_key 'name' that required to add
        into this instance.
    :param network_name: A string. Network 'name' that required to create
        this instance.

    ssh_key should be added to openstack project before running automation.
    Else the automation will fail.

    The following environment variables affect this command:

    FLAVOR_NAME
        Openstack flavor name to create compute settings for instance
    NETWORK_NAME
        Name of the network where the instance is created
    SSH_KEY
        ssh key to be added into the instance from openstack
    """
    env.user = '******'
    env.disable_known_hosts = True
    if flavor_name is None:
        flavor_name = flavor_name or os.environ.get('FLAVOR_NAME')
    if network_name is None:
        network_name = network_name or os.environ.get('NETWORK_NAME')
    if ssh_key is None:
        ssh_key = ssh_key or os.environ.get('OSP_SSHKEY')
    openstack_client = shade.openstack_cloud(cloud='satellite-jenkins')
    # Validate image is added into openstack project
    image = openstack_client.get_image(image_name)
    volume_name = '{0}_volume'.format(instance_name)
    logger.info('Creating new Openstack Volume {0}'.format(volume_name))
    openstack_client.create_volume(size=volume_size,
                                   name=volume_name,
                                   bootable=True,
                                   image=image.id)
    # Create instance from the given parameters
    logger.info('Creating new Openstack instance {0}'.format(instance_name))
    instance = openstack_client.create_server(name=instance_name,
                                              flavor=flavor_name,
                                              boot_from_volume=True,
                                              key_name=ssh_key,
                                              network=network_name,
                                              boot_volume=volume_name,
                                              terminate_volume=True,
                                              wait=True)
    if instance.interface_ip:
        ip_addr = instance.interface_ip
    else:
        logger.error("No floating Ip assigned")

    # Wait till DNS resolves the IP
    logger.info('Pinging the Host by IP:{0} ..........'.format(ip_addr))
    host_ssh_availability_check(ip_addr)
    host_pings(str(ip_addr))
    logger.info('SUCCESS !! The given IP has been pinged!!\n')
    logger.info('Now, Getting the hostname from IP......\n')
    hostname = get_osp_hostname(ip_addr)
    if not hostname:
        sys.exit(1)
    logger.info('Pinging the Hostname:{0} ..........'.format(hostname))
    host_pings(hostname)
    logger.info('SUCCESS !! The obtained hostname from IP is pinged !!')
    # Update the /etc/hosts file
    execute(lambda: run("hostnamectl set-hostname {0}".format(hostname)),
            host=hostname)
    execute(
        lambda: run("echo {0} {1} >> /etc/hosts".format(ip_addr, hostname)),
        host=hostname)
    with open('/tmp/instance.info', 'w') as outfile:
        outfile.write('OSP_HOSTNAME={0}'.format(hostname))
    return instance