From 7356b6cc88de581e18af8e7f5e75f2a91b0a7dc5 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Fri, 3 Jun 2022 13:10:47 +0200 Subject: [PATCH] Build: Add cleanup function to build_package --- build_scripts/appimage/appimage.sh | 16 ++++++++-------- build_scripts/build_package.sh | 17 ++++++++++++++++- build_scripts/docker/docker.sh | 22 +++++++--------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/build_scripts/appimage/appimage.sh b/build_scripts/appimage/appimage.sh index 16652c081..3602b46a1 100755 --- a/build_scripts/appimage/appimage.sh +++ b/build_scripts/appimage/appimage.sh @@ -110,17 +110,11 @@ remove_python_appdir_artifacts() { } build_package() { - local commit_id=$2 - local dist_dir=$3 + local version=$1 + local dist_dir=$2 log_message "Building AppImage" - if [ -n "$1" ]; then - local version="v$1" - else - local version="$commit_id" - fi - pushd "$APPIMAGE_DIR" ARCH="x86_64" linuxdeploy \ --appdir "$APPIMAGE_DIR/squashfs-root" \ @@ -139,3 +133,9 @@ build_package() { move_package_to_dist_dir() { mv Infection*Monkey*.AppImage "$1/$2" } + +cleanup() { + echo "Cleaning appimage build dirs" + + rm -rf "$APPIMAGE_DIR/squashfs-root" +} diff --git a/build_scripts/build_package.sh b/build_scripts/build_package.sh index 2074ef5ef..60de0d51b 100755 --- a/build_scripts/build_package.sh +++ b/build_scripts/build_package.sh @@ -105,6 +105,17 @@ install_build_prereqs() { install_nodejs } +format_version() { + local unformatted_version=$1 + local commit_id=$2 + + if [ -n "$unformatted_version" ]; then + echo "v$monkey_version" + else + echo "$commit_id" + fi +} + agent_binary_dir="" as_root=false branch="develop" @@ -209,7 +220,11 @@ fi setup_build_dir "$agent_binary_dir" "$monkey_repo" "$deployment_type" "$is_release_build" -build_package "$monkey_version" "$commit_id" "$DIST_DIR" +monkey_version=$(format_version "$monkey_version" "$commit_id") + +build_package "$monkey_version" "$DIST_DIR" + +cleanup "$monkey_version" log_message "Finished building package: $package" exit 0 diff --git a/build_scripts/docker/docker.sh b/build_scripts/docker/docker.sh index 97764570f..3aa4d1bee 100755 --- a/build_scripts/docker/docker.sh +++ b/build_scripts/docker/docker.sh @@ -1,4 +1,5 @@ DOCKER_DIR="$(realpath $(dirname $BASH_SOURCE[0]))" +DOCKER_IMAGE_NAME="guardicore/monkey-island" source "$DOCKER_DIR/../common.sh" @@ -37,28 +38,18 @@ copy_server_config_to_build_dir() { build_package() { local version=$1 - local commit_id=$2 - local dist_dir=$3 + local dist_dir=$2 pushd ./docker - if [ -n "$1" ]; then - version="v$version" - else - version="$commit_id" - fi - - docker_image_name="guardicore/monkey-island:$version" tar_name="$DOCKER_DIR/InfectionMonkey-docker-$version.tar" - build_docker_image_tar "$docker_image_name" "$tar_name" + build_docker_image_tar "$DOCKER_IMAGE_NAME:$version" "$tar_name" tgz_name="$DOCKER_DIR/InfectionMonkey-docker-$version.tgz" build_docker_image_tgz "$tar_name" "$tgz_name" move_package_to_dist_dir $tgz_name $dist_dir - clean_stale_images "$docker_image_name" - popd } @@ -78,9 +69,10 @@ move_package_to_dist_dir() { mv "$1" "$2/" } -clean_stale_images() { - echo "Cleaning images" +cleanup() { + local tag=$1 + echo "Cleaning docker images" - sudo docker rmi $1 + sudo docker rmi "$DOCKER_IMAGE_NAME:$tag" sudo docker image prune --force }