def _to_node(self, vm): """ Turns a (json) dictionary into a Node object. This returns only running VMs. """ #Check state if not vm['state'] == "running": return None #IPs iplist = [interface['ip'] for interface in vm['interfaces'] if interface['ip'] != '127.0.0.1'] public_ips = [] private_ips = [] for ip in iplist: try: socket.inet_aton(ip) except socket.error: # not a valid ip continue if is_private_subnet(ip): private_ips.append(ip) else: public_ips.append(ip) #Create the node object n = Node( id=vm['uuid'], name=vm['name'], state=NodeState.RUNNING, public_ips=public_ips, private_ips=private_ips, driver=self, ) return n
def _to_node(self, el): def get_ips(el): return [ip.get('addr') for ip in el] def get_meta_dict(el): d = {} for meta in el: d[meta.get('key')] = meta.text return d public_ip = get_ips(self._findall(el, 'addresses/public/ip')) private_ip = get_ips(self._findall(el, 'addresses/private/ip')) metadata = get_meta_dict(self._findall(el, 'metadata/meta')) n = Node(id=el.get('id'), name=el.get('name'), state=self.NODE_STATE_MAP.get(el.get('status'), NodeState.UNKNOWN), public_ip=public_ip, private_ip=private_ip, driver=self.connection.driver, extra={ 'password': el.get('adminPass'), 'hostId': el.get('hostId'), 'imageId': el.get('imageId'), 'flavorId': el.get('flavorId'), 'uri': "https://%s%s/servers/%s" % (self.connection.host, self.connection.request_path, el.get('id')), 'metadata': metadata, }) return n
def test_rebuild_node_with_progress(self): conn = mock.Mock() conn.ex_rebuild.return_value = True api = self._get_api_with_mocked_conn(conn) progress = StringIO() rebuild_node = Node('1', 'server1', None, ['50.50.50.50'], [], None) conn.list_nodes.return_value = [rebuild_node] with mock.patch('littlechef_rackspace.api.time'): api.rebuild_node(name='server1', image="image-id", public_key_file=StringIO("some key"), progress=progress) self.assertEquals([ "Rebuilding node {0} ({1})...".format(rebuild_node.name, rebuild_node.id), 'Waiting for node to begin rebuilding{0}'.format("."), "Waiting for node to become active{0}".format( "." * 5), "Node active! (host: 50.2.3.4)" ], progress.getvalue().splitlines())
def _to_node(self, data): state = NODE_STATE_MAP[data['status']] public_ips = [] private_ips = [] extra = {} if 'plan_id' in data: extra['size'] = data['plan_id'] if 'os_id' in data: extra['image'] = data['os_id'] if 'location_id' in data: extra['location'] = data['location_id'] if 'ip' in data: public_ips.append(data['ip']) node = Node(id=data['mbpkgid'], name=data['fqdn'], state=state, public_ips=public_ips, private_ips=private_ips, driver=self.connection.driver, extra=extra) return node
def create_node(self, **kwargs): num = next(self._numiter) sizename = kwargs.pop('size', 'defsize') name = kwargs.pop('name', 'dummy-%d' % (num)) n = Node(id=num, name=name, state=NodeState.RUNNING, public_ips=['127.0.0.%d' % (num)], private_ips=[], driver=self, size=NodeSize(id='s1', name=sizename, ram=2048, disk=160, bandwidth=None, price=0.0, driver=self), image=NodeImage(id='i2', name='image', driver=self), extra={'foo': 'bar'}) self.nl.append(n) return n
def _to_node(self, data): state = NODE_STATE_MAP[data['state']] public_ips = [] private_ips = [] extra = {} for ip in data['ips']: if is_private_subnet(ip): private_ips.append(ip) else: public_ips.append(ip) if 'credentials' in data['metadata']: extra['password'] = data['metadata']['credentials']['root'] node = Node(id=data['id'], name=data['name'], state=state, public_ips=public_ips, private_ips=private_ips, driver=self.connection.driver, extra=extra) return node
def create_node(self, **kwargs): """Create a new Dreamhost node See L{NodeDriver.create_node} for more keyword args. @keyword ex_movedata: Copy all your existing users to this new PS @type ex_movedata: C{str} """ size = kwargs['size'].ram params = { 'cmd': 'dreamhost_ps-add_ps', 'movedata': kwargs.get('movedata', 'no'), 'type': kwargs['image'].name, 'size': size } data = self.connection.request('/', params).object return Node(id=data['added_web'], name=data['added_web'], state=NodeState.PENDING, public_ip=[], private_ip=[], driver=self.connection.driver, extra={'type': kwargs['image'].name})
def _to_node(self, data): extra_keys = ['cores', 'power', 'memory', 'current_price', 'relations'] extra = self._extract_values_to_dict(data=data, keys=extra_keys) ips = [] for diction in data['relations']['public_ips']: ips.append(diction['ip']) state = '' if data['power'] is True: state = NodeState.RUNNING else: state = NodeState.STOPPED node = Node(id=data['object_uuid'], name=data['name'], state=state, public_ips=ips, created_at=parse_date(data['create_time']), private_ips=None, driver=self.connection.driver, extra=extra) return node
def _to_node(self, element): if findtext(element, 'isStarted', SERVER_NS) == 'true': state = NodeState.RUNNING else: state = NodeState.TERMINATED status = self._to_status(element.find(fixxpath('status', SERVER_NS))) extra = { 'description': findtext(element, 'description', SERVER_NS), 'sourceImageId': findtext(element, 'sourceImageId', SERVER_NS), 'networkId': findtext(element, 'networkId', SERVER_NS), 'machineName': findtext(element, 'machineName', SERVER_NS), 'deployedTime': findtext(element, 'deployedTime', SERVER_NS), 'cpuCount': findtext(element, 'machineSpecification/cpuCount', SERVER_NS), 'memoryMb': findtext(element, 'machineSpecification/memoryMb', SERVER_NS), 'osStorageGb': findtext(element, 'machineSpecification/osStorageGb', SERVER_NS), 'additionalLocalStorageGb': findtext(element, 'machineSpecification/additionalLocalStorageGb', SERVER_NS), 'OS_type': findtext(element, 'machineSpecification/operatingSystem/type', SERVER_NS), 'OS_displayName': findtext(element, 'machineSpecification/operatingSystem/displayName', SERVER_NS), 'status': status, } n = Node(id=findtext(element, 'id', SERVER_NS), name=findtext(element, 'name', SERVER_NS), state=state, public_ips=[], private_ips=findtext(element, 'privateIpAddress', SERVER_NS), driver=self.connection.driver, extra=extra) return n
def create_node(self, **kwargs): """ Creates a dummy node; the node id is equal to the number of nodes in the node list >>> from libcloud.compute.drivers.dummy import DummyNodeDriver >>> driver = DummyNodeDriver(0) >>> sorted([node.name for node in driver.list_nodes()]) ['dummy-1', 'dummy-2'] >>> nodeA = driver.create_node() >>> sorted([node.name for node in driver.list_nodes()]) ['dummy-1', 'dummy-2', 'dummy-3'] >>> driver.create_node().name 'dummy-4' >>> driver.destroy_node(nodeA) True >>> sorted([node.name for node in driver.list_nodes()]) ['dummy-1', 'dummy-2', 'dummy-4'] """ l = len(self.nl) + 1 n = Node(id=l, name='dummy-%d' % l, state=NodeState.RUNNING, public_ips=['127.0.0.%d' % l], private_ips=[], driver=self, size=NodeSize(id='s1', name='foo', ram=2048, disk=160, bandwidth=None, price=0.0, driver=self), image=NodeImage(id='i2', name='image', driver=self), extra={'foo': 'bar'}) self.nl.append(n) return n
def _to_node(self, api_node): return Node( id=api_node['id'], name=api_node['name'], state=self.NODE_STATE_MAP.get(api_node['status'], NodeState.UNKNOWN), public_ips=[addr_desc['addr'] for addr_desc in api_node['addresses'].get('public', [])], private_ips=[addr_desc['addr'] for addr_desc in api_node['addresses'].get('private', [])], driver=self, extra=dict( hostId=api_node['hostId'], # Docs says "tenantId", but actual is "tenant_id". *sigh* # Best handle both. tenantId=api_node.get('tenant_id') or api_node['tenantId'], imageId=api_node['image']['id'], flavorId=api_node['flavor']['id'], uri=next(link['href'] for link in api_node['links'] if link['rel'] == 'self'), metadata=api_node['metadata'], password=api_node.get('adminPass'), ), )
def _to_node(self, data): """Convert node in Node instances""" state = NODE_STATE_MAP.get(data.get("power_status"), "4") public_ips = [] private_ips = [] ip_addresses = data.get("ipaddresses", "") # E.g. "ipaddresses": "198.120.14.6, 10.132.60.1" if ip_addresses: for ip in ip_addresses.split(","): ip = ip.replace(" ", "") if is_private_subnet(ip): private_ips.append(ip) else: public_ips.append(ip) extra = { "zone_data": data.get("zone"), "zone": data.get("zone", {}).get("name"), "image": data.get("image", {}).get("friendly_name"), "create_time": data.get("create_time"), "network_ports": data.get("network_ports"), "is_console_enabled": data.get("is_console_enabled"), "service_type": data.get("service_type", {}).get("friendly_name"), "hostname": data.get("hostname"), } node = Node( id=data.get("id"), name=data.get("name"), state=state, public_ips=public_ips, private_ips=private_ips, driver=self, extra=extra, ) return node
def _to_node(self, data): extra_keys = [ 'memory', 'vcpus', 'disk', 'region', 'image', 'size_slug', 'locked', 'created_at', 'networks', 'kernel', 'backup_ids', 'snapshot_ids', 'features' ] if 'status' in data: state = self.NODE_STATE_MAP.get(data['status'], NodeState.UNKNOWN) else: state = NodeState.UNKNOWN created = parse_date(data['created_at']) networks = data['networks'] private_ips = [] public_ips = [] if networks: for net in networks['v4']: if net['type'] == 'private': private_ips = [net['ip_address']] if net['type'] == 'public': public_ips = [net['ip_address']] extra = {} for key in extra_keys: if key in data: extra[key] = data[key] node = Node(id=data['id'], name=data['name'], state=state, public_ips=public_ips, private_ips=private_ips, created_at=created, driver=self, extra=extra) return node
def _to_node(self, data, ssh_password=None): try: state = NODE_STATE_MAP[data['status']] except KeyError: state = NodeState.UNKNOWN if isinstance(data['nic:0:dhcp'], list): public_ip = data['nic:0:dhcp'] else: public_ip = [data['nic:0:dhcp']] extra = { 'cpu': data['cpu'], 'smp': data['smp'], 'mem': data['mem'], 'started': data['started'] } if 'vnc:ip' in data: extra['vnc:ip'] = data['vnc:ip'] if 'vnc:password' in data: extra['vnc:password'] = data['vnc:password'] if ssh_password: extra.update({'password': ssh_password}) node = Node(id=data['server'], name=data['name'], state=state, public_ips=public_ip, private_ips=None, driver=self.connection.driver, extra=extra) return node
def create_node(self, name, size, image, auth=None, **kwargs): """ Create a new VM from a template VM and start it. """ auth = self._get_and_check_auth(auth) source = self._get_source(image) target = self._get_target() self._clone(source, target) target.name = name node = Node(target.vmx, name, NodeState.PENDING, None, None, self) # If a NodeSize is provided then we can control the amount of RAM the # VM has. Number of CPU's would be easy to scale too, but this isn't # exposed on a NodeSize # if size: # if size.ram: # self.ex_set_runtime_variable(node, "displayName", name, str(size.ram)) # self._action("writeVariable", target, "runtimeConfig", "memsize", str(size.ram)) self.ex_start(node) self.ex_set_runtime_variable(node, "displayName", name) self.apply_auth(target, auth) return node
def list_nodes(self): domain_ids = self.connection.listDomainsID() domains = [self.connection.lookupByID(id) for id in domain_ids] nodes = [] for domain in domains: state, max_mem, memory, vcpu_count, used_cpu_time = domain.info() if state in self.NODE_STATE_MAP: state = self.NODE_STATE_MAP[state] else: state = NodeState.UNKNOWN # TODO: Use XML config to get Mac address and then parse ips extra = {'uuid': domain.UUIDString(), 'os_type': domain.OSType(), 'types': self.connection.getType(), 'used_memory': memory / 1024, 'vcpu_count': vcpu_count, 'used_cpu_time': used_cpu_time} node = Node(id=domain.ID(), name=domain.name(), state=state, public_ips=[], private_ips=[], driver=self, extra=extra) nodes.append(node) return nodes
def create_node(self, **kwargs): """Create a new VCL reservation size and name ignored, image is the id from list_image @inherits: :class:`NodeDriver.create_node` :keyword image: image is the id from list_image :type image: ``str`` :keyword start: start time as unix timestamp :type start: ``str`` :keyword length: length of time in minutes :type length: ``str`` """ image = kwargs["image"] start = kwargs.get('start', int(time.time())) length = kwargs.get('length', '60') res = self._vcl_request( "XMLRPCaddRequest", image.id, start, length ) return Node( id=res['requestid'], name=image.name, state=self.NODE_STATE_MAP[res['status']], public_ips=[], private_ips=[], driver=self, image=image.name )
def _to_nodes(self, object): nodes = [] for element in object.findall('devices/device'): if element.findtext("type") == "Virtual Server": try: state = self.NODE_STATE_MAP[element.attrib['status']] except KeyError: state = NodeState.UNKNOWN public_ip = private_ip = None ipassignments = element.findall("ipassignments/ipassignment") for ip in ipassignments: if ip.attrib["type"] =="frontend": public_ip = ip.text elif ip.attrib["type"] == "backend": private_ip = ip.text nodes.append(Node(id= element.attrib['id'], name=element.attrib['label'], state=state, public_ips= public_ip, private_ips= private_ip, driver=self.connection.driver)) return nodes
def test_destroy_node(self): node = Node(90967, None, None, None, None, self.driver) ret = self.driver.destroy_node(node) self.assertTrue(ret)
def test_base_node(self): Node(id=0, name=0, state=0, public_ips=0, private_ips=0, driver=FakeDriver())
def test_reboot_node(self): node = Node(90967, None, None, None, None, self.driver) ret = self.driver.reboot_node(node) self.assertTrue(ret)
def test_ex_delete_tags(self): node = Node('i-4382922a', None, None, None, None, self.driver) self.driver.ex_delete_tags(node, {'sample': 'tag'})
def test_ex_describe_addresses_for_node(self): # overridden from EC2Tests -- Nimbus doesn't support elastic IPs. node = Node('i-4382922a', None, None, None, None, self.driver) ip_addresses = self.driver.ex_describe_addresses_for_node(node) self.assertEqual(len(ip_addresses), 0)
def test_reboot_node(self): node = Node('i-4382922a', None, None, None, None, self.driver) ret = self.driver.reboot_node(node) self.assertTrue(ret)
def test_destroy_node(self): node = Node('i-4382922a', None, None, None, None, self.driver) ret = self.driver.destroy_node(node) self.assertTrue(ret)
from libcloud.dns.types import RecordDoesNotExistError from libcloud.dns.drivers.rackspace import RackspacePTRRecord from libcloud.dns.drivers.rackspace import RackspaceDNSDriver from libcloud.loadbalancer.base import LoadBalancer from libcloud.test import MockHttp from libcloud.test.file_fixtures import DNSFileFixtures from libcloud.test.secrets import DNS_PARAMS_RACKSPACE # only the 'extra' will be looked at, so pass in minimal data RDNS_NODE = Node('370b0ff8-3f57-4e10-ac84-e9145ce005841', 'server1', None, [], [], None, extra={ 'uri': 'https://ord.servers.api.rackspacecloud' '.com/v2/905546514/servers/370b0ff8-3f57' '-4e10-ac84-e9145ce00584', 'service_name': 'cloudServersOpenStack' }) RDNS_LB = LoadBalancer('370b0ff8-3f57-4e10-ac84-e9145ce005841', 'server1', None, None, None, None, extra={ 'uri': 'https://ord.loadbalancers.api.' 'rackspacecloud.com/v2/905546514/'
def create_node(self, name, size, image, server_key=None, console_key=None, zone=None, **kwargs): """Creates the node, and sets the ssh key, console key NephoScale will respond with a 200-200 response after sending a valid request. If nowait=True is specified in the args, we then ask a few times until the server is created and assigned a public IP address, so that deploy_node can be run >>> from libcloud.compute.providers import get_driver >>> driver = get_driver('nephoscale') >>> conn = driver('nepho_user','nepho_password') >>> conn.list_nodes() >>> name = 'staging-server' >>> size = conn.list_sizes()[0] <NodeSize: id=27, ...name=CS025 - 0.25GB, 10GB, ...> >>> image = conn.list_images()[9] <NodeImage: id=49, name=Linux Ubuntu Server 10.04 LTS 64-bit, ...> >>> server_keys = conn.ex_list_keypairs(key_group=1)[0] <NodeKey: id=71211, name=markos> >>> server_key = conn.ex_list_keypairs(key_group=1)[0].id 70867 >>> console_keys = conn.ex_list_keypairs(key_group=4)[0] <NodeKey: id=71213, name=mistio28434> >>> console_key = conn.ex_list_keypairs(key_group=4)[0].id 70907 >>> node = conn.create_node(name=name, size=size, image=image, \ console_key=console_key, server_key=server_key) We can also create an ssh key, plus a console key and deploy node with them >>> server_key = conn.ex_create_keypair(name, public_key='123') 71211 >>> console_key = conn.ex_create_keypair(name, key_group=4) 71213 We can increase the number of connect attempts to wait until the node is created, so that deploy_node has ip address to deploy the script We can also specify the location >>> location = conn.list_locations()[0] >>> node = conn.create_node(name=name, >>> ... size=size, >>> ... image=image, >>> ... console_key=console_key, >>> ... server_key=server_key, >>> ... connect_attempts=10, >>> ... nowait=True, >>> ... zone=location.id) """ hostname = kwargs.get('hostname', name) service_type = size.id image = image.id connect_attempts = int(kwargs.get('connect_attempts', CONNECT_ATTEMPTS)) data = { 'name': name, 'hostname': hostname, 'service_type': service_type, 'image': image, 'server_key': server_key, 'console_key': console_key, 'zone': zone } params = urlencode(data) try: node = self.connection.request('/server/cloud/', data=params, method='POST') except Exception as e: raise Exception("Failed to create node %s" % e) node = Node(id='', name=name, state=NodeState.UNKNOWN, public_ips=[], private_ips=[], driver=self) nowait = kwargs.get('ex_wait', False) if not nowait: return node else: # try to get the created node public ips, for use in deploy_node # At this point we don't have the id of the newly created Node, # so search name in nodes created_node = False while connect_attempts > 0: nodes = self.list_nodes() created_node = [ c_node for c_node in nodes if c_node.name == name ] if created_node: return created_node[0] else: time.sleep(60) connect_attempts = connect_attempts - 1 return node
def test_ex_revert_to_snapshot(self): node = Node( 'https://vm-vcloud/api/vApp/vapp-8c57a5b6-e61b-48ca-8a78-3b70ee65ef6b', 'testNode', NodeState.RUNNING, [], [], self.driver) self.driver.ex_revert_to_snapshot(node)
def test_ex_set_metadata_entry(self): node = Node( 'https://vm-vcloud/api/vApp/vapp-8c57a5b6-e61b-48ca-8a78-3b70ee65ef6b', 'testNode', NodeState.RUNNING, [], [], self.driver) self.driver.ex_set_metadata_entry(node, 'foo', 'bar')
def test_ex_get_metadata(self): node = Node( 'https://vm-vcloud/api/vApp/vapp-8c57a5b6-e61b-48ca-8a78-3b70ee65ef6b', 'testNode', NodeState.RUNNING, [], [], self.driver) metadata = self.driver.ex_get_metadata(node) self.assertEqual(metadata, {'owners': '*****@*****.**'})