def _openl2tp_cleanup(self, myip, gwip, wait=True): """Cleanup Openl2tp state.""" locks.l2tpconfig_lock_acquire() try: self._openl2tp_cleanup_raw(myip, gwip, wait) except: _log.exception(self._fmt('_openl2tp_cleanup failed')) locks.l2tpconfig_lock_release()
def _openl2tp_start(self): """Start Openl2tp.""" # cleanup has already been called when we come here, so extra PID files # etc have been nuked already locks.l2tpconfig_lock_acquire() try: run_command([constants.CMD_MODPROBE, 'pppol2tp']) d = daemonstart.DaemonStart(_log) d.start_daemon(command=constants.CMD_OPENL2TP, pidfile=constants.OPENL2TP_PIDFILE) # FIXME: add local IP option if using this. # d.start_daemon(command=constants.CMD_OPENL2TP, args=['-a', '172.20.100.202'], pidfile=constants.OPENL2TP_PIDFILE) except: _log.exception('_openl2tp_start failed') locks.l2tpconfig_lock_release() raise locks.l2tpconfig_lock_release()
def _openl2tp_cleanup_raw(self, myip, gwip, wait): """Actual cleanup.""" if self.tunnel_id is None or self.session_id is None: _log.info( self._fmt( 'tunnel or session id missing, not doing openl2tp cleanup') ) return ppp_profile_name = 'ppp-prof-%s' % myip tunnel_profile_name = 'tunnel-prof-%s' % myip session_profile_name = 'session-prof-%s' % myip peer_profile_name = 'peer-prof-%s' % myip tunnel_name = 'tunnel-%s' % myip session_name = 'session-%s' % myip _tunnel_deleted_re = re.compile('.*Tunnel not found.*') _session_deleted_re = re.compile('.*Session not found.*') # Delete session and wait for removal run_command([ constants.CMD_OPENL2TPCONFIG, 'session', 'delete', 'tunnel_id=%s' % str(self.tunnel_id), 'session_id=%s' % str(self.session_id) ]) # ignore errors while True: [rv, stdout, stderr] = run_command([ constants.CMD_OPENL2TPCONFIG, 'session', 'show', 'tunnel_id=%s' % str(self.tunnel_id), 'session_id=%s' % str(self.session_id) ]) # ignore errors m = _session_deleted_re.match(stderr) if m is not None: _log.info( self._fmt('session no longer exists, stop complete: %s' % stderr)) break m = _tunnel_deleted_re.match(stderr) if m is not None: _log.info( self._fmt('tunnel no longer exists, stop complete: %s' % stderr)) break if not wait: _log.warning( self._fmt( 'session %s (in tunnel %s) still exists, ignoring' % (str(self.session_id, str(self.tunnel_id))))) break _log.info( self._fmt('session %s (in tunnel %s) still exists, waiting' % (str(self.session_id), str(self.tunnel_id)))) locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() # Delete our tunnel and wait that it is removed run_command([ constants.CMD_OPENL2TPCONFIG, 'tunnel', 'delete', 'tunnel_id=%s' % str(self.tunnel_id) ]) # ignore errors while True: [rv, stdout, stderr] = run_command([ constants.CMD_OPENL2TPCONFIG, 'tunnel', 'show', 'tunnel_id=%s' % str(self.tunnel_id) ]) # ignore errors m = _tunnel_deleted_re.match(stderr) if m is not None: _log.info( self._fmt('tunnel no longer exists, stop complete: %s' % stderr)) break if not wait: _log.warning( self._fmt('tunnel %s still exists, ignoring' % str(self.tunnel_id))) break _log.info( self._fmt('tunnel %s still exists, waiting' % str(self.tunnel_id))) locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() # delete existing profiles just to be sure for i in [ 'ppp profile delete profile_name=%s' % ppp_profile_name, 'tunnel profile delete profile_name=%s' % tunnel_profile_name, 'session profile delete profile_name=%s' % session_profile_name, 'peer profile delete profile_name=%s' % peer_profile_name ]: cmd = '%s\nquit\n' % i run_command([constants.CMD_OPENL2TPCONFIG], stdin=cmd) # ignore errors
def _openl2tp_config(self, myip, gwip, index=0): """Configure and start Openl2tp through l2tpconfig.""" _log.debug(self._fmt('_openl2tp_config')) locks.l2tpconfig_lock_acquire() try: self._openl2tp_config_raw(myip, gwip, index) except: _log.exception(self._fmt('_openl2tp_config failed')) locks.l2tpconfig_lock_release() return _log.info(self._fmt('starting tunnel')) try: self._openl2tp_start_tunnel(myip, gwip, index) except: _log.exception('start tunnel failed') locks.l2tpconfig_lock_release() return # Wait for tunnel to come up (tunnel retry timeout is 20 seconds) _tunnel_check_re = re.compile('.+\s+%s.*ESTABLISHED.*' % str(self.tunnel_id)) tunnel_wait_count = 0 while True: [rv, stdout, stderr ] = run_command([constants.CMD_OPENL2TPCONFIG, 'tunnel', 'list']) found = False for l in stdout.split('\n'): m = _tunnel_check_re.match(l) if m is not None: _log.info(self._fmt('l2tp tunnel found')) found = True if found: break tunnel_wait_count = tunnel_wait_count + 1 if tunnel_wait_count > 7: _log.error(self._fmt('waited too long for l2tp tunnel')) locks.l2tpconfig_lock_release() raise Exception('failed to setup tunnel') locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() _log.info(self._fmt('created new tunnel (%s) ' % str(self.tunnel_id))) _log.info(self._fmt('starting session')) try: self._openl2tp_start_session(myip, gwip, index) except: _log.exception('start session failed') locks.l2tpconfig_lock_release() return # Wait for session to come up (session timeout is what?) _session_check_re = re.compile('\s+%s.*' % str(self.session_id)) session_wait_count = 0 while True: [rv, stdout, stderr] = run_command([ constants.CMD_OPENL2TPCONFIG, 'session', 'list', 'tunnel_id=%s' % self.tunnel_id ]) found = False for l in stdout.split('\n'): m = _session_check_re.match(l) if m is not None: _log.info(self._fmt('l2tp session found')) found = True if found: break session_wait_count = tunnel_wait_count + 1 if session_wait_count > 5: _log.error(self._fmt('waited too long for l2tp sesion')) locks.l2tpconfig_lock_release() raise Exception('failed to setup session') locks.l2tpconfig_lock_release() time.sleep(5) locks.l2tpconfig_lock_acquire() _log.info( self._fmt('created new tunnel and session (%s/%s) ' % (str(self.tunnel_id), str(self.session_id)))) locks.l2tpconfig_lock_release()
def _openl2tp_cleanup_raw(self, myip, gwip, wait): """Actual cleanup.""" if self.tunnel_id is None or self.session_id is None: _log.info(self._fmt('tunnel or session id missing, not doing openl2tp cleanup')) return ppp_profile_name = 'ppp-prof-%s' % myip tunnel_profile_name = 'tunnel-prof-%s' % myip session_profile_name = 'session-prof-%s' % myip peer_profile_name = 'peer-prof-%s' % myip tunnel_name = 'tunnel-%s' % myip session_name = 'session-%s' % myip _tunnel_deleted_re = re.compile('.*Tunnel not found.*') _session_deleted_re = re.compile('.*Session not found.*') # Delete session and wait for removal run_command([constants.CMD_OPENL2TPCONFIG, 'session', 'delete', 'tunnel_id=%s' % str(self.tunnel_id), 'session_id=%s' % str(self.session_id)]) # ignore errors while True: [rv, stdout, stderr] = run_command([constants.CMD_OPENL2TPCONFIG, 'session', 'show', 'tunnel_id=%s' % str(self.tunnel_id), 'session_id=%s' % str(self.session_id)]) # ignore errors m = _session_deleted_re.match(stderr) if m is not None: _log.info(self._fmt('session no longer exists, stop complete: %s' % stderr)) break m = _tunnel_deleted_re.match(stderr) if m is not None: _log.info(self._fmt('tunnel no longer exists, stop complete: %s' % stderr)) break if not wait: _log.warning(self._fmt('session %s (in tunnel %s) still exists, ignoring' % (str(self.session_id, str(self.tunnel_id))))) break _log.info(self._fmt('session %s (in tunnel %s) still exists, waiting' % (str(self.session_id), str(self.tunnel_id)))) locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() # Delete our tunnel and wait that it is removed run_command([constants.CMD_OPENL2TPCONFIG, 'tunnel', 'delete', 'tunnel_id=%s' % str(self.tunnel_id)]) # ignore errors while True: [rv, stdout, stderr] = run_command([constants.CMD_OPENL2TPCONFIG, 'tunnel', 'show', 'tunnel_id=%s' % str(self.tunnel_id)]) # ignore errors m = _tunnel_deleted_re.match(stderr) if m is not None: _log.info(self._fmt('tunnel no longer exists, stop complete: %s' % stderr)) break if not wait: _log.warning(self._fmt('tunnel %s still exists, ignoring' % str(self.tunnel_id))) break _log.info(self._fmt('tunnel %s still exists, waiting' % str(self.tunnel_id))) locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() # delete existing profiles just to be sure for i in [ 'ppp profile delete profile_name=%s' % ppp_profile_name, 'tunnel profile delete profile_name=%s' % tunnel_profile_name, 'session profile delete profile_name=%s' % session_profile_name, 'peer profile delete profile_name=%s' % peer_profile_name ]: cmd = '%s\nquit\n' % i run_command([constants.CMD_OPENL2TPCONFIG], stdin=cmd) # ignore errors
def _openl2tp_config(self, myip, gwip, index=0): """Configure and start Openl2tp through l2tpconfig.""" _log.debug(self._fmt('_openl2tp_config')) locks.l2tpconfig_lock_acquire() try: self._openl2tp_config_raw(myip, gwip, index) except: _log.exception(self._fmt('_openl2tp_config failed')) locks.l2tpconfig_lock_release() return _log.info(self._fmt('starting tunnel')) try: self._openl2tp_start_tunnel(myip, gwip, index) except: _log.exception('start tunnel failed') locks.l2tpconfig_lock_release() return # Wait for tunnel to come up (tunnel retry timeout is 20 seconds) _tunnel_check_re = re.compile('.+\s+%s.*ESTABLISHED.*' % str(self.tunnel_id)) tunnel_wait_count = 0 while True: [rv, stdout, stderr] = run_command([constants.CMD_OPENL2TPCONFIG, 'tunnel', 'list']) found = False for l in stdout.split('\n'): m = _tunnel_check_re.match(l) if m is not None: _log.info(self._fmt('l2tp tunnel found')) found = True if found: break tunnel_wait_count = tunnel_wait_count + 1 if tunnel_wait_count > 7: _log.error(self._fmt('waited too long for l2tp tunnel')) locks.l2tpconfig_lock_release() raise Exception('failed to setup tunnel') locks.l2tpconfig_lock_release() time.sleep(10) locks.l2tpconfig_lock_acquire() _log.info(self._fmt('created new tunnel (%s) ' % str(self.tunnel_id))) _log.info(self._fmt('starting session')) try: self._openl2tp_start_session(myip, gwip, index) except: _log.exception('start session failed') locks.l2tpconfig_lock_release() return # Wait for session to come up (session timeout is what?) _session_check_re = re.compile('\s+%s.*' % str(self.session_id)) session_wait_count = 0 while True: [rv, stdout, stderr] = run_command([constants.CMD_OPENL2TPCONFIG, 'session', 'list', 'tunnel_id=%s' % self.tunnel_id]) found = False for l in stdout.split('\n'): m = _session_check_re.match(l) if m is not None: _log.info(self._fmt('l2tp session found')) found = True if found: break session_wait_count = tunnel_wait_count + 1 if session_wait_count > 5: _log.error(self._fmt('waited too long for l2tp sesion')) locks.l2tpconfig_lock_release() raise Exception('failed to setup session') locks.l2tpconfig_lock_release() time.sleep(5) locks.l2tpconfig_lock_acquire() _log.info(self._fmt('created new tunnel and session (%s/%s) ' % (str(self.tunnel_id), str(self.session_id)))) locks.l2tpconfig_lock_release()