Skip to content

Commit 8df8432

Browse files
authored
feat: bash installer script (#275)
1 parent e3f6593 commit 8df8432

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

install.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/bin/sh
2+
3+
# This install script is intended to download and install the latest available
4+
# release of the Ecsact SDK.
5+
#
6+
# You can install using this script:
7+
# curl -L https://ecsact.dev/install.sh | sh
8+
# Or directly from GitHub:
9+
# curl https://raw.githubusercontent.com/ecsact-dev/ecsact_sdk/main/install.sh | sh
10+
11+
# Installer script inspired by: https://github.com/wasmerio/wasmer-install/blob/master/install.sh
12+
13+
RELEASES_URL="https://github.com/ecsact-dev/ecsact_sdk/releases"
14+
15+
reset="\033[0m"
16+
red="\033[31m"
17+
green="\033[32m"
18+
yellow="\033[33m"
19+
white="\033[37m"
20+
bold="\e[1m"
21+
dim="\e[2m"
22+
23+
init_env_vars() {
24+
ARCH=$(uname -m)
25+
case $ARCH in
26+
amd64) ARCH="amd64" ;;
27+
x86_64) ARCH="amd64" ;;
28+
*) echo "${ARCH} systems are not yet supported. Please open an issue on the project if you would like to use Ecsact in your project: https://github.com/ecsact-dev/ecsact_sdk"; exit 1 ;;
29+
esac
30+
31+
OS=$(uname | tr '[:upper:]' '[:lower:]')
32+
}
33+
34+
download_json() {
35+
url="$2"
36+
37+
# echo "Fetching $url.."
38+
if test -x "$(command -v curl)"; then
39+
response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url")
40+
body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')
41+
code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
42+
elif test -x "$(command -v wget)"; then
43+
temp=$(mktemp)
44+
body=$(wget -q --header='Accept: application/json' -O - --server-response "$url" 2>"$temp")
45+
code=$(awk '/^HTTP/{print $2}' <"$temp" | tail -1)
46+
rm "$temp"
47+
else
48+
printf "Neither curl nor wget was available to perform http requests"
49+
return 1
50+
fi
51+
if [ "$code" != 200 ]; then
52+
echo "File download failed with code $code"
53+
return 1
54+
fi
55+
56+
eval "$1='$body'"
57+
return 0
58+
}
59+
60+
download_file() {
61+
url="$1"
62+
destination="$2"
63+
64+
if test -x "$(command -v curl)"; then
65+
code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
66+
elif test -x "$(command -v wget)"; then
67+
code=$(wget --quiet -O "$destination" --server-response "$url" 2>&1 | awk '/^ HTTP/{print $2}' | tail -1)
68+
else
69+
printf "Neither curl nor wget was available to perform http requests."
70+
return 1
71+
fi
72+
73+
if [ "$code" = 404 ]; then
74+
printf "\nNot Found - $url\n"
75+
printf " Your platform may not yet be supported ($OS-$ARCH).$reset\n"
76+
printf " Please open an issue on the project if you would like to use Ecsact in your project: https://github.com/ecsact-dev/ecsact_sdk\n\n"
77+
return 1
78+
elif [ "$code" != 200 ]; then
79+
printf "File download failed with code $code"
80+
return 1
81+
fi
82+
return 0
83+
}
84+
85+
install_ecsact_sdk_deb_package() {
86+
printf "${dim}Fetching latest release...${reset}\n"
87+
download_json LATEST_RELEASE "$RELEASES_URL/latest" || return 1
88+
89+
RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
90+
91+
printf "Download and install Ecsact SDK ${green}$RELEASE_TAG?${reset}"
92+
confirm_or_exit || return 1
93+
94+
DEB_PACKAGE="ecsact_sdk_${RELEASE_TAG}_${ARCH}.deb"
95+
DEB_PACKAGE_URL="$RELEASES_URL/download/$RELEASE_TAG/$DEB_PACKAGE"
96+
DOWNLOAD_FILE=$(mktemp -t ecsact_sdk_$RELEASE_TAG.XXXXXXXXXX.deb)
97+
98+
download_file "$DEB_PACKAGE_URL" "$DOWNLOAD_FILE" || return 1
99+
100+
printf "Installing Ecsact SDK with dpkg...\n"
101+
sudo dpkg -i $DOWNLOAD_FILE
102+
rm -f $DOWNLOAD_FILE
103+
}
104+
105+
confirm_or_exit() {
106+
if [ -n "$BASH_VERSION" ]; then
107+
read -p "$1 [y/N] " -n 1 -r
108+
echo
109+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
110+
printf "installation aborted\n"
111+
return 1
112+
fi
113+
return 0
114+
fi
115+
116+
read -p "$1 [y/N] " yn
117+
case $yn in
118+
[Yy]*) break ;;
119+
[Nn]*) printf "${red}Installation aborted${reset}\n"; return 1 ;;
120+
*) printf "${red}Installation aborted${reset}\n"; return 1 ;;
121+
esac
122+
123+
return 0
124+
}
125+
126+
install_ecsact_sdk() {
127+
cyan="${reset}\033[36;1m"
128+
oran="${reset}\e[38;5;214m"
129+
130+
printf "${reset}Welcome to the Ecsact SDK bash installer!${reset}\n"
131+
132+
printf "
133+
${cyan} /##########\
134+
${cyan} #### #/ \##\
135+
${cyan} ###### \##\
136+
${cyan} #### ##\ \# ${oran}|####
137+
${cyan} \##\ #### ${oran} #########
138+
${oran} ############### ${cyan}###### ${oran}##############
139+
${cyan} #### ${oran} #########
140+
${cyan} /# ${oran}|####
141+
${cyan} #### /####/
142+
${cyan} ###### ####/
143+
${cyan} ####
144+
${reset}
145+
"
146+
if test -x "$(command -v dpkg)"; then
147+
install_ecsact_sdk_deb_package
148+
else
149+
echo "Only deb package supporting linux distributions can use this install script."
150+
exit 1
151+
fi
152+
}
153+
154+
init_env_vars
155+
install_ecsact_sdk
156+
unset -f init_env_vars install_ecsact_sdk download_json download_file install_ecsact_sdk_deb_package confirm_or_exit
157+

0 commit comments

Comments
 (0)