-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto-spacemesh.sh
executable file
·313 lines (297 loc) · 8.36 KB
/
auto-spacemesh.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
308
309
310
311
312
313
#!/usr/bin/env bash
#
# Execute the auto-spacemesh pipeline.
#
# Usage:
#
# wget -O- https://raw.githubusercontent.com/smeshcloud/auto-spacemesh/main/auto-spacemesh.sh | bash
#
# Author:
#
# Zanoryt <zanoryt@protonmail.com>
#
version="1.0.0"
cloud_provider=""
ssh_user=""
ssh_host=""
ssh_key=""
exec_destination="local"
config_path=""
coinbase_address=""
valid_choices=("1" "2" "3" "4")
valid_cloud_providers=("runpod" "valt")
display_help() {
echo "Usage: auto-spacemesh.sh [OPTIONS]"
echo "Options:"
echo " -h, --help Display this help message and exit"
echo " -v, --version Display version information and exit"
echo " -c, --config PATH Configure the script"
echo " -C, --coinbase ADDR Set the Coinbase address"
echo " -l, --local Execute locally (default)"
echo " -s, --ssh USER@HOST Execute remotely via SSH"
echo " -k, --ssh-key KEY Use a specific SSH key"
echo " -b, --cloud PROVIDER Execute in the cloud (runpod or valt)"
echo " -r, --run CHOICE Run choice directly (1-4)"
}
display_version() {
echo "auto-spacemesh version $version"
}
parse_options() {
exec_destination="" # Set default to empty
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
display_help
exit 0
;;
-v | --version)
display_version
exit 0
;;
-c | --config)
if [[ -z $2 ]]; then
echo "Option --config requires an argument"
exit 1
fi
echo "Config option selected with argument: $2"
config_path="$2"
shift 2
;;
-C | --coinbase)
if [[ -z $2 ]]; then
echo "Option --coinbase requires an argument"
exit 1
fi
echo "Coinbase option selected with argument: $2"
coinbase_address="$2"
shift 2
;;
-l | --local)
if [[ $exec_destination != "" ]]; then
echo "Cannot specify both --local and --ssh or --cloud options"
exit 1
fi
echo "Local option selected"
exec_destination="local"
cloud_provider=""
ssh_user=""
ssh_host=""
ssh_key=""
shift
;;
-s | --ssh)
if [[ $exec_destination != "" ]]; then
echo "Cannot specify both --local and --ssh or --cloud options"
exit 1
fi
if [[ -z $2 ]]; then
echo "Option --ssh requires an argument"
exit 1
fi
echo "SSH option selected with argument: $2"
exec_destination="ssh"
ssh_user="${2%@*}"
ssh_host="${2##*@}"
cloud_provider=""
shift 2
;;
-k | --ssh-key)
if [[ $exec_destination != "" ]]; then
echo "Cannot specify both --local and --ssh or --cloud options"
exit 1
fi
if [[ -z $2 ]]; then
echo "Option --ssh-key requires an argument"
exit 1
fi
echo "SSH key option selected with argument: $2"
exec_destination="ssh"
ssh_key="$2"
cloud_provider=""
shift 2
;;
-b | --cloud)
if [[ $exec_destination != "" ]]; then
echo "Cannot specify both --local and --ssh or --cloud options"
exit 1
fi
if [[ -z $2 ]]; then
echo "Option --cloud requires an argument"
exit 1
fi
cloud_arg="${2#*=}"
if [[ -z $cloud_arg ]]; then
echo "Option --cloud requires an argument"
exit 1
fi
echo "Cloud provider option selected with argument: $cloud_arg"
exec_destination="cloud"
cloud_provider="$cloud_arg"
ssh_user=""
ssh_host=""
ssh_key=""
shift 2
;;
--cloud-key)
if [[ $exec_destination != "cloud" ]]; then
echo "Option --cloud-key can only be used with --cloud"
exit 1
fi
if [[ -z $2 ]]; then
echo "Option --cloud-key requires an argument"
exit 1
fi
echo "Cloud key option selected with argument: $2"
cloud_key="$2"
shift 2
;;
-r | --run)
if [[ -z $2 ]]; then
echo "Option --run requires an argument (1, 2, or 3)"
exit 1
fi
case "$2" in
1 | 2 | 3)
echo "Run option selected with choice: $2"
run_choice="$2"
;;
*)
echo "Invalid choice: $2. Option --run requires an argument (1, 2, or 3)"
exit 1
;;
esac
shift 2
;;
--)
shift
break
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
done
# Handle non-option arguments (if any)
for arg in "$@"; do
echo "Non-option argument: $arg"
done
# Set default to local if no option is provided
if [[ $exec_destination == "" ]]; then
exec_destination="local"
echo "No option specified. Defaulting to local."
fi
}
display_version
parse_options "$@"
echo ""
# Install dependencies
echo "Installing dependencies"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Linux detected"
sudo apt-get update
sudo apt-get install -y -q jq wget python3 python3-pip git
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "MacOS detected"
brew install -q jq wget python3 git
elif [[ "$OSTYPE" == "win32" ]]; then
echo "Windows detected"
exit 1
else
echo "Unsupported OS"
exit 1
fi
# Clone the auto-spacemesh repo
echo "Cloning auto-spacemesh repo"
rm -rf /tmp/auto-spacemesh
git clone -q https://github.com/smeshcloud/auto-spacemesh.git /tmp/auto-spacemesh
cd /tmp/auto-spacemesh
# Install Python dependencies
echo "Installing Python dependencies"
pip3 install -qqq -r requirements.txt
# Present the user with choices and loop if run option is not specified
while true; do
if [[ -z $run_choice ]]; then
echo ""
echo "Run option:"
echo ""
echo "1. Generate a new Spacemesh node/smesher config"
echo "2. Generate a new Spacemesh node/smesher config and start smeshing"
echo "3. Start smeshing using an existing Spacemesh node/smesher config"
echo "4. Start smeshing using an existing Spacemesh node/smesher config and start smeshing"
echo "5. Exit"
read -p "> " choice
echo ""
else
choice=$run_choice
run_choice=""
fi
# Execute the user's choice
case "$choice" in
1)
echo "Generating a new Spacemesh node/smesher config"
bash stage1.sh
;;
2)
echo "Generating a new Spacemesh node/smesher stage1 config and start smeshing"
bash stage1.sh
python3 stage2.py
;;
3)
# Ask the user for the directory of the Spacemesh node/smesher config and default to /tmp/stage1 on Linux and MacOS and C:\TEMP\stage1 on Windows
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
default_path="/tmp/stage1"
elif [[ "$OSTYPE" == "darwin"* ]]; then
default_path="/tmp/stage1"
elif [[ "$OSTYPE" == "win32" ]]; then
default_path="C:\TEMP\stage1"
else
echo "Unsupported OS"
exit 1
fi
echo -n "Path to existing Spacemesh node/smesher stage1 config [${default_path}]: "
read -r path
path=${path:-$default_path}
echo ""
# Check if the directory exists
if [[ ! -d "$path" ]]; then
echo "Directory does not exist"
else
echo "Starting the smesher using stage1 config in $path"
python3 stage2.py "$path"
fi
;;
4)
# Ask the user for the directory of the Spacemesh node/smesher config and default to /tmp/stage1 on Linux and MacOS and C:\TEMP\stage1 on Windows
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
default_path="/tmp/stage1"
elif [[ "$OSTYPE" == "darwin"* ]]; then
default_path="/tmp/stage1"
elif [[ "$OSTYPE" == "win32" ]]; then
default_path="C:\TEMP\stage1"
else
echo "Unsupported OS"
exit 1
fi
echo -n "Path to existing Spacemesh node/smesher stage1 config [${default_path}]: "
read -r path
path=${path:-$default_path}
echo ""
# Check if the directory exists
if [[ ! -d "$path" ]]; then
echo "Directory does not exist"
else
echo "Starting the smesher using stage1 config in $path"
python3 stage2.py "$path"
python3 stage3.py
fi
;;
5)
echo "Exiting"
exit 0
;;
*)
echo "Invalid choice"
;;
esac
done