def ssa_vm(request, local_setup_provider, provider, vm_analysis_provisioning_data,
           appliance, analysis_type):
    """ Fixture to provision instance on the provider """
    vm_name = 'test-ssa-{}-{}'.format(fauxfactory.gen_alphanumeric(), analysis_type)
    vm = VM.factory(vm_name, provider, template_name=vm_analysis_provisioning_data.image)
    request.addfinalizer(lambda: cleanup_vm(vm_name, provider))

    provision_data = vm_analysis_provisioning_data.copy()
    del provision_data['image']

    vm.create_on_provider(find_in_cfme=True, **provision_data)

    if provider.one_of(OpenStackProvider):
        public_net = provider.data['public_network']
        vm.provider.mgmt.assign_floating_ip(vm.name, public_net)

    logger.info("VM %s provisioned, waiting for IP address to be assigned", vm_name)

    @wait_for_decorator(timeout="20m", delay=5)
    def get_ip_address():
        logger.info("Power state for {} vm: {}, is_vm_stopped: {}".format(
            vm_name, provider.mgmt.vm_status(vm_name), provider.mgmt.is_vm_stopped(vm_name)))
        if provider.mgmt.is_vm_stopped(vm_name):
            provider.mgmt.start_vm(vm_name)

        ip = provider.mgmt.current_ip_address(vm_name)
        logger.info("Fetched IP for %s: %s", vm_name, ip)
        return ip is not None

    connect_ip = provider.mgmt.get_ip_address(vm_name)
    assert connect_ip is not None

    # Check that we can at least get the uptime via ssh this should only be possible
    # if the username and password have been set via the cloud-init script so
    # is a valid check
    if vm_analysis_provisioning_data['fs-type'] not in ['ntfs', 'fat32']:
        logger.info("Waiting for %s to be available via SSH", connect_ip)
        ssh_client = ssh.SSHClient(
            hostname=connect_ip, username=vm_analysis_provisioning_data['username'],
            password=vm_analysis_provisioning_data['password'], port=22)
        wait_for(ssh_client.uptime, num_sec=3600, handle_exception=True)
        vm.ssh = ssh_client
    vm.system_type = detect_system_type(vm)
    logger.info("Detected system type: %s", vm.system_type)
    vm.image = vm_analysis_provisioning_data['image']
    vm.connect_ip = connect_ip

    # TODO:  if rhev and iscsi, it need direct_lun
    if provider.type == 'rhevm':
        logger.info("Setting a relationship between VM and appliance")
        cfme_rel = Vm.CfmeRelationship(vm)
        cfme_rel.set_relationship(appliance.server.name, appliance.server_id())

    yield vm

    # Close the SSH client if we have one
    if getattr(vm, 'ssh', None):
        vm.ssh.close()
def instance(request, local_setup_provider, provider, vm_name, vm_analysis_data, appliance):
    """ Fixture to provision instance on the provider """

    vm = VM.factory(vm_name, provider, template_name=vm_analysis_data['image'])
    request.addfinalizer(lambda: cleanup_vm(vm_name, provider))

    provision_data = vm_analysis_data.copy()
    del provision_data['image']
    vm.create_on_provider(find_in_cfme=True, **provision_data)

    if provider.type == "openstack":
        vm.provider.mgmt.assign_floating_ip(vm.name, 'public')

    logger.info("VM %s provisioned, waiting for IP address to be assigned", vm_name)

    mgmt_system = provider.get_mgmt_system()

    @pytest.wait_for(timeout="20m", delay=5)
    def get_ip_address():
        logger.info("Power state for {} vm: {}, is_vm_stopped: {}".format(
            vm_name, mgmt_system.vm_status(vm_name), mgmt_system.is_vm_stopped(vm_name)))
        if mgmt_system.is_vm_stopped(vm_name):
            mgmt_system.start_vm(vm_name)

        ip = mgmt_system.current_ip_address(vm_name)
        logger.info("Fetched IP for %s: %s", vm_name, ip)
        return ip is not None

    connect_ip = mgmt_system.get_ip_address(vm_name)
    assert connect_ip is not None

    # Check that we can at least get the uptime via ssh this should only be possible
    # if the username and password have been set via the cloud-init script so
    # is a valid check
    if vm_analysis_data['fs-type'] not in ['ntfs', 'fat32']:
        logger.info("Waiting for %s to be available via SSH", connect_ip)
        ssh_client = ssh.SSHClient(hostname=connect_ip, username=vm_analysis_data['username'],
                                   password=vm_analysis_data['password'], port=22)
        wait_for(ssh_client.uptime, num_sec=3600, handle_exception=True)
        vm.ssh = ssh_client
    vm.system_type = detect_system_type(vm)
    logger.info("Detected system type: %s", vm.system_type)
    vm.image = vm_analysis_data['image']
    vm.connect_ip = connect_ip

    # TODO:  This is completely wrong and needs to be fixed
    #   CFME relationship is suppose to be set to the appliance, which is required
    #   to be placed within the same datastore that the VM resides
    #
    #   Also, if rhev and iscsi, it need direct_lun
    if provider.type == 'rhevm':
        logger.info("Setting a relationship between VM and appliance")
        from cfme.infrastructure.virtual_machines import Vm
        cfme_rel = Vm.CfmeRelationship(vm)
        server_name = appliance.server_name()
        cfme_rel.set_relationship(str(server_name), configuration.server_id())
    return vm
예제 #3
0
def instance(request, local_setup_provider, provider, vm_name,
             vm_analysis_data):
    """ Fixture to provision instance on the provider """

    template = vm_analysis_data.get('image', None)
    host_name, datastore_name = map(vm_analysis_data.get,
                                    ('host', 'datastore'))

    mgmt_system = provider.get_mgmt_system()

    provisioning_data = {
        'vm_name': vm_name,
        'host_name': {
            'name': [host_name]
        },
        'datastore_name': {
            'name': [datastore_name]
        },
    }

    try:
        provisioning_data['vlan'] = vm_analysis_data['vlan']
    except KeyError:
        # provisioning['vlan'] is required for rhevm provisioning
        if provider.type == 'rhevm':
            raise pytest.fail(
                'rhevm requires a vlan value in provisioning info')

    vm = VM.factory(vm_name, provider)

    connect_ip = None
    if provider.type == "openstack":
        image = vm_analysis_data['image']
        vm = VM.factory(vm_name, provider, image)
        request.addfinalizer(vm.delete_from_provider)
        connect_ip = mgmt_system.get_first_floating_ip()
        provider.refresh_provider_relationships(method='ui')
        inst_args = {
            'email':
            '*****@*****.**',
            'first_name':
            'Image',
            'last_name':
            'Provisioner',
            'template_name':
            image,
            'notes':
            ('Testing provisioning from image {} to vm {} on provider {}'.
             format(image, vm_name, provider.key)),
            'instance_type':
            vm_analysis_data['instance_type'],
            'availability_zone':
            vm_analysis_data['availability_zone'],
            'security_groups': [vm_analysis_data['security_group']],
            'cloud_network':
            vm_analysis_data['cloud_network'],
            'public_ip_address':
            connect_ip,
        }
        vm.create(**inst_args)
    else:
        request.addfinalizer(lambda: cleanup_vm(vm_name, provider))
        do_vm_provisioning(template,
                           provider,
                           vm_name,
                           provisioning_data,
                           request,
                           None,
                           num_sec=6000)
    logger.info("VM %s provisioned, waiting for IP address to be assigned",
                vm_name)

    @pytest.wait_for(timeout="20m", delay=5)
    def get_ip_address():
        logger.info("Power state for {} vm: {}, is_vm_stopped: {}".format(
            vm_name, mgmt_system.vm_status(vm_name),
            mgmt_system.is_vm_stopped(vm_name)))
        if mgmt_system.is_vm_stopped(vm_name):
            mgmt_system.start_vm(vm_name)

        ip = mgmt_system.current_ip_address(vm_name)
        logger.info("Fetched IP for %s: %s", vm_name, ip)
        return ip is not None

    connect_ip = mgmt_system.get_ip_address(vm_name)
    assert connect_ip is not None

    # Check that we can at least get the uptime via ssh this should only be possible
    # if the username and password have been set via the cloud-init script so
    # is a valid check
    if vm_analysis_data['fs-type'] not in ['ntfs', 'fat32']:
        logger.info("Waiting for %s to be available via SSH", connect_ip)
        ssh_client = ssh.SSHClient(hostname=connect_ip,
                                   username=vm_analysis_data['username'],
                                   password=vm_analysis_data['password'],
                                   port=22)
        wait_for(ssh_client.uptime, num_sec=3600, handle_exception=False)
        vm.ssh = ssh_client

    vm.system_type = detect_system_type(vm)
    logger.info("Detected system type: %s", vm.system_type)
    vm.image = vm_analysis_data['image']
    vm.connect_ip = connect_ip

    if provider.type == 'rhevm':
        logger.info("Setting a relationship between VM and appliance")
        from cfme.infrastructure.virtual_machines import Vm
        cfme_rel = Vm.CfmeRelationship(vm)
        cfme_rel.set_relationship(str(configuration.server_name()),
                                  configuration.server_id())
    return vm