Created an Update and Upgrade commands in the manager script

This commit is contained in:
2025-03-13 03:54:01 +03:00
parent c909d5b20a
commit e1af31c641
2 changed files with 192 additions and 25 deletions

View File

@@ -2,7 +2,11 @@
# Application settings
APP_NAME="Database Middleware"
APP_PORT=8080
API_PORT="8081"
# DatabaseS settings
DBS_PORTS=3306
# Docker Container settings
IMAGE_NAME="db-middleware"
CONTAINER_NAME="con-db-middleware"
# Databases settings
HAS_LOCAL_DBS=1

View File

@@ -2,13 +2,96 @@
APP_DIR="$HOME/.db-middleware"
CODE_DIR="$APP_DIR/code"
CONFIG_DIR="$APP_DIR/config"
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
@@ -16,11 +99,41 @@ load_config() {
}
show_config() {
if [[ $LOADED_CONFIG -eq 0 ]]; then
echo "Didn't load config"
fi
echo "Current Config:"
echo "CONTAINER_NAME: $CONTAINER_NAME"
echo "APP_PORT: $APP_PORT"
echo "DBS_PORTS: $DBS_PORTS"
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() {
@@ -32,7 +145,7 @@ install() {
cd "$CODE_DIR"
# docker build -t db-middleware .
build_docker_image
echo
echo "+----------------------------------------+"
@@ -42,13 +155,59 @@ install() {
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 db-middleware -p <port>:<port> -v /path/to/app/directory/"
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 "Running the 'upgrade' function."
# Add your upgrade logic here
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
@@ -90,31 +249,35 @@ main() {
case "$1" in
install)
install
;;
;;
update_code)
load_config
update_code
;;
upgrade)
load_config
load_config
upgrade
;;
;;
status)
load_config
load_config
status
;;
;;
start)
load_config
load_config
start
;;
;;
show_config)
load_config
show_config
load_config
show_config
;;
stop)
load_config
load_config
stop
;;
;;
*)
echo "Invalid argument: $1"
usage
;;
;;
esac
}