migration_utilities 3.76 KB
Newer Older
Emanuel Calvo's avatar
Emanuel Calvo committed
1 2 3 4
#!/bin/bash

function do_step(){
  echo "Executing: $@"
Matteo Melli's avatar
Matteo Melli committed
5 6 7 8 9 10 11 12
  echo
  if "$@"
  then
    echo
    echo "==================== Step $@ ran OK ===================="
  else
    echo
    >&2 echo "!!!!!!!!!!!!!!!!!!!!! Step $@ Failed !!!!!!!!!!!!!!!!!!!!!"
13
    return 1
Matteo Melli's avatar
Matteo Melli committed
14
  fi
Emanuel Calvo's avatar
Emanuel Calvo committed
15 16 17

}

18 19 20 21
function step_type(){
  echo "$1"|cut -d _ -f 1|cut -d : -f 1
}

22
function step_3digit_number(){
23
  printf "%03d" "$(step_number "$1")"
24 25 26
}

function step_number(){
27 28 29 30 31 32 33
  for number in "${!steps[@]}"
  do
   if [ "${steps[$number]}" = "$1" ]
   then
       echo "$number"
   fi
  done
34 35
}

Matteo Melli's avatar
Matteo Melli committed
36
function step_script(){
37
  echo "$1"|cut -d : -f 2-
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}

function has_step(){
  found=false
  for step in "${steps[@]}"
  do
    if [ "$(step_3digit_number "$step")" == "$1" ]
    then
      found=true
      break;
    fi
  done
  $found
}

53 54 55 56 57 58 59 60 61 62 63
function step_check(){
  script_type="$(step_type "$1")"
  script="$(step_script "$1")"
  if [ "$script_type" == "function" ]
  then
    type "$script" > /dev/null 2>&1
  else
    [ -x "$script" ]
  fi
}

Matteo Melli's avatar
Matteo Melli committed
64 65 66 67 68 69 70 71
function next_step(){
  printf "%03d" "$(($(step_number "$1")+1))"
}

function prepend_timestamp(){
  while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done
}

Emanuel Calvo's avatar
Emanuel Calvo committed
72
function do_menu(){
73 74
  next_step="$(step_3digit_number "${steps[0]}")"
  while true
Emanuel Calvo's avatar
Emanuel Calvo committed
75
  do
Matteo Melli's avatar
Matteo Melli committed
76 77 78 79 80 81 82 83
    echo "Environment:"
    echo "Azure master: $AZURE_MASTER"
    echo "Azure slaves: ${AZURE_SLAVES[*]}"
    echo "Azure pgbouncers: ${AZURE_PGBOUNCERS[*]}"
    echo "GCP master candidate: $GCP_MASTER_CANDIDATE"
    echo "GCP slaves: ${GCP_SLAVES[*]}"
    echo "GCP pgbouncers: ${GCP_PGBOUNCERS[*]}"
    echo
84
    echo "Available steps:"
Matteo Melli's avatar
Matteo Melli committed
85
    echo
86 87
    for step in "${steps[@]}"
    do
Matteo Melli's avatar
Matteo Melli committed
88
      echo "$(step_3digit_number "$step")) $(step_script "$step")"
89
    done
Matteo Melli's avatar
Matteo Melli committed
90
    echo
91 92
    if has_step "$next_step"
    then
93
      read_prompt="Enter the step number to execute, next to execute step $next_step or quit to exit? "
94
    else
95 96 97 98
      read_prompt="Enter the step number to execute or quit to exit? "
    fi
    if which rlwrap > /dev/null 2>&1
    then
99
      command="$(rlwrap -f migration.dict -C migration bash -c 'read -r -p "'"$read_prompt"'" command; echo "$command"')"
100
    else
101
      read -r -p "$read_prompt" command
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    fi
    step=
    case "$command" in
      quit)
        exit 0
        ;;
      next)
        found=false
        for step in "${steps[@]}"
        do
          if [ "$(step_3digit_number "$step")" == "$next_step" ]
          then
            found=true
            break;
          fi
        done
        if ! $found
        then
          step=
        fi
        ;;
      [0-9][0-9][0-9])
        found=false
        for step in "${steps[@]}"
        do
          if [ "$(step_3digit_number "$step")" == "$command" ]
          then
            found=true
            break;
          fi
        done
        if ! $found
        then
          echo "Sorry, no step $command found! Please enter all step digits."
          step=
        fi
        ;;
    esac
    if [ ! -z "$step" ]
Matteo Melli's avatar
Matteo Melli committed
141
    then
142
      script_type="$(step_type "$step")"
143
      script="$(step_script "$step")"
144
      echo "Step $step will be executed ($script_type $script):"
145
      echo
146 147 148 149
      if [ "$script_type" == "function" ]
      then
        type "$script" | head -n -1 | tail -n +4
      else
150
        script="${BASE}/$(step_script "$step")"
151 152
        cat "$script"
      fi
153
      echo
154
      read -r -s -N 1 -p "Press [y] to continue, any other key to abort." key
Matteo Melli's avatar
Matteo Melli committed
155
      echo
156 157
      if [ "$key" == "y" ]
      then
158 159 160 161 162 163
        if do_step "$script" > >(prepend_timestamp | tee -a migration.log) 2> >(prepend_timestamp | tee -a migration.log >&2)
        then
          next_step="$(next_step "$step")"
        else
          next_step="$(step_3digit_number "$step")"
        fi
Matteo Melli's avatar
Matteo Melli committed
164 165
        # wait for the redirect subshells to complete
        sleep 1
166 167 168
      fi
      echo
      step=
Matteo Melli's avatar
Matteo Melli committed
169
    fi
170 171
  done
}