def add_ip(self, ip): if isinstance(ip, basestring): try: ip = IPAddress(ip) except AddrFormatError: return False ipnum = ip.value if rds.sismember(self.gatekey, ipnum): rds.srem(self.gatekey, ipnum) rds.sadd(self.storekey, ipnum) return True
def acquire_gateway_ip(self, host): ipnum = rds.spop(self.gatekey) if not ipnum: return vg = VLanGateway.create(ipnum, self.id, host.id) if not vg: rds.sadd(self.gatekey, ipnum) return return vg
def fix_ip(n): network = n.network base = int(network.network_address) for ipnums in more_itertools.chunked(xrange(base+n.gateway_count, base+network.num_addresses), 500): rds.sadd(n.storekey, *ipnums) rds.sadd(n.gatekey, *range(base, base+n.gateway_count)) for ip in n.ips.all(): rds.srem(n.storekey, ip.ipnum) for gateway in n.gates.all(): rds.srem(n.gatekey, gateway.ipnum)
def fix_ip(n): network = n.network base = int(network.network_address) for ipnums in more_itertools.chunked( xrange(base + n.gateway_count, base + network.num_addresses), 500): rds.sadd(n.storekey, *ipnums) rds.sadd(n.gatekey, *range(base, base + n.gateway_count)) for ip in n.ips.all(): rds.srem(n.storekey, ip.ipnum) for gateway in n.gates.all(): rds.srem(n.gatekey, gateway.ipnum)
def create(cls, name, netspace, gateway_count=100): """create network and store ips(int) under this network in redis""" try: n = cls(name, netspace, gateway_count) db.session.add(n) db.session.commit() # create sub IPs network = n.network base = network.first # 一次写500个吧 # 写容器可用IP for ipnums in more_itertools.chunked(xrange(base+gateway_count, base+network.size), 500): rds.sadd(n.storekey, *ipnums) # 写宿主机可用IP rds.sadd(n.gatekey, *range(base, base+gateway_count)) return n except sqlalchemy.exc.IntegrityError: db.session.rollback() return None
def create(cls, name, netspace, gateway_count=100): """create network and store ips(int) under this network in redis""" try: n = cls(name, netspace, gateway_count) db.session.add(n) db.session.commit() # create sub IPs network = n.network base = network.first # 一次写500个吧 # 写容器可用IP for ipnums in more_itertools.chunked( xrange(base + gateway_count, base + network.size), 500): rds.sadd(n.storekey, *ipnums) # 写宿主机可用IP rds.sadd(n.gatekey, *range(base, base + gateway_count)) return n except sqlalchemy.exc.IntegrityError: db.session.rollback() return None
def add_container_backends(container): rds.hset(_app_key(container), container.entrypoint, _entrypoint_key(container)) backends = container.get_backends() if backends: rds.sadd(_entrypoint_key(container), *backends)
def release_gateway(self, ip): rds.sadd(self.gatekey, int(ip))
def release_ip(self, ip): rds.sadd(self.storekey, int(ip))
def release_eip(self, eip): rds.sadd(self.EIP_POOL_KEY, eip.value)
def add_eip(self, *eips): rds.sadd(self.EIP_POOL_KEY, *[eip.value for eip in eips])