deploy-infra.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. # If the deploy succeeded, get the IP, create inventory and configure the created instances
  28. if [ $? -eq 0 ]; then
  29. VALIDATORS=$(aws cloudformation list-exports \
  30. --profile $CLI_PROFILE \
  31. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}PublicIp')].Value" \
  32. --output text | sed 's/\t\t*/\n/g')
  33. RPC_NODES=$(aws cloudformation list-exports \
  34. --profile $CLI_PROFILE \
  35. --query "Exports[?starts_with(Name,'${NEW_STACK_NAME}RPCPublicIp')].Value" \
  36. --output text | sed 's/\t\t*/\n/g')
  37. echo -e "[validators]\n$VALIDATORS\n\n[rpc]\n$RPC_NODES" > inventory
  38. if [ -z "$EC2_AMI_ID" ]
  39. then
  40. echo -e "\n\n=========== Configuring the servers ==========="
  41. ansible-playbook -i inventory --private-key $KEY_PATH build-code.yml --extra-vars "branch_name=$BRANCH_NAME"
  42. fi
  43. echo -e "\n\n=========== Configuring the chain spec file ==========="
  44. ansible-playbook -i inventory --private-key $KEY_PATH chain-spec-configuration.yml \
  45. --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX data_path=data-$NEW_STACK_NAME"
  46. fi