-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·308 lines (268 loc) · 11.4 KB
/
install.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
set -euo pipefail # Unofficial Bash Strict Mode
IFS=$'\n\t' # http://redsymbol.net/articles/unofficial-bash-strict-mode/
#/ Blynk CLI
#/
#/ Description: A short utility to manage Blynk server from CLI
#/
#/ Usage:
#/ Type `blinkcli` to show this help message.
#/
#/ Examples:
#/ blynkcli server install # install the latest release of blynk server
#/ blynkcli server start # start the server
#/ blynkcli server status # check status of the server
#/ blynkcli server stop # stop the server
#/
#/ blynkcli server backup my-backup-name # make a backup of the server
#/ blynkcli server restore my-backup-name_2017-05-01_00-30-28 # restore from backup
#/
#/ Options:
#/ remove - uninstall blynkcli
#/
#/ setup - install blynkcli
#/
#/ self-update - check if an update is available for Blynk CLI
#/
#/ version - print version of blynkcli
#/
#/ server [OPTION]
#/ install - install latest blynk server release
#/
#/ uninstall - uninstall blynk server
#/
#/ start - start blynk server
#/
#/ stop - stop blynk server
#/
#/ restart - restart blynk server
#/
#/ status - print whether the server is online or offline
#/
#/ update - update the server to latest release available
#/
#/ backup - make a backup of blynk server's data folder
#/
#/ restore [BACKUP] - restore blynk server's data folder from backup
#/
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; }
DATETIME=$(date +"%d %h %Y %H:%M:%S")
BLYNKCLI_VERSION="v0.3.0"
BLYNKCLI_EXECUTABLE="/usr/bin/blynkcli"
BLYNK_JAR=""
BLYNK_FOLDER="/var/blynk"
BLYNK_DATA="$BLYNK_FOLDER/data"
BLYNK_BACKUP_FOLDER="$BLYNK_FOLDER/backup"
BLYNK_SERVER_CONFIG="$BLYNK_FOLDER/server.properties"
BLYNK_PID_PATH="/run/blynk.pid"
info() { echo -e "\e[32m[INFO]\e[0m $DATETIME - $*"; }
warning() { echo -e "\e[33m[WARNING]\e[0m $DATETIME - $*"; }
error() { echo -e "\e[31m[ERROR]\e[0m $DATETIME - $*"; }
fatal() { echo -e "\e[1;31m[FATAL]\e[0m $DATETIME - $*"; exit 1; }
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
if [[ "$0" != "$BLYNKCLI_EXECUTABLE" ]]; then
if which blynkcli >> /dev/null; then
blynkcli self-update
else
info "Installing Blynk CLI"
sudo cp "$0" $BLYNKCLI_EXECUTABLE
info "Installation complete"
info "Use \`blynkcli\` to get help."
fi
else
if [[ -z ${1+x} ]]; then
usage
else
if [[ $1 == "server" ]]; then
if [[ -z ${2+x} ]]; then
usage
else
if [[ $2 == "install" ]]; then
if [[ ! -f $BLYNK_JAR ]]; then
info "Installing Blynk server"
set +e # Disallow temporary the programm to exit if an error occurs
dpkg -l libxrender &> /dev/null || true
lib_exists=$?
set -e
if ! which java >> /dev/null || [[ $lib_exists -eq 1 ]]; then
info "Installing dependencies"
sudo apt update
sudo apt install oracle-java8-jdk libxrender1
fi
if [[ ! -d $BLYNK_FOLDER ]]; then
sudo mkdir -p $BLYNK_FOLDER
fi
if ! id "blynk" >/dev/null 2>&1; then
info "Creating Blynk user"
sudo adduser --system --no-create-home --disabled-login --group --quiet blynk
fi
# Update blynk user rights for the blynk folder
sudo chown -R blynk:blynk $BLYNK_FOLDER
sudo chmod -R g+w $BLYNK_FOLDER
# Retrieve server's release informations
serverFile="$(curl -s "https://api.github.com/repos/blynkkk/blynk-server/releases/latest" | grep 'browser_' | cut -d\" -f4 | head -n 1)"
jar="$BLYNK_FOLDER/$(basename "$serverFile")"
info "Downloading latest server release: $(basename "$serverFile")"
sudo wget -c -q --show-progress "$serverFile" -O "$jar"
sudo sed -i -e "s#^BLYNK_JAR=\".*\"#BLYNK_JAR=\"$jar\"#" $BLYNKCLI_EXECUTABLE
# Update default settings for enhanced security
printf "admin.email=admin@blynk.cc\nadmin.pass=fablab\nlogs.folder=%s/logs" $BLYNK_FOLDER | sudo -u blynk tee "$BLYNK_SERVER_CONFIG" >> /dev/null
if ! grep -q "# Added by Blynk CLI" /etc/rc.local; then
info "Setting Blynk server to launch on startup"
last_line=$(grep -n '^exit 0' /etc/rc.local | tail -1 | cut -d: -f1)
sudo sed -i "$last_line c \
# Added by Blynk CLI\nif which blynkcli >> /dev/null; then\n\tblynkcli server start >> /dev/null\nfi\n\nexit 0" /etc/rc.local
fi
info "Blynk successfully installed"
else
error "Blynk server already installed"
fi
elif [[ $2 == "uninstall" ]]; then
blynkcli server stop
info "Uninstalling Blynk server"
if [[ -d $BLYNK_FOLDER ]]; then
sudo rm -R $BLYNK_FOLDER
fi
sudo sed -i -e "s#^BLYNK_JAR=\".*\"#BLYNK_JAR=\"\"#" $BLYNKCLI_EXECUTABLE
# Turn off Blynk CLI startup on system boot
first_line=$(grep -nr "# Added by Blynk CLI" /etc/rc.local | cut -d : -f 1 )
last_line=$((first_line + 4))
sudo sed -i "$first_line,$last_line d" "/etc/rc.local"
elif [[ $2 == "update" ]]; then
info "Check for Blynk server update"
# Retrieve last server release informations
latest=$(curl -s "https://api.github.com/repos/blynkkk/blynk-server/releases/latest" | grep 'browser_' | cut -d\" -f4 | head -n 1)
new_jar=$(basename "$latest")
new_path="$BLYNK_FOLDER/$new_jar"
if [[ ! -z $BLYNK_JAR ]] && [[ $new_jar == "$(basename "$BLYNK_JAR")" ]]; then
warning "No update available for Blynk server"
else
info "An update is available"
start_server=0
if [[ -f $BLYNK_PID_PATH ]]; then
blynkcli server stop
start_server=1
fi
info "Downloading new version ($new_jar)"
sudo -u blynk wget -c -q --show-progress "$latest" -O "$new_path"
# Replace old server
if [[ -f "$BLYNK_JAR" ]]; then
sudo -u blynk rm "$BLYNK_JAR"
fi
sudo sed -i -e "s#^BLYNK_JAR=\".*\"#BLYNK_JAR=\"$new_path\"#" $BLYNKCLI_EXECUTABLE
if [[ $start_server == 1 ]]; then
blynkcli server start
fi
info "Update complete"
fi
elif [[ $2 == "start" ]]; then
if [[ ! -f $BLYNK_PID_PATH ]]; then
if [[ -f $BLYNK_JAR ]]; then
info "Starting server"
nohup sudo -u blynk java -jar "$BLYNK_JAR" -dataFolder $BLYNK_DATA -serverConfig $BLYNK_SERVER_CONFIG 0<&- 1>&- 2>&- &
echo $! | sudo tee $BLYNK_PID_PATH >> /dev/null
else
error "Server must be installed first. Run \`blynkcli server install\` first"
fi
else
error "Server already running"
fi
elif [[ $2 == "stop" ]]; then
info "Stopping server"
if [[ -f $BLYNK_PID_PATH ]] && ps -p "$(cat /run/blynk.pid)" -ge 1 > /dev/null; then
sudo kill -USR1 "$(cat /run/blynk.pid)"
sudo rm $BLYNK_PID_PATH
else
error "Server is already offline"
fi
elif [[ $2 == "restart" ]]; then
if [[ -f $BLYNK_PID_PATH ]]; then
blynkcli server stop
fi
blynkcli server start
elif [[ $2 == "status" ]]; then
if [[ -f $BLYNK_PID_PATH ]] && ps -p "$(cat /run/blynk.pid)" -ge 1 > /dev/null; then
info "Server is \e[32monline\e[0m"
exit 0
else
info "Server is \e[31moffline\e[0m"
exit 1
fi
elif [[ $2 == "backup" ]]; then
if [[ ! -d "$BLYNK_BACKUP_FOLDER" ]]; then
sudo -u blynk mkdir "$BLYNK_BACKUP_FOLDER"
fi
if [[ -d "$BLYNK_DATA" ]]; then
if [[ -z ${3+x} ]]; then
echo -n "Enter the backup name: "
read -r -e backup_name
else
backup_name="$3"
fi
if [[ "$backup_name" =~ ^[a-zA-Z0-9-]+$ ]]; then
info "Backing up data folder"
backup_folder="$BLYNK_BACKUP_FOLDER/${backup_name}_$(date +"%Y-%m-%d_%H-%M-%S")"
sudo -u blynk cp -R "$BLYNK_DATA" "$backup_folder"
info "Backup $backup_name saved as $backup_folder"
else
error "Invalid name. Only alphanumeric characters and hyphen -are supported."
fi
else
error "There are no data to save. You must run the server once first before making a backup"
fi
elif [[ $2 == "restore" ]]; then
if [[ -z ${3+x} ]]; then
error "You must provide the name of a backup.
Examples:
blynkcli restore my-backup-name
blynkcli restore my-backup-name_2017-05-16_00-30-54"
else
if [[ $3 =~ .*_?[0-9]{4}(-[0-9]{2}){2}_([0-9]{2}-){2}[0-9]{2}$ ]]; then
target_folder="$BLYNK_BACKUP_FOLDER/$3"
else
backup_count=$(find $BLYNK_BACKUP_FOLDER -name "$3*" | wc -l)
if [[ $backup_count == 1 ]]; then
target_folder=$(find $BLYNK_BACKUP_FOLDER -name "$3*")
elif [[ $backup_count == 0 ]]; then
error "There is no backup using this name."
exit 1
else
error "There are more than one backup following this name. Use the full name instead"
exit 1
fi
blynkcli server stop
info "Restoring from backup $target_folder"
sudo -u blynk rm -R "$BLYNK_DATA"
sudo -u blynk cp -R "$target_folder" "$BLYNK_DATA"
blynkcli server start
fi
fi
else
usage
fi
fi
elif [[ $1 == "remove" ]]; then
sudo rm $BLYNKCLI_EXECUTABLE
elif [[ $1 == "self-update" ]]; then
info "Updating Blynk CLI"
latest="$(curl -s "https://api.github.com/repos/booteille/blynkcli-bash/releases/latest" | grep 'browser_' | cut -d\" -f4 | head -n 1)"
version=$(echo "$latest" | cut -d / -f8)
if [[ "$(blynkcli version)" == "$version" ]]; then
warning "No update available."
else
info "New version available. Updating to $version"
jar=$(sed -n -e '/^BLYNK_JAR/p' /usr/bin/blynkcli | cut -d \" -f2 )
sudo wget -c -q --show-progress "$latest" -O /tmp/blynkcli
sudo mv /tmp/blynkcli $BLYNKCLI_EXECUTABLE
sudo chmod +x $BLYNKCLI_EXECUTABLE
sudo sed -i -e "s#^BLYNK_JAR=\".*\"#BLYNK_JAR=\"$jar\"#" $BLYNKCLI_EXECUTABLE
info "Update complete."
fi
elif [[ $1 == "version" ]]; then
echo $BLYNKCLI_VERSION
else
usage
fi
fi
fi
fi