-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
Β·307 lines (266 loc) Β· 8.41 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
#!/usr/bin/env bash
stty -echoctl
trap 'exit' INT TERM
trap 'kill 0' EXIT QUIT
trap '__exit_on_error' ERR
FABKIT_DEFAULT_PATH="${HOME}/.fabkit"
FABKIT_DOCKER_IMAGE="everledgerio/fabkit"
FABKIT_LOGFILE=".fabkit.log"
# TODO: Retrieve latest available version from web
FABKIT_VERSION="latest"
ALIASES=("fabkit" "fk")
__error() {
local input
if [ -n "$1" ]; then
input="$1"
red "[ERROR] $input"
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") [ERROR] $input" >>"$FABKIT_LOGFILE"
else
local count=0
while read -r input; do
if [ $count -eq 0 ]; then
((count++)) || true
echo
fi
red "[ERROR] $input"
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") [ERROR] $input" >>"$FABKIT_LOGFILE"
done
fi
}
__exit_on_error() {
echo "Check the log file for more details: $(yellow "cat $FABKIT_LOGFILE")"
kill 0
}
__check_deps() {
if ! type -p git &>/dev/null; then
__error "git required but not installed"
exit 1
fi
}
__overwrite_line() {
local pattern=$1
local text_to_replace=$2
local file=$3
local new_text=${text_to_replace//\//\\\/}
sed -i'.bak' '/'"${pattern}"'/s/.*/'"${new_text}"'/' "${file}"
mv "${file}.bak" "$HOME" &>/dev/null
}
__setup() {
local shell="$1"
local profile="${HOME}/.${shell}rc"
local cmd=""
if [ "$shell" = "fish" ]; then
profile="${HOME}/.config/fish/config.fish"
fi
if ! grep -q "^export FABKIT_ROOT=" <"$profile"; then
cmd+="export FABKIT_ROOT=\"${FABKIT_ROOT}\"\n"
else
__overwrite_line "export FABKIT_ROOT" "export FABKIT_ROOT=\"${FABKIT_ROOT}\"" "$profile"
fi
for alias in "${ALIASES[@]}"; do
if ! grep -q "alias ${alias}" <"$profile"; then
cmd+="alias ${alias}=\"${FABKIT_CMD}\"\n"
else
__overwrite_line "alias ${alias}" "alias ${alias}=\"${FABKIT_CMD}\"" "$profile"
fi
done
if [ -n "$cmd" ]; then
echo -e "\n# Fabkit vars to run commands with ease\n${cmd}" >>"$profile"
fi
echo
green "Fabkit aliases have been added to your default shell!"
echo
yellow "!!!!! ATTENTION !!!!!"
echo "You have one last thing to do. Run the following in your terminal:"
echo
echo -e "$(yellow "\tsource $profile")"
echo
echo "And then try any of: $(
for a in "${ALIASES[@]}"; do echo -en "$(cyan "${a} | ")"; done
tput cub 2
) - with something like:"
echo
echo -e "$(cyan "\tfabkit network start")"
echo
}
__install() {
unset FABKIT_ROOT
while [[ (-n "$yn" && ! "$yn" =~ ^(Y|y)) || -z "$FABKIT_ROOT" || (-n "$FABKIT_ROOT" && ! -d "$FABKIT_ROOT") ]]; do
read -rp "Where would you like to install Fabkit? [$(yellow "$FABKIT_DEFAULT_PATH")] " FABKIT_ROOT
FABKIT_ROOT=${FABKIT_ROOT:-${FABKIT_DEFAULT_PATH}}
FABKIT_ROOT=${FABKIT_ROOT%/}
# for security reasons we will create a new directory "fabkit" if the path is not empty and it is not an existing fabkit's root
if [[ "$FABKIT_ROOT" = "$HOME" || ("$FABKIT_ROOT" != "$HOME" && "$(ls -A "$FABKIT_ROOT" &>/dev/null)" && ! -f ${FABKIT_ROOT}/fabkit) ]]; then
FABKIT_ROOT+="/fabkit"
fi
if [ -d "$FABKIT_ROOT" ]; then
yellow "!!!!! ATTENTION !!!!!"
yellow "Directory ${FABKIT_ROOT} already exists and cannot be overwritten"
read -rp "Do you want to delete this directory in order to proceed with the installation? (y/N) " yn
case $yn in
[Yy]*)
if [ -w "$FABKIT_ROOT" ]; then
rm -rf "$FABKIT_ROOT" &>/dev/null
fi
mkdir -p "$FABKIT_ROOT" &>/dev/null
;;
*) unset FABKIT_ROOT ;;
esac
else
mkdir -p "$FABKIT_ROOT" &>/dev/null
fi
done
if [ -w "$FABKIT_ROOT" ]; then
(
if echo -n "Downloading and installing into $(cyan "${FABKIT_ROOT}")" && (__fetch 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal") > >(__error >&2); then
__error "There was an error extracting ${FABKIT_TARBALL} into ${FABKIT_ROOT}"
exit 1
fi
) &
__spinner
echo
else
yellow "!!!!! ATTENTION !!!!!"
yellow "Directory \"${FABKIT_ROOT}\" requires superuser permissions"
yellow "Be sure you are setting an installation path accessible from your current user (preferably under ${HOME})!"
exit
fi
}
__fetch() {
git clone https://github.com/everledger/fabkit.git ${FABKIT_ROOT}
}
__set_installation_type() {
FABKIT_RUNNER="${FABKIT_ROOT}/fabkit"
FABKIT_RUNNER_DOCKER="docker run --rm -it --name fabkit -e \"FABKIT_HOST_ROOT=\$FABKIT_ROOT\" -v /var/run/docker.sock:/var/run/docker.sock -v \"\$FABKIT_ROOT\":/home/fabkit everledgerio/fabkit:$FABKIT_VERSION ./fabkit \"\$@\""
OS="$(uname)"
case $OS in
Linux | FreeBSD | Darwin)
green "Great! It looks like your system supports Fabric natively, but you can also run it in a docker container if you like."
while [[ ! "$cmd" =~ (1|2) ]]; do
echo
blue "Choose any of these options: "
echo
cyan "1) Local installation (recommended)"
cyan "2) Docker installation"
read -r cmd
case $cmd in
1)
FABKIT_CMD="${FABKIT_RUNNER}"
green "Roger. Initiating local install!"
;;
2)
FABKIT_CMD="${FABKIT_RUNNER_DOCKER}"
green "Roger. Initiating docker install!"
__install_docker &
__spinner
;;
*) ;;
esac
done
;;
*)
yellow "It looks like your system does NOT support natively Fabric, but don't worry, we've got you covered!"
FABKIT_CMD="${FABKIT_RUNNER_DOCKER}"
;;
esac
}
__install_docker() {
FABKIT_DOCKER_IMAGE+=":${FABKIT_VERSION:-latest}"
echo -en "Downloading the official Fabkit docker image $(cyan ${FABKIT_DOCKER_IMAGE})..."
if (docker pull "${FABKIT_DOCKER_IMAGE}" 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal") > >(__error >&2); then
__error "Error pulling ${FABKIT_DOCKER_IMAGE}"
exit 1
fi
if (docker tag "${FABKIT_DOCKER_IMAGE}" "${FABKIT_DOCKER_IMAGE/$FABKIT_VERSION/latest}" 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal") > >(__error >&2); then
__error "Error tagging ${FABKIT_DOCKER_IMAGE} to latest"
exit 1
fi
}
__spinner() {
local LC_CTYPE=C
local pid=$!
local spin='β£Ύβ£½β£»β’Ώβ‘Ώβ£β£―β£·'
local charwidth=3
echo -en "\033[3Cβ "
local i=0
tput cuf1
while kill -0 "$pid" 2>/dev/null; do
tput civis
local i=$(((i + charwidth) % ${#spin}))
printf "%s" "${spin:$i:$charwidth}"
tput cub1
sleep 0.05
done
tput el
tput cnorm
if wait "$pid"; then
echo -e "β
"
else
echo -e "β "
exit 1
fi
}
purple() {
echo -e "\033[1;35m${1}\033[0m"
}
red() {
echo -e "\033[1;31m${1}\033[0m"
}
green() {
echo -e "\033[1;32m${1}\033[0m"
}
yellow() {
echo -e "\033[1;33m${1}\033[0m"
}
blue() {
echo -e "\033[1;34m${1}\033[0m"
}
cyan() {
echo -e "\033[1;36m${1}\033[0m"
}
purple "
ββββββββ β¬ββ β¬ββ¬β
β β£ βββ€ββ΄βββ΄β β β
β β΄ β΄ββββ΄ β΄ β΄ β΄
β -β -β
"
blue "Welcome to the Fabkit's interactive setup π§° "
echo
read -n 1 -s -r -p "Press any key to start! (or CTRL-C to exit) "
echo
echo
__check_deps
if git remote -v 2>/dev/null | grep -q "fabkit"; then
cyan "Looks like you want to run Fabkit from inside its repository path."
echo
echo "Setting to $(cyan "${FABKIT_VERSION}") version."
echo "Going to set - $(cyan "${PWD}") - as your installation path!"
echo
FABKIT_ROOT="$PWD"
else
__install
fi
sleep 1
__set_installation_type
sleep 1
echo
green "Congrats! Fabkit is now installed! π"
echo
sleep 1
cyan "And now the final few touches so you can run fabkit anywhere! π΅βπ«π"
case $SHELL in
*bash)
__setup bash
;;
*zsh)
__setup zsh
;;
*fish)
__setup fish
;;
esac
sleep 1
echo "For more information visit: $(cyan "https://github.com/everledger/fabkit/tree/main/docs")"
echo
echo
green "Have fun! ππΊ"