deploy-infra.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. set -e
  3. if [ -z "$1" ]; then
  4. echo "ERROR: Configuration file not passed"
  5. echo "Please use ./deploy-infra.sh PATH/TO/CONFIG to run this script"
  6. exit 1
  7. else
  8. echo "Using $1 file for config"
  9. source $1
  10. fi
  11. if [ $ACCOUNT_ID == None ]; then
  12. echo "Couldn't find Account ID, please check if AWS Profile $CLI_PROFILE is set"
  13. exit 1
  14. fi
  15. if [ ! -f "$KEY_PATH" ]; then
  16. echo "Key file not found at $KEY_PATH"
  17. exit 1
  18. fi
  19. # Deploy the CloudFormation template
  20. echo -e "\n\n=========== Deploying main.yml ==========="
  21. aws cloudformation deploy \
  22. --region $REGION \
  23. --profile $CLI_PROFILE \
  24. --stack-name $NEW_STACK_NAME \
  25. --template-file infrastructure.yml \
  26. --no-fail-on-empty-changeset \
  27. --capabilities CAPABILITY_NAMED_IAM \
  28. --parameter-overrides \
  29. EC2InstanceType=$DEFAULT_EC2_INSTANCE_TYPE \
  30. ValidatorEC2InstanceType=$VALIDATOR_EC2_INSTANCE_TYPE \
  31. RPCEC2InstanceType=$RPC_EC2_INSTANCE_TYPE \
  32. BuildEC2InstanceType=$BUILD_EC2_INSTANCE_TYPE \
  33. KeyName=$AWS_KEY_PAIR_NAME \
  34. EC2AMI=$EC2_AMI_ID \
  35. NumberOfValidators=$NUMBER_OF_VALIDATORS
  36. # If the deploy succeeded, get the IP, create inventory and configure the created instances
  37. if [ $? -eq 0 ]; then
  38. # Install additional Ansible roles from requirements
  39. ansible-galaxy install -r requirements.yml
  40. ASG=$(get_aws_export "AutoScalingGroup")
  41. VALIDATORS=""
  42. INSTANCES=$(aws autoscaling describe-auto-scaling-instances --profile $CLI_PROFILE \
  43. --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
  44. for ID in $INSTANCES
  45. do
  46. IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --profile $CLI_PROFILE --output text)
  47. VALIDATORS+="$IP\n"
  48. done
  49. RPC_NODES=$(get_aws_export "RPCPublicIp")
  50. BUILD_SERVER=$(get_aws_export "BuildPublicIp")
  51. BUCKET_NAME=$(get_aws_export "S3BucketName")
  52. DOMAIN_NAME=$(get_aws_export "DomainName")
  53. mkdir -p $DATA_PATH
  54. echo -e "[build]\n$BUILD_SERVER\n\n[validators]\n$VALIDATORS\n[rpc]\n$RPC_NODES" > $INVENTORY_PATH
  55. if [ -z "$EC2_AMI_ID" ]
  56. then
  57. echo -e "\n\n=========== Configuring the node servers ==========="
  58. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH build-code.yml \
  59. --extra-vars "branch_name=$BRANCH_NAME git_repo=$GIT_REPO build_local_code=$BUILD_LOCAL_CODE data_path=data-$NEW_STACK_NAME"
  60. fi
  61. echo -e "\n\n=========== Configuring the Build server ==========="
  62. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH setup-admin.yml \
  63. --extra-vars "local_dir=$LOCAL_CODE_PATH build_local_code=$BUILD_LOCAL_CODE"
  64. echo -e "\n\n=========== Configuring the chain spec file and Pioneer app ==========="
  65. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH chain-spec-pioneer.yml \
  66. --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX
  67. data_path=data-$NEW_STACK_NAME bucket_name=$BUCKET_NAME number_of_validators=$NUMBER_OF_VALIDATORS"
  68. echo -e "\n\n Pioneer URL: https://$DOMAIN_NAME"
  69. fi