- def boot_vm(self, vmname, image_id, flavor, network_id, test_ip, sg_id):
- config = dict()
- config['name'] = vmname
- config['flavor'] = flavor
- config['image'] = image_id
- config['nics'] = [{"net-id": network_id}]
- self.boot_vm_preparation(config, vmname, test_ip)
- self.logger.info("Creating instance '%s'..." % vmname)
- self.logger.debug("Configuration: %s" % config)
- vm = self.nova_client.servers.create(**config)
-
- # wait until VM status is active
- if not self.waitVmActive(self.nova_client, vm):
- vm_status = os_utils.get_instance_status(self.nova_client, vm)
- self.logger.error("Instance '%s' cannot be booted. Status is '%s'"
- % (vmname, vm_status))
- return None
- else:
- self.logger.info("Instance '%s' is ACTIVE." % vmname)
-
- self.add_secgroup(vmname, vm.id, sg_id)
-
- return vm
-
- def waitVmActive(self, nova, vm):
- # sleep and wait for VM status change
- sleep_time = 3
- count = self.vm_boot_timeout / sleep_time
- while True:
- status = os_utils.get_instance_status(nova, vm)
- self.logger.debug("Status: %s" % status)
- if status == "ACTIVE":
- return True
- if status == "ERROR" or status == "error":
- return False
- if count == 0:
- self.logger.debug("Booting a VM timed out...")
- return False
- count -= 1
- time.sleep(sleep_time)
- return False
-
- def add_secgroup(self, vmname, vm_id, sg_id):
- self.logger.info("Adding '%s' to security group '%s'..." %
- (vmname, self.sg_name))
- os_utils.add_secgroup_to_instance(self.nova_client, vm_id, sg_id)
-
- def get_test_ip(self, vm):
- test_ip = vm.networks.get(self.private_net_name)[0]
- self.logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
- return test_ip
-
- def parse_result(self, code, start_time, stop_time):
- test_status = "FAIL"
- if code == 0:
- self.logger.info("vPing OK")
- duration = round(stop_time - start_time, 1)
- self.logger.info("vPing duration:'%s'" % duration)
- test_status = "PASS"
- elif code == -2:
- duration = 0
- self.logger.info("Userdata is not supported in nova boot. "
- "Aborting test...")
- else:
- duration = 0
- self.logger.error("vPing FAILED")
-
- self.details = {'timestart': start_time,
- 'duration': duration,
- 'status': test_status}
- self.result = test_status
-
- @staticmethod
- def pMsg(msg):
- """pretty printing"""
- pprint.PrettyPrinter(indent=4).pprint(msg)
-