-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmithril-install.sh
More file actions
executable file
·199 lines (167 loc) · 6.1 KB
/
Copy pathmithril-install.sh
File metadata and controls
executable file
·199 lines (167 loc) · 6.1 KB
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
#!/bin/sh
set -e
# Function to display usage
usage() {
echo "Install or upgrade a Mithril node"
echo "Usage: $0 [-c node] [-v version] [-d distribution] [-p path]"
echo " -c node : Mithril node to install or upgrade (mithril-signer, mithril-aggregator, mithril-client)"
echo " -d distribution : Distribution to upgrade to (latest, unstable or distribution version e.g '2445.0')"
echo " -p path : Path to install the component"
echo "Environment variables:"
echo " GITHUB_TOKEN : Optional GitHub token used to raise the GitHub API rate limit"
exit 1
}
# Function to display an error message and exit
error_exit() {
echo "$1" 1>&2
exit 1
}
# Function to check that required tools are installed
check_requirements() {
which curl >/dev/null ||
error_exit "It seems 'curl' is not installed or not in the path.";
which jq >/dev/null ||
error_exit "It seems 'jq' is not installed or not in the path.";
}
check_glibc_min_version() {
glibc_version=$(ldd --version | awk 'NR==1{print $NF}')
if [ "$(echo "$glibc_version" | grep -cE -e "2\.[0-2][0-9]" -e "2\.3[0-4]")" -gt 0 ]; then
error_exit "Error: Your GLIBC version is $glibc_version, but the minimum required version is 2.35."
fi
}
# Function to fetch the release information, with retries on transient errors and accurate error reporting
fetch_release_information() {
release_url=$1
destination_file=$2
max_attempts=3
retry_delay=3
attempt=1
while [ "$attempt" -le "$max_attempts" ]; do
if [ -n "$GITHUB_TOKEN" ]; then
http_code=$(curl -sL -o "$destination_file" -w '%{http_code}' -H "Authorization: Bearer $GITHUB_TOKEN" "$release_url") || http_code="000"
else
http_code=$(curl -sL -o "$destination_file" -w '%{http_code}' "$release_url") || http_code="000"
fi
case "$http_code" in
2*)
return 0
;;
403 | 429)
error_exit "Error: GitHub API rate limit exceeded. Retry later or set the 'GITHUB_TOKEN' environment variable to raise the limit."
;;
404)
error_exit "Error: The distribution '$DISTRIBUTION' does not exist."
;;
4*)
error_exit "Error: Failed to fetch the release information (HTTP status $http_code)."
;;
esac
if [ "$attempt" -lt "$max_attempts" ]; then
echo "Attempt $attempt to fetch the release information failed (HTTP status $http_code). Retrying in $retry_delay seconds..." 1>&2
sleep "$retry_delay"
fi
attempt=$((attempt + 1))
done
error_exit "Error: Failed to fetch the release information after $max_attempts attempts (last HTTP status $http_code)."
}
# --- MAIN execution ---
# Default values
DISTRIBUTION="latest"
GITHUB_ORGANIZATION="IntersectMBO"
GITHUB_REPOSITORY="mithril"
check_requirements
# Parse command line arguments
while getopts "c:v:d:p:" opt; do
case ${opt} in
c )
NODE=$OPTARG
;;
d )
DISTRIBUTION=$OPTARG
;;
p )
INSTALL_PATH=$OPTARG
;;
* )
usage
;;
esac
done
# Detect the operating system
OS=$(uname -s)
OS_CODE=$(echo "$OS" | awk '{print tolower($0)}')
case "$OS" in
Linux) : ;;
Darwin) OS_CODE="macos" ;;
*) error_exit "Unsupported operating system $OS for $NODE" ;;
esac
# Detect the architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH_NAME="x64"
;;
arm64|aarch64)
ARCH_NAME="arm64"
;;
*)
error_exit "Unsupported architecture: $ARCH"
;;
esac
# Set temp file
TEMP_FILE="${INSTALL_PATH}/temp.json"
# Check if node and path are provided
if [ -z "$NODE" ] || [ -z "$DISTRIBUTION" ] || [ -z "$INSTALL_PATH" ]; then
usage
fi
# Create the install path if it doesn't exist
mkdir -p "$INSTALL_PATH"
# Check if install path is writable
if [ ! -w "$INSTALL_PATH" ]; then
error_exit "Error: The specified install path '$INSTALL_PATH' is not writable."
fi
# Validate node
if [ "$NODE" != "mithril-signer" ] && [ "$NODE" != "mithril-aggregator" ] && [ "$NODE" != "mithril-client" ]; then
echo "Invalid node: $NODE"
usage
fi
# Determine the URL for the specified tag
if [ "$DISTRIBUTION" = "latest" ]; then
RELEASE_URL="https://api.github.com/repos/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}/releases/latest"
else
RELEASE_URL="https://api.github.com/repos/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}/releases/tags/${DISTRIBUTION}"
fi
# Fetch the release information
echo "Fetching release information from $RELEASE_URL..."
fetch_release_information "$RELEASE_URL" "$TEMP_FILE"
# Find valid binary for the node
ASSETS_DOWNLOAD_URL=$(jq -r --arg os_code "$OS_CODE" --arg arch_name "$ARCH_NAME" '.assets[] | select(.name | contains($os_code)) | select(.name | contains($arch_name)) | .browser_download_url' < "$TEMP_FILE")
rm -f "$TEMP_FILE"
if [ -z "$ASSETS_DOWNLOAD_URL" ]; then
error_exit "Error: Failed to fetch assets from release. This probably means that your OS and architecture are not supported."
fi
# Download the distribution assets
echo "Downloading $NODE to $DISTRIBUTION from $ASSETS_DOWNLOAD_URL..."
ASSETS_FILE_PATH="$INSTALL_PATH/mithril-assets.tar.gz"
curl --fail -sL -o "$ASSETS_FILE_PATH" "$ASSETS_DOWNLOAD_URL" || error_exit "Error: Failed to download the asset file from the release"
# Search for a node file
NODE_FILE_NAME=$(tar -tzf "$ASSETS_FILE_PATH" | grep "$NODE" | awk '{print $NF}')
if [ -z "$NODE_FILE_NAME" ]; then
error_exit "Error: Failed to find $NODE binary in the distribution asset. This probably means that your OS and architecture are not supported."
fi
tar -xz -f "$ASSETS_FILE_PATH" -C "$INSTALL_PATH" "$NODE"
chmod +x "$INSTALL_PATH/$NODE"
rm -f "$ASSETS_FILE_PATH"
# Check glibc version if the binary is dynamic
if [ "$OS" = "Linux" ]; then
LDD_OUTPUT=$(ldd "$INSTALL_PATH/$NODE" 2>&1 || true)
if echo "$LDD_OUTPUT" | grep -Eq "statically linked|not a dynamic executable"; then
echo "Binary is static, skipping glibc check."
else
echo "Checking glibc version"
check_glibc_min_version
fi
fi
VERSION_OUTPUT=$("$INSTALL_PATH/$NODE" --version)
VERSION=$(echo "$VERSION_OUTPUT" | awk '{print $NF}')
echo "Congrats! ${NODE} has been upgraded to ${VERSION} from distribution ${DISTRIBUTION} and installed at ${INSTALL_PATH}!"