Updated the update code login
This commit is contained in:
@@ -11,6 +11,32 @@ REPO_URL="https://gitea.abdulhade.com/abdulhade/db-middleware.git"
|
||||
|
||||
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() {
|
||||
echo "Building Docker image..."
|
||||
if docker build -t "$IMAGE_NAME" .; then
|
||||
@@ -28,7 +54,6 @@ test() {
|
||||
|
||||
exec_in_container() {
|
||||
# Returns 0 if the command was successful
|
||||
echo 0
|
||||
local CONTAINER_NAME=$CONTAINER_NAME
|
||||
local COMMAND="$1"
|
||||
# Check if the container is running
|
||||
@@ -36,17 +61,14 @@ exec_in_container() {
|
||||
echo "Error: Container '$CONTAINER_NAME' is not running."
|
||||
return 1
|
||||
fi
|
||||
echo 1
|
||||
|
||||
|
||||
# Execute the command in the container
|
||||
OUTPUT=$(docker exec "$CONTAINER_NAME" bash -c "$COMMAND" 2>&1)
|
||||
echo 2
|
||||
local EXIT_CODE=$?
|
||||
echo 3
|
||||
|
||||
# Check if the command succeeded
|
||||
if [[ $EXIT_CODE -eq 0 ]]; then
|
||||
echo "$OUTPUT" # Return the output
|
||||
EXECUTION_MESSAGE="$OUTPUT" # Return the output
|
||||
return 0
|
||||
else
|
||||
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)
|
||||
REMOTE_HEAD=$(git rev-parse origin/main)
|
||||
exec_in_container "git rev-parse HEAD"
|
||||
local COMMIT_HASH=$?
|
||||
|
||||
if [[ $COMMIT_HASH -eq 0 ]]; then
|
||||
echo "Commit hash in container: $COMMIT_HASH"
|
||||
local RETURN_CODE=$?
|
||||
CONTAINER_HEAD=$EXECUTION_MESSAGE
|
||||
EXECUTION_MESSAGE="NO-RETURN"
|
||||
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
|
||||
echo "Failed to get commit hash from container."
|
||||
fi
|
||||
|
||||
# Compare the commit hashes
|
||||
if [[ "$LOCAL_HEAD" != "$REMOTE_HEAD" ]]; then
|
||||
echo "Repository has new changes. Pulling latest changes..."
|
||||
|
||||
echo
|
||||
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
|
||||
echo "Failed to pull changes."
|
||||
return 1 # Error
|
||||
fi
|
||||
echo "Repository updated successfully."
|
||||
echo "Local Repository updated successfully."
|
||||
LOCAL_HEAD=$(git rev-parse HEAD)
|
||||
UPDATED=0 # Changes were applied
|
||||
else
|
||||
echo "Repository is already up to date."
|
||||
echo "Local Repository is already up to date."
|
||||
UPDATED=-1 # No changes
|
||||
# echo $UPDATED
|
||||
fi
|
||||
|
||||
if [[ "$LOCAL_HEAD" != "$CONTAINER_HEAD" ]]; then
|
||||
echo "Container Repository is not up to date."
|
||||
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."
|
||||
@@ -254,41 +291,39 @@ pull_clone_repo() {
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Repository setup completed successfully: $UPDATED."
|
||||
echo "Repository setup completed successfully."
|
||||
|
||||
return $UPDATED # -1 = No changes, 0 = Changes applied
|
||||
}
|
||||
|
||||
update_code() {
|
||||
print_header "Checking for updates..."
|
||||
|
||||
echo
|
||||
echo "+-------------------------+"
|
||||
echo "| Checking for updates... |"
|
||||
echo "+-------------------------+"
|
||||
echo
|
||||
|
||||
pull_clone_repo
|
||||
local UPDATED=$?
|
||||
echo
|
||||
|
||||
case $UPDATED in
|
||||
|
||||
-1)
|
||||
echo "No changes detected."
|
||||
return -1 # No changes
|
||||
;;
|
||||
255) # 255 is -1, because Bash return codes are unsigned 8-bit integer, limited to the range 0 to 255.
|
||||
echo "No changes detected."
|
||||
# 255 is -1, because Bash return codes are unsigned 8-bit integer, limited to the range 0 to 255.
|
||||
-1|255)
|
||||
print_header "No changes detected."
|
||||
return -1 # No changes
|
||||
;;
|
||||
-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
|
||||
;;
|
||||
1)
|
||||
echo "Failed to update or clone repository."
|
||||
print_header "Failed to update or clone repository."
|
||||
return 1 # Error
|
||||
;;
|
||||
*)
|
||||
echo "Wrong return code: \`$UPDATED\` from pull_clone_repo."
|
||||
print_header "Wrong return code: \`$UPDATED\` from pull_clone_repo."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
@@ -382,7 +417,8 @@ stop() {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -413,3 +449,5 @@ main() {
|
||||
# Run the script with the provided arguments
|
||||
|
||||
main "$@"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user