-
Notifications
You must be signed in to change notification settings - Fork 10
/
update_sh.sh
158 lines (123 loc) · 4.24 KB
/
update_sh.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/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}"
echo ''
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 "${GREEN}${BOLD}WARNING${NORMAL}${BOLD}: $1${NORMAL}"
shift
while [ "$#" -gt "0" ]; do
echo " - $1"
shift
done
}
log_step "Checking for updates..."
current_version=$(head -n 1 version)
echo "Current version is ${current_version}"
license_url="https://license.dronahq.com"
updates=$(curl -s --insecure "${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 "pulling image from docker."
if [[ "$($MAYBE_SUDO docker images -q dronahq/self-hosted:$target_version 2> /dev/null)" == "" ]]; then
log_step "Target docker image not present locally."
$MAYBE_SUDO docker pull dronahq/self-hosted:$target_version
else
log_step "Target docker image present locally."
log_step "Create dump from previous DB "
$MAYBE_SUDO chmod 755 ./backup_sh.sh
./backup_sh.sh
fi
if [ $( $MAYBE_SUDO docker ps -a | grep dronahq-self-hosted-webapp | wc -l ) -gt 0 ]; then
log_step "Webapp Container running. Removing old container"
$MAYBE_SUDO docker stop dronahq-self-hosted-webapp
$MAYBE_SUDO docker rm dronahq-self-hosted-webapp
fi
echo ""
if [[ "$($MAYBE_SUDO docker images -q dronahq/self-hosted:$current_version 2> /dev/null)" != "" ]]; then
read -p "Do you want to remove old docker image? (type 'y' for yes): " remove_consent
if [ "$remove_consent" == "y" ]; then
log_step "Cleaning old image."
$MAYBE_SUDO docker rmi dronahq/self-hosted:$current_version
fi
fi
log_step "Fetching docker-compose file for target version"
curl --insecure $license_url/repository/docker-compose/$target_version --header 'Authorization: S9wbseRCkzE23fRK5soIkuUBpGW4sLUG' -o docker-compose.yml
log_step "Creating new container"
$MAYBE_SUDO docker-compose up -d webapp
log_step "Updating version file"
echo "$target_version" > version
log_step "Begining with database updates..."
log_step "Clearing old files..."
if [ -f "update.sql" ]; then
$MAYBE_SUDO rm update.sql
fi
rootpassword=$(grep MYSQL_ROOT_PASSWORD ./dronahq.env | cut -d "=" -f2 | cut -d "'" -f2 | cut -d '"' -f2)
log_step "Fetching updates..."
curl --insecure "${license_url}/api/getdbupdates?empty=false¤t_version=${current_version}&target_version=${target_version}" --header 'Authorization: S9wbseRCkzE23fRK5soIkuUBpGW4sLUG' -o update.sql
log_step "Copying updates to container..."
$MAYBE_SUDO docker cp ./update.sql dronahq-self-hosted-mysqldb:/
log_step "Applying updates..."
$MAYBE_SUDO docker exec dronahq-self-hosted-mysqldb /bin/sh -c "mysql -u root -p$rootpassword </update.sql"
log_step "Database Update completed."
# log_step "Removing temporary files..."
# if [ -f "update.sql" ]; then
# $MAYBE_SUDO rm update.sql
# fi
log_step "Update successful, Please refresh your browser window to reflect the changes"