def initialize(self, service_dir): super(NetworkResourceService, self).initialize(service_dir) # The <svcroot>/vips directory is used to allocate/de-allocate # container vips. vips_dir = os.path.join(service_dir, self._VIPS_DIR) # Initialize vips self._vips = vipfile.VipMgr(vips_dir, self._service_rsrc_dir) # Clear all environment assignments here. They will be re-assigned # below. for containers_set in set(_SET_BY_ENVIRONMENT.values()): iptables.create_set(containers_set, set_type='hash:ip', family='inet', hashsize=1024, maxelem=65536) need_init = False try: netdev.link_set_up(self._TM_DEV0) netdev.link_set_up(self._TM_DEV1) netdev.link_set_up(self._TMBR_DEV) except subproc.CalledProcessError: need_init = True if need_init: # Reset the bridge self._bridge_initialize() # These two are also done here because they are idempotent # Disable bridge forward delay netdev.bridge_setfd(self._TMBR_DEV, 0) # Enable route_localnet so that we can redirect traffic from the # container to the node's loopback address. netdev.dev_conf_route_localnet_set(self._TM_DEV0, True) # Read bridge status self._bridge_mtu = netdev.dev_mtu(self._TMBR_DEV) # Read current status self._devices = {} for device in netdev.bridge_brif(self._TMBR_DEV): # Ignore local device that is used pass external traffic into the # Treadmill container network. if device == self._TM_DEV1: continue dev_info = _device_info(device) self._devices[dev_info['alias']] = dev_info # Read the currently assigned vIPs for (ip, resource) in self._vips.list(): self._devices.setdefault(resource, {})['ip'] = ip # Mark all the above information as stale for device in self._devices: self._devices[device]['stale'] = True
def setUp(self): self.root = tempfile.mkdtemp() self.vips_dir = os.path.join(self.root, 'vips') owner_dirs = os.path.join(self.root, 'owners') os.mkdir(owner_dirs) for owner in xrange(0, 15): with open(os.path.join(owner_dirs, str(owner)), 'w'): pass self.vips = vipfile.VipMgr(self.vips_dir, owner_dirs)
def setUp(self): self.root = tempfile.mkdtemp() self.vips_dir = os.path.join(self.root, 'vips') owner_dirs = os.path.join(self.root, 'owners') os.mkdir(owner_dirs) for owner in six.moves.range(0, 15): with io.open(os.path.join(owner_dirs, str(owner)), 'w'): pass self.vips = vipfile.VipMgr(cidr='10.0.0.0/8', path=self.vips_dir, owner_path=owner_dirs)
def _init_networks(state_dir, cidrs): """Initializes a CIDR for usage by the Warpgate server. """ vips_path = os.path.join(state_dir, 'vips') sessions_path = os.path.join(state_dir, 'sessions') fs.mkdir_safe(sessions_path) # Clean up old sessions for old_session in os.listdir(sessions_path): _LOGGER.info('Cleaning up old session %r', old_session) os.unlink(os.path.join(sessions_path, old_session)) # Clean up old interfaces for old_devname in _utils.wg_dev_list(): _LOGGER.info('Cleaning up old device %r', old_devname) _utils.wg_dev_delete(old_devname) networks = {} for cidr in cidrs: _LOGGER.debug('Setting up %r', cidr) pool = vipfile.VipMgr( cidr=cidr, path=vips_path, owner_path=sessions_path ) pool.initialize() wg_ip = pool.alloc('self') # XXX: Point to self # blackhole all the managed network ranges try: netdev.route_add(cidr, rtype='blackhole') except netdev.subproc.CalledProcessError as err: if err.returncode == 2: # route already exists pass else: raise networks[cidr] = { 'gateway_ip': wg_ip, 'pool': pool } return networks