Browse Source

Add chain spec configuration ansible playbook, add README

Anuj Bansal 3 years ago
parent
commit
4d8e624249

+ 33 - 0
devops/infrastructure/README.md

@@ -0,0 +1,33 @@
+## Setup
+
+### Configuring the AWS CLI
+We’re going to use the AWS CLI to access AWS resources from the command line. 
+
+Follow [the official directions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) for your system.
+
+Once the AWS CLI is installed, configure a profile
+
+`aws configure --profile joystream-user`
+
+### Create a key pair
+```
+aws ec2 create-key-pair --key-name joystream-key --query 'KeyMaterial' --output text > joystream-key.pem
+```
+
+Set the permissions for the key pair 
+
+`chmod 400 joystream-key.pem`
+
+### Install Ansible
+On Mac run the command:
+* `brew install ansible`
+
+Follow [the official installation guide](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html) for your system.
+
+# How to run
+Run the `deploy-infra.sh` script to deploy the infrastructure
+
+```
+cd devops/infrastructure
+./deploy-infra.sh
+```

+ 59 - 0
devops/infrastructure/chain-spec-configuration.yml

@@ -0,0 +1,59 @@
+- name: Configure chain spec on the deployed servers
+  hosts: all
+  vars:
+    base_dir: ~/Joystream/joystream
+    random_suffix: 5154
+    change_spec_path: ./data/chainspec.json
+  tasks:
+  - name: Run subkey to generate node keys
+    local_action: ansible.builtin.command {{ base_dir }}/target/release/chain-spec-builder generate -a 2 --chain-spec-path {{ change_spec_path }} --deployment live --endowed 1 --keystore-path ./data/
+    register: chain_spec_output
+    run_once: true
+
+  - name: Run subkey to generate node keys
+    local_action: ansible.builtin.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
+    run_once: true
+
+  - name: Change chain spec name, id, protocolId
+    delegate_to: localhost
+    json_modify:
+      change_spec_path: "{{ change_spec_path }}"
+      prefix: "{{ random_suffix }}"
+      all_nodes: "{{ hostvars }}"
+    register: result
+    run_once: true
+
+  - debug:
+      var: result
+
+  - name: Copying chain spec file to server
+    copy:
+      src: "{{ change_spec_path }}"
+      dest: ~/joystream/chainspec.json
+  
+  - name: Creating chains directory
+    file:
+      path: "{{ item }}"
+      state: directory
+    loop:
+      - "~/joystream/chains/{{ result.result.id }}/network"
+
+  - name: Copy secret to remote host
+    copy:
+      dest: "~/joystream/chains/{{ result.result.id }}/network/secret"
+      content: "{{ subkey_output.stdout }}"
+
+  - name: Copy auth directory to remote host
+    copy:
+      src: "./data/auth-{{ ansible_play_batch.index(inventory_hostname) }}/"
+      dest: "~/joystream/chains/{{ result.result.id }}/keystore/"

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

@@ -28,4 +28,7 @@ if [ $? -eq 0 ]; then
 
   echo -e "\n\n=========== Configuring the servers ==========="
   ansible-playbook -i inventory -v --private-key $KEY_PATH configure.yml
+
+  echo -e "\n\n=========== Configuring the chain spec file ==========="
+  ansible-playbook -i inventory -v --private-key $KEY_PATH chain-spec-configuration.yml
 fi

+ 50 - 0
devops/infrastructure/library/json_modify.py

@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+from ansible.module_utils.basic import *
+import json
+
+
+def main():
+    fields = {
+        "change_spec_path": {"required": True, "type": "str"},
+        "file_content": {"required": False, "type": "str" },
+        "prefix": {"required": False, "type": "str" },
+        "all_nodes": {"required": False, "type": "dict" },
+        # "description": {"required": False, "type": "str"},
+        # "private": {"default": False, "type": "bool" },
+        # "state": {
+        # 	"default": "present", 
+        # 	"choices": ['present', 'absent'],  
+        # 	"type": 'str' 
+        # },
+    }
+    module = AnsibleModule(argument_spec=fields)
+    prefix = module.params["prefix"]
+    change_spec_path = module.params["change_spec_path"]
+    all_nodes = module.params["all_nodes"]
+
+    with open(change_spec_path) as f:
+        data = json.load(f)
+    # data = json.loads(module.params["file_content"])
+    # print(data)
+    # response = {"hello": "world"}
+    response = {
+        "name": f'{data["name"]} {prefix}',
+        "id": f'{data["id"]}_{prefix}',
+        "protocolId": f'{data["protocolId"]}{prefix}'
+    }
+
+    boot_node_list = data["bootNodes"]
+    for key in all_nodes:
+        public_key = all_nodes[key]["subkey_output"]["stderr"]
+        boot_node_list.append(f"/ip4/{key}/tcp/30333/p2p/{public_key}")
+
+    response["bootNodes"] = boot_node_list
+
+    data.update(response)
+    with open(change_spec_path, 'w') as outfile:
+        json.dump(data, outfile, indent=4)
+    module.exit_json(changed=False, result=response)
+
+if __name__ == '__main__':
+    main()