瀏覽代碼

Add service file, better use of Ansible variables, support to use prebuilt AMI

Anuj Bansal 3 年之前
父節點
當前提交
87dae8c665

+ 1 - 1
devops/infrastructure/ansible.cfg

@@ -4,4 +4,4 @@ remote_user = ubuntu
 # Use the YAML callback plugin.
 stdout_callback = yaml
 # Use the stdout_callback when running ad-hoc commands.
-bin_ansible_callbacks = True
+bin_ansible_callbacks = True

+ 34 - 6
devops/infrastructure/chain-spec-configuration.yml

@@ -1,4 +1,4 @@
-- name: Configure chain spec on the deployed servers
+- name: Configure chain spec and start joystream-node service on the servers
   hosts: all
   vars:
     local_dir: ~/Joystream/joystream
@@ -6,6 +6,8 @@
     random_suffix: "{{ 10000 | random(1000) }}"
     data_path: ./data
     change_spec_path: "{{ data_path }}/chainspec.json"
+    remote_code_path: "/home/ubuntu/joystream"
+    remote_chain_spec_path: "{{ remote_code_path }}/chainspec.json"
   tasks:
   - name: Run subkey to generate node keys
     local_action: ansible.builtin.command {{ local_dir }}/target/release/chain-spec-builder generate -a 2 --chain-spec-path {{ change_spec_path }} --deployment live --endowed 1 --keystore-path {{ data_path }}
@@ -27,7 +29,7 @@
     run_once: true
 
   - name: Save output of chain spec to local file
-    local_action: copy content={{ chain_spec_output.stdout }} dest=./chain_spec_output.txt
+    local_action: copy content={{ chain_spec_output.stdout }} dest="{{ data_path }}/chain_spec_output.txt"
 
   - name: Change chain spec name, id, protocolId
     delegate_to: localhost
@@ -45,21 +47,47 @@
   - name: Copying chain spec file to server
     copy:
       src: "{{ change_spec_path }}"
-      dest: ~/joystream/chainspec.json
+      dest: "{{ remote_chain_spec_path }}"
+
+  - set_fact:
+      chain_path: "{{ remote_code_path }}/chains/{{ result.result.id }}"
+
+  - set_fact:
+      network_path: "{{ chain_path }}/network"
+      keystore_path: "{{ chain_path }}/keystore/"
   
+  - set_fact:
+      secret_path: "{{ network_path }}/secret"
+
   - name: Creating chains directory
     file:
       path: "{{ item }}"
       state: directory
     loop:
-      - "~/joystream/chains/{{ result.result.id }}/network"
+      - "{{ network_path }}"
 
   - name: Copy secret to remote host
     copy:
-      dest: "~/joystream/chains/{{ result.result.id }}/network/secret"
+      dest: "{{ secret_path }}"
       content: "{{ subkey_output.stdout }}"
 
   - name: Copy auth directory to remote host
     copy:
       src: "{{ data_path }}/auth-{{ ansible_play_batch.index(inventory_hostname) }}/"
-      dest: "~/joystream/chains/{{ result.result.id }}/keystore/"
+      dest: "{{ keystore_path }}"
+
+  - name: Create a service file
+    template:
+      src: joystream-node.service.j2
+      dest: /etc/systemd/system/joystream-node.service
+    vars:
+      keystore_path: "{{ keystore_path }}"
+      secret_path: "{{ secret_path }}"
+      keystore_path: "{{ remote_chain_spec_path }}"
+    become: yes
+
+  - name: Start service joystream-node, if not started
+    service:
+      name: joystream-node
+      state: started
+    become: yes

+ 18 - 9
devops/infrastructure/deploy-infra.sh

@@ -3,35 +3,44 @@
 STACK_NAME=joystream-node
 REGION=us-east-1
 CLI_PROFILE=joystream-user
-KEY_PATH="./joystream-key.pem"
-
+KEY_PATH=""
+AWS_KEY_PAIR_NAME=""
 BRANCH_NAME=sumer
-
 LOCAL_CODE_PATH="~/Joystream/joystream"
-
 EC2_INSTANCE_TYPE=t2.xlarge
+# Set a prebuilt AMI if required
+EC2_AMI_ID=""
+
+ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
+
+NEW_STACK_NAME="${STACK_NAME}-${ACCOUNT_ID}"
 
 # Deploy the CloudFormation template
 echo -e "\n\n=========== Deploying main.yml ==========="
 aws cloudformation deploy \
   --region $REGION \
   --profile $CLI_PROFILE \
-  --stack-name $STACK_NAME \
+  --stack-name $NEW_STACK_NAME \
   --template-file main.yml \
   --no-fail-on-empty-changeset \
   --capabilities CAPABILITY_NAMED_IAM \
   --parameter-overrides \
-    EC2InstanceType=$EC2_INSTANCE_TYPE
+    EC2InstanceType=$EC2_INSTANCE_TYPE \
+    KeyName=$AWS_KEY_PAIR_NAME \
+    EC2AMI=$EC2_AMI_ID
 
 # If the deploy succeeded, get the IP, create inventory and configure the created instances
 if [ $? -eq 0 ]; then
   aws cloudformation list-exports \
     --profile $CLI_PROFILE \
-    --query "Exports[?starts_with(Name,'${STACK_NAME}PublicIp')].Value" \
+    --query "Exports[?starts_with(Name,'${NEW_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 build-code.yml --extra-vars "branch_name=$BRANCH_NAME"
+  if [ -z "$EC2_AMI_ID" ]
+  then
+    echo -e "\n\n=========== Configuring the servers ==========="
+    ansible-playbook -i inventory -v --private-key $KEY_PATH build-code.yml --extra-vars "branch_name=$BRANCH_NAME"
+  fi
 
   echo -e "\n\n=========== Configuring the chain spec file ==========="
   ansible-playbook -i inventory -v --private-key $KEY_PATH chain-spec-configuration.yml --extra-vars "local_dir=$LOCAL_CODE_PATH"

+ 21 - 0
devops/infrastructure/joystream-node.service.j2

@@ -0,0 +1,21 @@
+[Unit]
+Description=Joystream Node
+After=network.target
+
+[Service]
+Type=simple
+User=ubuntu
+WorkingDirectory=/home/ubuntu/joystream/
+ExecStart=/home/ubuntu/joystream/target/release/joystream-node \
+        --chain {{ remote_chain_spec_path }} \
+        --pruning archive \
+        --node-key-file {{ secret_path }} \
+        --keystore-path {{ keystore_path }}\
+        --validator \
+        --log runtime,txpool,transaction-pool,trace=sync
+Restart=on-failure
+RestartSec=3
+LimitNOFILE=10000
+
+[Install]
+WantedBy=multi-user.target