From 1fd1e47ac99a3a25df9ed656fb39efad4d8d9646 Mon Sep 17 00:00:00 2001 From: webpwnized Date: Sun, 10 Nov 2024 17:27:19 -0500 Subject: [PATCH] 1.0.75 Update build scripts --- .tools/remove-all-images.sh | 40 +++++++++++++++---------- .tools/start-containers.sh | 59 ++++++++++++------------------------- version | 2 +- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/.tools/remove-all-images.sh b/.tools/remove-all-images.sh index ddbb849..b183422 100755 --- a/.tools/remove-all-images.sh +++ b/.tools/remove-all-images.sh @@ -38,29 +38,39 @@ done # Check if Docker is installed and running if ! command -v docker &> /dev/null; then handle_error "Docker is not installed or not in PATH. Please install Docker." + exit 1 fi # Clean up Docker resources print_message "Cleaning up Docker resources for Mutillidae" -# Stop and remove all containers -print_message "Stopping and removing all containers" -docker stop $(docker ps -a -q) || handle_error "Failed to stop containers" -docker rm $(docker ps -a -q) || handle_error "Failed to remove containers" +# Stop and remove all containers, with checks to ensure there are containers to stop +CONTAINERS=$(docker ps -a -q) +if [[ -n "$CONTAINERS" ]]; then + print_message "Stopping and removing all containers" + docker rm -f $CONTAINERS || handle_error "Failed to remove containers" +else + print_message "No containers to remove" +fi -# Remove all images -print_message "Removing all images" -docker rmi $(docker images -a -q) || handle_error "Failed to remove images" +# Remove all images, with a check to ensure there are images to remove +IMAGES=$(docker images -a -q) +if [[ -n "$IMAGES" ]]; then + print_message "Removing all images" + docker rmi -f $IMAGES || handle_error "Failed to remove images" +else + print_message "No images to remove" +fi -# Prune containers, images, volumes, networks -print_message "Pruning containers, images, volumes, networks" -docker container prune -f || handle_error "Failed to prune containers" -docker image prune --all -f || handle_error "Failed to prune images" -docker volume prune -f || handle_error "Failed to prune volumes" -docker network prune -f || handle_error "Failed to prune networks" +# Prune containers, images, volumes, and networks with error handling +print_message "Pruning containers, images, volumes, and networks" +docker container prune -f || true +docker image prune --all -f || true +docker volume prune -f || true +docker network prune -f || true -# System-wide prune -print_message "Pruning system" +# System-wide prune to ensure complete cleanup +print_message "Performing system-wide prune" docker system prune --all --volumes -f || handle_error "Failed to prune system" # Success message diff --git a/.tools/start-containers.sh b/.tools/start-containers.sh index 3ae70e6..c1b8689 100755 --- a/.tools/start-containers.sh +++ b/.tools/start-containers.sh @@ -1,7 +1,6 @@ #!/bin/bash # Purpose: Start Docker containers defined in docker-compose.yml # Usage: ./start-containers.sh [options] -f -# Description: This script is used to start and optionally initialize the containers. # Function to print messages with a timestamp print_message() { @@ -27,45 +26,21 @@ show_help() { echo " -l, --ldif-file Specify the path to the LDIF file (required with --initialize-containers)." echo " -h, --help Display this help message." echo "" - echo "Description:" - echo "This script is used to start and optionally initialize the containers." - echo "The script must be run from the mutillidae-docker directory." - echo "" - echo "When run without options, the script starts the Docker containers defined in" - echo "the specified docker-compose.yml and waits for user input before clearing the screen." - echo "" - echo "When the --initialize-containers option is provided, the script will:" - echo " 1. Start the Docker containers." - echo " 2. Wait for the database to start." - echo " 3. Request the database to be built." - echo " 4. Upload the specified LDIF file to the LDAP directory server." - echo "" - echo "When the --rebuild-containers option is provided, the script will:" - echo " 1. Remove all existing containers and container images." - echo " 2. Start the Docker containers." - echo "" - echo "When the --unattended option is provided, the script will not wait for user" - echo "input and will not clear the screen after execution." - echo "" echo "Examples:" - echo " Start containers without initialization:" - echo " $0 --compose-file .build/docker-compose.yml" - echo "" - echo " Start containers and initialize them:" - echo " $0 --compose-file .build/docker-compose.yml --initialize-containers --ldif-file .build/ldap/configuration/ldif/mutillidae.ldif" + echo " 1. Start containers without initialization:" + echo " ./.tools/start-containers.sh --compose-file ./.build/docker-compose.yml" echo "" - echo " Rebuild containers and start them:" - echo " $0 --compose-file .build/docker-compose.yml --rebuild-containers" + echo " 2. Rebuild containers and start them:" + echo " ./.tools/start-containers.sh --compose-file ./.build/docker-compose.yml --rebuild-containers" echo "" - echo " Run script unattended:" - echo " $0 --compose-file .build/docker-compose.yml --unattended" + echo " 3. Start and initialize containers with an LDIF file:" + echo " ./.tools/start-containers.sh --compose-file ./.build/docker-compose.yml --initialize-containers --ldif-file ./.build/ldap/configuration/ldif/mutillidae.ldif" echo "" - echo " Run script with initialization and unattended:" - echo " $0 --compose-file .build/docker-compose.yml --initialize-containers --ldif-file .build/ldap/configuration/ldif/mutillidae.ldif --unattended" + echo " 4. Run script unattended with initialization and rebuild:" + echo " ./.tools/start-containers.sh --compose-file ./.build/docker-compose.yml --initialize-containers --ldif-file ./.build/ldap/configuration/ldif/mutillidae.ldif --rebuild-containers --unattended" echo "" - echo " Run script with all available arguments:" - echo " $0 --compose-file .build/docker-compose.yml --rebuild-containers --initialize-containers --ldif-file .build/ldap/configuration/ldif/mutillidae.ldif --unattended" - exit 0 + echo " 5. Run script with help option:" + echo " ./.tools/start-containers.sh --help" } # Parse options @@ -81,7 +56,7 @@ while [[ "$#" -gt 0 ]]; do -i|--initialize-containers) INITIALIZE_CONTAINERS=true ;; -r|--rebuild-containers) REBUILD_CONTAINERS=true ;; -u|--unattended) UNATTENDED=true ;; - -l|--ldif-file) + -l|--ldif-file) if [[ -n "$2" && ! "$2" =~ ^- ]]; then LDIF_FILE="$2" shift @@ -99,7 +74,7 @@ while [[ "$#" -gt 0 ]]; do show_help exit 1 fi ;; - -h|--help) show_help ;; + -h|--help) show_help; exit 0 ;; *) print_message "Unknown parameter passed: $1" show_help exit 1 ;; @@ -134,9 +109,13 @@ if [[ "$REBUILD_CONTAINERS" = true ]]; then docker compose --file "$COMPOSE_FILE" down --rmi all -v || handle_error "Failed to remove existing containers and images" fi -# Start Docker containers +# Start Docker containers, forcing a rebuild if required print_message "Starting containers" -docker compose --file "$COMPOSE_FILE" up --detach || handle_error "Failed to start Docker containers" +if [[ "$REBUILD_CONTAINERS" = true ]]; then + docker compose --file "$COMPOSE_FILE" up --detach --build --force-recreate || handle_error "Failed to start Docker containers" +else + docker compose --file "$COMPOSE_FILE" up --detach || handle_error "Failed to start Docker containers" +fi # Check if containers need to be initialized if [[ "$INITIALIZE_CONTAINERS" = true ]]; then @@ -155,7 +134,7 @@ if [[ "$INITIALIZE_CONTAINERS" = true ]]; then if [[ $status -eq 0 ]]; then print_message "LDAP entries added successfully." elif [[ $status -eq 68 ]]; then - print_message "At least one of the LDAP entry already exists, but others added where possible." + print_message "At least one of the LDAP entries already exists, but others were added where possible." else handle_error "LDAP add operation failed with status: $status" fi diff --git a/version b/version index ea2f1d3..e9acec7 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.0.74 +1.0.75