deploy-infra.sh 2.7 KB

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