From e4dff3ded7f20cefb60e209bc4ddc890f3f0de2c Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Mon, 11 Dec 2023 14:56:16 +0000 Subject: [PATCH] lammps-migration: build in container from cmdline and check at every period --- examples/lammps-migration | 2 +- tasks/env.py | 7 +++++ tasks/lammps.py | 50 ++++++++++++++++++++++++++++------ tasks/util.py | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 tasks/util.py diff --git a/examples/lammps-migration b/examples/lammps-migration index 3cad6ab..65eaa46 160000 --- a/examples/lammps-migration +++ b/examples/lammps-migration @@ -1 +1 @@ -Subproject commit 3cad6abb4a324e5e04ab62dc84cfca4dcebac52a +Subproject commit 65eaa46ac4e95b4b6a4d94cbbadfb05ab9181421 diff --git a/tasks/env.py b/tasks/env.py index 7ba153c..272913a 100644 --- a/tasks/env.py +++ b/tasks/env.py @@ -1,17 +1,20 @@ from faasmtools.build import FAASM_LOCAL_DIR from faasmtools.docker import ACR_NAME from faasmtools.env import get_version as get_cpp_version +from os import environ from os.path import dirname, abspath, join from re import search as re_search from subprocess import run PROJ_ROOT = dirname(dirname(abspath(__file__))) +EXAMPLES_IN_DOCKER_ROOT = "/code/examples" DOCKER_ROOT = join(PROJ_ROOT, "docker") EXAMPLES_DIR = join(PROJ_ROOT, "examples") DEV_FAASM_LOCAL = join(PROJ_ROOT, "dev", "faasm-local") WASM_DIR = join(PROJ_ROOT, "wasm") # Docker variables +EXAMPLES_BUILD_IMAGE_CTR = "examples-build-workon" EXAMPLES_BUILD_IMAGE_NAME = "{}/examples-build".format(ACR_NAME) EXAMPLES_BUILD_DOCKERFILE = join(DOCKER_ROOT, "build.dockerfile") EXAMPLES_RUN_IMAGE_NAME = "{}/examples-run".format(ACR_NAME) @@ -115,3 +118,7 @@ def get_version(name="build"): return "{}_{}".format(get_cpp_version(), get_python_version()) if name == "run": return "{}".format(get_faasm_version()) + + +def in_docker(): + return "IN_DOCKER" in environ diff --git a/tasks/lammps.py b/tasks/lammps.py index 33fb971..810c47a 100644 --- a/tasks/lammps.py +++ b/tasks/lammps.py @@ -1,7 +1,8 @@ from faasmtools.build import CMAKE_TOOLCHAIN_FILE, FAASM_BUILD_ENV_DICT from faasmtools.compile_util import wasm_copy_upload from faasmtools.env import LLVM_VERSION -from tasks.env import EXAMPLES_DIR +from tasks.env import DEV_FAASM_LOCAL, EXAMPLES_DIR, in_docker +from tasks.util import run_docker_build_cmd from invoke import task from os import environ, makedirs from os.path import exists, join @@ -51,16 +52,49 @@ def build(ctx, clean=False, native=False, migration=False): "-DLAMMPS_FAASM=ON", "-DCMAKE_TOOLCHAIN_FILE={}".format(CMAKE_TOOLCHAIN_FILE), ] - cmake_cmd += [cmake_dir] - cmake_cmd = " ".join(cmake_cmd) + if in_docker(): + cmake_cmd += [cmake_dir] + cmake_cmd = " ".join(cmake_cmd) - work_env = environ.copy() - work_env.update(FAASM_BUILD_ENV_DICT) + work_env = environ.copy() + work_env.update(FAASM_BUILD_ENV_DICT) - run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env) - run("ninja", shell=True, check=True, cwd=build_dir) + run(cmake_cmd, shell=True, check=True, cwd=build_dir, env=work_env) + run("ninja", shell=True, check=True, cwd=build_dir) + + else: + in_docker_cmake_dir = cmake_dir + in_docker_cmake_dir = cmake_dir.removeprefix(EXAMPLES_DIR) + in_docker_cmake_dir = "/code/examples/examples" + in_docker_cmake_dir + in_docker_build_dir = build_dir + in_docker_build_dir = build_dir.removeprefix(EXAMPLES_DIR) + in_docker_build_dir = "/code/examples/examples" + in_docker_build_dir + + cmake_cmd += [in_docker_cmake_dir] + cmake_cmd = " ".join(cmake_cmd) + + run_docker_build_cmd( + [cmake_cmd, "ninja"], + cwd=in_docker_build_dir, + env=FAASM_BUILD_ENV_DICT, + ) if not native: # Copy the binary to lammps/main/function.wasm lammps_func_name = "migration" if migration else "main" - wasm_copy_upload("lammps", lammps_func_name, join(build_dir, "lmp")) + lammps_func = join(build_dir, "lmp") + + if in_docker(): + wasm_copy_upload("lammps", lammps_func_name, lammps_func) + else: + dest_folder = join( + DEV_FAASM_LOCAL, "wasm", "lammps", lammps_func_name + ) + + makedirs(dest_folder, exist_ok=True) + dest_file = join(dest_folder, "function.wasm") + run( + "sudo cp {} {}".format(lammps_func, dest_file), + shell=True, + check=True, + ) diff --git a/tasks/util.py b/tasks/util.py new file mode 100644 index 0000000..3e92cfa --- /dev/null +++ b/tasks/util.py @@ -0,0 +1,56 @@ +from subprocess import run +from tasks.env import ( + DEV_FAASM_LOCAL, + EXAMPLES_BUILD_IMAGE_CTR, + EXAMPLES_BUILD_IMAGE_NAME, + PROJ_ROOT, + get_version, +) + + +def start_docker_build_ctr(): + docker_cmd = [ + "docker run", + "-td", + "-v {}:/code/examples".format(PROJ_ROOT), + "-v {}:{}".format(DEV_FAASM_LOCAL, "/usr/local/faasm"), + "--name {}".format(EXAMPLES_BUILD_IMAGE_CTR), + "{}:{}".format(EXAMPLES_BUILD_IMAGE_NAME, get_version()), + ] + + docker_cmd = " ".join(docker_cmd) + out = run(docker_cmd, shell=True, capture_output=True) + assert out.returncode == 0, "Error starting build container: {}".format( + out.stderr.decode("utf-8") + ) + + +def stop_docker_build_ctr(): + out = run( + "docker rm -f {}".format(EXAMPLES_BUILD_IMAGE_CTR), + shell=True, + capture_output=True, + ) + assert out.returncode == 0, "Error stopping build container: {}".format( + out.stderr.decode("utf-8") + ) + + +def run_docker_build_cmd(cmd_list, cwd=None, env=None): + start_docker_build_ctr() + + for cmd in cmd_list: + docker_cmd = [ + "docker exec", + "--workdir {}".format(cwd) if cwd is not None else "", + " ".join(['--env {}="{}"'.format(k, env[k]) for k in env]) + if env is not None + else "", + EXAMPLES_BUILD_IMAGE_CTR, + cmd, + ] + + docker_cmd = " ".join(docker_cmd) + run(docker_cmd, shell=True, check=True) + + stop_docker_build_ctr()