def test_dict2str(self): d = {'smp': 5, 'cpu': 2200, 'mem': 1024} result = dict2str(d) self.assertTrue(len(result) > 0) self.assertTrue(result.find('smp 5') >= 0) self.assertTrue(result.find('cpu 2200') >= 0) self.assertTrue(result.find('mem 1024') >= 0)
def test_dict2str(self): d = {"smp": 5, "cpu": 2200, "mem": 1024} result = dict2str(d) self.assertTrue(len(result) > 0) self.assertTrue(result.find("smp 5") >= 0) self.assertTrue(result.find("cpu 2200") >= 0) self.assertTrue(result.find("mem 1024") >= 0)
def ex_set_node_configuration(self, node, **kwargs): """ Update a node configuration. Changing most of the parameters requires node to be stopped. """ valid_keys = ('^name$', '^parent$', '^cpu$', '^smp$', '^mem$', '^boot$', '^nic:0:model$', '^nic:0:dhcp', '^nic:1:model$', '^nic:1:vlan$', '^nic:1:mac$', '^vnc:ip$', '^vnc:password$', '^vnc:tls', '^ide:[0-1]:[0-1](:media)?$', '^scsi:0:[0-7](:media)?$', '^block:[0-7](:media)?$') invalid_keys = [] keys = list(kwargs.keys()) for key in keys: matches = False for regex in valid_keys: if re.match(regex, key): matches = True break if not matches: invalid_keys.append(key) if invalid_keys: raise CloudSigmaException( 'Invalid configuration key specified: %s' % (','.join(invalid_keys))) response = self.connection.request(action='/servers/%s/set' % (node.id), data=dict2str(kwargs), method='POST') return (response.status == 200 and response.body != '')
def ex_set_node_configuration(self, node, **kwargs): """ Update a node configuration. Changing most of the parameters requires node to be stopped. @param node: Node which should be used @type node: L{Node} @param kwargs: keyword arguments @type kwargs: C{dict} @rtype: C{bool} """ valid_keys = ('^name$', '^parent$', '^cpu$', '^smp$', '^mem$', '^boot$', '^nic:0:model$', '^nic:0:dhcp', '^nic:1:model$', '^nic:1:vlan$', '^nic:1:mac$', '^vnc:ip$', '^vnc:password$', '^vnc:tls', '^ide:[0-1]:[0-1](:media)?$', '^scsi:0:[0-7](:media)?$', '^block:[0-7](:media)?$') invalid_keys = [] keys = list(kwargs.keys()) for key in keys: matches = False for regex in valid_keys: if re.match(regex, key): matches = True break if not matches: invalid_keys.append(key) if invalid_keys: raise CloudSigmaException( 'Invalid configuration key specified: %s' % ( ',' .join(invalid_keys))) response = self.connection.request( action='/servers/%s/set' % (node.id), data=dict2str(kwargs), method='POST') return (response.status == 200 and response.body != '')
def create_node(self, **kwargs): """ Creates a CloudSigma instance @inherits: :class:`NodeDriver.create_node` :keyword name: String with a name for this new node (required) :type name: ``str`` :keyword smp: Number of virtual processors or None to calculate based on the cpu speed :type smp: ``int`` :keyword nic_model: e1000, rtl8139 or virtio (is not specified, e1000 is used) :type nic_model: ``str`` :keyword vnc_password: If not set, VNC access is disabled. :type vnc_password: ``bool`` :keyword drive_type: Drive type (ssd|hdd). Defaults to hdd. :type drive_type: ``str`` """ size = kwargs['size'] image = kwargs['image'] smp = kwargs.get('smp', 'auto') nic_model = kwargs.get('nic_model', 'e1000') vnc_password = kwargs.get('vnc_password', None) drive_type = kwargs.get('drive_type', 'hdd') if nic_model not in ['e1000', 'rtl8139', 'virtio']: raise CloudSigmaException('Invalid NIC model specified') if drive_type not in ['hdd', 'ssd']: raise CloudSigmaException('Invalid drive type "%s". Valid types' ' are: hdd, ssd' % (drive_type)) drive_data = {} drive_data.update({ 'name': kwargs['name'], 'size': '%sG' % (kwargs['size'].disk), 'driveType': drive_type }) response = self.connection.request(action='/drives/%s/clone' % image.id, data=dict2str(drive_data), method='POST').object if not response: raise CloudSigmaException('Drive creation failed') drive_uuid = response[0]['drive'] response = self.connection.request(action='/drives/%s/info' % (drive_uuid)).object imaging_start = time.time() while 'imaging' in response[0]: response = self.connection.request(action='/drives/%s/info' % (drive_uuid)).object elapsed_time = time.time() - imaging_start if 'imaging' in response[0] and elapsed_time >= IMAGING_TIMEOUT: raise CloudSigmaException('Drive imaging timed out') time.sleep(1) node_data = {} node_data.update({ 'name': kwargs['name'], 'cpu': size.cpu, 'mem': size.ram, 'ide:0:0': drive_uuid, 'boot': 'ide:0:0', 'smp': smp }) node_data.update({'nic:0:model': nic_model, 'nic:0:dhcp': 'auto'}) if vnc_password: node_data.update({'vnc:ip': 'auto', 'vnc:password': vnc_password}) response = self.connection.request(action='/servers/create', data=dict2str(node_data), method='POST').object if not isinstance(response, list): response = [response] node = self._to_node(response[0]) if node is None: # Insufficient funds, destroy created drive self.ex_drive_destroy(drive_uuid) raise CloudSigmaInsufficientFundsException( 'Insufficient funds, node creation failed') # Start the node after it has been created started = self.ex_start_node(node) if started: node.state = NodeState.RUNNING return node
def create_node(self, **kwargs): """ Creates a CloudSigma instance See L{NodeDriver.create_node} for more keyword args. @keyword name: String with a name for this new node (required) @type name: C{string} @keyword smp: Number of virtual processors or None to calculate based on the cpu speed @type smp: C{int} @keyword nic_model: e1000, rtl8139 or virtio (is not specified, e1000 is used) @type nic_model: C{string} @keyword vnc_password: If not set, VNC access is disabled. @type vnc_password: C{bool} @keyword drive_type: Drive type (ssd|hdd). Defaults to hdd. @type drive_type: C{str} """ size = kwargs['size'] image = kwargs['image'] smp = kwargs.get('smp', 'auto') nic_model = kwargs.get('nic_model', 'e1000') vnc_password = kwargs.get('vnc_password', None) drive_type = kwargs.get('drive_type', 'hdd') if nic_model not in ['e1000', 'rtl8139', 'virtio']: raise CloudSigmaException('Invalid NIC model specified') if drive_type not in ['hdd', 'ssd']: raise CloudSigmaException('Invalid drive type "%s". Valid types' ' are: hdd, ssd' % (drive_type)) drive_data = {} drive_data.update({'name': kwargs['name'], 'size': '%sG' % (kwargs['size'].disk), 'driveType': drive_type}) response = self.connection.request(action = '/drives/%s/clone' % image.id, data = dict2str(drive_data), method = 'POST').object if not response: raise CloudSigmaException('Drive creation failed') drive_uuid = response[0]['drive'] response = self.connection.request(action = '/drives/%s/info' % (drive_uuid)).object imaging_start = time.time() while 'imaging' in response[0]: response = self.connection.request(action = '/drives/%s/info' % (drive_uuid)).object elapsed_time = time.time() - imaging_start if 'imaging' in response[0] and elapsed_time >= IMAGING_TIMEOUT: raise CloudSigmaException('Drive imaging timed out') time.sleep(1) node_data = {} node_data.update({'name': kwargs['name'], 'cpu': size.cpu, 'mem': size.ram, 'ide:0:0': drive_uuid, 'boot': 'ide:0:0', 'smp': smp}) node_data.update({'nic:0:model': nic_model, 'nic:0:dhcp': 'auto'}) if vnc_password: node_data.update({'vnc:ip': 'auto', 'vnc:password': vnc_password}) response = self.connection.request(action = '/servers/create', data = dict2str(node_data), method = 'POST').object if not isinstance(response, list): response = [ response ] node = self._to_node(response[0]) if node is None: # Insufficient funds, destroy created drive self.ex_drive_destroy(drive_uuid) raise CloudSigmaInsufficientFundsException('Insufficient funds, node creation failed') # Start the node after it has been created started = self.ex_start_node(node) if started: node.state = NodeState.RUNNING return node