def run_lambda(task, event): client = docker.from_env() container_name = NAME_CONTAINER_MAPPING.get(task['runtime']) if not container_name: return f"Container {task['runtime']} is not found" mount = Mount(type="volume", source=task['task_id'], target="/var/task") env_vars = loads(task.get("env_vars", "{}")) response = client.containers.run(f"lambci/{container_name}", command=[f"{task['task_handler']}", dumps(event)], mounts=[mount], stderr=True, remove=True, environment=env_vars) log = response.decode("utf-8", errors='ignore') if container_name == "lambda:python3.7": results = re.findall(r'({.+?})', log)[-1] else: # TODO: magic of 2 enters is very flaky, Need to think on how to workaround, probably with specific logging results = log.split("\n\n")[1] data = {"ts": int(mktime(datetime.utcnow().timetuple())), 'results': results, 'stderr': log} headers = { "Content-Type": "application/json", "Token": task['token'] } auth_token = unsecret("{{secret.auth_token}}", project_id=task['project_id']) if auth_token: headers['Authorization'] = f'bearer {auth_token}' post(f'{APP_HOST}/api/v1/task/{task["task_id"]}/results', headers=headers, data=dumps(data)) return results
def add_task(): if request.method == 'GET': return render_template('lambdas/add_task.html', runtimes=NAME_CONTAINER_MAPPING.keys()) if request.method == 'POST': if 'file' not in request.files: flash('No file part') return '' file = request.files['file'] if file.filename == '': flash('No selected file') return '' if file and allowed_file(file.filename): filename = str(uuid4()) filename = secure_filename(filename) file.save( os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) app = run.connect_to_celery(1) celery_task = app.signature('tasks.volume', kwargs={ 'task_id': filename, 'file_path': current_app.config['UPLOAD_FOLDER'] }) celery_task.apply_async() task = Task(task_id=filename, zippath=filename, task_name=request.form['funcname'], task_handler=request.form['invoke_func'], runtime=request.form['runtime'], env_vars=request.form['env_vars']) task.insert() return f"{filename}"
def call_lambda(task_name): if request.method == "GET": return render_template("lambdas/task.html", task=Task.query.filter_by(task_id=task_name).first(), runtimes=NAME_CONTAINER_MAPPING.keys()) else: if request.content_type == "application/json": task = Task.query.filter_by(task_id=task_name).first().to_json() event = request.get_json() app = run.connect_to_celery(1) celery_task = app.signature('tasks.execute', kwargs={'task': task, 'event': event}) celery_task.apply_async() return "Accepted", 201 elif request.content_type == "application/x-www-form-urlencoded": return f"Calling {task_name} with {request.form}"
def call_lambda(task_id: str): if request.method == "POST": if request.content_type == "application/json": task = Task.query.filter( and_(Task.task_id == task_id)).first().to_json() event = request.get_json() app = run.connect_to_celery(1) celery_task = app.signature("tasks.execute", kwargs={ "task": task, "event": event }) celery_task.apply_async() return "Accepted", 201 elif request.content_type == "application/x-www-form-urlencoded": return f"Calling {task_id} with {request.form}" task = Task.query.filter(and_(Task.task_id == task_id)).first() return render_template("lambdas/task.html", task=task, runtimes=NAME_CONTAINER_MAPPING.keys())
def add_task(project: Project): return render_template("lambdas/add_task.html", runtimes=NAME_CONTAINER_MAPPING.keys())