deploy-infra.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 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. # If the deploy succeeded, get the IP, create inventory and configure the created instances
  38. if [ $? -eq 0 ]; then
  39. # Install additional Ansible roles from requirements
  40. ansible-galaxy install -r requirements.yml
  41. ASG=$(get_aws_export $NEW_STACK_NAME "AutoScalingGroup")
  42. VALIDATORS=""
  43. INSTANCES=$(aws autoscaling describe-auto-scaling-instances --profile $CLI_PROFILE \
  44. --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
  45. for ID in $INSTANCES
  46. do
  47. IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --profile $CLI_PROFILE --output text)
  48. VALIDATORS+="$IP\n"
  49. done
  50. RPC_NODES=$(get_aws_export $NEW_STACK_NAME "RPCPublicIp")
  51. BUILD_SERVER=$(get_aws_export $NEW_STACK_NAME "BuildPublicIp")
  52. BUCKET_NAME=$(get_aws_export $NEW_STACK_NAME "S3BucketName")
  53. DOMAIN_NAME=$(get_aws_export $NEW_STACK_NAME "DomainName")
  54. mkdir -p $DATA_PATH
  55. echo -e "[build]\n$BUILD_SERVER\n\n[validators]\n$VALIDATORS\n[rpc]\n$RPC_NODES" > $INVENTORY_PATH
  56. if [ -z "$EC2_AMI_ID" ]
  57. then
  58. echo -e "\n\n=========== Compile joystream-node on build server ==========="
  59. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH build-code.yml \
  60. --extra-vars "branch_name=$BRANCH_NAME git_repo=$GIT_REPO build_local_code=$BUILD_LOCAL_CODE data_path=data-$NEW_STACK_NAME"
  61. echo -e "\n\n=========== Install additional utils on build server ==========="
  62. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH setup-admin.yml
  63. fi
  64. echo -e "\n\n=========== Configure and start new validators, rpc node and pioneer ==========="
  65. ansible-playbook -i $INVENTORY_PATH --private-key $KEY_PATH chain-spec-pioneer.yml \
  66. --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX
  67. data_path=data-$NEW_STACK_NAME bucket_name=$BUCKET_NAME number_of_validators=$NUMBER_OF_VALIDATORS"
  68. echo -e "\n\n Pioneer URL: https://$DOMAIN_NAME"
  69. fi