Skip to content

Commit 3c6c5c6

Browse files
committed
Update pool_creation.sh
1 parent 4e4d954 commit 3c6c5c6

File tree

1 file changed

+133
-26
lines changed

1 file changed

+133
-26
lines changed

pool_creation.sh

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ LOG_DIR="/var/log"
1212

1313
# Function to generate a random 25-character password
1414
generate_password() {
15-
echo "generating password"
16-
sudo mkdir -p $(dirname "$PASSWORD_FILE")
17-
sudo cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c 25 > "$PASSWORD_FILE"
15+
echo "Checking if a password needs to be generated..."
16+
if [ ! -f "$PASSWORD_FILE" ]; then
17+
echo "Password file does not exist. Generating password..."
18+
sudo mkdir -p $(dirname "$PASSWORD_FILE")
19+
sudo cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c 25 > "$PASSWORD_FILE"
20+
echo "Password generated and saved to $PASSWORD_FILE"
21+
else
22+
echo "Password file already exists. No new password generated."
23+
fi
1824
}
1925

26+
2027
# Function to install Go 1.21 from source
2128
install_go() {
2229
echo "Installing go"
@@ -44,8 +51,6 @@ install_go() {
4451
fi
4552
}
4653

47-
48-
4954
# Function to install Rust and Cargo
5055
install_rust() {
5156
echo "Installing rust"
@@ -128,7 +133,10 @@ setup_node_service() {
128133
echo "setup_node_service at $node_service_file_path"
129134
# Check if the file exists and then remove it
130135
if [ -f "$node_service_file_path" ]; then
136+
sudo systemctl stop sugarfunge-node.service
137+
sudo systemctl disable sugarfunge-node.service
131138
sudo rm "$node_service_file_path"
139+
sudo systemctl daemon-reload
132140
echo "Removed $node_service_file_path."
133141
else
134142
echo "$node_service_file_path does not exist."
@@ -178,7 +186,10 @@ setup_api_service() {
178186
echo "setup_api_service at $api_service_file_path"
179187
# Check if the file exists and then remove it
180188
if [ -f "$api_service_file_path" ]; then
189+
sudo systemctl stop sugarfunge-api.service
190+
sudo systemctl disable sugarfunge-api.service
181191
sudo rm "$api_service_file_path"
192+
sudo systemctl daemon-reload
182193
echo "Removed $api_service_file_path."
183194
else
184195
echo "$api_service_file_path does not exist."
@@ -228,7 +239,10 @@ setup_gofula_service() {
228239
echo "setup go-fula service at $gofula_service_file_path"
229240
# Check if the file exists and then remove it
230241
if [ -f "$gofula_service_file_path" ]; then
242+
sudo systemctl stop go-fula.service
243+
sudo systemctl disable go-fula.service
231244
sudo rm "$gofula_service_file_path"
245+
sudo systemctl daemon-reload
232246
echo "Removed $gofula_service_file_path."
233247
else
234248
echo "$gofula_service_file_path does not exist."
@@ -240,7 +254,7 @@ After=network.target
240254
241255
[Service]
242256
Type=simple
243-
ExecStart=/home/$USER/go-fula/go-fula
257+
ExecStart=/home/$USER/go-fula/go-fula --config /home/$USER/.fula/config.yaml
244258
Restart=always
245259
RestartSec=10s
246260
StartLimitInterval=5min
@@ -257,40 +271,76 @@ EOF
257271

258272
# Function to fund an account
259273
fund_account() {
260-
echo "fund_account"
261-
secret_seed=$(cat "$SECRET_DIR/secret_seed.txt")
274+
echo "Checking account balance before funding..."
262275
account=$(cat "$SECRET_DIR/account.txt")
263-
curl -X POST https://api.node3.functionyard.fula.network/account/fund \
276+
277+
# Make the API request and capture the HTTP status code
278+
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST https://api.node3.functionyard.fula.network/account/balance \
264279
-H "Content-Type: application/json" \
265-
-d "{\"seed\": \"$MASTER_SEED\", \"amount\": 1000000000000000000, \"to\": \"$account\"}"
280+
-d "{\"account\": \"$account\"}")
281+
282+
# Check if the status code is anything other than 200
283+
if [ "$response" != "200" ]; then
284+
echo "Account is not funded or an error occurred. HTTP Status: $response. Attempting to fund account..."
285+
secret_seed=$(cat "$SECRET_DIR/secret_seed.txt")
286+
287+
# Fund the account
288+
fund_response=$(curl -s -X POST https://api.node3.functionyard.fula.network/account/fund \
289+
-H "Content-Type: application/json" \
290+
-d "{\"seed\": \"$MASTER_SEED\", \"amount\": 1000000000000000000, \"to\": \"$account\"}")
291+
292+
echo "Fund response: $fund_response"
293+
else
294+
echo "Account is already funded. HTTP Status: $response."
295+
fi
266296
}
267297

298+
299+
268300
# Function to create a pool
269301
create_pool() {
270-
echo "create_pool"
271-
seed=$(cat "$SECRET_DIR/secret_seed.txt")
272-
node_peerid=$(cat "$SECRET_DIR/node_peerid.txt")
302+
echo "Checking if pool already exists..."
273303
pool_name=$1
274304
region=$2
275305

276-
response=$(curl -X POST https://api.node3.functionyard.fula.network/fula/pool/create \
277-
-H "Content-Type: application/json" \
278-
-d "{\"seed\": \"$seed\", \"pool_name\": \"$pool_name\", \"peer_id\": \"$node_peerid\", \"region\": \"$region\"}")
306+
# Get the list of existing pools
307+
pools_response=$(curl -s -X GET https://api.node3.functionyard.fula.network/fula/pool \
308+
-H "Content-Type: application/json")
279309

280-
pool_id=$(echo $response | jq '.pool_id')
281-
echo "Pool ID: $pool_id"
310+
# Check if the current region exists in the list of pools
311+
if echo "$pools_response" | jq --arg region "$region" '.pools[] | select(.region == $region)' | grep -q 'pool_id'; then
312+
echo "Pool for region $region already exists. No need to create a new one."
313+
else
314+
echo "No existing pool found for region $region. Attempting to create a new pool..."
315+
seed=$(cat "$SECRET_DIR/secret_seed.txt")
316+
node_peerid=$(cat "$SECRET_DIR/node_peerid.txt")
317+
318+
# Create the pool
319+
create_response=$(curl -s -X POST https://api.node3.functionyard.fula.network/fula/pool/create \
320+
-H "Content-Type: application/json" \
321+
-d "{\"seed\": \"$seed\", \"pool_name\": \"$pool_name\", \"peer_id\": \"$node_peerid\", \"region\": \"$region\"}")
322+
323+
pool_id=$(echo $create_response | jq '.pool_id')
324+
echo "Created Pool ID: $pool_id"
282325

283-
# Update the Fula config file with the pool ID
284-
setup_fula_config "$pool_id"
326+
# Update the Fula config file with the pool ID
327+
setup_fula_config "$pool_id"
328+
fi
285329
}
286330

331+
287332
# Function to setup the Fula config file
288333
setup_fula_config() {
289-
echo "setup_fula_config"
334+
echo "Setting up Fula config..."
290335
pool_id="$1"
291-
mkdir -p /home/$USER/.fula/blox/store
292-
cat > /home/$USER/.fula/config.yaml << EOF
293-
identity:
336+
config_path="/home/$USER/.fula/config.yaml"
337+
338+
# Check if the Fula config file already exists
339+
if [ ! -f "$config_path" ]; then
340+
mkdir -p /home/$USER/.fula/blox/store
341+
342+
# Create the config file if it doesn't exist
343+
cat > "$config_path" << EOF
294344
storeDir: /home/$USER/.fula/blox/store
295345
poolName: "$pool_id"
296346
logLevel: info
@@ -317,10 +367,58 @@ ipniPublishDisabled: true
317367
ipniPublishInterval: 10s
318368
IpniPublishDirectAnnounce:
319369
- https://cid.contact/ingest/announce
320-
ipniPublisherIdentity:
321370
EOF
371+
echo "Fula config file created at $config_path."
372+
else
373+
echo "Fula config file already exists at $config_path. No changes made."
374+
fi
322375
}
323376

377+
verify_pool_creation() {
378+
echo "Verifying pool creation..."
379+
region=$1 # Pass the region as an argument to the function
380+
381+
# Get the list of existing pools
382+
pools_response=$(curl -s -X GET https://api.node3.functionyard.fula.network/fula/pool \
383+
-H "Content-Type: application/json")
384+
385+
# Check if the specified region exists in the list of pools
386+
if echo "$pools_response" | jq --arg region "$region" '.pools[] | select(.region == $region)' | grep -q 'pool_id'; then
387+
echo "Verification successful: Pool for region $region exists."
388+
else
389+
echo "ERROR: Verification failed: No pool found for region $region."
390+
fi
391+
}
392+
393+
check_services_status() {
394+
echo "Checking status of services..."
395+
396+
# Define your services
397+
declare -a services=("go-fula" "sugarfunge-node" "sugarfunge-api")
398+
399+
# Initialize a flag to keep track of service status
400+
all_services_running=true
401+
402+
for service in "${services[@]}"; do
403+
# Check the status of each service
404+
if ! sudo systemctl is-active --quiet "$service"; then
405+
echo "Error: Service $service is not running."
406+
all_services_running=false
407+
else
408+
echo "Service $service is running."
409+
fi
410+
done
411+
412+
# Final check to see if any service wasn't running
413+
if [ "$all_services_running" = false ]; then
414+
echo "ERROR: One or more services are not running. Please check the logs for more details."
415+
else
416+
echo "All services are running as expected."
417+
fi
418+
}
419+
420+
421+
324422
cleanup() {
325423
echo "Cleaning up..."
326424

@@ -355,7 +453,10 @@ main() {
355453

356454
# Set LIBCLANG_PATH for the user
357455
# echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" | sudo tee /etc/profile.d/libclang.sh
358-
echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" >> ~/.profile
456+
if ! grep -q 'export LIBCLANG_PATH=/usr/lib/llvm-14/lib/' ~/.profile; then
457+
echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" >> ~/.profile
458+
fi
459+
359460
source ~/.profile
360461

361462
# Install Go 1.21 from source
@@ -394,6 +495,12 @@ main() {
394495
cleanup
395496

396497
unset MASTER_SEED
498+
499+
# Verify pool creation
500+
verify_pool_creation "$region"
501+
502+
# Check the status of the services
503+
check_services_status
397504

398505
echo "Setup complete. Please review the logs and verify the services are running correctly."
399506
}

0 commit comments

Comments
 (0)