-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·304 lines (281 loc) · 8.72 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
#!/usr/bin/env bash
# Debugging
if [ -f ".debug" ]; then
exec 5>debug.log
BASH_XTRACEFD="5"
set -x
fi
version="v1.0.0"
short_name="core"
daemon_name="core"
daemon_version="0"
root_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
self_name=$(basename "$(readlink -f "${BASH_SOURCE[0]}")")
lock_selfname=".${self_name}.lock"
daemon_dir="${root_dir}/daemon"
functions_dir="${daemon_dir}/functions"
data_dir="${daemon_dir}/data"
daemon_list="${data_dir}/daemon_list.csv"
daemon_list_menu="${data_dir}/daemon_list_menu.csv"
daemon_files="${root_dir}/coins/${short_name}"
log_dir="${daemon_files}/logs"
daemon_logs="${log_dir}/coin.log"
tmp_dir="${daemon_files}/tmp"
daemon_cfg_dir="${daemon_files}/config"
daemon_config_file="${daemon_cfg_dir}/coin.conf"
daemon_installed="${daemon_files}/.installed"
default_config_dir="${daemon_dir}/configurations"
daemon_bin_dir="${daemon_files}/bin"
daemon_data_dir="${daemon_files}/data"
console_log="${log_dir}/console.log"
user_input="${1}"
## Github Values
githubuser="Kryptos-Team"
githubrepo="daemon"
githubbranch="master"
# Core function that is required first
core_functions.sh() {
function_file="${FUNCNAME[0]}"
fn_bootstrap_fetch_file_github "daemon/functions" "core_functions.sh" "${functions_dir}" "chmodx" "run" "noforcedl" "nomd5"
}
# Bootstrap
# Fetches the core functions before passed off to core_dl.sh
fn_bootstrap_fetch_file() {
remote_file_url="${1}"
local_file_dir="${2}"
local_file_name="${3}"
chmodx="${4:-0}"
run="${5:-0}"
forcedl="${6:-0}"
md5="${7:-0}"
# Download file if missing or download forced
if [ ! -f "${local_file_dir}/${local_file_name}" ] || [ "${forcedl}" == "forcedl" ]; then
if [ ! -d "${local_file_dir}" ]; then
mkdir -p "${local_file_dir}"
fi
# If curl exists, download file
if [ -n "$(command -v curl 2>/dev/null)" ]; then
echo -en "fetching ${local_file_name}...\c"
curlcmd=$(curl -s --fail -L -o "${local_file_dir}/${local_file_name}" "${remote_file_url}" 2>&1)
local exitcode=$?
if [ ${exitcode} -ne 0 ]; then
echo -e "FAIL"
if [ -f "${daemon_logs}" ]; then
echo -e "${remote_file_url}" | tee -a "${daemon_logs}"
echo -e "${curlcmd}" | tee -a "${daemon_logs}"
fi
exit 1
else
echo -e "OK"
fi
else
echo -e "[ FAIL ] Curl is not installed"
exit 1
fi
# Make file chmodx if chmodx is set
if [ "${chmodx}" == "chmodx" ]; then
chmod +x "${local_file_dir}/${local_file_name}"
fi
fi
if [ -f "${local_file_dir}/${local_file_name}" ]; then
# Run file if run is set
if [ "${run}" == "run" ]; then
source "${local_file_dir}/${local_file_name}"
fi
fi
}
fn_bootstrap_fetch_file_github() {
github_file_url_dir="${1}"
github_file_url_name="${2}"
github_url="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
remote_file_url="${github_url}"
local_file_dir="${3}"
local_file_name="${github_file_url_name}"
chmodx="${4:-0}"
run="${5:-0}"
forcedl="${6:-0}"
md5="${7:-0}"
# Passes variables to the file download function
fn_bootstrap_fetch_file "${remote_file_url}" "${local_file_dir}" "${local_file_name}" "${chmodx}" "${run}" "${forcedl}" "${md5}"
}
# Installer menu
fn_print_center() {
columns=$(tput cols)
line="$*"
printf "%*s\n" $(((${#line} + columns) / 2)) "${line}"
}
fn_print_horizontal() {
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "="
}
fn_install_menu_bash() {
local resultvar=$1
title=$2
caption=$3
options=$4
fn_print_horizontal
fn_print_center "${title}"
fn_print_center "${caption}"
fn_print_horizontal
menu_options=()
while read -r line || [[ -n "${line}" ]]; do
var=$(echo -e "${line}" | awk -F "," '{print $1 " - " $2}')
menu_options+=("${var}")
done <"${options}"
menu_options+=("Cancel")
select option in "${menu_options[@]}"; do
if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
eval "$resultvar=\"${option/%\ */}\""
fi
break
done
}
fn_install_menu_whiptail() {
local menucmd=$1
local resultvar=$2
title=$3
caption=$4
options=$5
height=${6:-40}
width=${7:-80}
menuheight=${8:-30}
IFS=","
menu_options=()
while read -r line; do
key=$(echo -e "${line}" | awk -F "," '{print $2}')
val=$(echo -e "${line}" | awk -F "," '{print $1}')
menu_options+=("${val//\"/}" "${key//\"/}")
done <"${options}"
OPTION=$(${menucmd} --title "${title}" --menu "${caption}" "${height}" "${width}" "${menuheight}" "${menu_options[@]}" 3>&1 1>&2 2>&3)
if [ $? == 0 ]; then
eval "$resultvar=\"${OPTION}\""
else
eval "$resultvar="
fi
}
# Menu selector
fn_install_menu() {
local resultvar=$1
local selection=""
title=$2
caption=$3
options=$4
# Get menu command
for menucmd in whiptail dialog bash; do
if [ -x "$(command -v "${menucmd}")" ]; then
menucmd=$(command -v "${menucmd}")
break
fi
done
case "$(basename "${menucmd}")" in
whiptail | dialog)
fn_install_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30
;;
*)
fn_install_menu_bash selection "${title}" "${caption}" "${options}"
;;
esac
eval "$resultvar=\"${selection}\""
}
# Gets server info from daemon_list.csv and puts into array
fn_daemon_info() {
IFS=","
daemon_info_array=($(grep -aw "$user_input" "${daemon_list}"))
short_name="${daemon_info_array[1]}" # LTC
daemon_name="${daemon_info_array[0]}" # litecoind
daemon_full_name="${daemon_info_array[2]}" # Litecoin
daemon_version="${daemon_info_array[3]}" # 0.17.1
}
fn_install_getopt() {
user_input="empty"
echo -e "Usage: $0 [option]"
echo -e ""
echo -e "Installer - Kryptos Coin Daemon - Version ${version}"
echo -e "https://github.com/Kryptos-Team"
echo -e ""
echo -e "Commands"
echo -e "install\t\t| Select daemon to install"
echo -e "coin_name\t| Enter coin name daemon to install. e.g $0 litecoind"
echo -e "list\t\t| List all coin daemons available to install"
exit
}
fn_install_file() {
local_file_name="${daemon_name}"
if [ -e "${local_file_name}" ]; then
i=2
while [ -e "$local_file_name-${i}" ]; do
((i++))
done
local_file_name="${local_file_name}-${i}"
fi
cp -R "${self_name}" "${local_file_name}"
sed -i -e "s/short_name=\"core\"/short_name=\"${short_name}\"/g" "${local_file_name}"
sed -i -e "s/daemon_name=\"core\"/daemon_name=\"${daemon_name}\"/g" "${local_file_name}"
sed -i -e "s/daemon_version=\"0\"/daemon_version=\"${daemon_version}\"/g" "${local_file_name}"
echo -e "Installed ${daemon_name} daemon as ${local_file_name}"
echo -e ""
if [ ! -d "${daemon_files}" ]; then
echo -e "./${local_file_name} install"
else
echo -e "Remember to check daemon ports"
echo -e "./${local_file_name} details"
fi
echo -e ""
exit
}
# Prevents install.sh to run as root, except if doing a dependency install
if [ "$(whoami)" == "root" ]; then
if [ "${user_input}" == "install" ]; then
echo -e "[ FAIL ] Do NOT run this script as root!"
exit 1
elif [ ! -f "${functions_dir/core_messages.sh}" ]; then
echo -e "[ FAIL ] Do NOT run this script as root!"
exit 1
else
core_functions.sh
check_root.sh
fi
fi
# Kryptos Installer mode
if [ "${short_name}" == "core" ]; then
# Download the latest daemon list. This is the list of all supported daemons
fn_bootstrap_fetch_file_github "daemon/data" "daemon_list.csv" "${data_dir}" "nochmodx" "norun" "forcedl" "nomd5"
if [ ! -f "${daemon_list}" ]; then
echo -e "[ FAIL ] daemon_list.csv could not be loaded"
exit 1
fi
if [ "${user_input}" == "list" ] || [ "${user_input}" == "l" ]; then
{
tail -n +1 "${daemon_list}" | awk -F "," '{print $1 "\t" $2 "\t" $3 "\t" $4}'
} | column -s $'\t' -t | more
exit
elif [ "${user_input}" == "install" ] || [ "${user_input}" == "i" ]; then
tail -n +2 "${daemon_list}" | awk -F "," '{print $1 "," $2 "," $3 "," $4}' >"${daemon_list_menu}"
fn_install_menu result "Kryptos Team" "Select coin daemon name to install" "${daemon_list_menu}"
user_input="${result}"
fn_daemon_info
if [ "${result}" == "${daemon_name}" ]; then
fn_install_file
elif [ "${result}" == "" ]; then
echo -e "Install cancelled"
else
echo -e "[ FAIL ] menu result does not match daemon name"
echo -e "result: ${result}"
echo -e "daemon name: ${daemon_name}"
fi
elif [ -n "${user_input}" ]; then
fn_daemon_info
if [ "${user_input}" == "${daemon_name}" ] || [ "${user_input}" == "${short_name}" ]; then
fn_install_file
else
echo -e "[ FAIL ] unknown daemon name"
fi
else
fn_install_getopt
fi
# Daemon mode
else
core_functions.sh
fn_ansi_loader
getopt=$1
core_getopt.sh
fi