Skip to content

Commit

Permalink
1.0.75 Update build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
webpwnized committed Nov 10, 2024
1 parent f0d31b0 commit 1fd1e47
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
40 changes: 25 additions & 15 deletions .tools/remove-all-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 19 additions & 40 deletions .tools/start-containers.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash
# Purpose: Start Docker containers defined in docker-compose.yml
# Usage: ./start-containers.sh [options] -f <compose-file>
# Description: This script is used to start and optionally initialize the containers.

# Function to print messages with a timestamp
print_message() {
Expand All @@ -27,45 +26,21 @@ show_help() {
echo " -l, --ldif-file <path> 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
Expand All @@ -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
Expand All @@ -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 ;;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.74
1.0.75

0 comments on commit 1fd1e47

Please sign in to comment.