Просмотр исходного кода

Add Ansible playbook, add 2nd instance and instance template

Anuj Bansal 3 лет назад
Родитель
Сommit
3e79576d51

+ 7 - 0
devops/infrastructure/ansible.cfg

@@ -0,0 +1,7 @@
+[defaults]
+host_key_checking = False
+remote_user = ubuntu
+# Use the YAML callback plugin.
+stdout_callback = yaml
+# Use the stdout_callback when running ad-hoc commands.
+bin_ansible_callbacks = True

+ 19 - 0
devops/infrastructure/configure.yml

@@ -0,0 +1,19 @@
+---
+
+- name: Configure Joystream Node
+  hosts: all
+  tasks:
+    - name: Creat bash profile file
+      command: "touch /home/ubuntu/.bash_profile"
+    - name: Git checkout
+      ansible.builtin.git:
+        repo: 'https://github.com/Joystream/joystream.git'
+        dest: ~/joystream
+    - name: Run setup script
+      command: ./setup.sh
+      args:
+        chdir: ~/joystream
+    - name: Build joystream node
+      shell: . ~/.bash_profile && yarn cargo-build >> output.log
+      args:
+        chdir: ~/joystream

+ 7 - 2
devops/infrastructure/deploy-infra.sh

@@ -3,8 +3,9 @@
 STACK_NAME=joystream-node
 REGION=us-east-1
 CLI_PROFILE=joystream-user
+KEY_PATH=""
 
-EC2_INSTANCE_TYPE=t2.micro
+EC2_INSTANCE_TYPE=t2.xlarge
 
 # Deploy the CloudFormation template
 echo -e "\n\n=========== Deploying main.yml ==========="
@@ -22,5 +23,9 @@ aws cloudformation deploy \
 if [ $? -eq 0 ]; then
   aws cloudformation list-exports \
     --profile $CLI_PROFILE \
-    --query "Exports[?Name=='InstanceDNS'].Value"
+    --query "Exports[?starts_with(Name,'${STACK_NAME}PublicIp')].Value" \
+    --output text | sed 's/\t\t*/\n/g' > inventory
+
+  echo -e "\n\n=========== Configuring the servers ==========="
+  ansible-playbook -i inventory -v --private-key $KEY_PATH configure.yml
 fi

+ 58 - 41
devops/infrastructure/main.yml

@@ -35,65 +35,82 @@ Resources:
         - Key: Name
           Value: !Ref AWS::StackName
 
-  Instance:
-    Type: AWS::EC2::Instance
-    CreationPolicy:
-      ResourceSignal:
-        Timeout: PT5M
-        Count: 1
+  InstanceLaunchTemplate:
+    Type: AWS::EC2::LaunchTemplate
     Metadata:
       AWS::CloudFormation::Init:
         config:
           packages:
-            yum:
+            apt:
               wget: []
               unzip: []
     Properties:
-      ImageId: !Ref EC2AMI
-      InstanceType: !Ref EC2InstanceType
-      KeyName: !Ref KeyName
-      SecurityGroupIds:
-        - !GetAtt SecurityGroup.GroupId
-      UserData:
-        Fn::Base64: !Sub |
-          #!/bin/bash -xe
-
-          # send script output to /tmp so we can debug boot failures
-          exec > /tmp/userdata.log 2>&1
-
-          # Update all packages
-          apt-get update -y
+      LaunchTemplateName: !Sub 'LaunchTemplate_${AWS::StackName}'
+      LaunchTemplateData:
+        ImageId: !Ref EC2AMI
+        InstanceType: !Ref EC2InstanceType
+        KeyName: !Ref KeyName
+        SecurityGroupIds:
+          - !GetAtt SecurityGroup.GroupId
+        BlockDeviceMappings:
+          - DeviceName: /dev/sda1
+            Ebs:
+              VolumeSize: '40'
+        UserData:
+          Fn::Base64: !Sub |
+            #!/bin/bash -xe
 
-          # Get latest cfn scripts;
-          # apt-get install -y python3-pip
-          apt-get install -y python3-setuptools
-          mkdir -p /opt/aws/bin
+            # send script output to /tmp so we can debug boot failures
+            exec > /tmp/userdata.log 2>&1
 
-          # pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
-          wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
-          python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
+            # Update all packages
+            apt-get update -y
 
-          ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
+            # Get latest cfn scripts;
+            # apt-get install -y python3-pip
+            apt-get install -y python3-setuptools
+            mkdir -p /opt/aws/bin
 
-          cd /home/ubuntu/
+            # pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
+            wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
+            python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
 
-          git clone https://github.com/Joystream/joystream.git
+            # curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
+            # echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
+            # apt-get update -y
+            # apt-get install -y yarn
 
-          cd joystream/
+            ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
 
-          touch setup_complete.txt
-
-          echo "Done" > setup_complete.txt
+  Instance:
+    Type: AWS::EC2::Instance
+    Properties:
+      LaunchTemplate:
+        LaunchTemplateId: !Ref InstanceLaunchTemplate
+        Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
+      Tags:
+        - Key: Name
+          Value: !Sub '${AWS::StackName}_1'
 
-          # Signal to CloudFormation that the instance is ready
-          /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --region ${AWS::Region} --resource Instance
+  Instance2:
+    Type: AWS::EC2::Instance
+    Properties:
+      LaunchTemplate:
+        LaunchTemplateId: !Ref InstanceLaunchTemplate
+        Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
       Tags:
         - Key: Name
-          Value: !Ref AWS::StackName
+          Value: !Sub '${AWS::StackName}_2'
 
 Outputs:
-  InstanceDNS:
+  PublicIp:
+    Description: The DNS name for the created instance
+    Value:  !Sub "${Instance.PublicIp}"
+    Export:
+      Name: !Sub "${AWS::StackName}PublicIp"
+
+  PublicIp2:
     Description: The DNS name for the created instance
-    Value: !Sub "http://${Instance.PublicDnsName}"
+    Value:  !Sub "${Instance2.PublicIp}"
     Export:
-      Name: InstanceDNS 
+      Name: !Sub "${AWS::StackName}PublicIp2"