From a9fd5a7f61a354f9892774f2a5a7d1fd74d804c0 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Thu, 7 Apr 2022 10:22:53 +0200 Subject: [PATCH] Containerize RGW with minimal dependencies - Add script to setup and manage the build environment. - Add script to build the radosgw binary. - Add script to build the s3gw container. Fixes: https://github.com/aquarist-labs/s3gw-core/issues/9 Signed-off-by: Volker Theile --- .editorconfig | 14 ++++ .gitignore | 1 + build/Dockerfile.build-s3gw | 9 +++ build/README.md | 93 ++++++++++++++++++++++++ build/build-container.sh | 66 +++++++++++++++++ build/build-radosgw.sh | 35 +++++++++ buildenvadm.sh | 139 ++++++++++++++++++++++++++++++++++++ 7 files changed, 357 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 build/Dockerfile.build-s3gw create mode 100644 build/README.md create mode 100755 build/build-container.sh create mode 100755 build/build-radosgw.sh create mode 100755 buildenvadm.sh diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..68d3f54 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# EditorConfig is awesome: http://EditorConfig.org +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true +end_of_line = lf + +[*.md] +max_line_length = off +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/build/Dockerfile.build-s3gw b/build/Dockerfile.build-s3gw new file mode 100644 index 0000000..59ee390 --- /dev/null +++ b/build/Dockerfile.build-s3gw @@ -0,0 +1,9 @@ +FROM opensuse/tumbleweed + +ENV CEPH_DIR=/srv/ceph + +RUN zypper -n install bash make nano vim git ccache cmake ninja +COPY build-radosgw.sh /usr/bin/build-radosgw.sh + +VOLUME ["/srv/ceph"] +ENTRYPOINT ["/usr/bin/build-radosgw.sh"] diff --git a/build/README.md b/build/README.md new file mode 100644 index 0000000..d4c21fc --- /dev/null +++ b/build/README.md @@ -0,0 +1,93 @@ +This README will guide you through the several steps to build the `s3gw` +container image. + +# Prerequisite +The build scripts expect the following directory hierarchy. + +``` +| +|- ceph/ +| |- build/ +| ... +| +|- s3gw-core/ + |- build/ + ... +``` + +If the Ceph Git repository is stored in a different path, then this +can be customized via the `CEPH_DIR` environment variable. + +# Build the build environment +To isolate the build system from your host, we provide a build environment +based on openSUSE Tumbleweed. This containerized build environment will +contain all necessary dependencies. + +## Install management tool +To create the buidl environment, make sure to install all necessary +tools. This can be done by running the command: + +``` +$ cd ~/git/s3gw-core +$ sudo buildenvadm.sh install +``` + +## Create the build environment +To create the containerized build environment run the following +command: + +``` +$ cd ~/git/s3gw-core +$ buildenvadm.sh create +``` + +The source code of Ceph will be mounted to `/srv/ceph` and the +s3gw-core code is available at `/srv/s3gw-core`. + +## Start the build environment +To start the build environment, run the following command: + +``` +$ cd ~/git/s3gw-core +$ buildenvadm.sh start +``` + +You will be redirected to the `/srv/s3gw-core/build` directory within +the containerized build environment. + +# Building radosgw binary +If the binary is not compiled yet, simply run the following commands: + +``` +# cd ~/git/s3gw-core +# cd build +# ./build-radosgw.sh +``` + +If you are not running openSUSE Tumbleweed on your host, you can use +the `Dockerfile.build-s3gw` file to setup a container that will +automatically build the binary when the container is started. + +``` +$ cd ~/git/s3gw-core/build +$ podman build --tag build-s3gw -f ./Dockerfile.build-s3gw +``` + +To trigger a build run, execute the following commands: +``` +$ cd ~/git/s3gw-core/build +$ podman run --replace --name build-s3gw -v ../../ceph:/srv/ceph/ localhost/build-s3gw +``` + +# Build the container image +If the Ceph `radosgw` binary is compiled, the container image can be build +with the following commands: + +``` +# cd ~/git/s3gw-core +# cd build +# ./build-container.sh +``` + +The container build script expects the `radosgw` binary at the relative +path `../ceph/build/bin`. diff --git a/build/build-container.sh b/build/build-container.sh new file mode 100755 index 0000000..ef9fef5 --- /dev/null +++ b/build/build-container.sh @@ -0,0 +1,66 @@ +#!/bin/sh + +set -e + +BASE_IMAGE=${BASE_IMAGE:-"registry.opensuse.org/opensuse/tumbleweed:latest"} +IMAGE_NAME=${IMAGE_NAME:-"s3gw"} +CEPH_DIR=$(realpath ${CEPH_DIR:-"../../ceph/"}) +INSTALL_PACKAGES=${INSTALL_PACKAGES:-"libblkid1 libexpat1 libtcmalloc4 libfmt8 libibverbs1 librdmacm1 liboath0 libicu70"} + +registry= +registry_args= + +check_deps() { + if [ ! $(which buildah) ]; then + echo 'Unable to find "buildah". Please make sure it is installed.' + exit 1 + fi +} + +build_container_image() { + echo "Building container image ..." + echo "BASE_IMAGE=${BASE_IMAGE}" + echo "IMAGE_NAME=${IMAGE_NAME}" + echo "CEPH_DIR=${CEPH_DIR}" + + tmpfile=$(mktemp) + cat > ${tmpfile} << EOF +ctr=\$(buildah from ${BASE_IMAGE}) +buildah run \${ctr} /bin/sh -c 'zypper -n install ${INSTALL_PACKAGES}' +mnt=\$(buildah mount \${ctr}) +mkdir -p \${mnt}/data/ +cp --verbose ${CEPH_DIR}/build/bin/radosgw \${mnt}/usr/bin/radosgw +cp --verbose --no-dereference ${CEPH_DIR}/build/lib/*.so* \${mnt}/usr/lib64/ +buildah unmount \${ctr} +buildah config --volume /data/ \${ctr} +buildah config --env ID=s3gw \${ctr} +buildah config --env EXTRA_ARGS= \${ctr} +buildah config --entrypoint "radosgw -d --no-mon-config --rgw-backend-store dbstore --id \${ID} --rgw-data /data/ --run-dir /run/ \${EXTRA_ARGS}" \${ctr} +buildah commit --rm \${ctr} ${IMAGE_NAME} +EOF + buildah unshare sh ${tmpfile} + rm -f ${tmpfile} + + if [ -n "${registry}" ]; then + buildah push ${registry_args} localhost/${IMAGE_NAME} \ + ${registry}/${IMAGE_NAME} + fi +} + +while [ $# -ge 1 ]; do + case $1 in + --registry) + registry=$2 + shift + ;; + --no-registry-tls) + registry_args="--tls-verify=false" + ;; + esac + shift +done + +check_deps +build_container_image + +exit 0 diff --git a/build/build-radosgw.sh b/build/build-radosgw.sh new file mode 100755 index 0000000..9088e14 --- /dev/null +++ b/build/build-radosgw.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +set -e + +CEPH_DIR=$(realpath ${CEPH_DIR:-"../../ceph/"}) +CEPH_CMAKE_ARGS="-DENABLE_GIT_VERSION=ON -DWITH_PYTHON3=3 -DWITH_CCACHE=ON ${CEPH_CMAKE_ARGS}" +CEPH_CMAKE_ARGS="-DWITH_TESTS=OFF -DCMAKE_BUILD_TYPE=Release ${CEPH_CMAKE_ARGS}" +CEPH_CMAKE_ARGS="-DWITH_RADOSGW_AMQP_ENDPOINT=OFF -DWITH_RADOSGW_KAFKA_ENDPOINT=OFF ${CEPH_CMAKE_ARGS}" +CEPH_CMAKE_ARGS="-DWITH_RADOSGW_SELECT_PARQUET=OFF -DWITH_RADOSGW_MOTR=OFF ${CEPH_CMAKE_ARGS}" +CEPH_CMAKE_ARGS="-DWITH_RADOSGW_DBSTORE=ON ${CEPH_CMAKE_ARGS}" +NPROC=${NPROC:-$(nproc --ignore=2)} + +build_radosgw_binary() { + echo "Building radosgw binary ..." + echo "CEPH_DIR=${CEPH_DIR}" + echo "NPROC=${NPROC}" + + cd ${CEPH_DIR} + + ./install-deps.sh || true + + if [ -d "build" ]; then + cd build/ + cmake -DBOOST_J=${NPROC} ${CEPH_CMAKE_ARGS} .. + else + ./do_cmake.sh ${CEPH_CMAKE_ARGS} + cd build/ + fi + + ninja -j${NPROC} bin/radosgw +} + +build_radosgw_binary + +exit 0 diff --git a/buildenvadm.sh b/buildenvadm.sh new file mode 100755 index 0000000..a249c1e --- /dev/null +++ b/buildenvadm.sh @@ -0,0 +1,139 @@ +#!/bin/sh + +set -e + +BASE_IMAGE=${BASE_IMAGE:-"registry.opensuse.org/opensuse/tumbleweed:latest"} +IMAGE_NAME=${IMAGE_NAME:-"s3gw-build-env"} +CEPH_DIR=$(realpath ${CEPH_DIR:-"../ceph/"}) +WORKING_DIR=$(pwd) +CCACHE_DIR= + +usage() { + cat <> ~/.inputrc +"\C-[OA": history-search-backward +"\C-[[A": history-search-backward +"\C-[OB": history-search-forward +"\C-[[B": history-search-forward +EOF +' + buildah config --workingdir '/srv/s3gw-core/build/' ${ctr} + buildah commit --rm ${ctr} ${IMAGE_NAME} +} + +remove() { + podman rm --ignore ${IMAGE_NAME} + podman image rm ${IMAGE_NAME} +} + +start() { + EXTRA_ARGS= + if [ -z "${CCACHE_DIR}" ]; then + if [ -d ~/.ccache/ ]; then + CCACHE_DIR=~/.ccache/s3gw-core + mkdir -p ${CCACHE_DIR} + fi + fi + if [ -n "${CCACHE_DIR}" ]; then + echo "Using CCACHE_DIR=${CCACHE_DIR}" + EXTRA_ARGS="--volume $(realpath ${CCACHE_DIR}):/root/.ccache/ ${EXTRA_ARGS}" + fi + podman run \ + --interactive \ + --tty \ + --replace \ + --privileged \ + --volume ${WORKING_DIR}:/srv/s3gw-core/ \ + --volume ${CEPH_DIR}:/srv/ceph/ \ + --hostname ${IMAGE_NAME} \ + --name ${IMAGE_NAME} \ + ${EXTRA_ARGS} \ + ${IMAGE_NAME} \ + /bin/bash +} + +install() { + . /etc/os-release + + case "${ID}" in + debian|ubuntu) + dirname=Debian_${VERSION_ID} + [ "${ID}" = "ubuntu" ] && dirname=xUbuntu_${VERSION_ID} + echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/${dirname}/ /" | sudo tee /etc/apt/sources.list.d/opensuse_devel_kubic_libcontainers_stable.list + curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/${dirname}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/opensuse_devel_kubic_libcontainers_stable.gpg > /dev/null + sudo apt-get -y update + sudo apt-get -y install buildah podman + ;; + opensuse*|suse|sles) + sudo zypper -n install buildah podman + ;; + esac +} + +while getopts ":?h" option +do + case ${option} in + h|help|?) + usage >&2 + exit 2 + ;; + esac +done + +shift $((OPTIND-1)) + +case $@ in +create) + check_deps + create + ;; +remove) + check_deps + remove + ;; +start) + check_deps + start + ;; +install) + install + ;; +*) + usage >&2 + exit 2 + ;; +esac + +exit 0