def create_network_interface(interface_uuid, netdesc, instance_uuid, order): if 'macaddress' not in netdesc or not netdesc['macaddress']: with etcd.get_lock('macaddress', None, 'all', ttl=120): possible_mac = str(randmac.RandMac( '00:00:00:00:00:00', False)).lstrip('\'').rstrip('\'') while etcd.get('macaddress', None, possible_mac): possible_mac = str(randmac.RandMac( '00:00:00:00:00:00', False)).lstrip('\'').rstrip('\'') etcd.put('macaddress', None, possible_mac, {'interface_uuid': interface_uuid}) 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_network(netblock, provide_dhcp=True, provide_nat=False, name=None, namespace=None): netid = str(uuid.uuid4()) ipm = ipmanager.NetBlock(netblock) etcd.put('ipmanager', None, netid, ipm.save()) with etcd.get_lock('vxlan', None, 'all'): vxid = 1 while etcd.get('vxlan', None, vxid): vxid += 1 etcd.put('vxlan', None, vxid, {'network_uuid': netid}) d = { 'uuid': netid, '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, netid, d) return d
def _await_election(self): # Attempt to acquire the cluster maintenance lock forever. We never # release the lock, it gets cleared on a crash. This is so that only # one node at a time is performing cluster maintenance. while not self.exit.is_set(): self.lock = etcd.get_lock('cluster', None, None, ttl=900, timeout=10, op='Cluster maintenance') result = self.lock.acquire() if result: self.is_elected = True return self.exit.wait(10)
def get_lock(objecttype, subtype, name, ttl=60, timeout=ETCD_ATTEMPT_TIMEOUT, relatedobjects=None): return etcd.get_lock(objecttype, subtype, name, ttl=ttl, timeout=timeout, relatedobjects=relatedobjects)
def get_lock(objecttype, subtype, name, ttl=60, timeout=constants.ETCD_ATTEMPT_TIMEOUT, relatedobjects=None, log_ctx=LOG, op=None): return etcd.get_lock(objecttype, subtype, name, ttl=ttl, timeout=timeout, log_ctx=log_ctx, op=op)
def allocate_console_port(instance_uuid): node = config.parsed.get('NODE_NAME') with etcd.get_lock('console', None, node): consumed = [] for value in etcd.get_all('console', node): consumed.append(value['port']) port = random.randint(30000, 50000) while port in consumed or not _port_free(port): port = random.randint(30000, 50000) etcd.put('console', node, port, { 'instance_uuid': instance_uuid, 'port': port, }) return port
def allocate_console_port(instance_uuid): see_this_node() node = config.parsed.get('NODE_NAME') with etcd.get_lock('sf/console/%s' % node) as _: consumed = [] for value in etcd.get_all('console', node): consumed.append(value['port']) port = random.randint(30000, 50000) while port in consumed: port = random.randint(30000, 50000) etcd.put('console', node, port, { 'instance_uuid': instance_uuid, 'port': port, }) return port
def get_lock(name, ttl=60): return etcd.get_lock(name, ttl=ttl)