-
Notifications
You must be signed in to change notification settings - Fork 10
/
get-database-updates.sh
125 lines (97 loc) · 3.03 KB
/
get-database-updates.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env bash
# NB: local trial script has to be self-contained
# See https://sipb.mit.edu/doc/safe-shell/
set -euf -o pipefail
if [[ "$OSTYPE" == "linux-gnu" ]]; then
export MAYBE_SUDO="sudo"
else
export MAYBE_SUDO=""
fi
if [ -t 1 ]; then
export NORMAL="$(tput sgr0)"
export RED="$(tput setaf 1)"
export GREEN="$(tput setaf 2)"
export MAGENTA="$(tput setaf 5)"
export CYAN="$(tput setaf 6)"
export WHITE="$(tput setaf 7)"
export BOLD="$(tput bold)"
else
export NORMAL=""
export RED=""
export GREEN=""
export MAGENTA=""
export CYAN=""
export WHITE=""
export BOLD=""
fi
error_exit() {
echo ''
echo "${RED}${BOLD}ERROR${NORMAL}${BOLD}: $1${NORMAL}"
shift
while [ "$#" -gt "0" ]; do
echo " - $1"
shift
done
exit 1
}
log_step() {
echo ''
echo "${GREEN}${BOLD}INFO${NORMAL}${BOLD}: $1${NORMAL}"
shift
while [ "$#" -gt "0" ]; do
echo " - $1"
shift
done
}
log_warn() {
echo ''
echo "${CYAN}${BOLD}WARNING${NORMAL}${BOLD}: $1${NORMAL}"
shift
while [ "$#" -gt "0" ]; do
echo " - $1"
shift
done
}
export DISTRO=$( (lsb_release -ds || cat /etc/*release || uname -om) 2>/dev/null | head -n1)
command_present() {
type "$1" >/dev/null 2>&1
}
# ------------------------------------------------------------------------------------------------
export DRONAHQ_LICENSE_URL="https://license.dronahq.com"
current_version=''
if [ -f "version" ]; then
current_version=$(head -n 1 version)
fi
if [ -z "$current_version" ]; then
read -p "Enter current version to update from: " current_version
fi
echo "Current version is ${current_version}"
log_step "Checking for updates..."
updates=$(curl -s --insecure "${DRONAHQ_LICENSE_URL}/api/checkforupdates?empty=false&version=${current_version}" --header 'Authorization: S9wbseRCkzE23fRK5soIkuUBpGW4sLUG')
if [ -z "$updates" ]; then
log_step "No new updates are available for you."
exit 1
fi
IFS=',' read -r -a update_list <<< "$updates"
echo "Select any version from below list to update to"
for i in "${update_list[@]}"; do
echo "$i"
done
read -p "Enter target version here: " target_version
if [[ ! " ${update_list[*]} " =~ " ${target_version} " ]]; then
error_exit "Invalid target version selected. Please try again."
fi
if [ "$current_version" = "$target_version" ]; then
error_exit "You are already in target version. aborting the process".
fi
# ------------------------------------------------------------------------------------------------
log_step "Begining with database updates..."
log_step "Clearing old files..."
if [ -f "update.sql" ]; then
# $MAYBE_SUDO rm update.sql
mv update.sql update-$(date +"%Y-%m-%d_%H-%M-%S").sql
fi
log_step "Fetching updates..."
curl --insecure "${DRONAHQ_LICENSE_URL}/api/getdbupdates?empty=false¤t_version=${current_version}&target_version=${target_version}" --header 'Authorization: S9wbseRCkzE23fRK5soIkuUBpGW4sLUG' -o update.sql
log_step "Updates downloaded in file 'update.sql'"
# ------------------------------------------------------------------------------------------------