diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..249ace41 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.venv/ +tools/pcommand +build/cmake/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index c11dee14..72e9c2c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ multiple `SockAddr`s; it will attempt to contact the host on all of them and use whichever responds first. This allows us to pass both ipv4 and ipv6 addresses when available and transparently use whichever is more performant. +- Added `docker-build` and `docker-run` targets to Makefile ### 1.7.34 (build 21823, api 8, 2024-04-26) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dd21a689..30865a77 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -63,4 +63,5 @@ - Added support for joining using ipv6 address ### Loup Garou -- Added sphinx documentation generation \ No newline at end of file +- Added sphinx documentation generation +- Added docker build \ No newline at end of file diff --git a/Makefile b/Makefile index 4efaf825..eec952de 100644 --- a/Makefile +++ b/Makefile @@ -222,6 +222,12 @@ pcommandbatch_speed_test: env # Prebuilt binaries for various platforms. +docker-build: + $(PCOMMAND) build_docker + +docker-run: + docker run -it bombsquad_server + # WSL is Linux but running under Windows, so it can target either. By default # we want these top level targets (prefab-gui-debug, etc.) to yield native # Windows builds from WSL, but this env var can be set to override that. diff --git a/config/docker/Dockerfile b/config/docker/Dockerfile new file mode 100644 index 00000000..63dee431 --- /dev/null +++ b/config/docker/Dockerfile @@ -0,0 +1,91 @@ +# if provided it will make debug build +ARG cmake_build_type=Release + +# system to start with the build with +# currently will break for non ubuntu system +ARG base_image=ubuntu:24.04 + +#-------------------------------BUILDER-------------------------------- +# Start with the base image +FROM ${base_image} AS builder + +# Renew the arg +ARG cmake_build_type + +ENV LANG en_US.utf8 +ENV LANGUAGE=en_US +ENV DEBIAN_FRONTEND=noninteractive +ENV CMAKE_BUILD_TYPE=${cmake_build_type} + +# Install build dependencies +RUN apt-get update -y && \ + apt-get install -y \ + python3.12-dev \ + python3.12-venv \ + python3-pip \ + libsdl2-dev \ + libvorbisfile3 \ + freeglut3-dev \ + libopenal-dev \ + make \ + curl \ + rsync \ + clang-format \ + cmake \ + libvorbis-dev + +# Copy source code +COPY ./ /home/ubuntu/ballistica + +WORKDIR /home/ubuntu/ballistica + +# Compile the application +RUN make cmake-server-build +RUN mkdir ./../ballistica_cmake_server +RUN mv build/cmake/* ./../ballistica_cmake_server + +#-------------------------------RUNNER-------------------------------- +# Create a new stage for the runtime environment +FROM ${base_image} + +ENV LANG en_US.utf8 +ENV LANGUAGE=en_US +ENV DEBIAN_FRONTEND=noninteractive + +# Renew the arg +ARG cmake_build_type +LABEL BUILD_TYPE=${cmake_build_type} + +ARG bombsquad_build=N/A +LABEL BOMBSQUAD_BUILD=${bombsquad_build} + +ARG bombsquad_version=N/A +LABEL BOMBSQUAD_VERSION=${bombsquad_version} + +# Install runtime dependencies +RUN apt-get update -y && \ + apt-get install -y \ + python3.12-venv \ + python3-pip \ + libsdl2-dev \ + libvorbisfile3 \ + freeglut3-dev \ + libopenal1 \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Copy the compiled application from the builder stage +COPY --from=builder /home/ubuntu/ballistica_cmake_server/*/staged \ + /home/ubuntu/ballistica +# ballisticakit_headless in staged is a symlink +COPY --from=builder /home/ubuntu/ballistica_cmake_server/*/ballisticakit_headless \ + /home/ubuntu/ballistica/dist + +WORKDIR /home/ubuntu/ballistica + +# Expose the necessary port +EXPOSE 43210/udp +EXPOSE 43210/tcp + +# Set the default command to run the application +CMD [ "./ballisticakit_server" ] diff --git a/config/spinoffconfig.py b/config/spinoffconfig.py index 47d390eb..4da1e2ee 100644 --- a/config/spinoffconfig.py +++ b/config/spinoffconfig.py @@ -169,6 +169,7 @@ ctx.no_filter_dirs = { ctx.filter_file_names = { 'Makefile', '.gitignore', + '.dockerignore', '.gitattributes', 'README', 'README.md', diff --git a/tools/batools/build.py b/tools/batools/build.py index bb0bf65c..876f3e8e 100644 --- a/tools/batools/build.py +++ b/tools/batools/build.py @@ -644,3 +644,55 @@ def cmake_prep_dir(dirname: str, verbose: bool = False) -> None: else: if verbose: print(f'{Clr.BLD}{title}:{Clr.RST} Keeping existing build dir.') + + +def _docker_build( + image_name: str, + dockerfile_dir: str, + bombsquad_version: str | None = None, + bombsquad_build: str | int | None = None, + cmake_build_type: str | None = None, +) -> None: + + build_cmd = [ + 'docker', + 'image', + 'build', + '-t', + image_name, + dockerfile_dir, + ] + if bombsquad_version is not None: + build_cmd = build_cmd + [ + '--build-arg', + f'bombsquad_version={bombsquad_version}', + ] + if bombsquad_build is not None: + build_cmd = build_cmd + [ + '--build-arg', + f'bombsquad_build={str(bombsquad_build)}', + ] + if cmake_build_type is not None: + build_cmd = build_cmd + [ + '--build-arg', + f'cmake_build_type={cmake_build_type}', + ] + subprocess.run(build_cmd, check=True) + + +def docker_build() -> None: + """Build docker image.""" + import shutil + + # todo: add option to toggle between prefab and cmake + shutil.copy('config/docker/Dockerfile', '.') + from batools import version + + version_num, build_num = version.get_current_version() + _docker_build( + 'bombsquad_server', + '.', + version_num, + build_num, + ) + os.remove('Dockerfile') diff --git a/tools/batools/pcommandmain.py b/tools/batools/pcommandmain.py index b0790e27..581af8d8 100644 --- a/tools/batools/pcommandmain.py +++ b/tools/batools/pcommandmain.py @@ -104,6 +104,7 @@ from batools.pcommands import ( ensure_prefab_platform, prefab_run_var, prefab_binary_path, + build_docker, make_prefab, lazybuild, efro_gradle, diff --git a/tools/batools/pcommands.py b/tools/batools/pcommands.py index 6bc07aac..d5a08277 100644 --- a/tools/batools/pcommands.py +++ b/tools/batools/pcommands.py @@ -667,6 +667,13 @@ def prefab_binary_path() -> None: ) +def build_docker() -> None: + """Build the docker image with bombsquad cmake server.""" + import batools.build + + batools.build.docker_build() + + def make_prefab() -> None: """Run prefab builds for the current platform.""" import subprocess