-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_pool_stack_creation.sh
139 lines (123 loc) · 5.9 KB
/
aws_pool_stack_creation.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
#!/bin/bash
bucket_name="fula-validator" # Change this to your actual S3 bucket name
pem_key_path="s3://${bucket_name}/functionland.pem"
public_key_path="s3://${bucket_name}/functionland-public.b64"
cloudformation_yaml_path="https://raw.githubusercontent.com/functionland/automatic-poolnode-creation-script/main/aws-pools.yaml"
# Download key pair from S3
aws s3 cp "$pem_key_path" /home/cloudshell-user/functionland.pem
aws s3 cp "$public_key_path" /home/cloudshell-user/functionland-public.b64
# Download the CloudFormation YAML file
curl -o /home/cloudshell-user/aws-pools.yaml "$cloudformation_yaml_path"
# Set permissions for the key file
chmod 600 /home/cloudshell-user/functionland.pem
# Define your regions
regions=(
us-east-2
us-west-1
#af-south-1
#ap-east-1
#ap-south-1
#ap-south-2
#ap-northeast-2
#ap-southeast-1
#ap-southeast-2
#ap-northeast-1
#ca-central-1
#eu-central-1
#eu-west-1
#eu-west-2
#eu-south-1
#eu-west-3
eu-north-1
#eu-central-2
eu-south-2
#me-central-1
#il-central-1
#sa-east-1
)
# Seed parameter
seed_parameter=$1
cf_zone_parameter=$2
cf_token=$3
# Maximum number of retries
max_retries=3
# Array to keep track of failed regions
declare -a failed_regions
declare -a instance_details
# Function to process region
process_region() {
local region=$1
echo "Processing region: $region"
# Import key pair and create CloudFormation stack
# Import key pair
aws ec2 import-key-pair --key-name functionland --public-key-material file:///home/cloudshell-user/functionland-public.b64 --region $region
stack_existing_status=$(aws cloudformation describe-stacks --stack-name FulaPoolStack --region $region --query 'Stacks[0].StackStatus' --output text 2>&1)
# If the stack is in ROLLBACK_COMPLETE status, delete it
if [ "$stack_existing_status" == "ROLLBACK_COMPLETE" ]; then
echo "Stack in ROLLBACK_COMPLETE status, deleting stack in region $region"
aws cloudformation delete-stack --stack-name FulaPoolStack --region $region
echo "Waiting for stack deletion to complete..."
aws cloudformation wait stack-delete-complete --stack-name FulaPoolStack --region $region
elif [[ $stack_existing_status == "CREATE_COMPLETE" ]]; then
echo "Stack already exists in region $region"
return
fi
sleep 15
echo "creating stack for region $region"
# Create CloudFormation stack
creation_output=$(aws cloudformation create-stack --stack-name FulaPoolStack --template-body file:///home/cloudshell-user/aws-pools.yaml --parameters ParameterKey=UbuntuAmiId,ParameterValue=$(aws ec2 describe-images --region $region --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*" "Name=state,Values=available" --query "Images | sort_by(@, &CreationDate) | [-1].ImageId" --output text) ParameterKey=SeedParameter,ParameterValue="$seed_parameter" --region $region --capabilities CAPABILITY_IAM 2>&1)
# Wait for stack creation to complete
echo "waitting for creation of stack for region $region"
aws cloudformation wait stack-create-complete --stack-name FulaPoolStack --region $region
stack_status=$(aws cloudformation describe-stacks --stack-name FulaPoolStack --region $region --query 'Stacks[0].StackStatus' --output text)
if [[ $stack_status == "CREATE_COMPLETE"* ]]; then
echo "Stack creation succeeded in region $region"
sleep 20
# Retrieve the public IP of the instance
instance_ip=$(aws ec2 describe-instances --region $region --query 'Reservations[].Instances[?State.Name==`running`].PublicIpAddress' --output text)
if [ -n "$instance_ip" ]; then
instance_details+=("$region, $instance_ip, SUCCESS")
echo "Instance IP: $instance_ip"
sleep 10
# SSH Command (This part needs to be run from a system where SSH is possible)
ssh -o StrictHostKeyChecking=no -i /home/cloudshell-user/functionland.pem ubuntu@$instance_ip "nohup bash ~/automatic-poolnode-creation-script/pool_creation.sh --seed=$seed_parameter --cloudflarezone=$cf_zone_parameter --cloudflaretoken=$cf_token --domain=functionyard.fula.network --api=https://api.node3.functionyard.fula.network > ~/pool_creation_log.txt 2>&1 &" &
else
echo "Failed to retrieve the instance IP address."
instance_details+=("$region, no IP, FAILED")
fi
elif [[ $stack_status == "ROLLBACK_COMPLETE" ]]; then
echo "Stack creation failed in region $region, attempting to delete stack"
aws cloudformation describe-stack-events --stack-name FulaPoolStack --region $region --query 'StackEvents[?ResourceStatus==`CREATE_FAILED`].[ResourceStatusReason]' --output text
aws cloudformation delete-stack --stack-name FulaPoolStack --region $region
echo "Waiting for stack deletion to complete..."
aws cloudformation wait stack-delete-complete --stack-name FulaPoolStack --region $region
instance_details+=("$region, $instance_ip, DELETED")
failed_regions+=("$region")
fi
}
# Initial loop through each region
for region in "${regions[@]}"; do
process_region "$region"
sleep 10
done
sleep 180
# Retry failed regions
for (( i=0; i<max_retries; i++ )); do
if [ ${#failed_regions[@]} -eq 0 ]; then
echo "All regions processed successfully."
break
fi
echo "Retrying failed regions: ${failed_regions[*]}"
current_failed=("${failed_regions[@]}")
failed_regions=() # Reset failed regions
for region in "${current_failed[@]}"; do
process_region "$region"
sleep 10
done
done
if [ ${#failed_regions[@]} -ne 0 ]; then
echo "Failed to create stacks in the following regions after $max_retries attempts: ${failed_regions[*]}"
fi
# Write instance details to file
echo "Writing instance details to /home/cloudshell-user/status.txt"
printf "%s\n" "${instance_details[@]}" > /home/cloudshell-user/status.txt