deploy-infra.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. get_aws_export () {
  20. RESULT=$(aws cloudformation list-exports \
  21. --profile $CLI_PROFILE \
  22. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}$1')].Value" \
  23. --output text | sed 's/\t\t*/\n/g')
  24. echo -e $RESULT | tr " " "\n"
  25. }
  26. # Deploy the CloudFormation template
  27. echo -e "\n\n=========== Deploying main.yml ==========="
  28. aws cloudformation deploy \
  29. --region $REGION \
  30. --profile $CLI_PROFILE \
  31. --stack-name $NEW_STACK_NAME \
  32. --template-file infrastructure.yml \
  33. --no-fail-on-empty-changeset \
  34. --capabilities CAPABILITY_NAMED_IAM \
  35. --parameter-overrides \
  36. EC2InstanceType=$DEFAULT_EC2_INSTANCE_TYPE \
  37. ValidatorEC2InstanceType=$VALIDATOR_EC2_INSTANCE_TYPE \
  38. RPCEC2InstanceType=$RPC_EC2_INSTANCE_TYPE \
  39. BuildEC2InstanceType=$BUILD_EC2_INSTANCE_TYPE \
  40. KeyName=$AWS_KEY_PAIR_NAME \
  41. EC2AMI=$EC2_AMI_ID \
  42. NumberOfValidators=$NUMBER_OF_VALIDATORS
  43. # If the deploy succeeded, get the IP, create inventory and configure the created instances
  44. if [ $? -eq 0 ]; then
  45. # Install additional Ansible roles from requirements
  46. ansible-galaxy install -r requirements.yml
  47. ASG=$(get_aws_export "AutoScalingGroup")
  48. VALIDATORS=""
  49. INSTANCES=$(aws autoscaling describe-auto-scaling-instances --profile $CLI_PROFILE \
  50. --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
  51. for ID in $INSTANCES
  52. do
  53. IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --profile $CLI_PROFILE --output text)
  54. VALIDATORS+="$IP\n"
  55. done
  56. RPC_NODES=$(get_aws_export "RPCPublicIp")
  57. BUILD_SERVER=$(get_aws_export "BuildPublicIp")
  58. BUCKET_NAME=$(get_aws_export "S3BucketName")
  59. DOMAIN_NAME=$(get_aws_export "DomainName")
  60. mkdir -p $DATA_PATH
  61. echo -e "[build]\n$BUILD_SERVER\n\n[validators]\n$VALIDATORS\n[rpc]\n$RPC_NODES" > $INVENTORY_PATH
  62. if [ -z "$EC2_AMI_ID" ]
  63. then
  64. echo -e "\n\n=========== Configuring the node servers ==========="
  65. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH build-code.yml \
  66. --extra-vars "branch_name=$BRANCH_NAME git_repo=$GIT_REPO build_local_code=$BUILD_LOCAL_CODE data_path=data-$NEW_STACK_NAME"
  67. fi
  68. echo -e "\n\n=========== Configuring the Build server ==========="
  69. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH setup-admin.yml \
  70. --extra-vars "local_dir=$LOCAL_CODE_PATH build_local_code=$BUILD_LOCAL_CODE"
  71. echo -e "\n\n=========== Configuring the chain spec file and Pioneer app ==========="
  72. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH chain-spec-pioneer.yml \
  73. --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX
  74. data_path=data-$NEW_STACK_NAME bucket_name=$BUCKET_NAME number_of_validators=$NUMBER_OF_VALIDATORS"
  75. echo -e "\n\n Pioneer URL: https://$DOMAIN_NAME"
  76. fi