Jelajahi Sumber

Add Admin server configuration, break playbook into roles, add local code support

Anuj Bansal 3 tahun lalu
induk
melakukan
695944dc3f

+ 1 - 0
devops/infrastructure/ansible.cfg

@@ -5,3 +5,4 @@ remote_user = ubuntu
 stdout_callback = yaml
 # Use the stdout_callback when running ad-hoc commands.
 bin_ansible_callbacks = True
+interpreter_python = /usr/bin/python3

+ 16 - 3
devops/infrastructure/bash-config.cfg

@@ -1,12 +1,25 @@
+#### PARAMETERS USED BY AWS
+
 STACK_NAME=joystream-node
 REGION=us-east-1
 CLI_PROFILE=joystream-user
 KEY_PATH="/Users/anuj/Joystream/anuj-key.pem"
 AWS_KEY_PAIR_NAME="anuj-key"
-BRANCH_NAME=sumer
-LOCAL_CODE_PATH="~/Joystream/joystream"
 EC2_INSTANCE_TYPE=t2.xlarge
-NETWORK_SUFFIX=7891
+
+# If true will create another instance to generate chain spec file and keys
 CREATE_ADMIN_SERVER=true
+
 # Set a prebuilt AMI if required
 EC2_AMI_ID="ami-064f99551235fb1ac"
+
+#### PARAMETERS USED BY ANSIBLE
+
+LOCAL_CODE_PATH="~/Joystream/joystream"
+NETWORK_SUFFIX=7891
+
+GIT_REPO="https://github.com/Joystream/joystream.git"
+BRANCH_NAME=sumer
+
+# If true will build LOCAL_CODE_PATH otherwise will pull from GIT_REPO:BRANCH_NAME
+BUILD_LOCAL_CODE=false

+ 9 - 18
devops/infrastructure/build-code.yml

@@ -2,22 +2,13 @@
 
 - name: Get latest Joystream code and build it
   hosts: all
-  vars:
-    # branch_name can be sha1, tag, branch
-    branch_name: "master"
   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
-        version: "{{ branch_name }}"
-    - name: Run setup script
-      command: ./setup.sh
-      args:
-        chdir: ~/joystream
-    - name: Build joystream node
-      shell: . ~/.bash_profile && yarn cargo-build
-      args:
-        chdir: ~/joystream
+    - name: Get code from local or git repo
+      include_role:
+        name: common
+        tasks_from: "{{ 'get-code-local' if build_local_code|bool else 'get-code-git' }}"
+
+    - name: Run setup and build
+      include_role:
+        name: common
+        tasks_from: run-setup-build

+ 9 - 2
devops/infrastructure/chain-spec-configuration.yml

@@ -3,9 +3,16 @@
 
 - name: Create and copy the chain-spec file
   hosts: all
+  gather_facts: no
 
-  roles:
-    - common
+  tasks:
+    - name: Generate chain-spec file and data keys either on localhost or admin server
+      include_role:
+        name: common
+        tasks_from: chain-spec-node-keys
+      vars:
+        local_or_admin: "{{ groups['admin'][0] if run_on_admin_server|bool else 'localhost' }}"
+        admin_code_dir: "{{ remote_code_path if run_on_admin_server|bool else local_dir }}"
 
 - name: Copy secret, auth and start joystream-node service for validators
   hosts: validators

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

@@ -58,11 +58,15 @@ if [ $? -eq 0 ]; then
 
   if [ -z "$EC2_AMI_ID" ]
   then
-    echo -e "\n\n=========== Configuring the servers ==========="
-    ansible-playbook -i inventory --private-key $KEY_PATH build-code.yml --extra-vars "branch_name=$BRANCH_NAME"
+    echo -e "\n\n=========== Configuring the node servers ==========="
+    ansible-playbook -i inventory --private-key $KEY_PATH build-code.yml --extra-vars "branch_name=$BRANCH_NAME git_repo=$GIT_REPO build_local_code=$BUILD_LOCAL_CODE"
   fi
 
+  echo -e "\n\n=========== Configuring the Admin server ==========="
+  ansible-playbook -i inventory --private-key $KEY_PATH setup-admin.yml \
+    --extra-vars "local_dir=$LOCAL_CODE_PATH run_on_admin_server=$CREATE_ADMIN_SERVER build_local_code=$BUILD_LOCAL_CODE"
+
   echo -e "\n\n=========== Configuring the chain spec file ==========="
   ansible-playbook -i inventory --private-key $KEY_PATH chain-spec-configuration.yml \
-    --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX data_path=data-$NEW_STACK_NAME"
+    --extra-vars "local_dir=$LOCAL_CODE_PATH network_suffix=$NETWORK_SUFFIX data_path=data-$NEW_STACK_NAME run_on_admin_server=$CREATE_ADMIN_SERVER"
 fi

+ 8 - 1
devops/infrastructure/group_vars/all

@@ -1,10 +1,17 @@
 ---
 # Variables applicable to all hosts
 
+branch_name: sumer
+git_repo: "https://github.com/Joystream/joystream.git"
+
 local_dir: ~/Joystream/joystream
+
 # Generates random number between 1000..9999
 network_suffix: "{{ 10000 | random(1000) }}"
+
 data_path: ./data
-change_spec_path: "{{ data_path }}/chainspec.json"
+chain_spec_path: "{{ data_path }}/chainspec.json"
 remote_code_path: "/home/ubuntu/joystream"
 remote_chain_spec_path: "{{ remote_code_path }}/chainspec.json"
+run_on_admin_server: false
+build_local_code: false

+ 4 - 4
devops/infrastructure/library/json_modify.py

@@ -6,17 +6,17 @@ from ansible.module_utils.basic import AnsibleModule
 
 def main():
     fields = {
-        "change_spec_path": {"required": True, "type": "str"},
+        "chain_spec_path": {"required": True, "type": "str"},
         "file_content": {"required": False, "type": "str" },
         "prefix": {"required": False, "type": "str" },
         "all_nodes": {"required": False, "type": "dict" }
     }
     module = AnsibleModule(argument_spec=fields)
     prefix = module.params["prefix"]
-    change_spec_path = module.params["change_spec_path"]
+    chain_spec_path = module.params["chain_spec_path"]
     all_nodes = module.params["all_nodes"]
 
-    with open(change_spec_path) as f:
+    with open(chain_spec_path) as f:
         data = json.load(f)
 
     response = {
@@ -34,7 +34,7 @@ def main():
     response["bootNodes"] = boot_node_list
 
     data.update(response)
-    with open(change_spec_path, 'w') as outfile:
+    with open(chain_spec_path, 'w') as outfile:
         json.dump(data, outfile, indent=4)
     module.exit_json(changed=False, result=response)
 

+ 18 - 0
devops/infrastructure/roles/admin/tasks/main.yml

@@ -0,0 +1,18 @@
+---
+# Configure admin server to be able to create chain-spec file and subkey commands
+
+- name: Copy bash_profile content
+  shell: cat ~/.bash_profile
+  register: bash_data
+
+- name: Copy bash_profile content to bashrc for non-interactive sessions
+  blockinfile:
+    block: "{{ bash_data.stdout }}"
+    path: ~/.bashrc
+    insertbefore: BOF
+
+- name: Get dependencies for subkey
+  shell: curl https://getsubstrate.io -sSf | bash -s -- --fast
+
+- name: Install subkey
+  shell: cargo install --force subkey --git https://github.com/paritytech/substrate --version 2.0.1 --locked

+ 72 - 0
devops/infrastructure/roles/common/tasks/chain-spec-node-keys.yml

@@ -0,0 +1,72 @@
+---
+# Create chain spec files and keys and copy to all the servers
+
+- name: Debug to test variable
+  debug:
+    msg: "Data path: {{ data_path }}, Chain Spec path: {{ chain_spec_path }}"
+  run_once: true
+
+- name: Run chain-spec-builder to generate chainspec.json file
+  command: "{{ admin_code_dir }}/target/release/chain-spec-builder generate -a 2 --chain-spec-path {{ chain_spec_path }} --deployment live --endowed 1 --keystore-path {{ data_path }}"
+  register: chain_spec_output
+  delegate_to: "{{ local_or_admin }}"
+  run_once: true
+
+- name: Run subkey to generate node keys
+  shell: subkey generate-node-key
+  delegate_to: "{{ local_or_admin }}"
+  register: subkey_output
+
+- name: Print to stdout
+  debug:
+    msg:
+    - "Public Key: {{ subkey_output.stderr }}"
+    - "Private Key: {{ subkey_output.stdout }}"
+
+- name: Print to stdout chain spec
+  debug: var=chain_spec_output.stdout
+  run_once: true
+
+- name: Save output of chain spec to local file
+  copy:
+    content: "{{ chain_spec_output.stdout }}"
+    dest: "{{ data_path }}/chain_spec_output.txt"
+  delegate_to: "{{ local_or_admin }}"
+  run_once: true
+
+- name: Change chain spec name, id, protocolId
+  json_modify:
+    chain_spec_path: "{{ chain_spec_path }}"
+    prefix: "{{ network_suffix }}"
+    all_nodes: "{{ hostvars }}"
+  delegate_to: "{{ local_or_admin }}"
+  register: result
+  run_once: true
+
+- name: Print output of modified chainspec
+  debug:
+    var: result.result
+  run_once: true
+
+- name: Copying chain spec files to localhost
+  synchronize:
+    src: "/home/ubuntu/{{ data_path }}/"
+    dest: "{{ data_path }}"
+    mode: pull
+  delegate_to: "{{ local_or_admin }}"
+  run_once: true
+  when: run_on_admin_server|bool
+
+- name: Copy joystream-node binary to localhost
+  fetch:
+    src: "{{ admin_code_dir }}/target/release/joystream-node"
+    dest: "{{ data_path }}/joystream-node"
+    flat: yes
+  delegate_to: "{{ local_or_admin }}"
+  run_once: true
+  when: run_on_admin_server|bool
+
+- name: Copying chain spec file to all servers
+  copy:
+    src: "{{ chain_spec_path }}"
+    dest: "{{ remote_chain_spec_path }}"

+ 13 - 0
devops/infrastructure/roles/common/tasks/get-code-git.yml

@@ -0,0 +1,13 @@
+---
+# Get the latest code
+
+- name: Delete remote code directory if exists
+  file:
+    state: absent
+    path: "{{ remote_code_path }}"
+
+- name: Git checkout
+  git:
+    repo: "{{ git_repo }}"
+    dest: "{{ remote_code_path }}"
+    version: "{{ branch_name }}"

+ 27 - 0
devops/infrastructure/roles/common/tasks/get-code-local.yml

@@ -0,0 +1,27 @@
+---
+# Get the latest code
+
+- name: Archive the current Git repository
+  command: git archive --format tar HEAD
+  args:  
+    chdir: "{{ local_dir }}"
+  delegate_to: localhost
+  register: archive_output
+
+- name: Save output the git repo as an archive
+  local_action: copy content={{ archive_output.stdout }} dest="{{ local_dir }}/code-archive.tar"
+
+- name: Delete remote code directory if exists
+  file:
+    state: absent
+    path: "{{ remote_code_path }}"
+
+- name: create directory for unarchiving
+  file:
+    path: "{{ remote_code_path }}"
+    state: directory
+
+- name: Extract code into path
+  unarchive:
+    src: "{{ local_dir }}/code-archive.tar"
+    dest: "{{ remote_code_path }}"

+ 0 - 43
devops/infrastructure/roles/common/tasks/main.yml

@@ -1,43 +0,0 @@
----
-# Configure chain spec and start joystream-node service on the servers
-
-- name: Run subkey to generate node keys
-  local_action: 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 }}
-  register: chain_spec_output
-  run_once: true
-
-- name: Run subkey to generate node keys
-  local_action: command subkey generate-node-key
-  register: subkey_output
-
-- name: Print to stdout
-  debug:
-    msg:
-    - "Public Key: {{ subkey_output.stderr }}"
-    - "Private Key: {{ subkey_output.stdout }}"
-
-- name: Print to stdout chain spec
-  debug: var=chain_spec_output.stdout
-  run_once: true
-
-- name: Save output of chain spec to local file
-  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
-  json_modify:
-    change_spec_path: "{{ change_spec_path }}"
-    prefix: "{{ network_suffix }}"
-    all_nodes: "{{ hostvars }}"
-  register: result
-  run_once: true
-
-- name: Print output of modified chainspec
-  debug:
-    var: result.result
-  run_once: true
-
-- name: Copying chain spec file to server
-  copy:
-    src: "{{ change_spec_path }}"
-    dest: "{{ remote_chain_spec_path }}"

+ 15 - 0
devops/infrastructure/roles/common/tasks/run-setup-build.yml

@@ -0,0 +1,15 @@
+---
+# Run setup and build code
+
+- name: Creat bash profile file
+  command: "touch /home/ubuntu/.bash_profile"
+
+- name: Run setup script
+  command: ./setup.sh
+  args:
+    chdir: "{{ remote_code_path }}"
+
+- name: Build joystream node
+  shell: . ~/.bash_profile && yarn cargo-build
+  args:
+    chdir: "{{ remote_code_path }}"

+ 9 - 0
devops/infrastructure/setup-admin.yml

@@ -0,0 +1,9 @@
+---
+# Setup Admin server, copy code, install subkey
+
+- name: Setup Admin server, install subkey
+  hosts: admin
+
+  roles:
+    - role: admin
+      when: run_on_admin_server|bool