Beispiel #1
0
    def _get_vm(self, vm_name=None):
        """ Returns a vm from the RHEVM object.

        Args:
            vm_name: The name of the VM.

        Returns: an ovirtsdk vm object.
        """
        if vm_name is None:
            raise VMInstanceNotFound(vm_name)
        else:
            vm = self.api.vms.get(name=vm_name)
            if vm is None:
                raise VMInstanceNotFound(vm_name)
            return vm
Beispiel #2
0
 def _find_template_by_name(self, name):
     templates = self._get_all_templates()
     for template in templates:
         if template.name == name:
             return template
     else:
         raise VMInstanceNotFound("template {}".format(name))
Beispiel #3
0
 def _wait_template_ok(self, template_name):
     try:
         wait_for(
             lambda:
             self.api.templates.get(name=template_name).get_status().state == "ok",
             num_sec=30 * 60, message="template is OK", delay=45)
     except AttributeError:  # .get() returns None when template not found
         raise VMInstanceNotFound("Template {} not found!".format(template_name))
Beispiel #4
0
 def _find_instance_by_name(self, instance_name):
     try:
         instance = self._instances.get(project=self._project,
                                        zone=self._zone,
                                        instance=instance_name).execute()
         return instance
     except Exception:
         raise VMInstanceNotFound(instance_name)
Beispiel #5
0
 def _find_instance_by_name(self, name):
     """
     OpenStack Nova Client does have a find method, but it doesn't
     allow the find method to be used on other tenants. The list()
     method is the only one that allows an all_tenants=True keyword
     """
     instances = self._get_all_instances()
     for instance in instances:
         if instance.name == name:
             return instance
     else:
         raise VMInstanceNotFound(name)
Beispiel #6
0
 def _find_instance_by_name(self, instance_name):
     try:
         instance = self._instances.get(
             project=self._project, zone=self._zone, instance=instance_name).execute()
         return instance
     except Exception as e:
         self.logger.error(e)
         self.logger.info("Searching instance {} in all other zones".format(instance_name))
         zones = self._compute.zones().list(project=self._project).execute()
         for zone in zones.get('items', []):
             zone_name = zone.get('name', None)
             for instance in self._get_zone_instances(zone_name).get('items', []):
                 if instance['name'] == instance_name:
                     return instance
         self.logger.error("Instance {} not found in any of the zones".format(instance_name))
         raise VMInstanceNotFound(instance_name)
Beispiel #7
0
 def vm_status(self, vm_name, resource_group=None):
     self.logger.info("Attempting to Retrieve Azure VM Status {}".format(vm_name))
     azure_data = self.run_script(
         "Get-AzureRmVm -ResourceGroup \"{}\" -Name \"{}\" -Status | convertto-xml -as String"
         .format(resource_group or self.resource_group, vm_name))
     data = self.clean_azure_xml(azure_data)
     statusValue = json.loads(etree.parse(StringIO(data)).getroot().xpath(
         "./Object/Property[@Name='StatusesText']/text()")[0])
     # If script runs completely but the result isn't the one we need - better to show Azure
     # message
     if statusValue[0]['DisplayStatus'] == 'Provisioning failed':
         raise VMInstanceNotFound(statusValue[0]['Message'])
     powerStatus = statusValue[1]
     powerDisplayStatus = powerStatus['DisplayStatus']
     self.logger.info("Returned Status was {}".format(powerDisplayStatus))
     return powerDisplayStatus
Beispiel #8
0
    def _get_vm(self, vm_name, force=False):
        """Returns a vm from the VI object.

        Args:
            vm_name (string): The name of the VM
            force (bool): Ignore the cache when updating
        Returns:
             pyVmomi.vim.VirtualMachine: VM object
        """
        if vm_name not in self._vm_cache or force:
            vm = self._get_obj(vim.VirtualMachine, vm_name)
            if not vm:
                raise VMInstanceNotFound(vm_name)
            self._vm_cache[vm_name] = vm
        else:
            self._vm_cache[vm_name] = self._get_updated_obj(self._vm_cache[vm_name])
        return self._vm_cache[vm_name]
Beispiel #9
0
 def vm_status(self, vm_name, resource_group=None):
     self.logger.info(
         "Attempting to Retrieve Azure VM Status {}".format(vm_name))
     azure_data = self.run_script(
         'Get-AzureRmVm -ResourceGroup "{}" -Name "{}" -Status|Select -ExpandProperty Statuses|'
         'Select -Property Code, DisplayStatus, Message, Time|'
         'convertto-json'.format(resource_group or self.resource_group,
                                 vm_name))
     statusValue = json.loads(azure_data)
     # If script runs completely but the result isn't the one we need - better to show Azure
     # message
     if statusValue[0]['DisplayStatus'] == 'Provisioning failed':
         raise VMInstanceNotFound(statusValue[0]['Message'])
     powerStatus = statusValue[1]
     powerDisplayStatus = powerStatus['DisplayStatus']
     self.logger.info("Returned Status was {}".format(powerDisplayStatus))
     return powerDisplayStatus
Beispiel #10
0
    def _get_instance_id_by_name(self, instance_name):
        # Quick validation that the instance name isn't actually an ID
        # If people start naming their instances in such a way to break this,
        # check, that would be silly, but we can upgrade to regex if necessary.
        if instance_name.startswith('i-') and len(instance_name) == 10:
            # This is already an instance id, return it!
            return instance_name

        # Filter by the 'Name' tag
        filters = {
            'tag:Name': instance_name,
        }
        reservations = self.api.get_all_instances(filters=filters)
        instances = self._get_instances_from_reservations(reservations)
        if not instances:
            raise VMInstanceNotFound(instance_name)
        elif len(instances) > 1:
            raise MultipleInstancesError('Instance name "%s" is not unique' % instance_name)

        # We have an instance! return its ID
        return instances[0].id
Beispiel #11
0
    def _get_instance_id_by_name(self, instance_name):
        # Quick validation that the instance name isn't actually an ID
        # If people start naming their instances in such a way to break this,
        # check, that would be silly, but we can upgrade to regex if necessary.
        pattern = re.compile('^i-\w{8,17}$')
        if pattern.match(instance_name):
            return instance_name

        # Filter by the 'Name' tag
        filters = {
            'tag:Name': instance_name,
        }
        reservations = self.api.get_all_instances(filters=filters)
        instances = self._get_instances_from_reservations(reservations)
        if not instances:
            raise VMInstanceNotFound(instance_name)
        elif len(instances) > 1:
            raise MultipleInstancesError('Instance name "%s" is not unique' % instance_name)

        # We have an instance! return its ID
        return instances[0].id
Beispiel #12
0
 def _rename_template(self, old_name, new_name):
     template = self.api.templates.get(name=old_name)
     if template is None:
         raise VMInstanceNotFound("Template {} not found!".format(old_name))
     template.set_name(new_name)
     template.update()