-
Notifications
You must be signed in to change notification settings - Fork 715
/
availup.sh
executable file
Β·295 lines (285 loc) Β· 11.9 KB
/
availup.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
#!/usr/bin/env bash
echo "π Starting Availup..."
while [ $# -gt 0 ]; do
if [[ $1 = "--"* ]]; then
v="${1/--/}"
declare "$v"="$2"
shift
fi
shift
done
# enable default upgrades by default
upgrade="${upgrade:-y}"
# generate folders if missing
if [ ! -d "$HOME/.avail" ]; then
mkdir $HOME/.avail
fi
if [ ! -d "$HOME/.avail/identity" ]; then
mkdir $HOME/.avail/identity
fi
# check if bash is current terminal shell, else check for zsh
if [ -z "$BASH_VERSION" ]; then
if [ -z "$ZSH_VERSION" ]; then
echo "π« Unable to locate a shell. Availup might not work as intended!"
else
CURRENT_TERM="zsh"
fi
else
CURRENT_TERM="bash"
fi
if [ "$CURRENT_TERM" = "bash" -a -f "$HOME/.bashrc" ]; then
PROFILE="$HOME/.bashrc"
elif [ "$CURRENT_TERM" = "bash" -a -f "$HOME/.bash_profile" ]; then
PROFILE="$HOME/.bash_profile"
elif [ "$CURRENT_TERM" = "bash" -a -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
elif [ "$CURRENT_TERM" = "bash" -a -f "$HOME/.zsh_profile" ]; then
PROFILE="$HOME/.zsh_profile"
elif [ "$CURRENT_TERM" = "zsh" -a -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
elif [ "$CURRENT_TERM" = "zsh" -a -f "$HOME/.zsh_profile" ]; then
PROFILE="$HOME/.zsh_profile"
elif [ "$CURRENT_TERM" = "bash" ]; then
PROFILE="$HOME/.bashrc"
touch $HOME/.bashrc
elif [ "$CURRENT_TERM" = "zsh" ]; then
PROFILE="$HOME/.zshrc"
touch $HOME/.zshrc
else
echo "π«£ Unable to locate a compatible shell or rc file, using POSIX default, availup might not work as intended!"
PROFILE="/etc/profile"
fi
if [ -z "$network" ]; then
echo "π No network selected. Defaulting to mainnet."
NETWORK="mainnet"
else
NETWORK="$network"
fi
UPGRADE=0
if [ "$NETWORK" = "mainnet" ]; then
echo "π Mainnet selected."
elif [ "$NETWORK" = "turing" ]; then
echo "π Turing testnet selected."
elif [ "$NETWORK" = "local" ]; then
echo "π Local testnet selected."
fi
TURING_CONFIG_PARAMS="bootstraps=['/dns/bootnode.1.lightclient.turing.avail.so/tcp/37000/p2p/12D3KooWBkLsNGaD3SpMaRWtAmWVuiZg1afdNSPbtJ8M8r9ArGRT']\nfull_node_ws=['wss://turing-rpc.avail.so/ws','wss://avail-turing.public.blastapi.io','wss://turing-testnet.avail-rpc.com']\nconfidence=80.0\navail_path='$HOME/.avail/$NETWORK/data'\nkad_record_ttl=43200\not_collector_endpoint='http://otel.lightclient.turing.avail.so:4317'\ngenesis_hash='d3d2f3a3495dc597434a99d7d449ebad6616db45e4e4f178f31cc6fa14378b70'\n"
MAINNET_CONFIG_PARAMS="bootstraps=['/dns/bootnode.1.lightclient.mainnet.avail.so/tcp/37000/p2p/12D3KooW9x9qnoXhkHAjdNFu92kMvBRSiFBMAoC5NnifgzXjsuiM']\nfull_node_ws=['wss://mainnet-rpc.avail.so/ws','wss://mainnet.avail-rpc.com','wss://avail-mainnet.public.blastapi.io']\nconfidence=80.0\navail_path='$HOME/.avail/$NETWORK/data'\nkad_record_ttl=43200\ngenesis_hash='b91746b45e0346cc2f815a520b9c6cb4d5c0902af848db0a80f85932d2e8276a'\not_collector_endpoint='http://otel.lightclient.mainnet.avail.so:4317'\n"
AVAIL_BIN=$HOME/.avail/$NETWORK/bin/avail-light
if [ ! -d "$HOME/.avail/$NETWORK" ]; then
mkdir $HOME/.avail/$NETWORK
fi
if [ ! -d "$HOME/.avail/$NETWORK/bin" ]; then
mkdir $HOME/.avail/$NETWORK/bin
fi
if [ ! -d "$HOME/.avail/$NETWORK/data" ]; then
mkdir $HOME/.avail/$NETWORK/data
fi
if [ ! -d "$HOME/.avail/$NETWORK/config" ]; then
mkdir $HOME/.avail/$NETWORK/config
fi
readonly MAINNET_VERSION="avail-light-client-v1.12.2"
readonly TURING_VERSION="avail-light-client-v1.12.2"
readonly LOCAL_VERSION="avail-light-client-v1.12.2"
if [ "$NETWORK" = "mainnet" ]; then
VERSION=$MAINNET_VERSION
if [ -z "$config" ]; then
CONFIG="$HOME/.avail/$NETWORK/config/config.yml"
if [ -f "$CONFIG" ]; then
echo "ποΈ Wiping old config file at $CONFIG."
rm $CONFIG
else
echo "π€· No configuration file set. This will be automatically generated at startup."
fi
touch $CONFIG
if [ ! -z "$config_url" ]; then
echo "π₯ Downloading configuration file from $config_url..."
if command -v curl >/dev/null 2>&1; then
curl -sL $config_url >>$CONFIG
echo -e "avail_path='$HOME/.avail/$NETWORK/data'\n" >>$CONFIG
elif command -v wget >/dev/null 2>&1; then
wget -qO- $config_url >>$CONFIG
echo -e "avail_path='$HOME/.avail/$NETWORK/data'\n" >>$CONFIG
else
echo "π« Neither curl nor wget are available. Please install one of these and try again."
exit 1
fi
else
echo -e $MAINNET_CONFIG_PARAMS >>$CONFIG
fi
else
CONFIG="$config"
fi
elif [ "$NETWORK" = "turing" ]; then
VERSION=$TURING_VERSION
if [ -z "$config" ]; then
CONFIG="$HOME/.avail/$NETWORK/config/config.yml"
if [ -f "$CONFIG" ]; then
echo "ποΈ Wiping old config file at $CONFIG."
rm $CONFIG
else
echo "π€· No configuration file set. This will be automatically generated at startup."
fi
touch $CONFIG
echo -e $TURING_CONFIG_PARAMS >>$CONFIG
else
CONFIG="$config"
fi
elif [ "$NETWORK" = "local" ]; then
echo "π Local testnet selected."
VERSION=$LOCAL_VERSION
if [ -z "$config" ]; then
echo "π« No configuration file was provided for local testnet, exiting."
exit 1
fi
else
echo "π« Invalid network selected. Select one of the following: turing, local."
exit 1
fi
if [ -z "$app_id" ]; then
echo "π² No app ID specified. Defaulting to light client mode."
else
APPID="$app_id"
fi
if [ -z "$identity" ]; then
IDENTITY=$HOME/.avail/identity/identity.toml
if [ -f "$IDENTITY" ]; then
echo "π Identity found at $IDENTITY."
else
echo "π€· No identity set. This will be automatically generated at startup."
fi
else
IDENTITY="$identity"
fi
# handle WSL systems
if uname -r | grep -qEi "(Microsoft|WSL)"; then
# force remove IO lock
if [ -d "$HOME/.avail/$NETWORK/data" ]; then
rm -rf $HOME/.avail/$NETWORK/data
mkdir $HOME/.avail/$NETWORK/data
fi
if [ "$force_wsl" != 'y' -a "$force_wsl" != 'yes' ]; then
echo "π WSL detected. This script is not fully compatible with WSL. Please download the Windows runner instead by clicking this link: https://github.com/availproject/avail-light/releases/download/$VERSION/avail-light-windows-runner.zip Alternatively, rerun the command with --force_wsl y"
exit 1
else
echo "π WSL detected. The binary is not fully compatible with WSL but forcing the run anyway."
fi
fi
# check if the default upgrade option is enabled
# if enabled, proceed directly to upgrading the binary
# if itβs disabled, verify the current version, ask for permission and upgrade if itβs not the latest
if [ "$upgrade" = "n" ] || [ "$upgrade" = "N" ]; then
echo "π Checking for updates..."
if [ -f $AVAIL_BIN ]; then
CURRENT_VERSION="$($HOME/.avail/$NETWORK/bin/avail-light --version | awk '{print $1"-v"$2}')"
if [ "$CURRENT_VERSION" != "$VERSION" ]; then
echo "β¬οΈ Avail binary is out of date. Your current version is $CURRENT_VERSION, but the latest is $VERSION."
read -p "Do you want to upgrade to the latest version? (y/n): " upgrade_response
if [[ "$upgrade_response" = "y" || "$upgrade_response" = "Y" ]]; then
UPGRADE=1
echo "π Upgrading to the latest version..."
else
echo "π« Upgrade skipped."
fi
fi
fi
else
if [ -f $AVAIL_BIN ]; then
UPGRADE=1
echo "β¬οΈ Triggering default upgrade of Avail binary..."
fi
fi
onexit() {
chmod 600 $IDENTITY
echo "π Avail stopped. Future instances of the light client can be started by invoking the avail-light binary or rerunning this script$EXTRAPROMPT"
if [[ ":$PATH:" != *":$HOME/.avail/$NETWORK/bin:"* ]]; then
if ! grep -q "export PATH=\"\$PATH:$HOME/.avail/$NETWORK/bin\"" "$PROFILE"; then
echo -e "export PATH=\"\$PATH:$HOME/.avail/$NETWORK/bin\"\n" >>$PROFILE
fi
echo -e "π Avail has been added to your profile. Run the following command to load it in the current session:\n. $PROFILE\n"
fi
exit 0
}
run_binary() {
trap onexit EXIT
$AVAIL_BIN --config $CONFIG --identity $IDENTITY ${APPID:+--app-id $APPID}
exit $?
}
# check if avail-light binary is available and check if upgrade variable is set to 0
if [ -f $AVAIL_BIN -a "$UPGRADE" = 0 ]; then
echo "β
Avail is already installed. Starting Avail..."
trap onexit EXIT
run_binary
fi
if [ "$UPGRADE" = 1 ]; then
echo "ποΈ Wiping state data..."
if [ -f $AVAIL_BIN ]; then
rm $AVAIL_BIN
if [ -d "$HOME/.avail/$NETWORK/data" ]; then
rm -rf $HOME/.avail/$NETWORK/data
mkdir $HOME/.avail/$NETWORK/data
fi
else
echo "π€ Avail was not installed with availup. Attemping to uninstall with cargo..."
cargo uninstall avail-light || echo "π Avail was not installed with cargo, upgrade might not be required!"
if command -v avail-light >/dev/null 2>&1; then
echo "π« Avail was not uninstalled. Please uninstall manually and try again."
exit 1
fi
fi
fi
if [ "$(uname -m)" = "arm64" -a "$(uname -s)" = "Darwin" ]; then
ARCH_STRING="apple-arm64"
elif [ "$(uname -m)" = "x86_64" -a "$(uname -s)" = "Darwin" ]; then
ARCH_STRING="apple-x86_64"
elif [ "$(uname -m)" = "aarch64" -o "$(uname -m)" = "arm64" ]; then
ARCH_STRING="linux-arm64"
elif [ "$(uname -m)" = "x86_64" ]; then
ARCH_STRING="linux-amd64"
fi
if [ -z "$ARCH_STRING" ]; then
echo "π₯ No binary available for this architecture, building from source instead. This can take a while..."
# check if cargo is not available, else attempt to install through rustup
if command -v cargo >/dev/null 2>&1; then
echo "π¦ Cargo is available. Building from source..."
else
echo "π Cargo is not available. Attempting to install with Rustup..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
EXTRAPROMPT="\nβΉοΈ Cargo env needs to be loaded by running source \$HOME/.cargo/env"
echo "π¦ Cargo is now available. Reattempting to build from source..."
fi
# check if avail-light folder exists in home directory, if yes, pull latest changes, else clone the repo
AVAIL_LIGHT_DIR=$HOME/avail-light
if [ -d $AVAIL_LIGHT_DIR ]; then
echo "π Updating avail-light repository and building..."
cd $AVAIL_LIGHT_DIR
git pull -q origin $VERSION
git checkout -q $VERSION
cargo build --release
cp $AVAIL_LIGHT_DIR/target/release/avail-light $AVAIL_BIN
else
echo "π Cloning avail-light repository and building..."
git clone -q -c advice.detachedHead=false --depth=1 --single-branch --branch $VERSION https://github.com/availproject/avail-light.git $AVAIL_LIGHT_DIR
cd $AVAIL_LIGHT_DIR
cargo build --release
mv $AVAIL_LIGHT_DIR/target/release/avail-light $AVAIL_BIN
rm -rf $AVAIL_LIGHT_DIR
fi
else
if command -v curl >/dev/null 2>&1; then
curl -sLO https://github.com/availproject/avail-light/releases/download/$VERSION/avail-light-$ARCH_STRING.tar.gz
elif command -v wget >/dev/null 2>&1; then
wget -qO- https://github.com/availproject/avail-light/releases/download/$VERSION/avail-light-$ARCH_STRING.tar.gz
else
echo "π« Neither curl nor wget are available. Please install one of these and try again."
exit 1
fi
# use tar to extract the downloaded file and move it to .avail/bin/ directory
tar -xzf avail-light-$ARCH_STRING.tar.gz
chmod +x avail-light-$ARCH_STRING
mv avail-light-$ARCH_STRING $AVAIL_BIN
rm avail-light-$ARCH_STRING.tar.gz
fi
echo "β
Availup exited successfully."
echo "𧱠Starting Avail."
run_binary