Created
February 25, 2024 08:03
-
-
Save shubham-kshetre/e0fb0f7cce120015bc6c7d814ae825fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
IMAGE_NAME="suyashbhawsar/alm-app" | |
CONTAINER_NAME="alm" | |
# Function to check if container is running | |
check_container_status() { | |
docker ps --filter "name=$CONTAINER_NAME" --format "{{.Status}}" | |
} | |
# Function to start container if not already running | |
start_container() { | |
echo "Starting container..." | |
docker run -d -p 8080:80 --name $CONTAINER_NAME $IMAGE_NAME | |
} | |
# Function to stop and remove container | |
stop_and_remove_container() { | |
echo "Stopping and removing container..." | |
docker stop $CONTAINER_NAME | |
docker rm $CONTAINER_NAME | |
} | |
# Function to pull image, remove old containers, and start new container | |
update_container() { | |
echo "Updating container..." | |
docker pull $IMAGE_NAME | |
stop_and_remove_container | |
start_container | |
} | |
# Function to handle different commands | |
handle_command() { | |
case "$1" in | |
"install-nginx") | |
docker exec -it $CONTAINER_NAME install_nginx | |
;; | |
"remove-nginx") | |
docker exec -it $CONTAINER_NAME remove_nginx | |
;; | |
"update-config") | |
docker exec -it $CONTAINER_NAME update_conf | |
;; | |
"build") | |
docker exec -it $CONTAINER_NAME mvn clean | |
docker exec -it $CONTAINER_NAME mvn package | |
docker exec -it $CONTAINER_NAME mvn install | |
;; | |
"run") | |
docker exec -it $CONTAINER_NAME java -jar target/strealer-cache-manager-1.0-SNAPSHOT.jar | |
;; | |
"help") | |
echo "Usage: alm [command]" | |
echo "Commands:" | |
echo " install-nginx - Install nginx" | |
echo " remove-nginx - Remove nginx" | |
echo " update-config - Update configuration" | |
echo " build - Build project" | |
echo " run - Run project" | |
;; | |
*) | |
echo "Invalid command. Use 'alm help' for usage instructions." | |
;; | |
esac | |
} | |
# Main script logic | |
if [ $# -eq 0 ]; then | |
echo "No command provided. Use 'alm help' for usage instructions." | |
exit 1 | |
fi | |
# Check if container is running | |
status=$(check_container_status) | |
if [[ "$status" != *"Up"* ]]; then | |
echo "Container is not running. Starting container..." | |
start_container | |
elif [[ "$status" == *"Exited"* ]]; then | |
echo "Container is stopped. Removing and starting container..." | |
stop_and_remove_container | |
start_container | |
fi | |
# Handle command | |
case "$1" in | |
"update") | |
update_container | |
;; | |
*) | |
handle_command "$1" | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment