287 lines
6.9 KiB
Bash
Executable File
287 lines
6.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
APP_DIR="$HOME/.db-middleware"
|
|
CODE_DIR="$APP_DIR/code"
|
|
CONFIG_DIR="$APP_DIR/configs"
|
|
CONFIG_FILE="$CONFIG_DIR/app.conf"
|
|
LOADED_CONFIG=0
|
|
IMAGE_NAME="db-middleware"
|
|
CONTAINER_NAME="con-db-middleware"
|
|
REPO_URL="https://gitea.abdulhade.com/abdulhade/db-middleware.git"
|
|
|
|
build_docker_image() {
|
|
docker build -t $IMAGE_NAME .
|
|
}
|
|
|
|
set_up_scripts() {
|
|
# Copy scripts from the code directory to the app directory
|
|
cp "$CODE_DIR/scripts/"* "$APP_DIR/scripts/"
|
|
|
|
# Give execution permission to all scripts
|
|
chmod +x "$APP_DIR/scripts/"*
|
|
|
|
mkdir -p "$HOME/.local/bin"
|
|
ln -s "$APP_DIR/scripts/manager.sh" "$HOME/.local/bin/db-middleware"
|
|
export PATH="$HOME/.local/bin:$PATH"
|
|
source ~/.bashrc
|
|
}
|
|
|
|
pull_clone_repo() {
|
|
local UPDATED=0
|
|
|
|
# Check if the code directory exists
|
|
if [[ ! -d "$CODE_DIR" ]]; then
|
|
echo "Creating code directory at $CODE_DIR..."
|
|
fi
|
|
|
|
mkdir -p "$APP_DIR/{code,configs,scripts}"
|
|
|
|
# Navigate to the code directory
|
|
cd "$CODE_DIR" || { echo "Failed to navigate to $CODE_DIR"; exit 1; }
|
|
|
|
# Check if the directory is empty
|
|
if [[ -z "$(ls -A $CODE_DIR)" ]]; then
|
|
echo "Directory is empty. Cloning repository..."
|
|
git clone "$REPO_URL" .
|
|
echo "Repository cloned successfully."
|
|
UPDATED=1 # Newly cloned, so changes are "new"
|
|
else
|
|
# Check if the directory contains a Git repository
|
|
if [[ -d ".git" ]]; then
|
|
echo "Directory contains a Git repository."
|
|
|
|
# Fetch the latest changes from the remote repository
|
|
git fetch origin
|
|
|
|
# Get the local and remote HEAD commit hashes
|
|
LOCAL_HEAD=$(git rev-parse HEAD)
|
|
REMOTE_HEAD=$(git rev-parse origin/main) # Replace 'main' with your branch name
|
|
|
|
# Compare the commit hashes
|
|
if [[ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]]; then
|
|
echo "Repository has new changes. Pulling latest changes..."
|
|
git pull "$REPO_URL"
|
|
echo "Repository updated successfully."
|
|
UPDATED=1 # Changes were pulled
|
|
else
|
|
echo "Repository is already up to date."
|
|
UPDATED=0 # No changes
|
|
fi
|
|
else
|
|
echo "Directory is not empty and does not contain a Git repository."
|
|
echo "Please ensure the directory is empty or contains a valid Git repository."
|
|
echo " > $APP_DIR"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Check if the operation was successful
|
|
if [[ $? -eq 0 ]]; then
|
|
echo "Repository setup completed successfully."
|
|
else
|
|
echo "Failed to set up the repository."
|
|
exit 1
|
|
fi
|
|
|
|
# Return the UPDATED value
|
|
return $UPDATED
|
|
}
|
|
|
|
# Function to load the config file
|
|
load_config() {
|
|
if [[ -f "$CONFIG_FILE" ]]; then
|
|
source "$CONFIG_FILE"
|
|
LOADED_CONFIG=1
|
|
else
|
|
echo "Config file not found: $CONFIG_FILE"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
show_config() {
|
|
|
|
if [[ $LOADED_CONFIG -eq 0 ]]; then
|
|
echo "Didn't load config"
|
|
fi
|
|
|
|
echo "Current Config:"
|
|
echo "CONTAINER_NAME: $CONTAINER_NAME"
|
|
echo "API_PORT: $API_PORT"
|
|
echo "HAS_LOCAL_DBS: $HAS_LOCAL_DBS"
|
|
|
|
|
|
local RUN_COMMAND="docker run --name $CONTAINER_NAME"
|
|
if [[ $HAS_LOCAL_DBS -eq 1 ]]; then
|
|
RUN_COMMAND+=" --network host"
|
|
fi
|
|
RUN_COMMAND+=" -p $API_PORT:8080 $IMAGE_NAME"
|
|
echo "$RUN_COMMAND"
|
|
|
|
|
|
}
|
|
|
|
convert_ports_to_docker_args() {
|
|
local ports="$1"
|
|
local docker_args=""
|
|
|
|
# Split the ports by comma and trim whitespace
|
|
IFS=',' read -r -a port_array <<< "$ports"
|
|
|
|
# Loop through the ports and format them as Docker arguments
|
|
for port in "${port_array[@]}"; do
|
|
port=$(echo "$port" | xargs) # Trim whitespace
|
|
docker_args+=" -p $port:$port"
|
|
done
|
|
|
|
echo "$docker_args"
|
|
}
|
|
|
|
install() {
|
|
echo
|
|
echo "+------------------------------+"
|
|
echo "| Installing the Middleware... |"
|
|
echo "+------------------------------+"
|
|
echo
|
|
|
|
cd "$CODE_DIR"
|
|
|
|
build_docker_image
|
|
|
|
echo
|
|
echo "+----------------------------------------+"
|
|
echo "| Installed the Middleware Successfully! |"
|
|
echo "+----------------------------------------+"
|
|
echo
|
|
echo "- You can run the middleware simply using the manager:"
|
|
echo " >>> db-middleware start"
|
|
echo "- Or directly by running the docker container:"
|
|
echo " >>> docker run $IMAGE_NAME -p <port>:<port> -v /path/to/app/directory/"
|
|
}
|
|
|
|
update_code() {
|
|
pull_clone_repo
|
|
local UPDATED=$?
|
|
|
|
if [[ $UPDATED -eq 1 ]]; then
|
|
echo "Changes were detected and applied."
|
|
else
|
|
echo "No changes detected."
|
|
fi
|
|
|
|
return $UPDATED
|
|
}
|
|
|
|
# Function for the "upgrade" command
|
|
upgrade() {
|
|
echo
|
|
echo "+-----------------------------+"
|
|
echo "| Upgrading the Middleware... |"
|
|
echo "+-----------------------------+"
|
|
echo
|
|
|
|
update_code
|
|
local UPDATED=$?
|
|
|
|
if [[ $UPDATED -eq 1 ]]; then
|
|
echo
|
|
echo "+--------------------------------+"
|
|
echo "| Rebuilding the Docker Image... |"
|
|
echo "+--------------------------------+"
|
|
echo
|
|
|
|
build_docker_image
|
|
|
|
set_up_scripts
|
|
|
|
echo
|
|
echo "+---------------------------------------+"
|
|
echo "| Upgraded the Middleware Successfully! |"
|
|
echo "+---------------------------------------+"
|
|
echo
|
|
|
|
else
|
|
echo
|
|
echo "+-------------------------------------+"
|
|
echo "| No need to rebuild the Docker Image |"
|
|
echo "+-------------------------------------+"
|
|
echo
|
|
fi
|
|
|
|
|
|
}
|
|
|
|
# Function for the "status" command
|
|
status() {
|
|
echo "Running the 'status' function."
|
|
docker
|
|
# Add your status-checking logic here
|
|
}
|
|
|
|
# Function for the "start" command
|
|
start() {
|
|
echo "+---------------------------+"
|
|
echo "| Starting the Container... |"
|
|
echo "+---------------------------+"
|
|
echo
|
|
docker run db-middleware
|
|
}
|
|
|
|
# Function for the "stop" command
|
|
stop() {
|
|
echo "Running the 'stop' function."
|
|
# Add your stop logic here
|
|
}
|
|
|
|
# Function to display usage instructions
|
|
usage() {
|
|
echo "Usage: $0 {install|upgrade|status|start|stop}"
|
|
exit 1
|
|
}
|
|
|
|
# Main script logic
|
|
main() {
|
|
# Check if an argument is provided
|
|
if [[ $# -eq 0 ]]; then
|
|
usage
|
|
fi
|
|
|
|
# Handle the argument
|
|
case "$1" in
|
|
install)
|
|
install
|
|
;;
|
|
update_code)
|
|
load_config
|
|
update_code
|
|
;;
|
|
upgrade)
|
|
load_config
|
|
upgrade
|
|
;;
|
|
status)
|
|
load_config
|
|
status
|
|
;;
|
|
start)
|
|
load_config
|
|
start
|
|
;;
|
|
show_config)
|
|
load_config
|
|
show_config
|
|
;;
|
|
stop)
|
|
load_config
|
|
stop
|
|
;;
|
|
*)
|
|
echo "Invalid argument: $1"
|
|
usage
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Run the script with the provided arguments
|
|
|
|
main "$@"
|