Skript test_deploy.sh

Automaticky generovaná dokumentace skriptu scripts/test_deploy.sh.

  1#!/bin/bash
  2###########
  3#### SCRIPT: DEPLOYMENT from local repository into SWARM for testing
  4#### PARAMS: see -h
  5###########
  6#USAGE: deployment based on local repository
  7
  8ask_continue () {
  9    while true; do
 10        
 11        read -p "$1 (y or n): " input        
 12        case $input in
 13            [yY]*)
 14                echo '---Continuing!'
 15                break
 16                ;;
 17            [nN]*)
 18                echo '***So do THAT, exiting!'
 19                exit 1
 20                ;;
 21             *)
 22                echo '***Invalid input' >&2
 23        esac
 24    done
 25}
 26
 27echo_dec ()
 28{
 29    echo "---------------------------------------"
 30    echo "--> $1"
 31    echo "---------------------------------------"
 32}
 33
 34
 35er () {
 36    echo_dec "${1}"
 37    eval "${1}"
 38    ret_val=$?
 39    echo "....................."
 40    echo ">> DONE: ${1} with status: ${ret_val}"
 41    return $ret_val
 42}
 43
 44check_create_network () {
 45    
 46    docker network inspect ${network_name} > /dev/null 2>&1
 47    network_status=$?
 48    if [ ${network_status} -eq 1  ]; then
 49      er "docker network create -d overlay --attachable ${network_name}"
 50      echo_dec "Network ${network_name} created"
 51    else
 52       echo_dec "Network ${network_name} already exists"
 53    fi
 54}
 55
 56check_stack_exists ()
 57{
 58    docker stack ps ${stack_name} > /dev/null 2>&1
 59    stack_status=$?
 60    return ${stack_status}
 61}
 62
 63actualize_repo ()
 64{
 65
 66    valid_ans=false
 67
 68    while [ "$valid_ans" = false ]; do
 69        
 70        echo "Do you want to update the local repository to origin/main first before creating a docker image? (y/n)"
 71        read ans
 72
 73        if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
 74            echo "Update repository..."
 75            git fetch origin
 76            git clean -fd
 77            git restore .
 78            git checkout -B main origin/main || exit 1
 79            git reset --hard origin/main
 80            valid_ans=true
 81        elif [ "$ans" = "n" ] || [ "$ans" = "N" ]; then
 82            echo "Continue without update."
 83            valid_ans=true
 84        else
 85            echo "Invalid answer, enter 'y' for yes or 'n' for no."
 86        fi
 87    done    
 88}
 89
 90run_default ()
 91{  
 92    
 93   in_args="${1}"
 94   echo_dec "${msg_default_case}"
 95
 96   if [[ "$quiet_mode" != "yes" ]]; then
 97       actualize_repo
 98   fi
 99   
100   # Check network exists
101   check_create_network
102
103   #Update images
104   echo_dec "create image"
105   er "${cmd_create_images}"
106   er "${cmd_create_images_proxy}"
107   er "${cmd_create_images_redis}"
108
109   
110    er "${cmd_deploy_base} ${compose_test} ${stack_name}" && \
111    echo_dec "$msg_success" || echo_dec "${msg_fail_build}"
112    
113
114}
115
116Help ()
117{
118    cat <<EOF
119    !!!MUST BE RUN from REPOSIOTRY root like =>
120    usage: ./scrips/${script_name} [-x|b|h], 
121    ---
122       PURPOSE: manage deployment/run of production docker images build from GIT repository for AIS CR project in SWARM mode
123    ----
124    DEFAULT CASE: 
125      #just call the script without any args => deploy and run all services.
126      $./scripts/${script_name}
127
128    Examples on options:
129    1) Remove complete docker stack, i.e all services
130    
131         $./scripts/${script_name} -x  
132             
133    ---
134    2) Deploy or redeploy all services in swarm mode
135       
136       $./scripts/${script_name} -b   
137
138
139    -----
140    Summnary:
141    -h help
142    -x remove docker stack (all services)
143    -b (re)deploy all services in swarm mode (DEFAULT CASE)
144    -q quiet mode
145  
146EOF
147}
148
149script_name=$(basename ${0})
150passed_args="$@"
151
152
153#INPUTS
154
155stack_name="swarm_webamcr"
156network_name="prod-net" #MUST MATCH WITH COMPOSE FILES!!!
157
158compose_test="docker-compose-test.yml"
159
160msg_fail_build="!! DEPLOYMENT not successfull"
161
162
163msg_success="DEPLOYED in SWARM MODE ---> APPLICATION ACCESSIBLE on: port 8080"
164msg_default_case="DISCLAIMER: DEFAULT CASE will RE-DEPLOY ALL services again."
165
166#TRANSLATION backups path
167tr_path="$HOME/translations_backup"
168mkdir -p ${tr_path}
169
170#LOGGING
171log_dir="logs/test_deploy"
172start_time=$(date +%Y%m%dT%H%M%S)
173log_file="${start_time}_test-deployment_${passed_args}.log"
174mkdir -p ${log_dir}
175
176OPTIND=1
177
178#REDIRECT to log
179exec > >(tee "${log_dir}/${log_file}" )
180#exec 2>&1
181
182echo_dec "# DEPLOYMENT in SWARM MODE (i.e. host has to be joined or initiated as SWARM NODE) @${start_time}"
183
184#CHECK SWARM ACTIVATED
185docker node ls
186status_swarm=$?
187
188if [ $status_swarm -ne 0 ]; then
189    echo_dec "SWARM not initiated or joined at this HOST. Exiting"
190    exit 1
191fi
192
193#Build commands
194cmd_stack_rm="docker stack rm ${stack_name}"
195cmd_create_images="docker build -t test_prod -f Dockerfile --build-arg VERSION_APP=\"$(git rev-parse --short HEAD | head -c 8)\" --build-arg TAG_APP=local_build  ."
196cmd_create_images_proxy="docker build -t test_proxy -f proxy/Dockerfile --build-arg VERSION_APP="$(git rev-parse --short HEAD | head -c 8)" --build-arg TAG_APP=local_build  ./proxy"
197cmd_create_images_redis="docker build -t test_redis -f redis/Dockerfile --build-arg VERSION_APP="$(git rev-parse --short HEAD | head -c 8)" --build-arg TAG_APP=local_build  ./redis"
198cmd_deploy_base="docker stack deploy --compose-file"
199
200#Cleaning old images
201echo "Pruning unused Docker images..."
202docker image prune -f
203
204while getopts "hxqbut:d" option; do
205   option_passed="yes"
206   case ${option} in
207      h) # display Help
208         echo "OPTION: -h"
209         Help
210         exit;;
211      x) echo "OPTION: -x" 
212         echo_dec "Remove docker stack: ${stack_name}"
213         if check_stack_exists; then
214            er "${cmd_stack_rm}" && \
215            echo_dec "Stack ${stack_name} removal successful" || echo_dec "Stack ${stack_name} removal FAILED"
216         else
217             echo_dec "STACK ${stack_name} doesn't exist so can't be removed!!!"
218        fi
219        sleep 20 # Need to wait before network is really removed.
220         ;;
221      b) #deploy stack 
222        echo "OPTION: -b"
223        run_default b
224        ;;
225      q)
226         echo "OPTION: -q"
227         quiet_mode="yes"
228         run_default
229         ;;
230     \?) # Invalid option
231         echo_dec "OPTION: INVALID"
232         echo "Error: Invalid option ${option}"
233         exit;;
234   esac
235done
236
237# RUN docker compose [default option]
238if [ -z ${option_passed+x} ]; then
239   echo_dec "NO option passed as ARG so default case is called,"
240   run_default
241fi
242
243exit 0