|
| 1 | +/* eslint-disable max-len */ |
| 2 | +const Command = require('../../Command'); |
| 3 | +const installRoot = require('../root/install.cmd'); |
| 4 | +const { sdk } = require('../../../../logic'); |
| 5 | +const installRuntimeCmd = require('../runtimeEnvironments/install.cmd'); |
| 6 | +const { getKubeContext } = require('../../helpers/kubernetes'); |
| 7 | + |
| 8 | +const installAgentCmd = new Command({ |
| 9 | + root: false, |
| 10 | + parent: installRoot, |
| 11 | + command: 'agent', |
| 12 | + description: 'Install and create an agent on kubernetes cluster', |
| 13 | + webDocs: { |
| 14 | + category: 'Agents', |
| 15 | + title: 'Install', |
| 16 | + weight: 100, |
| 17 | + }, |
| 18 | + builder: yargs => yargs |
| 19 | + .env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv |
| 20 | + .option('name', { |
| 21 | + describe: 'Agent\'s name to be created if token is not provided', |
| 22 | + }) |
| 23 | + .option('token', { |
| 24 | + describe: 'Agent\'s token', |
| 25 | + }) |
| 26 | + .option('kube-context-name', { |
| 27 | + describe: 'Name of the kubernetes context on which venona should be installed [$CF_ARG_KUBE_CONTEXT_NAME]', |
| 28 | + }) |
| 29 | + .option('kube-node-selector', { |
| 30 | + describe: 'The kubernetes node selector "key=value" to be used by venona build resources (default is no node selector) (string)', |
| 31 | + }) |
| 32 | + .option('dry-run', { |
| 33 | + describe: 'Set to true to simulate installation', |
| 34 | + }) |
| 35 | + .option('in-cluster', { |
| 36 | + describe: 'Set flag if venona is been installed from inside a cluster', |
| 37 | + }) |
| 38 | + .option('kube-namespace', { |
| 39 | + describe: 'Name of the namespace on which venona should be installed [$CF_ARG_KUBE_NAMESPACE]', |
| 40 | + }) |
| 41 | + .option('kubernetes-runner-type', { |
| 42 | + describe: 'Set the runner type to kubernetes (alpha feature)', |
| 43 | + }) |
| 44 | + .option('tolerations', { |
| 45 | + describe: 'The kubernetes tolerations as path to a JSON file to be used by venona resources (default is no tolerations) (string)', |
| 46 | + }) |
| 47 | + .option('venona-version', { |
| 48 | + describe: 'Version of venona to install (default is the latest)', |
| 49 | + }) |
| 50 | + .option('kube-config-path', { |
| 51 | + describe: 'Path to kubeconfig file (default is $HOME/.kube/config)', |
| 52 | + }) |
| 53 | + .option('skip-version-check', { |
| 54 | + describe: 'Do not compare current Venona\'s version with latest', |
| 55 | + }) |
| 56 | + .option('install-runtime', { |
| 57 | + describe: 'Install and attach runtime on the same namespace as the agent (default is false)', |
| 58 | + }) |
| 59 | + .option('verbose', { |
| 60 | + describe: 'Print logs', |
| 61 | + }), |
| 62 | + handler: async (argv) => { |
| 63 | + let { |
| 64 | + name, token, |
| 65 | + } = argv; |
| 66 | + const { |
| 67 | + 'kube-node-selector': kubeNodeSelector, |
| 68 | + 'dry-run': dryRun, |
| 69 | + 'in-cluster': inCluster, |
| 70 | + 'kube-namespace': kubeNamespace, |
| 71 | + 'kubernetes-runner-type': kubernetesRunnerType, |
| 72 | + tolerations, |
| 73 | + 'venona-version': venonaVersion, |
| 74 | + 'kube-config-path': kubeConfigPath, |
| 75 | + 'skip-version-check': skipVersionCheck, |
| 76 | + 'install-runtime': installRuntime, |
| 77 | + verbose, |
| 78 | + } = argv; |
| 79 | + let agent; |
| 80 | + let { 'kube-context-name': kubeContextName } = argv; |
| 81 | + if (!kubeContextName) { |
| 82 | + kubeContextName = getKubeContext(kubeConfigPath); |
| 83 | + } |
| 84 | + |
| 85 | + if (!token) { // Create an agent if not provided |
| 86 | + name = name || `${kubeContextName}_${kubeNamespace}`; |
| 87 | + agent = await sdk.agents.create({ name }); |
| 88 | + // eslint-disable-next-line prefer-destructuring |
| 89 | + token = agent.token; |
| 90 | + console.log(`An agent with name: ${name} was created\n note this the last only time the token will be printed`); |
| 91 | + console.log(token); |
| 92 | + } else { |
| 93 | + // take the agent id from the token |
| 94 | + const apiKey = token.split('.')[0]; |
| 95 | + const agentData = await sdk.tokens.getById({ id: apiKey }); |
| 96 | + if (!agentData) { |
| 97 | + throw new Error('token is not valid'); |
| 98 | + } |
| 99 | + const { subject } = agentData; |
| 100 | + |
| 101 | + if (subject.type !== 'agent') { |
| 102 | + throw new Error('token is not assosicated with agent'); |
| 103 | + } |
| 104 | + const agentId = agentData.subject.ref; |
| 105 | + const data = await sdk.agents.get({ agentId }); |
| 106 | + // eslint-disable-next-line prefer-destructuring |
| 107 | + name = data.name; |
| 108 | + } |
| 109 | + const apiHost = sdk.config.context.url; |
| 110 | + const agentInstallStatusCode = await sdk.agents.install({ |
| 111 | + apiHost, |
| 112 | + kubeContextName, |
| 113 | + kubeNamespace, |
| 114 | + token, |
| 115 | + dryRun, |
| 116 | + inCluster, |
| 117 | + kubeNodeSelector, |
| 118 | + kubernetesRunnerType, |
| 119 | + tolerations, |
| 120 | + venonaVersion, |
| 121 | + kubeConfigPath, |
| 122 | + skipVersionCheck, |
| 123 | + verbose, |
| 124 | + agentId: name, |
| 125 | + terminateProcess: !installRuntime, |
| 126 | + }); |
| 127 | + if (agentInstallStatusCode !== 0) { |
| 128 | + throw new Error(`\nAgent installation failed with code ${agentInstallStatusCode}`); |
| 129 | + } |
| 130 | + if (installRuntime) { |
| 131 | + await installRuntimeCmd.handler({ |
| 132 | + 'runtime-kube-context-name': kubeContextName, |
| 133 | + 'runtime-kube-namespace': kubeNamespace, |
| 134 | + 'agent-name': name, |
| 135 | + 'runtime-kube-config-path': kubeConfigPath, |
| 136 | + 'attach-runtime': true, |
| 137 | + 'restart-agent': true, |
| 138 | + verbose, |
| 139 | + }); |
| 140 | + } |
| 141 | + }, |
| 142 | +}); |
| 143 | + |
| 144 | +module.exports = installAgentCmd; |
0 commit comments