deploy-node-network.yml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. name: Deploy Network
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. jsonInput:
  6. description: 'Json input (refer to .pipelines/deploy-node-network-inputs.json)'
  7. required: true
  8. default: ''
  9. defaults:
  10. run:
  11. working-directory: devops/aws
  12. jobs:
  13. deploy-node-network:
  14. name: Create CloudFormation stack and run Ansible playbook
  15. runs-on: ubuntu-latest
  16. env:
  17. STACK_NAME: ga-deploy-node-network-${{ github.run_number }}
  18. steps:
  19. - name: Checkout
  20. uses: actions/checkout@v2
  21. - name: Set env variables
  22. id: myoutputs # set the outputs
  23. run: |
  24. jsonInput=$(jq -r '.inputs.jsonInput' $GITHUB_EVENT_PATH)
  25. export ENCRYPTION_KEY=$(echo $jsonInput | jq -r '.encryptionKey.value')
  26. echo ::set-output name=encryptionKey::$ENCRYPTION_KEY
  27. echo "::add-mask::$ENCRYPTION_KEY"
  28. echo ::set-output name=gitRepo::$(echo $jsonInput | jq -r '.gitRepo.value')
  29. echo ::set-output name=branchName::$(echo $jsonInput | jq -r '.branchName.value')
  30. echo ::set-output name=keyName::$(echo $jsonInput | jq -r '.keyName.value')
  31. echo ::set-output name=numberOfValidators::$(echo $jsonInput | jq -r '.numberOfValidators.value')
  32. echo ::set-output name=validatorInstanceType::$(echo $jsonInput | jq -r '.validatorInstanceType.value')
  33. echo ::set-output name=buildInstanceType::$(echo $jsonInput | jq -r '.buildInstanceType.value')
  34. echo ::set-output name=rpcInstanceType::$(echo $jsonInput | jq -r '.rpcInstanceType.value')
  35. echo ::set-output name=ec2AMI::$(echo $jsonInput | jq -r '.ec2AMI.value')
  36. echo ::set-output name=networkSuffix::$(echo $jsonInput | jq -r '.networkSuffix.value')
  37. echo ::set-output name=deploymentType::$(echo $jsonInput | jq -r '.deploymentType.value')
  38. echo ::set-output name=volumeSize::$(echo $jsonInput | jq -r '.volumeSize.value')
  39. echo ::set-output name=rpcVolumeSize::$(echo $jsonInput | jq -r '.rpcVolumeSize.value')
  40. echo ::set-output name=skipChainSetup::$(echo $jsonInput | jq -r '.skipChainSetup.value')
  41. echo ::set-output name=runtimeProfile::$(echo $jsonInput | jq -r '.runtimeProfile.value')
  42. initialBalancesFile=$(echo $jsonInput | jq -r '.initialBalancesFile.value')
  43. initialMembersFile=$(echo $jsonInput | jq -r '.initialMembersFile.value')
  44. if [ -z "$initialBalancesFile" ]
  45. then
  46. echo ::set-output name=initialBalancesFilePath::''
  47. else
  48. wget $initialBalancesFile -O initial-balances.json
  49. echo ::set-output name=initialBalancesFilePath::'initial-balances.json'
  50. fi
  51. if [ -z "$initialMembersFile" ]
  52. then
  53. echo ::set-output name=initialMembersFilePath::''
  54. else
  55. wget $initialMembersFile -O initial-members.json
  56. echo ::set-output name=initialMembersFilePath::'initial-members.json'
  57. fi
  58. - name: Install Ansible dependencies
  59. run: pipx inject ansible-core boto3 botocore
  60. - name: Configure AWS credentials
  61. uses: aws-actions/configure-aws-credentials@v1
  62. with:
  63. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  64. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  65. aws-region: us-east-1
  66. - name: Check if CloudFormation stack exists
  67. id: stack_exists
  68. run: |
  69. if aws cloudformation describe-stacks --stack-name ${{ env.STACK_NAME }} >/dev/null 2>/dev/null; then
  70. echo "Stack already exists"
  71. exit 1
  72. else
  73. echo "Stack does not exist"
  74. fi
  75. - name: Deploy to AWS CloudFormation
  76. uses: aws-actions/aws-cloudformation-github-deploy@v1
  77. id: deploy_stack
  78. with:
  79. name: ${{ env.STACK_NAME }}
  80. template: devops/aws/cloudformation/infrastructure.yml
  81. no-fail-on-empty-changeset: '1'
  82. parameter-overrides: >-
  83. KeyName=${{ steps.myoutputs.outputs.keyName }},
  84. ValidatorEC2InstanceType=${{ steps.myoutputs.outputs.validatorInstanceType }},
  85. RPCEC2InstanceType=${{ steps.myoutputs.outputs.rpcInstanceType }},
  86. BuildEC2InstanceType=${{ steps.myoutputs.outputs.buildInstanceType }},
  87. EC2AMI=${{ steps.myoutputs.outputs.ec2AMI }},
  88. NumberOfValidators=${{ steps.myoutputs.outputs.numberOfValidators }},
  89. VolumeSize=${{ steps.myoutputs.outputs.volumeSize }},
  90. RPCVolumeSize=${{ steps.myoutputs.outputs.rpcVolumeSize }}
  91. - name: Prepare inventory for Ansible
  92. run: |
  93. ASG=${{ steps.deploy_stack.outputs.AutoScalingId }}
  94. VALIDATORS=""
  95. INSTANCES=$(aws autoscaling describe-auto-scaling-instances \
  96. --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
  97. for ID in $INSTANCES
  98. do
  99. IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --output text)
  100. VALIDATORS+="$IP\n"
  101. done
  102. echo -e "[build]\n${{ steps.deploy_stack.outputs.BuildPublicIp }}\n" >> inventory
  103. echo -e "[validators]\n$VALIDATORS" >> inventory
  104. echo -e "[rpc]\n${{ steps.deploy_stack.outputs.RPCPublicIp }}" >> inventory
  105. cat inventory
  106. - name: Run playbook to setup all hosts and compile joystream-node
  107. uses: dawidd6/action-ansible-playbook@v2
  108. # Build binaries if AMI not specified
  109. if: steps.myoutputs.outputs.ec2AMI == ''
  110. with:
  111. playbook: build-code.yml
  112. directory: devops/aws
  113. requirements: requirements.yml
  114. key: ${{ secrets.SSH_PRIVATE_KEY }}
  115. options: |
  116. --inventory inventory
  117. --extra-vars "branch_name=${{ steps.myoutputs.outputs.branchName }} \
  118. git_repo=${{ steps.myoutputs.outputs.gitRepo }} data_path=mydata \
  119. runtime_profile=${{ steps.myoutputs.outputs.runtimeProfile }}"
  120. - name: Run playbook to install additional utils on build server
  121. uses: dawidd6/action-ansible-playbook@v2
  122. if: steps.myoutputs.outputs.ec2AMI == ''
  123. with:
  124. playbook: setup-build-server.yml
  125. directory: devops/aws
  126. requirements: requirements.yml
  127. key: ${{ secrets.SSH_PRIVATE_KEY }}
  128. options: |
  129. --inventory inventory
  130. - name: Run playbook to configure chain-spec
  131. uses: dawidd6/action-ansible-playbook@v2
  132. with:
  133. playbook: configure-network.yml
  134. directory: devops/aws
  135. requirements: requirements.yml
  136. key: ${{ secrets.SSH_PRIVATE_KEY }}
  137. options: |
  138. --inventory inventory
  139. --extra-vars "network_suffix=${{ steps.myoutputs.outputs.networkSuffix }} data_path=mydata \
  140. number_of_validators=${{ steps.myoutputs.outputs.numberOfValidators }} \
  141. git_repo=${{ steps.myoutputs.outputs.gitRepo }} \
  142. deployment_type=${{ steps.myoutputs.outputs.deploymentType }} \
  143. branch_name=${{ steps.myoutputs.outputs.branchName }} \
  144. initial_members_file=${{ steps.myoutputs.outputs.initialMembersFilePath }} \
  145. initial_balances_file=${{ steps.myoutputs.outputs.initialBalancesFilePath }} \
  146. skip_chain_setup=${{ steps.myoutputs.outputs.skipChainSetup }}"
  147. - name: Terminate Build instance
  148. continue-on-error: true
  149. run: |
  150. echo "Deleting build instance with id ${{ steps.deploy_stack.outputs.BuildInstanceId }}"
  151. aws ec2 terminate-instances --instance-ids ${{ steps.deploy_stack.outputs.BuildInstanceId }}
  152. - name: Encrpyt the artifacts
  153. run: |
  154. 7z a -p${{ steps.myoutputs.outputs.encryptionKey }} chain-data.7z mydata/*
  155. - name: Save the output as an artifact
  156. uses: actions/upload-artifact@v2
  157. with:
  158. name: data-chainspec-auth
  159. path: devops/aws/chain-data.7z
  160. - name: Save the endpoints file as an artifact
  161. uses: actions/upload-artifact@v2
  162. with:
  163. name: endpoints
  164. path: devops/aws/endpoints.json
  165. - name: Delete CloudFormation Stack if any step failed
  166. # Skip only if stack already existed or all steps passed succesfully
  167. if: ( failure() || cancelled() ) && steps.stack_exists.outcome != 'failure'
  168. run: |
  169. echo "Deleting ${{ env.STACK_NAME }} stack"
  170. aws cloudformation delete-stack --stack-name ${{ env.STACK_NAME }}
  171. echo "Waiting for ${{ env.STACK_NAME }} to be deleted..."
  172. aws cloudformation wait stack-delete-complete --stack-name ${{ env.STACK_NAME }}