Skip to content

Commit a104349

Browse files
committed
Update pool_creation.sh
1 parent 98ce91a commit a104349

File tree

1 file changed

+85
-12
lines changed

1 file changed

+85
-12
lines changed

pool_creation.sh

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,65 @@ SECRET_DIR="/home/$USER/.secrets"
99
DATA_DIR="/home/$USER/data"
1010
LOG_DIR="/var/log"
1111

12+
# Function to map AWS region to your custom region naming convention
13+
get_region_name() {
14+
local aws_region=$1
15+
case $aws_region in
16+
us-east-1) echo "UsEastNVirginia" ;;
17+
us-east-2) echo "UsEastOhio" ;;
18+
us-west-1) echo "UsWestNCalifornia" ;;
19+
us-west-2) echo "UsWestOregon" ;;
20+
af-south-1) echo "AfricaCapeTown" ;;
21+
ap-east-1) echo "AsiaPacificHongKong" ;;
22+
ap-south-1) echo "AsiaPacificMumbai" ;;
23+
ap-northeast-3) echo "AsiaPacificOsaka" ;;
24+
ap-northeast-2) echo "AsiaPacificSeoul" ;;
25+
ap-southeast-1) echo "AsiaPacificSingapore" ;;
26+
ap-southeast-2) echo "AsiaPacificSydney" ;;
27+
ap-northeast-1) echo "AsiaPacificTokyo" ;;
28+
ca-central-1) echo "CanadaCentral" ;;
29+
eu-central-1) echo "EuropeFrankfurt" ;;
30+
eu-west-1) echo "EuropeIreland" ;;
31+
eu-west-2) echo "EuropeLondon" ;;
32+
eu-south-1) echo "EuropeMilan" ;;
33+
eu-west-3) echo "EuropeParis" ;;
34+
eu-north-1) echo "EuropeStockholm" ;;
35+
eu-central-2) echo "EuropeZurich" ;;
36+
eu-south-2) echo "EuropeSpain" ;;
37+
me-central-1) echo "MiddleEastUAE" ;;
38+
il-central-1) echo "IsraelTelAviv" ;;
39+
sa-east-1) echo "SouthAmericaSaoPaulo" ;;
40+
*) echo "" ;;
41+
esac
42+
}
43+
44+
# Function to get the AWS Token
45+
get_aws_token() {
46+
echo $(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" -s)
47+
}
48+
49+
# Function to get the AWS Region
50+
get_aws_region() {
51+
local token=$1
52+
echo $(curl -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/placement/region -s)
53+
}
54+
55+
# Main function to find pool region on aws
56+
find_pool_region_aws() {
57+
local token=$(get_aws_token)
58+
if [ -n "$token" ]; then
59+
local aws_region=$(get_aws_region "$token")
60+
if [ -n "$aws_region" ]; then
61+
local pool_region=$(get_region_name "$aws_region")
62+
echo "$pool_region"
63+
else
64+
echo ""
65+
fi
66+
else
67+
echo ""
68+
fi
69+
}
70+
1271
# Function to generate a random 25-character password
1372
generate_password() {
1473
echo "Checking if a password needs to be generated..."
@@ -96,7 +155,7 @@ setup_and_extract_keys() {
96155
echo "setup_and_extract_keys"
97156
mkdir -p "$SECRET_DIR"
98157
if [ ! -f "$SECRET_DIR/secret_phrase.txt" ] || [ ! -f "$SECRET_DIR/secret_seed.txt" ]; then
99-
output=$(./sugarfunge-node/target/release/sugarfunge-node key generate --scheme Sr25519 --password-filename="$PASSWORD_FILE" 2>&1)
158+
output=$(/home/$USER/sugarfunge-node/target/release/sugarfunge-node key generate --scheme Sr25519 --password-filename="$PASSWORD_FILE" 2>&1)
100159
echo "$output"
101160
secret_phrase=$(echo "$output" | grep "Secret phrase:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
102161
echo "$secret_phrase" > "$SECRET_DIR/secret_phrase.txt"
@@ -113,8 +172,8 @@ setup_and_extract_keys() {
113172
insert_keys() {
114173
echo "insert_keys"
115174
secret_phrase=$(cat "$SECRET_DIR/secret_phrase.txt")
116-
./sugarfunge-node/target/release/sugarfunge-node key insert --base-path="$DATA_DIR" --chain $HOME/sugarfunge-node/customSpecRaw.json --scheme Sr25519 --suri "$secret_phrase" --password-filename "$PASSWORD_FILE" --key-type aura
117-
./sugarfunge-node/target/release/sugarfunge-node key insert --base-path="$DATA_DIR" --chain $HOME/sugarfunge-node/customSpecRaw.json --scheme Ed25519 --suri "$secret_phrase" --password-filename "$PASSWORD_FILE" --key-type gran
175+
/home/$USER/sugarfunge-node/target/release/sugarfunge-node key insert --base-path="$DATA_DIR" --chain $HOME/sugarfunge-node/customSpecRaw.json --scheme Sr25519 --suri "$secret_phrase" --password-filename "$PASSWORD_FILE" --key-type aura
176+
/home/$USER/sugarfunge-node/target/release/sugarfunge-node key insert --base-path="$DATA_DIR" --chain $HOME/sugarfunge-node/customSpecRaw.json --scheme Ed25519 --suri "$secret_phrase" --password-filename "$PASSWORD_FILE" --key-type gran
118177
}
119178

120179
# Function to set up and start node service
@@ -241,7 +300,7 @@ setup_gofula_service() {
241300

242301
# Initialize go-fula and extract the blox peer ID
243302
init_output=$(/home/$USER/go-fula/go-fula --config /home/$USER/.fula/config.yaml --initOnly)
244-
blox_peer_id=$(echo "$init_output" | grep "blox peer ID" | awk '{print $5}')
303+
blox_peer_id=$(echo "$init_output" | awk '/blox peer ID:/ {print $NF}')
245304
# Check if blox_peer_id is empty and exit with an error if it is
246305
if [ -z "$blox_peer_id" ]; then
247306
echo "Error: Failed to extract blox peer ID. Exiting."
@@ -251,6 +310,7 @@ setup_gofula_service() {
251310
echo "Extracted blox peer ID: $blox_peer_id"
252311

253312
# Save the blox peer ID to the file
313+
mkdir -p "$SECRET_DIR"
254314
echo "$blox_peer_id" > "$SECRET_DIR/node_peerid.txt"
255315
echo "Blox peer ID saved to $SECRET_DIR/node_peerid.txt"
256316

@@ -412,9 +472,10 @@ verify_pool_creation() {
412472
generate_node_key() {
413473
config_path="/home/$USER/.fula/config.yaml"
414474
echo "Checking identity in $config_path..."
475+
sudo chmod +r /home/ubuntu/.fula/config.yaml
415476

416477
# Check if the identity field exists and has a value
417-
identity=$(yq e '.identity' "$config_path")
478+
identity=$(python3 -c "import yaml; print(yaml.safe_load(open('/home/ubuntu/.fula/config.yaml'))['identity'])")
418479

419480
if [ -z "$identity" ]; then
420481
echo "Error: 'identity' field is missing or empty in $config_path."
@@ -432,7 +493,7 @@ generate_node_key() {
432493
fi
433494

434495
# Generate the peer ID from the node key
435-
generated_peer_id=$(./target/release/sugarfunge-node key inspect-node-key --file "$SECRET_DIR/node_key.txt")
496+
generated_peer_id=$(/home/$USER/sugarfunge-node/target/release/sugarfunge-node key inspect-node-key --file "$SECRET_DIR/node_key.txt")
436497

437498
# Read the stored peer ID from the file
438499
stored_peer_id=$(cat "$SECRET_DIR/node_peerid.txt")
@@ -495,23 +556,35 @@ cleanup() {
495556
main() {
496557
# Set DEBIAN_FRONTEND to noninteractive to avoid interactive prompts
497558
export DEBIAN_FRONTEND=noninteractive
559+
echo "\$nrconf{restart} = 'a';" | sudo tee /etc/needrestart/needrestart.conf
498560

499-
# Check if a region is provided
500-
if [ $# -lt 2 ]; then
501-
echo "Please provide 1:MASTER seed and 2:region as arguments."
561+
# Check if a master seed is provided
562+
if [ $# -lt 1 ]; then
563+
echo "Please provide at least the MASTER seed as an argument."
502564
exit 1
503565
fi
504566

505567
export MASTER_SEED=$1
506568

507-
region=$2
569+
if [ $# -eq 1 ]; then
570+
# Only one argument provided, find the region automatically
571+
region=$(find_pool_region_aws)
572+
echo "region was determined from aws instance: $region"
573+
if [ -z "$region" ]; then
574+
echo "Could not determine the region automatically. Please provide the region as a second argument."
575+
exit 1
576+
fi
577+
else
578+
# Region provided as second argument
579+
region=$2
580+
fi
508581
pool_name=$(echo "$region" | sed -e 's/\([A-Z]\)/ \1/g' -e 's/^ //')
509582

510583
echo "creating region=$region and pool_name=$pool_name"
511584

512585
# Update and install dependencies
513586
sudo apt update
514-
sudo apt install -y wget git curl build-essential jq pkg-config libssl-dev protobuf-compiler llvm libclang-dev clang plocate cmake
587+
sudo apt install -y wget git curl build-essential jq pkg-config libssl-dev protobuf-compiler llvm libclang-dev clang plocate cmake
515588

516589
# Set LIBCLANG_PATH for the user
517590
# echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" | sudo tee /etc/profile.d/libclang.sh
@@ -565,7 +638,7 @@ main() {
565638
verify_pool_creation "$region"
566639

567640
# Check the status of the services
568-
sleep 5
641+
sleep 10
569642
check_services_status
570643

571644
echo "Setup complete. Please review the logs and verify the services are running correctly."

0 commit comments

Comments
 (0)