def _db_create(cls, object_uuid, metadata): LOG.with_fields(metadata).with_field( cls.object_type, object_uuid).debug('Object created') metadata['uuid'] = object_uuid etcd.create(cls.object_type, None, object_uuid, metadata) db.add_event(cls.object_type, object_uuid, 'db record created', None, None, None)
def new(cls, interface_uuid, netdesc, instance_uuid, order): if 'macaddress' not in netdesc or not netdesc['macaddress']: possible_mac = util_network.random_macaddr() mac_iface = {'interface_uuid': interface_uuid} while not etcd.create('macaddress', None, possible_mac, mac_iface): possible_mac = util_network.random_macaddr() netdesc['macaddress'] = possible_mac if not interface_uuid: # uuid should only be specified in testing interface_uuid = str(uuid4()) NetworkInterface._db_create( interface_uuid, { 'network_uuid': netdesc['network_uuid'], 'instance_uuid': instance_uuid, 'macaddr': netdesc['macaddress'], 'ipv4': netdesc['address'], 'order': order, 'model': netdesc['model'], 'version': cls.current_version } ) ni = NetworkInterface.from_db(interface_uuid) ni._db_set_attribute('floating', {'floating_address': None}) ni.state = NetworkInterface.STATE_INITIAL # TODO(andy): Integrate metadata into each object type # Initialise metadata db.persist_metadata('networkinterface', interface_uuid, {}) return ni
def _allocate_console_port(self): node = config.NODE_NAME consumed = [ value['port'] for _, value in etcd.get_all('console', node) ] while True: port = random.randint(30000, 50000) # avoid hitting etcd if it's probably in use if port in consumed: continue s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # We hold this port open until it's in etcd to prevent # anyone else needing to hit etcd to find out they can't # use it as well as to verify we can use it s.bind(('0.0.0.0', port)) allocatedPort = etcd.create('console', node, port, { 'instance_uuid': self.uuid, 'port': port, }) if allocatedPort: return port except socket.error: LOG.with_field('instance', self.uuid).info( 'Collided with in use port %d, selecting another' % port) consumed.append(port) finally: s.close()
def allocate_console_port(instance_uuid): node = config.NODE_NAME consumed = {value['port'] for value in etcd.get_all('console', node)} while True: port = random.randint(30000, 50000) # avoid hitting etcd if it's probably in use if port in consumed: continue s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # We hold this port open until it's in etcd to prevent # anyone else needing to hit etcd to find out they can't # use it as well as to verify we can use it s.bind(('0.0.0.0', port)) allocatedPort = etcd.create('console', node, port, { 'instance_uuid': instance_uuid, 'port': port, }) if allocatedPort: return port except socket.error as e: LOG.withField('instance', instance_uuid).info( "Exception during port allocation: %s" % e) finally: s.close()
def allocate_network(netblock, provide_dhcp=True, provide_nat=False, name=None, namespace=None): net_id = str(uuid.uuid4()) ipm = ipmanager.NetBlock(netblock) etcd.put('ipmanager', None, net_id, ipm.save()) vxid = 1 while not etcd.create('vxlan', None, vxid, {'network_uuid': net_id}): vxid += 1 d = { 'uuid': net_id, 'vxid': vxid, 'netblock': netblock, 'provide_dhcp': provide_dhcp, 'provide_nat': provide_nat, 'namespace': namespace, 'floating_gateway': None, 'name': name, 'state': 'initial', 'state_updated': time.time() } etcd.put('network', None, net_id, d) return d
def create_network_interface(interface_uuid, netdesc, instance_uuid, order): if 'macaddress' not in netdesc or not netdesc['macaddress']: possible_mac = util.random_macaddr() mac_iface = {'interface_uuid': interface_uuid} while not etcd.create('macaddress', None, possible_mac, mac_iface): possible_mac = util.random_macaddr() netdesc['macaddress'] = possible_mac etcd.put( 'networkinterface', None, interface_uuid, { 'uuid': interface_uuid, 'network_uuid': netdesc['network_uuid'], 'instance_uuid': instance_uuid, 'macaddr': netdesc['macaddress'], 'ipv4': netdesc['address'], 'order': order, 'floating': None, 'state': 'initial', 'state_updated': time.time(), 'model': netdesc['model'] })
def allocate_vxid(net_id): vxid = random.randint(1, 16777215) while not etcd.create('vxlan', None, vxid, {'network_uuid': net_id}): vxid = random.randint(1, 16777215) return vxid