-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrakpios-cli
executable file
·359 lines (296 loc) · 10.9 KB
/
rakpios-cli
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/bin/bash
# set -o errexit
# set -o nounset
# set -o pipefail
# set -u
# set -e
INSTALL=0
UPGRADE=0
DEBUG=0
SILENT=0
CONFIG_FILE=0
DIR_TMP="${HOME}/.cache/rakpios-cli"
DIR_BIN="${HOME}/.local/bin"
DIR_LIB="${HOME}/.local/lib"
DIR_CNF="${HOME}/.config/rakpios-cli"
org="RAKWireless"
repo="rakpios-cli"
function __msg_debug(){
[[ "${DEBUG}" == "1" ]] && echo -e "[DEBUG] $*"
}
# usage information
usage() {
cat 1>&2 <<EOF
rakpios-cli
Command line tool for RAKPiOS
USAGE:
rakpios-cli [FLAGS]
FLAGS:
-d, --debug Prints debug message
-h, --help Prints help information
-i, --install Install rakpios-cli
-u, --upgrade Upgrade rakpios-cli
-s, --silent Perform install/update silently (no user interaction)
-v, --version Prints version information
EOF
}
main() {
SHORT=i,u,d,h,v,s
LONG=install,upgrade,debug,help,version,silent
OPTS=$(getopt --options $SHORT --longoptions $LONG -- "$@")
if [ $? -ne 0 ]; then
usage
exit 0
fi
eval set -- "$OPTS"
while :
do
case $1 in
-d | --debug) # Prints debug message
DEBUG=1
shift
;;
-s | --silent) # Forces update without asking
SILENT=1
shift
;;
-h | --help) # Prints help information
usage
exit 0
;;
-i | --install) # Install rakpios-cli
INSTALL=1
shift
;;
-u | --upgrade) # Upgrade rakpios-cli
UPGRADE=1
shift
;;
-v | --version) # Prints version information
do_print_version
exit 0
;;
*)
# echo "invalid command: $1"
# usage
break
;;
esac
done
# Check dependencies
do_check_dependencies
# Configure tmux
echo -e "set-option -g mouse on\nbind-key -n C-C confirm-before kill-session" >~/.tmux.conf
if [ "$INSTALL" == "1" ] || [ "$UPGRADE" == "1" ]; then
do_install
exit 0
fi
# start the rakpios-cli if no options is passed to the script
if [[ $OPTIND -eq 1 ]]; then
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli/rakpios-cli-network-manage"
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli/rakpios-cli-service-deploy"
# shellcheck source=/dev/null
source "${DIR_LIB}/rakpios-cli/rakpios-cli-service-list"
do_check_for_update
do_rakpios_cli_menu
fi
}
do_check_dependencies() {
# check for necessary tools
# docker will be checked separately
need_cmd curl
need_cmd jq
need_cmd unzip
need_cmd whiptail
need_cmd network-manager
need_cmd tmux
}
do_install() {
# Check if installing on top of existing version
if [ $SILENT -eq 0 ]; then
if [ $INSTALL -eq 1 ]; then
if test -f "${DIR_BIN}/rakpios-cli"; then
whiptail --title "Confirmation" --yesno "Looks like rakpios-cli is already installed. \nDo you want to upgrade the rakpios-cli to the latest version?" 10 78 --yes-button "Yes" --no-button "No"
if [[ $? -eq 1 ]]; then
exit 0
fi
fi
fi
fi
# Install
do_fresh_install
# Show message
if [ $SILENT -eq 0 ]; then
if [ $INSTALL -eq 1 ]; then
whiptail --msgbox "rakpios-cli successfully installed!" 10 60
else
whiptail --msgbox "rakpios-cli successfully updated!" 10 60
fi
fi
}
do_print_install_location(){
__msg_debug "The rakpios-cli will be installed in the ${DIR_BIN}, ${DIR_LIB} and ${DIR_CNF} folders"
}
do_print_version(){
LOCAL_VERSION_PATH="${DIR_CNF}/portainer_app_template.json"
if [ -f "$LOCAL_VERSION_PATH" ] && [ -f "${DIR_CNF}/rakpios-cli.version" ]; then
__msg_debug "Found configuration files saved in ${LOCAL_VERSION_PATH} and ${DIR_CNF}/rakpios-cli.version"
do_get_version_information
echo "rakpios-cli version is ${local_version_rakpios_cli} (latest available version is ${remote_version_rakpios_cli})"
echo "service version is ${local_version_portainer_json} (latest available version is ${remote_version_portainer_json}) "
else
echo "Error: rakpios-cli is not installed. Run 'rakpios-cli --install' to install the tool first."
fi
}
do_clean_up(){
rm -rf "${DIR_TMP}" "${DIR_BIN}/rakpios-cli" "${DIR_LIB}" "${DIR_CNF}"
}
# Check install directory
do_check_directory() {
declare FILES=${*:-$(</dev/stdin)}
for PARAM in $FILES; do
# do what needs to be done on each input value
if test -d "$PARAM"; then
__msg_debug "$PARAM exists"
else
mkdir -p "$PARAM"
__msg_debug "$PARAM created"
fi
done
}
do_fresh_install() {
do_check_dependencies
do_clean_up
do_print_install_location
do_check_directory "${DIR_TMP}" "${DIR_CNF}" "${DIR_BIN}" "${DIR_LIB}"
# clone the rakpios-cli repo and move the files
if [[ $UPGRADE -eq 1 ]]; then
echo "Updating rakpios-cli tool..."
else
echo "Installing rakpios-cli tool..."
fi
if git clone --quiet https://github.com/${org}/${repo}.git "${DIR_TMP}/"; then
__msg_debug "Download completed"
cp -rf "${DIR_TMP}/lib" "${DIR_LIB}/rakpios-cli"
cp "${DIR_TMP}/rakpios-cli" "${DIR_BIN}/rakpios-cli"
git -C "${DIR_TMP}" tag -l --sort=v:refname | tail -1 > "${DIR_CNF}/rakpios-cli.version"
__msg_debug "Tool copy completed"
echo "Download successful"
else
echo "[Error] Network error, download failed"
exit 1
fi
# download the portainer app template and Stack file from portainer-template repo
__msg_debug "Updating service list..."
if curl -s https://github.com/RAKWireless/portainer-templates/archive/refs/heads/master.zip -L -o "${DIR_TMP}/files.zip" && unzip -q -o "${DIR_TMP}/files.zip" -d "${DIR_TMP}/"; then
cp "${DIR_TMP}/portainer-templates-master/portainer_app_template.json" "${DIR_CNF}"
cp -r "${DIR_TMP}/portainer-templates-master/Stack/" "${DIR_CNF}"
echo "Service update successful"
else
echo "[Error] Failed to update the service list"
exit 1
fi
# Clean up
__msg_debug "Remove cached files ${DIR_TMP} after installation"
rm -rf "${DIR_TMP}"
# Print version information after installation fininshed
if do_get_version_information; then
echo "rakpios-cli version ${local_version_rakpios_cli}"
echo "service version ${local_version_portainer_json}"
fi
}
do_check_docker_basic(){
if [ -x "$(command -v docker)" ]; then
if docker info --format '{{json .}}' | grep "permission denied"; then
whiptail --msgbox "Got permission denied while trying to connect to the Docker daemon socket. Please make sure the current user has the right premission." 10 60
exit 1
fi
else
whiptail --msgbox "Docker not install, please install docker first" 20 60
exit 1
fi
}
do_rakpios_cli_menu(){
main_menu_option=$(whiptail --notags --title "RAKPiOS Configuration Tool (rakpios-cli)" --menu "" 20 78 8 "network" "Manage networks" "container" "Deploy services" "list" "List services" --cancel-button Finish --ok-button Select 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
case $main_menu_option in
network)
do_network_main_menu
;;
container)
do_check_docker_basic
do_container
;;
list)
do_service_menu
;;
*)
echo -n "unknown"
;;
esac
do_rakpios_cli_menu
else
__msg_debug "User selected Cancel."
exit 0
fi
}
# function to install command if not installed before
need_cmd() {
local COMMAND="$1"
if ! check_cmd "$COMMAND"; then
read -rp "Missing dependency: '$COMMAND'. Do you want to install it now? [y/N]: " RESPONSE
if [[ "$RESPONSE" != "y" ]]; then
echo "Quitting..."
exit 0
fi
sudo apt install "$COMMAND" -y
fi
}
# function to check whether command is installed or not
check_cmd() {
if [[ $1 == "network-manager" ]] ; then
command -v nmcli > /dev/null 2>&1
else
command -v "$1" > /dev/null 2>&1
fi
}
do_get_version_information() {
REMOTE_VERSION_URL_portainer_json="https://raw.githubusercontent.com/RAKWireless/portainer-templates/master/portainer_app_template.json"
LOCAL_VERSION_PATH="${DIR_CNF}/portainer_app_template.json"
if [ -f "$LOCAL_VERSION_PATH" ] && [ -f "${DIR_CNF}/rakpios-cli.version" ]; then
# use portainer json key value to track the service version
remote_version_portainer_json=$(curl -s $REMOTE_VERSION_URL_portainer_json | jq '.version_for_rakpios_cli' | tr -d '"')
local_version_portainer_json=$(jq '.version_for_rakpios_cli' "${DIR_CNF}/portainer_app_template.json" | tr -d '"')
# use github tag to track rakpios-cli version
remote_version_rakpios_cli=$(curl https://api.github.com/repos/${org}/${repo}/releases/latest -s | jq .tag_name -r)
# retrieve the local git tag
local_version_rakpios_cli=$(cat "${DIR_CNF}/rakpios-cli.version")
else
__msg_debug "Can not found rakpios-cli or stack configuration files."
CONFIG_FILE=1
fi
}
# check for updates
do_check_for_update(){
do_get_version_information
if [ $CONFIG_FILE == 1 ] ; then
whiptail --title "Error" --msgbox "Unable to find rakpios-cli or stack configuration files. Try reinstall the tool to fix it." 10 78
return
fi
# Check versions format
VERSION_PATTERN="^v[0-99].[0-99]"
if [[ ! $remote_version_portainer_json =~ $VERSION_PATTERN ]] || [[ ! $local_version_portainer_json =~ $VERSION_PATTERN ]] || [[ ! $remote_version_rakpios_cli =~ $VERSION_PATTERN ]] || [[ ! $local_version_rakpios_cli =~ $VERSION_PATTERN ]]; then
whiptail --title "Error" --msgbox "Updates check failed, please check your network connection, and try again later." 10 78
return
fi
# Check updates for rakpios-cli and portainer template version
if [[ $remote_version_portainer_json > $local_version_portainer_json ]] || [[ $remote_version_rakpios_cli > $local_version_rakpios_cli ]]; then
whiptail --title "New Update is available" --msgbox "An update for rakpios-cli is available.\nRun 'rakpios-cli --upgrade' to upgrade." 8 78
__msg_debug "An update is available."
fi
}
###################################################################################################
main "$@" || exit 1