deploy-infra.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. set -e
  3. source bash-config.cfg
  4. ACCOUNT_ID=$(aws sts get-caller-identity --profile $CLI_PROFILE --query Account --output text)
  5. NEW_STACK_NAME="${STACK_NAME}-${ACCOUNT_ID}"
  6. if [ $ACCOUNT_ID == None ]; then
  7. echo "Couldn't find Account ID, please check if AWS Profile $CLI_PROFILE is set"
  8. exit 1
  9. fi
  10. if [ ! -f "$KEY_PATH" ]; then
  11. echo "Key file not found at $KEY_PATH"
  12. exit 1
  13. fi
  14. # Deploy the CloudFormation template
  15. echo -e "\n\n=========== Deploying main.yml ==========="
  16. aws cloudformation deploy \
  17. --region $REGION \
  18. --profile $CLI_PROFILE \
  19. --stack-name $NEW_STACK_NAME \
  20. --template-file main.yml \
  21. --no-fail-on-empty-changeset \
  22. --capabilities CAPABILITY_NAMED_IAM \
  23. --parameter-overrides \
  24. EC2InstanceType=$EC2_INSTANCE_TYPE \
  25. KeyName=$AWS_KEY_PAIR_NAME \
  26. EC2AMI=$EC2_AMI_ID \
  27. CreateAdminServer=$CREATE_ADMIN_SERVER
  28. # If the deploy succeeded, get the IP, create inventory and configure the created instances
  29. if [ $? -eq 0 ]; then
  30. VALIDATORS=$(aws cloudformation list-exports \
  31. --profile $CLI_PROFILE \
  32. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}PublicIp')].Value" \
  33. --output text | sed 's/\t\t*/\n/g')
  34. RPC_NODES=$(aws cloudformation list-exports \
  35. --profile $CLI_PROFILE \
  36. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}RPCPublicIp')].Value" \
  37. --output text | sed 's/\t\t*/\n/g')
  38. if [ "$CREATE_ADMIN_SERVER" = true ] ; then
  39. ADMIN_SERVER=$(aws cloudformation list-exports \
  40. --profile $CLI_PROFILE \
  41. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}AdminPublicIp')].Value" \
  42. --output text | sed 's/\t\t*/\n/g')
  43. ADMIN_INVENTORY="[admin]\n$ADMIN_SERVER\n\n"
  44. HOST="admin"
  45. fi
  46. echo -e "$ADMIN_INVENTORY[validators]\n$VALIDATORS\n\n[rpc]\n$RPC_NODES" > inventory
  47. if [ -z "$EC2_AMI_ID" ]
  48. then
  49. echo -e "\n\n=========== Configuring the node servers ==========="
  50. ansible-playbook -i inventory --private-key $KEY_PATH build-code.yml --extra-vars "branch_name=$BRANCH_NAME git_repo=$GIT_REPO build_local_code=$BUILD_LOCAL_CODE"
  51. fi
  52. echo -e "\n\n=========== Configuring the Admin server ==========="
  53. ansible-playbook -i inventory --private-key $KEY_PATH setup-admin.yml \
  54. --extra-vars "local_dir=$LOCAL_CODE_PATH run_on_admin_server=$CREATE_ADMIN_SERVER build_local_code=$BUILD_LOCAL_CODE"
  55. echo -e "\n\n=========== Configuring the chain spec file ==========="
  56. ansible-playbook -i inventory --private-key $KEY_PATH chain-spec-configuration.yml \
  57. --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX data_path=data-$NEW_STACK_NAME run_on_admin_server=$CREATE_ADMIN_SERVER"
  58. fi