deploy-infra.sh 3.5 KB

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