Exemplo n.º 1
0
def stop_server(auth_client, server_uuid):
    """Function to stop server"""
    # Check it's state first, as it is an error to stop it when it is already stopped (or in any other state
    # apart from running).
    server_state = get_server_state(auth_client, server_uuid)

    if (server_state == 'STARTING'):
        print("Server appears to be starting; waiting until it has completed before stopping")
        ret = wait_for_server(auth_client, server_uuid, 'RUNNING')
        if (ret != 0):
            raise Exception("Server not in RUNNING state, cannot be stopped")

        server_state = get_server_state(auth_client, server_uuid)
        # Fall-thru if the server made it to running state
        #print("fall-thru")

    if (server_state == 'RUNNING'):
        change_server_status(auth_client, server_uuid, 'STOPPED')

    if (server_state == 'NOT_FOUND'):
        return server_state

    # Check we actually made it to STOPPED state
    ret = wait_for_server(auth_client, server_uuid, 'STOPPED')
    if (ret != 0):
        raise Exception("Server failed to STOP")

    server_state = get_server_state(auth_client, server_uuid)
    return server_state
    def _detach_disk(self, node_instance):
        """Detach disk from the VM.
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        """
        print 'Scaling down in progress for NODE INSTANCE name: %s' %node_instance.get_name()
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']

        print 'Stopping the VM: %s to detach disk' %vm
        # Stop the VM before detaching the disk
        try:
            ret = StopVM(
                vm_uuid,
                self.user_info.get_cloud('user.uuid'),
                self.user_info.get_cloud_username(),
                self.user_info.get_cloud_password(),
                self.user_info.get_cloud_endpoint(),
                self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' % (vm_uuid, str(ex)))

        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(), self.user_info.get_cloud_username(),
                        self.user_info.get_cloud('user.uuid'), self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)
        server_resultset = list_resource_by_uuid(auth, vm_uuid, res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
            server = server_resultset['list'][l]
            disks = server['disks']

        # Get the UUID of the disk to be detached
        detach_diskUUID = node_instance.get_disk_detach_device()
        print '\n\n\n'
        print 'detach_diskUUID passed = %s' %detach_diskUUID

        found = False
        for disk in disks:
            if(disk['resourceUUID'] == detach_diskUUID):
                print 'Disk found on the server'
                found = True
        if (found == False):
            raise Exceptions.ExecutionException("Disk not found on the server %s" %vm_uuid)

        print 'Detaching the additional volatile disk'
        # Detach it now
        if (detach_diskUUID != ""):
	    detach_disk(auth, vm_uuid, detach_diskUUID)
            # Delete the resource
            rest_delete_resource(auth, detach_diskUUID, "DISK")

        print 'Restart the VM'
        server_data=[vm_uuid]
        start_server(auth, server_data)

        print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException("Server is not in RUNNING state")
Exemplo n.º 3
0
def WaitUntilVMRunning(server_uuid, customerUUID, customerUsername, customerPassword, endpoint, isVerbose=False):
    config.get_config("")

    config.CUST_UUID = customerUUID
    config.USER_LOGIN = customerUsername
    config.USER_PASSWORD = customerPassword
    config.HOST_NAME = endpoint
    # config.NETWORK_TYPE  = networkType

    auth_client = setup()
    server_state = get_server_state(auth_client, server_uuid)

    if (server_state != 'RUNNING'):
        ret = wait_for_server(server_client=auth_client, server_uuid=server_uuid, status='RUNNING')
        if (ret != 0):
            raise Exception("Server did not get to RUNNING state")

    return server_state
    def _resize(self, node_instance):
        """
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        """
        print 'Node instance name: %s' %node_instance.get_name()
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']
        # Get the Product Offer UUID of the Standard Server product
        product_offer = 'Standard Server'

        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(), self.user_info.get_cloud_username(),
                        self.user_info.get_cloud('user.uuid'), self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)

        server_resultset = list_resource_by_uuid(auth, vm_uuid, res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
            server = server_resultset['list'][l]
            vdc_uuid = server['vdcUUID']

        ram = 0
        cpu = 0
        # get the RAM in GB.
        ram = node_instance.get_ram()
        # get the Number of CPUs.
        cpu = node_instance.get_cpu()
        print '\n\n\n'
        print 'CPU = ', cpu
        print 'RAM = ', ram
        # Validate the size of the CPU and the RAM input
        validate_ram_cpu_size(auth, product_offer, cpu, ram)

        print 'Resizing in progress'
        print 'Stopping the VM: %s to resize CPU/RAM' %vm
        # Stop the VM before attaching the disk
        try:
            ret = StopVM(
                vm_uuid,
                self.user_info.get_cloud('user.uuid'),
                self.user_info.get_cloud_username(),
                self.user_info.get_cloud_password(),
                self.user_info.get_cloud_endpoint(),
                self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' % (vm_uuid, str(ex)))

        clusterUUID = server['clusterUUID']
        vdcUUID = server['vdcUUID']
        disks = server['disks']
        serverName = server['resourceName']
        # Resize the server
        resize_cpu_ram(auth, vm_uuid, serverName, clusterUUID, vdcUUID, cpu, ram)

        # Restart the VM and wait till it gets in RUNNING state
        print 'Restart the VM'
        server_data = [vm_uuid]
        start_server(auth, server_data)

        print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException("Server is not in RUNNING state")
    def _attach_disk(self, node_instance):

        """Attach extra disk to the VM.
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        :return: name of the device that was attached
        :rtype: string
        """
        print 'Scaling in progress'
        current_time = time.strftime("%Y-%m-%d %H:%M:%S")
        disk_device_name =  "Disk " + current_time + " #2"
        print 'Node instance name: %s' %node_instance.get_name()
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']
        
        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(), self.user_info.get_cloud_username(),
                        self.user_info.get_cloud('user.uuid'), self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)
       
        # Size of the disk to attach (in GB).
        disk_size_GB = int(node_instance.get_disk_attach_size())
        print '\n\n\n'
        print 'disk_size = ',disk_size_GB
        
        server_resultset = list_resource_by_uuid(auth, vm_uuid, res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
                vdc = server_resultset['list'][l]
                vdc_uuid = vdc['vdcUUID']

        # Validate the size of the disk to be attached        
        validate_disk_size(auth, 'Standard Disk', disk_size_GB, disk_device_name, vdc_uuid)
        
        print 'Stopping the VM: %s to attach disk' %vm
        # Stop the VM before attaching the disk
        try:
            ret = StopVM(
                vm_uuid,
                self.user_info.get_cloud('user.uuid'),
                self.user_info.get_cloud_username(),
                self.user_info.get_cloud_password(),
                self.user_info.get_cloud_endpoint(),
                self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' % (vm_uuid, str(ex)))
    
        disk_uuid = ""
        if (int(disk_size_GB) > 0):
            print('Creating additional volatile disk')
            # Create the disk
            disk_uuid = create_disk(auth, 'Standard Disk', disk_size_GB, disk_device_name, vdc_uuid)

        print 'Attaching the additional volatile disk'
        # If we created an extra disk, attach it now
        if (disk_uuid != ""):
            attach_disk(auth, vm_uuid, disk_uuid=disk_uuid, index='2')

	print 'Restart the VM'
        server_data=[vm_uuid]
        start_server(auth, server_data)

	print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException("Server is not in RUNNING state")
        # Return the created disk's uuid
	return disk_uuid
Exemplo n.º 6
0
    def _resize(self, node_instance):
        """
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        """
        print 'Node instance name: %s' % node_instance.get_name()
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']
        # Get the Product Offer UUID of the Standard Server product
        product_offer = 'Standard Server'

        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)

        server_resultset = list_resource_by_uuid(auth,
                                                 vm_uuid,
                                                 res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
            server = server_resultset['list'][l]
            vdc_uuid = server['vdcUUID']

        ram = 0
        cpu = 0
        # get the RAM in GB.
        ram = node_instance.get_ram()
        # get the Number of CPUs.
        cpu = node_instance.get_cpu()
        print '\n\n\n'
        print 'CPU = ', cpu
        print 'RAM = ', ram
        # Validate the size of the CPU and the RAM input
        validate_ram_cpu_size(auth, product_offer, cpu, ram)

        print 'Resizing in progress'
        print 'Stopping the VM: %s to resize CPU/RAM' % vm
        # Stop the VM before attaching the disk
        try:
            ret = StopVM(vm_uuid, self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud_password(),
                         self.user_info.get_cloud_endpoint(), self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' %
                             (vm_uuid, str(ex)))

        clusterUUID = server['clusterUUID']
        vdcUUID = server['vdcUUID']
        disks = server['disks']
        serverName = server['resourceName']
        # Resize the server
        resize_cpu_ram(auth, vm_uuid, serverName, clusterUUID, vdcUUID, cpu,
                       ram)

        # Restart the VM and wait till it gets in RUNNING state
        print 'Restart the VM'
        server_data = [vm_uuid]
        start_server(auth, server_data)

        print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException(
                "Server is not in RUNNING state")
Exemplo n.º 7
0
    def _detach_disk(self, node_instance):
        """Detach disk from the VM.
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        """
        print 'Scaling down in progress for NODE INSTANCE name: %s' % node_instance.get_name(
        )
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']

        print 'Stopping the VM: %s to detach disk' % vm
        # Stop the VM before detaching the disk
        try:
            ret = StopVM(vm_uuid, self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud_password(),
                         self.user_info.get_cloud_endpoint(), self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' %
                             (vm_uuid, str(ex)))

        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)
        server_resultset = list_resource_by_uuid(auth,
                                                 vm_uuid,
                                                 res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
            server = server_resultset['list'][l]
            disks = server['disks']

        # Get the UUID of the disk to be detached
        detach_diskUUID = node_instance.get_disk_detach_device()
        print '\n\n\n'
        print 'detach_diskUUID passed = %s' % detach_diskUUID

        found = False
        for disk in disks:
            if (disk['resourceUUID'] == detach_diskUUID):
                print 'Disk found on the server'
                found = True
        if (found == False):
            raise Exceptions.ExecutionException(
                "Disk not found on the server %s" % vm_uuid)

        print 'Detaching the additional volatile disk'
        # Detach it now
        if (detach_diskUUID != ""):
            detach_disk(auth, vm_uuid, detach_diskUUID)
            # Delete the resource
            rest_delete_resource(auth, detach_diskUUID, "DISK")

        print 'Restart the VM'
        server_data = [vm_uuid]
        start_server(auth, server_data)

        print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException(
                "Server is not in RUNNING state")
Exemplo n.º 8
0
    def _attach_disk(self, node_instance):
        """Attach extra disk to the VM.
        :param node_instance: node instance object
        :type node_instance: <NodeInstance>
        :return: name of the device that was attached
        :rtype: string
        """
        print 'Scaling in progress'
        current_time = time.strftime("%Y-%m-%d %H:%M:%S")
        disk_device_name = "Disk " + current_time + " #2"
        print 'Node instance name: %s' % node_instance.get_name()
        machine_name = node_instance.get_name()
        vm = self._get_vm(machine_name)
        vm_uuid = vm['id']

        endpoint = self.user_info.get_cloud_endpoint()
        token = getToken(self.user_info.get_cloud_endpoint(),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_password())
        auth = dict(endpoint=endpoint, token=token)

        # Size of the disk to attach (in GB).
        disk_size_GB = int(node_instance.get_disk_attach_size())
        print '\n\n\n'
        print 'disk_size = ', disk_size_GB

        server_resultset = list_resource_by_uuid(auth,
                                                 vm_uuid,
                                                 res_type='SERVER')
        for l in range(0, server_resultset['totalCount']):
            vdc = server_resultset['list'][l]
            vdc_uuid = vdc['vdcUUID']

        # Validate the size of the disk to be attached
        validate_disk_size(auth, 'Standard Disk', disk_size_GB,
                           disk_device_name, vdc_uuid)

        print 'Stopping the VM: %s to attach disk' % vm
        # Stop the VM before attaching the disk
        try:
            ret = StopVM(vm_uuid, self.user_info.get_cloud('user.uuid'),
                         self.user_info.get_cloud_username(),
                         self.user_info.get_cloud_password(),
                         self.user_info.get_cloud_endpoint(), self.verbose)
        except Exception as ex:
            raise CloudError('Failed to stop VM %s with: %s' %
                             (vm_uuid, str(ex)))

        disk_uuid = ""
        if (int(disk_size_GB) > 0):
            print('Creating additional volatile disk')
            # Create the disk
            disk_uuid = create_disk(auth, 'Standard Disk', disk_size_GB,
                                    disk_device_name, vdc_uuid)

        print 'Attaching the additional volatile disk'
        # If we created an extra disk, attach it now
        if (disk_uuid != ""):
            attach_disk(auth, vm_uuid, disk_uuid=disk_uuid, index='2')

        print 'Restart the VM'
        server_data = [vm_uuid]
        start_server(auth, server_data)

        print("Waiting for the server to get in RUNNING state")
        ret = wait_for_server(auth, vm_uuid, 'RUNNING')
        if (ret != 0):
            raise Exceptions.ExecutionException(
                "Server is not in RUNNING state")
        # Return the created disk's uuid
        return disk_uuid