def _to_node(self, element): attrs = [ 'name', 'image-id', 'progress', 'id', 'bw-out', 'bw-in', 'flavor-id', 'status', 'ip-address', 'root-password' ] node_attrs = {} for attr in attrs: node_attrs[attr] = element.findtext(attr) # slicehost does not determine between public and private, so we # have to figure it out public_ip = [] private_ip = [] ip_address = element.findtext('ip-address') if is_private_subnet(ip_address): private_ip.append(ip_address) else: public_ip.append(ip_address) for addr in element.findall('addresses/address'): ip = addr.text try: socket.inet_aton(ip) except socket.error: # not a valid ip continue if is_private_subnet(ip): private_ip.append(ip) else: public_ip.append(ip) public_ip = list(set(public_ip)) try: state = self.NODE_STATE_MAP[element.findtext('status')] except: state = NodeState.UNKNOWN # for consistency with other drivers, we put this in two places. node_attrs['password'] = node_attrs['root-password'] extra = {} for k in list(node_attrs.keys()): ek = k.replace("-", "_") extra[ek] = node_attrs[k] n = Node(id=element.findtext('id'), name=element.findtext('name'), state=state, public_ips=public_ip, private_ips=private_ip, driver=self.connection.driver, extra=extra) return n
def _to_node(self, element): attrs = [ 'name', 'image-id', 'progress', 'id', 'bw-out', 'bw-in', 'flavor-id', 'status', 'ip-address', 'root-password' ] node_attrs = {} for attr in attrs: node_attrs[attr] = element.findtext(attr) # slicehost does not determine between public and private, so we # have to figure it out public_ip = [] private_ip = [] ip_address = element.findtext('ip-address') if is_private_subnet(ip_address): private_ip.append(ip_address) else: public_ip.append(ip_address) for addr in element.findall('addresses/address'): ip = addr.text try: socket.inet_aton(ip) except socket.error: # not a valid ip continue if is_private_subnet(ip): private_ip.append(ip) else: public_ip.append(ip) public_ip = list(set(public_ip)) try: state = self.NODE_STATE_MAP[element.findtext('status')] except: state = NodeState.UNKNOWN # for consistency with other drivers, we put this in two places. node_attrs['password'] = node_attrs['root-password'] extra = {} for k in node_attrs.keys(): ek = k.replace("-", "_") extra[ek] = node_attrs[k] n = Node(id=element.findtext('id'), name=element.findtext('name'), state=state, public_ips=public_ip, private_ips=private_ip, driver=self.connection.driver, extra=extra) return n
def _to_node(self, data): public_ips = [] private_ips = [] for ip in self._iter_interfaces(data['id']): if is_private_subnet(ip): private_ips.append(ip) else: public_ips.append(ip) extra = { 'management_address': data['management_address'], 'cores': data['cores'], 'memory': data['memory'], 'hostname': data['hostname'], } if data['power_on']: state = NodeState.RUNNING elif data['deleted']: state = NodeState.TERMINATED else: state = NodeState.UNKNOWN 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 node @inherits: :class:`NodeDriver.create_node` :keyword ex_keyname: Name of existing keypair :type ex_keyname: ``str`` :keyword ex_userdata: String containing user data :type ex_userdata: ``str`` :keyword networks: The server is launched into a set of Networks. :type networks: :class:`CloudStackNetwork` :keyword ex_security_groups: List of security groups to assign to the node :type ex_security_groups: ``list`` of ``str`` :rtype: :class:`CloudStackNode` """ server_params = self._create_args_to_params(None, **kwargs) node = self._async_request('deployVirtualMachine', **server_params)['virtualmachine'] public_ips = [] private_ips = [] for nic in node['nic']: if is_private_subnet(nic['ipaddress']): private_ips.append(nic['ipaddress']) else: public_ips.append(nic['ipaddress']) keypair, password, securitygroup = None, None, None if 'keypair' in node.keys(): keypair = node['keypair'] if 'password' in node.keys(): password = node['password'] if 'securitygroup' in node.keys(): securitygroup = [sg['name'] for sg in node['securitygroup']] return CloudStackNode( id=node['id'], name=node['displayname'], state=self.NODE_STATE_MAP[node['state']], public_ips=public_ips, private_ips=private_ips, driver=self, extra={'zoneid': server_params['zoneid'], 'ip_addresses': [], 'ip_forwarding_rules': [], 'port_forwarding_rules': [], 'password': password, 'keyname': keypair, 'securitygroup': securitygroup, 'created': node['created'] } )
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 _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 create_node(self, **kwargs): """ Create a new node @inherits: :class:`NodeDriver.create_node` :keyword ex_keyname: Name of existing keypair :type ex_keyname: ``str`` :keyword ex_userdata: String containing user data :type ex_userdata: ``str`` :keyword networks: The server is launched into a set of Networks. :type networks: :class:`CloudStackNetwork` :keyword ex_security_groups: List of security groups to assign to the node :type ex_security_groups: ``list`` of ``str`` :rtype: :class:`CloudStackNode` """ server_params = self._create_args_to_params(None, **kwargs) node = self._async_request('deployVirtualMachine', **server_params)['virtualmachine'] public_ips = [] private_ips = [] for nic in node['nic']: if is_private_subnet(nic['ipaddress']): private_ips.append(nic['ipaddress']) else: public_ips.append(nic['ipaddress']) keypair, password, securitygroup = None, None, None if 'keypair' in node.keys(): keypair = node['keypair'] if 'password' in node.keys(): password = node['password'] if 'securitygroup' in node.keys(): securitygroup = [sg['name'] for sg in node['securitygroup']] return CloudStackNode(id=node['id'], name=node['displayname'], state=self.NODE_STATE_MAP[node['state']], public_ips=public_ips, private_ips=private_ips, driver=self, extra={ 'zoneid': server_params['zoneid'], 'ip_addresses': [], 'ip_forwarding_rules': [], 'port_forwarding_rules': [], 'password': password, 'keyname': keypair, 'securitygroup': securitygroup, 'created': node['created'] })
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 _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, 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 list_nodes(self): """ @inherits: :class:`NodeDriver.list_nodes` :rtype: ``list`` of :class:`CloudStackNode` """ vms = self._sync_request('listVirtualMachines') addrs = self._sync_request('listPublicIpAddresses') public_ips_map = {} for addr in addrs.get('publicipaddress', []): if 'virtualmachineid' not in addr: continue vm_id = str(addr['virtualmachineid']) if vm_id not in public_ips_map: public_ips_map[vm_id] = {} public_ips_map[vm_id][addr['ipaddress']] = addr['id'] nodes = [] for vm in vms.get('virtualmachine', []): state = self.NODE_STATE_MAP[vm['state']] public_ips = [] private_ips = [] for nic in vm['nic']: if 'ipaddress' in nic: private_ips.append(nic['ipaddress']) public_ips = public_ips_map.get(vm['id'], {}).keys() public_ips = list(public_ips) public_ips.extend( [ip for ip in private_ips if not is_private_subnet(ip)]) keypair, password, securitygroup = None, None, None if 'keypair' in vm.keys(): keypair = vm['keypair'] if 'password' in vm.keys(): password = vm['password'] if 'securitygroup' in vm.keys(): securitygroup = [sg['name'] for sg in vm['securitygroup']] node = CloudStackNode(id=vm['id'], name=vm.get('displayname', None), state=state, public_ips=public_ips, private_ips=private_ips, driver=self, extra={ 'zoneid': vm['zoneid'], 'password': password, 'keyname': keypair, 'securitygroup': securitygroup, 'created': vm['created'] }) addresses = public_ips_map.get(vm['id'], {}).items() addresses = [CloudStackAddress(node, v, k) for k, v in addresses] node.extra['ip_addresses'] = addresses rules = [] for addr in addresses: result = self._sync_request('listIpForwardingRules') for r in result.get('ipforwardingrule', []): if str(r['virtualmachineid']) == node.id: rule = CloudStackIPForwardingRule( node, r['id'], addr, r['protocol'].upper(), r['startport'], r['endport']) rules.append(rule) node.extra['ip_forwarding_rules'] = rules rules = [] public_ips = self.ex_list_public_ips() result = self._sync_request('listPortForwardingRules') for r in result.get('portforwardingrule', []): if str(r['virtualmachineid']) == node.id: addr = [ a for a in public_ips if a.address == r['ipaddress'] ] rule = CloudStackPortForwardingRule( node, r['id'], addr[0], r['protocol'].upper(), r['publicport'], r['privateport'], r['publicendport'], r['privateendport']) if not addr[0].address in node.public_ips: node.public_ips.append(addr[0].address) rules.append(rule) node.extra['port_forwarding_rules'] = rules nodes.append(node) return nodes