-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcelestia-node.sh
executable file
·283 lines (253 loc) · 9.56 KB
/
celestia-node.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
#!/bin/bash
# ASCII art
echo " _ _ _ _ "
echo " __ ___| |___ __| |_(_)__ _ ___ _ _ ___ __| |___ "
echo "/ _/ -_) / -_|_-< _| / _\` |___| ' \\/ _ \\/ _\` / -_)"
echo "\\__\\___|_\\___/__/\__|_\\__,_| |_||_\\___/\\__,_\\___|"
echo " "
# Declare a log file and a temp directory
LOGFILE="$HOME/celestia-node-temp/logfile.log"
TEMP_DIR="$HOME/celestia-node-temp"
# Parse command line arguments
while getopts "v:" opt; do
case $opt in
v) USER_VERSION="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
echo "Usage: $0 [-v version]"
echo "Example: $0 -v v0.11.0"
exit 1
;;
esac
done
# Check if the directory exists
if [ -d "$TEMP_DIR" ]; then
read -p "Directory $TEMP_DIR exists. Do you want to clear it out? (y/n) " -n 1 -r
echo # move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
rm -rf "$TEMP_DIR"
echo "Directory $TEMP_DIR has been removed."
fi
fi
# Create a temporary directory to work from
mkdir -p "$TEMP_DIR"
touch "$LOGFILE"
# Log and print the log file location
echo "Log file is located at: $LOGFILE" | tee -a "$LOGFILE"
# Change to $TEMP_DIR and print a message
cd "$TEMP_DIR" || exit 1
echo "Working from temporary directory: $TEMP_DIR" | tee -a "$LOGFILE"
# Set VERSION based on user input or fetch latest
if [ -n "$USER_VERSION" ]; then
VERSION="$USER_VERSION"
echo "Using specified version: $VERSION" | tee -a "$LOGFILE"
else
# Fetch the latest release tag from GitHub
VERSION=$(curl -s "https://api.github.com/repos/celestiaorg/celestia-node/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
# Check if VERSION is empty
if [ -z "$VERSION" ]; then
echo "Failed to fetch the latest version. Exiting." | tee -a "$LOGFILE"
exit 1
fi
echo "Using latest version: $VERSION" | tee -a "$LOGFILE"
fi
# Validate version format
if [[ ! $VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-.*)?$ ]]; then
echo "Invalid version format: $VERSION" | tee -a "$LOGFILE"
echo "Version should be in the format vX.X.X (e.g., v0.11.0)" | tee -a "$LOGFILE"
exit 1
fi
# Detect the operating system and architecture
OS=$(uname -s)
ARCH=$(uname -m)
# Translate architecture to expected format
case $ARCH in
x86_64)
ARCH="x86_64"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo "Unsupported architecture: $ARCH. Exiting." | tee -a "$LOGFILE"
exit 1
;;
esac
# Translate OS to expected format
case $OS in
Linux|Darwin)
;;
*)
echo "Unsupported operating system: $OS. Exiting." | tee -a "$LOGFILE"
exit 1
;;
esac
# Construct the download URL
PLATFORM="${OS}_${ARCH}"
URL="https://github.com/celestiaorg/celestia-node/releases/download/$VERSION/celestia-node_$PLATFORM.tar.gz"
# Check if URL is valid
if [[ ! $URL =~ ^https://github.com/celestiaorg/celestia-node/releases/download/[^/]+/celestia-node_[^/]+.tar.gz$ ]]; then
echo "Invalid URL: $URL. Exiting." | tee -a "$LOGFILE"
exit 1
fi
# Log and print a message
echo "Downloading from: $URL" | tee -a "$LOGFILE"
# Download the tarball
if ! curl -L "$URL" -o "celestia-node_$PLATFORM.tar.gz" >> "$LOGFILE" 2>&1; then
echo "Download failed. Exiting." | tee -a "$LOGFILE"
exit 1
fi
# Detect if running on macOS and use appropriate command for checksum
if [ "$OS" = "Darwin" ]; then
CALCULATED_CHECKSUM=$(shasum -a 256 "celestia-node_$PLATFORM.tar.gz" | awk '{print $1}')
else
CALCULATED_CHECKSUM=$(sha256sum "celestia-node_$PLATFORM.tar.gz" | awk '{print $1}')
fi
# Download checksums.txt
if ! curl -L "https://github.com/celestiaorg/celestia-node/releases/download/$VERSION/checksums.txt" -o "checksums.txt" >> "$LOGFILE" 2>&1; then
echo "Failed to download checksums. Exiting." | tee -a "$LOGFILE"
exit 1
fi
# Find the expected checksum in checksums.txt
EXPECTED_CHECKSUM=$(grep "celestia-node_$PLATFORM.tar.gz" checksums.txt | awk '{print $1}')
# Verify the checksum
if [ "$CALCULATED_CHECKSUM" != "$EXPECTED_CHECKSUM" ]; then
echo "Checksum verification failed. Expected: $EXPECTED_CHECKSUM, but got: $CALCULATED_CHECKSUM. Exiting." | tee -a "$LOGFILE"
exit 1
else
echo "Checksum verification successful." | tee -a "$LOGFILE"
fi
# Extract the tarball to the temporary directory
if ! tar -xzf "celestia-node_$PLATFORM.tar.gz" >> "$LOGFILE" 2>&1; then
echo "Extraction failed. Exiting." | tee -a "$LOGFILE"
exit 1
fi
# Log and print a message
echo "Binary extracted to: $TEMP_DIR" | tee -a "$LOGFILE"
# Remove the tarball to clean up
rm "celestia-node_$PLATFORM.tar.gz"
rm "checksums.txt"
# Log and print a message
echo "Temporary files cleaned up." | tee -a "$LOGFILE"
# Check if Go is installed
if command -v go >/dev/null 2>&1; then
GOPATH=${GOPATH:-$(go env GOPATH)}
GOBIN="$GOPATH/bin"
HAS_GO=true
else
HAS_GO=false
fi
# Ask the user where to install the binary
echo ""
if [ "$HAS_GO" = true ]; then
echo "Where would you like to install the celestia binary?"
echo "1) Go bin directory ($GOBIN) [Recommended]"
echo "2) System bin directory (/usr/local/bin)"
echo "3) Keep in current directory ($TEMP_DIR)"
else
echo "Go is not installed. Where would you like to install the celestia binary?"
echo "1) System bin directory (/usr/local/bin) [Recommended]"
echo "2) Keep in current directory ($TEMP_DIR)"
fi
read -p "Enter your choice: " -n 1 -r
echo # move to a new line
if [ "$HAS_GO" = true ]; then
case $REPLY in
1)
# Install to GOBIN
mkdir -p "$GOBIN"
mv "$TEMP_DIR/celestia" "$GOBIN/"
chmod +x "$GOBIN/celestia"
echo "Binary moved to $GOBIN" | tee -a "$LOGFILE"
# Create a symbolic link in the temporary directory
ln -s "$GOBIN/celestia" "$TEMP_DIR/celestia"
echo "Symbolic link created in $TEMP_DIR" | tee -a "$LOGFILE"
echo ""
# Check if GOBIN is in PATH
if [[ ":$PATH:" != *":$GOBIN:"* ]]; then
echo "NOTE: $GOBIN is not in your PATH. You may want to add it by adding this line to your ~/.bashrc or ~/.zshrc:"
echo "export PATH=\$PATH:$GOBIN"
echo ""
fi
echo "You can now run celestia from anywhere (if $GOBIN is in your PATH)." | tee -a "$LOGFILE"
;;
2)
# Install to /usr/local/bin
sudo mv "$TEMP_DIR/celestia" /usr/local/bin/
echo "Binary moved to /usr/local/bin" | tee -a "$LOGFILE"
# Create a symbolic link in the temporary directory
ln -s /usr/local/bin/celestia "$TEMP_DIR/celestia"
echo "Symbolic link created in $TEMP_DIR" | tee -a "$LOGFILE"
echo ""
echo "You can now run celestia from anywhere." | tee -a "$LOGFILE"
;;
3)
# Keep in current directory
chmod +x "$TEMP_DIR/celestia"
echo "Binary kept in $TEMP_DIR" | tee -a "$LOGFILE"
echo "You can run celestia from this directory using ./celestia" | tee -a "$LOGFILE"
;;
*)
echo ""
echo "Invalid choice. The binary remains in $TEMP_DIR" | tee -a "$LOGFILE"
chmod +x "$TEMP_DIR/celestia"
echo "You can run celestia from this directory using ./celestia" | tee -a "$LOGFILE"
;;
esac
else
case $REPLY in
1)
# Install to /usr/local/bin
sudo mv "$TEMP_DIR/celestia" /usr/local/bin/
echo "Binary moved to /usr/local/bin" | tee -a "$LOGFILE"
# Create a symbolic link in the temporary directory
ln -s /usr/local/bin/celestia "$TEMP_DIR/celestia"
echo "Symbolic link created in $TEMP_DIR" | tee -a "$LOGFILE"
echo ""
echo "You can now run celestia from anywhere." | tee -a "$LOGFILE"
;;
2)
# Keep in current directory
chmod +x "$TEMP_DIR/celestia"
echo "Binary kept in $TEMP_DIR" | tee -a "$LOGFILE"
echo "You can run celestia from this directory using ./celestia" | tee -a "$LOGFILE"
;;
*)
echo ""
echo "Invalid choice. The binary remains in $TEMP_DIR" | tee -a "$LOGFILE"
chmod +x "$TEMP_DIR/celestia"
echo "You can run celestia from this directory using ./celestia" | tee -a "$LOGFILE"
;;
esac
fi
echo ""
echo "To check its version and see the menu, execute the following command:" | tee -a "$LOGFILE"
if [ "$HAS_GO" = true ]; then
case $REPLY in
1) # Go bin installation
echo "celestia version && celestia --help" | tee -a "$LOGFILE"
;;
2) # System bin installation
echo "celestia version && celestia --help" | tee -a "$LOGFILE"
;;
3) # Current directory
echo "$TEMP_DIR/celestia version && $TEMP_DIR/celestia --help" | tee -a "$LOGFILE"
;;
*) # Invalid choice (kept in temp dir)
echo "$TEMP_DIR/celestia version && $TEMP_DIR/celestia --help" | tee -a "$LOGFILE"
;;
esac
else
case $REPLY in
1) # System bin installation
echo "celestia version && celestia --help" | tee -a "$LOGFILE"
;;
2) # Current directory
echo "$TEMP_DIR/celestia version && $TEMP_DIR/celestia --help" | tee -a "$LOGFILE"
;;
*) # Invalid choice (kept in temp dir)
echo "$TEMP_DIR/celestia version && $TEMP_DIR/celestia --help" | tee -a "$LOGFILE"
;;
esac
fi