def startup(app): with app.app_context(): # close all remote tunnel unicore = utils_file_loads.get_unicore() for infos in unicore.values(): for node in infos.get('nodes', []): try: remote_utils.remote(app.log, '<StartUp Call>', node, 'stop') sleep(0.5) remote_utils.remote(app.log, '<StartUp Call>', node, 'start') except: app.log.warning( "Could not setup remote tunnel {}".format(node)) tunnels = [] while True: try: with closing( psycopg2.connect( host=app.database.get('host'), port=app.database.get('port'), user=app.database.get('user'), password=app.database.get('password'), database=app.database.get( 'database'))) as con: # auto closes with closing(con.cursor()) as cur: # auto closes with con: # auto commit cmd = "SELECT system, hostname, port, node FROM tunnels" app.log.debug("Execute: {}".format(cmd)) cur.execute(cmd) tunnels = cur.fetchall() app.log.debug("Result: {}".format(tunnels)) except: app.log.debug( "StartUp - Could not connect to database. Sleep for 5 seconds." ) sleep(5) continue break for tunnel in tunnels: if not utils.is_tunnel_active(app.log, '<StartUp Call>', tunnel[2]): try: utils.build_tunnel(app.log, '<StartUP Call>', tunnel[0], tunnel[1], tunnel[2], tunnel[3]) except: app.log.exception( "Could not build tunnel. Arguments: {} {} {} {}". format(tunnel[0], tunnel[1], tunnel[2], tunnel[3]))
def post(self): # Track actions through different webservices. uuidcode = request.headers.get('uuidcode', '<no uuidcode>') app.log.info("uuidcode={} - Start remote tunnel".format(uuidcode)) app.log.trace("uuidcode={} - Arguments: {}".format( uuidcode, request.args)) app.log.trace("uuidcode={} - Headers: {}".format( uuidcode, request.headers.to_list())) app.log.trace("uuidcode={} - Json: {}".format(uuidcode, request.json)) validate_auth(app.log, uuidcode, request.headers.get('Intern-Authorization')) if not 'node' in request.json: app.log.warning("uuidcode={} - Invalid Parameters: {}.".format( uuidcode, request.json)) return "Invalid Parameters: {}. Please use only this parameter: node".format( request.json), 422 try: code = remote_utils.remote(app.log, uuidcode, request.json.get('node'), 'start') except subprocess.TimeoutExpired: app.log.exception( "uuidcode={} - Timeout while starting remote tunnel. {}". format(uuidcode, request.json)) return 'Timeout', 512 except: app.log.exception( "uuidcode={} - Exception while starting remote tunnel. {}". format(uuidcode, request.json)) return '', 512 if code == 217: return "", 217 elif code == 218: return "", 218 else: return "{}".format(code), 200