Browse Source

Add init containers for subkey, builder and json modifier

Anuj Bansal 3 years ago
parent
commit
2d3a001cd0

+ 29 - 0
devops/infrastructure/node-network/configMap.ts

@@ -0,0 +1,29 @@
+import * as pulumi from '@pulumi/pulumi'
+import * as k8s from '@pulumi/kubernetes'
+import * as fs from 'fs'
+
+export class configMapFromFile extends pulumi.ComponentResource {
+  public readonly configName?: pulumi.Output<string>
+
+  constructor(name: string, args: ConfigMapArgs, opts: pulumi.ComponentResourceOptions = {}) {
+    super('pkg:query-node:configMap', name, {}, opts)
+
+    this.configName = new k8s.core.v1.ConfigMap(
+      name,
+      {
+        metadata: {
+          namespace: args.namespaceName,
+        },
+        data: {
+          'fileData': fs.readFileSync(args.filePath).toString(),
+        },
+      },
+      opts
+    ).metadata.apply((m) => m.name)
+  }
+}
+
+export interface ConfigMapArgs {
+  filePath: string
+  namespaceName: pulumi.Output<string>
+}

+ 143 - 2
devops/infrastructure/node-network/index.ts

@@ -2,6 +2,7 @@ import * as awsx from '@pulumi/awsx'
 import * as eks from '@pulumi/eks'
 import * as pulumi from '@pulumi/pulumi'
 import * as k8s from '@pulumi/kubernetes'
+import { configMapFromFile } from './configMap'
 
 const config = new pulumi.Config()
 const awsConfig = new pulumi.Config('aws')
@@ -57,6 +58,44 @@ export const namespaceName = ns.metadata.name
 
 const appLabels = { appClass: name }
 
+// const subkeyJob = new k8s.batch.v1.Job(
+//   'subkey-job',
+//   {
+//     metadata: {
+//       namespace: namespaceName,
+//     },
+//     spec: {
+//       completions: 3,
+//       backoffLimit: 0,
+//       template: {
+//         spec: {
+//           containers: [
+//             {
+//               name: 'subkey-node',
+//               image: 'parity/subkey:latest',
+//               args: ['generate-node-key'],
+//             },
+//           ],
+//           restartPolicy: 'Never',
+//         },
+//       },
+//     },
+//   },
+//   { ...resourceOptions }
+// )
+
+const jsonModifyConfig = new configMapFromFile(
+  'json-modify-config',
+  {
+    filePath: 'json_modify.py',
+    namespaceName: namespaceName,
+  },
+  resourceOptions
+).configName
+
+const dataPath = '/subkey-data'
+const builderPath = '/builder-data'
+
 const deployment = new k8s.apps.v1.Deployment(
   name,
   {
@@ -72,14 +111,111 @@ const deployment = new k8s.apps.v1.Deployment(
           labels: appLabels,
         },
         spec: {
+          initContainers: [
+            {
+              name: 'subkey-node',
+              image: 'parity/subkey:latest',
+              command: ['/bin/sh', '-c'],
+              args: [
+                `subkey generate-node-key >> ${dataPath}/subkey 2>> ${dataPath}/subkey; echo '\n\n' >> ${dataPath}/subkey`,
+              ],
+              volumeMounts: [
+                {
+                  name: 'subkey-data',
+                  mountPath: dataPath,
+                },
+              ],
+            },
+            {
+              name: 'subkey-node-1',
+              image: 'parity/subkey:latest',
+              command: ['/bin/sh', '-c'],
+              args: [`subkey generate-node-key >> ${dataPath}/subkey 2>> ${dataPath}/subkey`],
+              volumeMounts: [
+                {
+                  name: 'subkey-data',
+                  mountPath: dataPath,
+                },
+              ],
+            },
+            {
+              name: 'busybox',
+              image: 'busybox',
+              command: ['/bin/sh', '-c'],
+              args: [`cat ${dataPath}/subkey`],
+              volumeMounts: [
+                {
+                  name: 'subkey-data',
+                  mountPath: dataPath,
+                },
+              ],
+            },
+            {
+              name: 'builder-node',
+              image: 'joystream/node:latest',
+              command: ['/bin/sh', '-c'],
+              args: [
+                '/joystream/chain-spec-builder generate -a 2 --chain-spec-path /builder-data/chainspec.json --deployment live --endowed 1 --keystore-path /builder-data/data >> /builder-data/seeds.txt',
+              ],
+              volumeMounts: [
+                {
+                  name: 'builder-data',
+                  mountPath: builderPath,
+                },
+              ],
+            },
+            {
+              name: 'json-modify',
+              image: 'python',
+              command: ['python'],
+              args: ['/scripts/json_modify.py', '--path', '/builder-data/chainspec.json', '--prefix', '8129'],
+              volumeMounts: [
+                {
+                  mountPath: '/scripts/json_modify.py',
+                  name: 'json-modify-script',
+                  subPath: 'fileData',
+                },
+                {
+                  name: 'builder-data',
+                  mountPath: builderPath,
+                },
+              ],
+            },
+          ],
           containers: [
             {
               name: 'joystream-node',
               image: 'joystream/node:latest',
               ports: [{ containerPort: 9944 }, { containerPort: 9933 }],
+              args: ['--dev'],
+              volumeMounts: [
+                {
+                  name: 'subkey-data',
+                  mountPath: dataPath,
+                },
+                {
+                  name: 'builder-data',
+                  mountPath: builderPath,
+                },
+              ],
+            },
+          ],
+          volumes: [
+            {
+              name: 'subkey-data',
+              emptyDir: {},
+            },
+            {
+              name: 'builder-data',
+              emptyDir: {},
+            },
+            {
+              name: 'json-modify-script',
+              configMap: {
+                name: jsonModifyConfig,
+              },
             },
           ],
-          volumes: [],
         },
       },
     },
@@ -100,6 +236,7 @@ const service = new k8s.core.v1.Service(
       name: 'query-node',
     },
     spec: {
+      type: 'NodePort',
       ports: [
         { name: 'port-1', port: 9944 },
         { name: 'port-2', port: 9933 },
@@ -116,4 +253,8 @@ export const serviceName = service.metadata.name
 // When "done", this will print the public IP.
 export let serviceHostname: pulumi.Output<string>
 
-serviceHostname = service.status.loadBalancer.ingress[0].hostname
+if (isMinikube) {
+  serviceHostname = service.spec.clusterIP
+} else {
+  serviceHostname = service.status.loadBalancer.ingress[0].hostname
+}

+ 41 - 0
devops/infrastructure/node-network/json_modify.py

@@ -0,0 +1,41 @@
+#!/usr/bin/python
+import argparse
+import json
+
+def main(chain_spec_path, prefix):
+    print("Updating chain spec file")
+    # all_nodes = module.params["all_nodes"]
+
+    with open(chain_spec_path) as f:
+        data = json.load(f)
+
+    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:
+    #     if "validators" in all_nodes[key]["group_names"]:
+    #         public_key = all_nodes[key]["subkey_output"]["stderr"]
+    #         boot_node_list.append(f"/ip4/{key}/tcp/30333/p2p/{public_key}")
+
+    telemetry_endpoints = data["telemetryEndpoints"]
+    telemetry_endpoints.append([
+        "/dns/telemetry.joystream.org/tcp/443/x-parity-wss/%2Fsubmit%2F", 0])
+
+    # response["bootNodes"] = boot_node_list
+    response["telemetryEndpoints"] = telemetry_endpoints
+
+    data.update(response)
+    with open(chain_spec_path, 'w') as outfile:
+        json.dump(data, outfile, indent=4)
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Modify Chain Spec file')
+    parser.add_argument('--path', required=True, help="Path to chain spec file")
+    parser.add_argument('--prefix', required=True, help="Network prefix")
+    args = parser.parse_args()
+    print(args.path)
+    main(chain_spec_path=args.path, prefix=args.prefix)