Skip to content

Commit

Permalink
1.0.67 Improve start container script
Browse files Browse the repository at this point in the history
  • Loading branch information
webpwnized committed Aug 15, 2024
1 parent a4ced6d commit ec13287
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions .tools/start-containers.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/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 rebuild the containers.
# The script must be run from the mutillidae-docker directory.
# Description: This script is used to start and optionally initialize the containers.

# Function to print messages with a timestamp
print_message() {
Expand All @@ -22,44 +21,55 @@ show_help() {
echo ""
echo "Options:"
echo " -f, --compose-file <path> Specify the path to the docker-compose.yml file (required)."
echo " -i, --initialize-containers Initialize the containers after starting them."
echo " -r, --rebuild-containers Rebuild the containers before starting them."
echo " -u, --unattended Run the script unattended without waiting for user input."
echo " -l, --ldif-file <path> Specify the path to the LDIF file (required with --rebuild-containers)."
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 rebuild the containers."
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. Rebuild the Docker containers."
echo " 1. Remove all existing containers and container images."
echo " 2. Start the Docker containers."
echo " 3. Wait for the database to start."
echo " 4. Request the database to be built."
echo " 5. Upload the specified LDIF file to the LDAP directory server."
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 rebuilding:"
echo " $0 -f .build/docker-compose.yml"
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 ""
echo " Rebuild containers and start them:"
echo " $0 -f .build/docker-compose.yml --rebuild-containers --ldif-file .build/ldap/configuration/ldif/mutillidae.ldif"
echo " $0 --compose-file .build/docker-compose.yml --rebuild-containers"
echo ""
echo " Run script unattended:"
echo " $0 -f .build/docker-compose.yml --unattended"
echo " $0 --compose-file .build/docker-compose.yml --unattended"
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 ""
echo " Run script with rebuilding and unattended:"
echo " $0 -f .build/docker-compose.yml --rebuild-containers --ldif-file .build/ldap/configuration/ldif/mutillidae.ldif --unattended"
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
}

# Parse options
INITIALIZE_CONTAINERS=false
REBUILD_CONTAINERS=false
UNATTENDED=false
LDIF_FILE=""
Expand All @@ -68,6 +78,7 @@ COMPOSE_FILE=""
# Loop through the command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-i|--initialize-containers) INITIALIZE_CONTAINERS=true ;;
-r|--rebuild-containers) REBUILD_CONTAINERS=true ;;
-u|--unattended) UNATTENDED=true ;;
-l|--ldif-file)
Expand Down Expand Up @@ -103,15 +114,15 @@ if [[ -z "$COMPOSE_FILE" ]]; then
exit 1
fi

# If rebuilding is required, ensure the LDIF file is provided
if [[ "$REBUILD_CONTAINERS" = true && -z "$LDIF_FILE" ]]; then
print_message "Error: The --ldif-file option is required when using --rebuild-containers."
# If initialization is required, ensure the LDIF file is provided
if [[ "$INITIALIZE_CONTAINERS" = true && -z "$LDIF_FILE" ]]; then
print_message "Error: The --ldif-file option is required when using --initialize-containers."
show_help
exit 1
fi

# Check if ldapadd is installed
if [[ "$REBUILD_CONTAINERS" = true ]]; then
if [[ "$INITIALIZE_CONTAINERS" = true ]]; then
if ! command -v ldapadd &> /dev/null; then
handle_error "ldapadd is not installed. Please install ldap-utils."
fi
Expand All @@ -120,15 +131,15 @@ fi
# Remove all current containers and container images if specified
if [[ "$REBUILD_CONTAINERS" = true ]]; then
print_message "Removing all existing containers and container images"
docker compose -f "$COMPOSE_FILE" down --rmi all -v || handle_error "Failed to remove existing containers and images"
docker compose --file "$COMPOSE_FILE" down --rmi all -v || handle_error "Failed to remove existing containers and images"
fi

# Start Docker containers
print_message "Starting containers"
docker compose -f "$COMPOSE_FILE" up -d || handle_error "Failed to start Docker containers"
docker compose --file "$COMPOSE_FILE" up --detach || handle_error "Failed to start Docker containers"

# Check if containers need to be rebuilt
if [[ "$REBUILD_CONTAINERS" = true ]]; then
# Check if containers need to be initialized
if [[ "$INITIALIZE_CONTAINERS" = true ]]; then
# Wait for the database container to start
print_message "Waiting for database to start"
sleep 10
Expand All @@ -144,7 +155,7 @@ if [[ "$REBUILD_CONTAINERS" = true ]]; then
if [[ $status -eq 0 ]]; then
print_message "LDAP entries added successfully."
elif [[ $status -eq 68 ]]; then
print_message "LDAP entry already exists."
print_message "At least one of the LDAP entry already exists, but others added where possible."
else
handle_error "LDAP add operation failed with status: $status"
fi
Expand Down

0 comments on commit ec13287

Please sign in to comment.