Updated the update code login

This commit is contained in:
2025-03-19 00:31:01 +03:00
parent 40e9367b6f
commit 4c844bcee3

View File

@@ -11,6 +11,32 @@ REPO_URL="https://gitea.abdulhade.com/abdulhade/db-middleware.git"
EXECUTION_MESSAGE="NO-RETURN" EXECUTION_MESSAGE="NO-RETURN"
print_header() {
local HEADER_TEXT="$1"
local MAX_LENGTH=0
# Split the header text into lines and find the maximum line length
while IFS= read -r LINE; do
local LINE_LENGTH=${#LINE}
if (( LINE_LENGTH > MAX_LENGTH )); then
MAX_LENGTH=$LINE_LENGTH
fi
done <<< "$HEADER_TEXT"
local BORDER_LENGTH=$((MAX_LENGTH + 2)) # Add 4 for padding (2 spaces)
# Print the top border
printf '+%*s+\n' "$BORDER_LENGTH" "" | tr ' ' '-'
# Print each line of the header text
while IFS= read -r LINE; do
printf "| %-*s |\n" "$MAX_LENGTH" "$LINE"
done <<< "$HEADER_TEXT"
# Print the bottom border
printf '+%*s+\n' "$BORDER_LENGTH" "" | tr ' ' '-'
}
build_docker_image() { build_docker_image() {
echo "Building Docker image..." echo "Building Docker image..."
if docker build -t "$IMAGE_NAME" .; then if docker build -t "$IMAGE_NAME" .; then
@@ -28,7 +54,6 @@ test() {
exec_in_container() { exec_in_container() {
# Returns 0 if the command was successful # Returns 0 if the command was successful
echo 0
local CONTAINER_NAME=$CONTAINER_NAME local CONTAINER_NAME=$CONTAINER_NAME
local COMMAND="$1" local COMMAND="$1"
# Check if the container is running # Check if the container is running
@@ -36,17 +61,14 @@ exec_in_container() {
echo "Error: Container '$CONTAINER_NAME' is not running." echo "Error: Container '$CONTAINER_NAME' is not running."
return 1 return 1
fi fi
echo 1
# Execute the command in the container # Execute the command in the container
OUTPUT=$(docker exec "$CONTAINER_NAME" bash -c "$COMMAND" 2>&1) OUTPUT=$(docker exec "$CONTAINER_NAME" bash -c "$COMMAND" 2>&1)
echo 2
local EXIT_CODE=$? local EXIT_CODE=$?
echo 3
# Check if the command succeeded # Check if the command succeeded
if [[ $EXIT_CODE -eq 0 ]]; then if [[ $EXIT_CODE -eq 0 ]]; then
echo "$OUTPUT" # Return the output EXECUTION_MESSAGE="$OUTPUT" # Return the output
return 0 return 0
else else
echo "Error: Command failed in container '$CONTAINER_NAME' with exit code $EXIT_CODE." echo "Error: Command failed in container '$CONTAINER_NAME' with exit code $EXIT_CODE."
@@ -225,28 +247,43 @@ pull_clone_repo() {
LOCAL_HEAD=$(git rev-parse HEAD) LOCAL_HEAD=$(git rev-parse HEAD)
REMOTE_HEAD=$(git rev-parse origin/main) REMOTE_HEAD=$(git rev-parse origin/main)
exec_in_container "git rev-parse HEAD" exec_in_container "git rev-parse HEAD"
local COMMIT_HASH=$? local RETURN_CODE=$?
CONTAINER_HEAD=$EXECUTION_MESSAGE
if [[ $COMMIT_HASH -eq 0 ]]; then EXECUTION_MESSAGE="NO-RETURN"
echo "Commit hash in container: $COMMIT_HASH" echo
echo "- Commit hash in remote origin: $REMOTE_HEAD"
echo "- Commit hash in local repo: $LOCAL_HEAD"
if [[ $RETURN_CODE -eq 0 ]]; then
echo "- Commit hash in container: $CONTAINER_HEAD"
else else
echo "Failed to get commit hash from container." echo "Failed to get commit hash from container."
fi fi
# Compare the commit hashes
if [[ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]]; then echo
echo "Repository has new changes. Pulling latest changes..." if [[ "$CONTAINER_HEAD" == "$LOCAL_HEAD" && "$LOCAL_HEAD" == "$REMOTE_HEAD" ]]; then
echo "Repo is up to date."
UPDATED=-1
elif [[ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]]; then
echo "Remote repository has new changes. Pulling latest changes..."
if ! git pull "$REPO_URL"; then if ! git pull "$REPO_URL"; then
echo "Failed to pull changes." echo "Failed to pull changes."
return 1 # Error return 1 # Error
fi fi
echo "Repository updated successfully." echo "Local Repository updated successfully."
LOCAL_HEAD=$(git rev-parse HEAD)
UPDATED=0 # Changes were applied UPDATED=0 # Changes were applied
else else
echo "Repository is already up to date." echo "Local Repository is already up to date."
UPDATED=-1 # No changes UPDATED=-1 # No changes
# echo $UPDATED # echo $UPDATED
fi fi
if [[ "$LOCAL_HEAD" != "$CONTAINER_HEAD" ]]; then
echo "Container Repository is not up to date."
fi
else else
echo "Directory is not empty and does not contain a Git repository." 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 "Please ensure the directory is empty or contains a valid Git repository."
@@ -254,41 +291,39 @@ pull_clone_repo() {
fi fi
fi fi
echo "Repository setup completed successfully: $UPDATED." echo "Repository setup completed successfully."
return $UPDATED # -1 = No changes, 0 = Changes applied return $UPDATED # -1 = No changes, 0 = Changes applied
} }
update_code() { update_code() {
print_header "Checking for updates..."
echo echo
echo "+-------------------------+"
echo "| Checking for updates... |"
echo "+-------------------------+"
echo
pull_clone_repo pull_clone_repo
local UPDATED=$? local UPDATED=$?
echo
case $UPDATED in case $UPDATED in
-1) # 255 is -1, because Bash return codes are unsigned 8-bit integer, limited to the range 0 to 255.
echo "No changes detected." -1|255)
return -1 # No changes print_header "No changes detected."
;;
255) # 255 is -1, because Bash return codes are unsigned 8-bit integer, limited to the range 0 to 255.
echo "No changes detected."
return -1 # No changes return -1 # No changes
;; ;;
-0) -0)
echo "Changes were detected and applied." print_header "Changes were detected and applied to local repo.
Need to rebuild the container, run:
>>> db-middleware upgrade
"
return 0 # Changes applied return 0 # Changes applied
;; ;;
1) 1)
echo "Failed to update or clone repository." print_header "Failed to update or clone repository."
return 1 # Error return 1 # Error
;; ;;
*) *)
echo "Wrong return code: \`$UPDATED\` from pull_clone_repo." print_header "Wrong return code: \`$UPDATED\` from pull_clone_repo."
return 1 return 1
;; ;;
esac esac
@@ -382,7 +417,8 @@ stop() {
} }
usage() { usage() {
echo "Usage: $0 {install|upgrade|update_code|status|start|stop|show_config}" print_header "Usage:
>>> $0 {install|upgrade|update_code|status|start|stop|show_config}"
exit 1 exit 1
} }
@@ -413,3 +449,5 @@ main() {
# Run the script with the provided arguments # Run the script with the provided arguments
main "$@" main "$@"