|
| 1 | +/* eslint-disable max-len */ |
| 2 | +const Command = require('../../Command'); |
| 3 | +const runnerRoot = require('../root/runner.cmd'); |
| 4 | +const inquirer = require('inquirer'); |
| 5 | +const { getAllKubeContexts, getKubeContext } = require('../../helpers/kubernetes'); |
| 6 | +const installAgent = require('../agent/install.cmd'); |
| 7 | +const createContext = require('../auth/create-context.cmd'); |
| 8 | +const getAgents = require('../agent/get.cmd'); |
| 9 | +const { getConfigForSdk } = require('../../commad-line-interface'); |
| 10 | +const colors = require('colors'); |
| 11 | +const DEFAULTS = require('../../defaults'); |
| 12 | +const sdk = require('../../../../logic/sdk'); |
| 13 | + |
| 14 | +const defaultNamespace = 'codefresh'; |
| 15 | + |
| 16 | +const initCmd = new Command({ |
| 17 | + root: false, |
| 18 | + parent: runnerRoot, |
| 19 | + command: 'init', |
| 20 | + description: 'Install codefresh runner solution\'s components on kubernetes cluster', |
| 21 | + webDocs: { |
| 22 | + category: 'Runner', |
| 23 | + title: 'Init', |
| 24 | + weight: 100, |
| 25 | + }, |
| 26 | + // requiresAuthentication: argv => argv && !argv.token, |
| 27 | + builder: yargs => yargs |
| 28 | + .env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv |
| 29 | + .option('name', { |
| 30 | + describe: 'Agent\'s name to be created if token is not provided', |
| 31 | + }) |
| 32 | + .option('token', { |
| 33 | + describe: 'Registration\'s token', |
| 34 | + }) |
| 35 | + .option('url', { |
| 36 | + describe: 'Codefresh system custom url', |
| 37 | + default: DEFAULTS.URL, |
| 38 | + }) |
| 39 | + .option('kube-context-name', { |
| 40 | + describe: 'Name of the kubernetes context on which venona should be installed [$CF_ARG_KUBE_CONTEXT_NAME]', |
| 41 | + }) |
| 42 | + .option('kube-node-selector', { |
| 43 | + describe: 'The kubernetes node selector "key=value" to be used by venona build resources (default is no node selector) (string)', |
| 44 | + }) |
| 45 | + .option('dry-run', { |
| 46 | + describe: 'Set to true to simulate installation', |
| 47 | + }) |
| 48 | + .option('in-cluster', { |
| 49 | + describe: 'Set flag if venona is been installed from inside a cluster', |
| 50 | + }) |
| 51 | + .option('kube-namespace', { |
| 52 | + describe: 'Name of the namespace on which venona should be installed [$CF_ARG_KUBE_NAMESPACE]', |
| 53 | + }) |
| 54 | + .option('kubernetes-runner-type', { |
| 55 | + describe: 'Set the runner type to kubernetes (alpha feature)', |
| 56 | + }) |
| 57 | + .option('tolerations', { |
| 58 | + describe: 'The kubernetes tolerations as path to a JSON file to be used by venona resources (default is no tolerations) (string)', |
| 59 | + }) |
| 60 | + .option('venona-version', { |
| 61 | + describe: 'Version of venona to install (default is the latest)', |
| 62 | + }) |
| 63 | + .option('kube-config-path', { |
| 64 | + describe: 'Path to kubeconfig file (default is $HOME/.kube/config)', |
| 65 | + }) |
| 66 | + .option('skip-version-check', { |
| 67 | + describe: 'Do not compare current Venona\'s version with latest', |
| 68 | + }) |
| 69 | + .option('verbose', { |
| 70 | + describe: 'Print logs', |
| 71 | + }), |
| 72 | + handler: async (argv) => { |
| 73 | + const { |
| 74 | + 'kube-node-selector': kubeNodeSelector, |
| 75 | + 'dry-run': dryRun, |
| 76 | + 'in-cluster': inCluster, |
| 77 | + 'kubernetes-runner-type': kubernetesRunnerType, |
| 78 | + tolerations, |
| 79 | + 'venona-version': venonaVersion, |
| 80 | + 'kube-config-path': kubeConfigPath, |
| 81 | + 'skip-version-check': skipVersionCheck, |
| 82 | + verbose, |
| 83 | + name, token, url, |
| 84 | + } = argv; |
| 85 | + let { |
| 86 | + 'kube-context-name': kubeContextName, |
| 87 | + 'kube-namespace': kubeNamespace, |
| 88 | + } = argv; |
| 89 | + const questions = []; |
| 90 | + if (!kubeContextName) { |
| 91 | + const contexts = getAllKubeContexts(kubeConfigPath); |
| 92 | + const currentKubeContext = getKubeContext(kubeConfigPath); |
| 93 | + |
| 94 | + questions.push({ |
| 95 | + type: 'list', |
| 96 | + name: 'context', |
| 97 | + message: 'Select Kubernetes context', |
| 98 | + default: currentKubeContext, |
| 99 | + choices: contexts, |
| 100 | + }); |
| 101 | + } |
| 102 | + if (!kubeNamespace) { |
| 103 | + questions.push({ |
| 104 | + type: 'input', |
| 105 | + name: 'namespace', |
| 106 | + default: defaultNamespace, |
| 107 | + message: 'Insert Kubernetes namespace (will be created if not exists) ', |
| 108 | + validate: value => (value !== undefined && value !== '') || 'Please enter namespace\'s name', |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + questions.push({ |
| 113 | + type: 'confirm', |
| 114 | + name: 'shouldMakeDefaultRe', |
| 115 | + default: true, |
| 116 | + message: 'Should mark the hybrid runtime as default runtime ?', |
| 117 | + |
| 118 | + }); |
| 119 | + |
| 120 | + questions.push({ |
| 121 | + type: 'confirm', |
| 122 | + name: 'shouldExecutePipeline', |
| 123 | + default: true, |
| 124 | + message: 'Run demo pipeline ?', |
| 125 | + |
| 126 | + }); |
| 127 | + |
| 128 | + console.log(colors.green('This installer will guide you through the hybrid installation process')); |
| 129 | + const answers = await inquirer.prompt(questions); |
| 130 | + kubeContextName = kubeContextName || answers.context; |
| 131 | + kubeNamespace = kubeNamespace || answers.namespace; |
| 132 | + const { shouldMakeDefaultRe, shouldExecutePipeline } = answers; |
| 133 | + console.log(colors.green(`Installation options summary : \n Context: ${colors.blue(kubeContextName)} \n Namespace: ${colors.blue(kubeNamespace)} \n Make hybrid runime as default: ${colors.blue(shouldMakeDefaultRe)}\nExecute hello hyrbird pipeline: ${colors.blue(shouldExecutePipeline)}`)); |
| 134 | + if (token) { // Add context |
| 135 | + await createContext.handler({ |
| 136 | + apiKey: token, |
| 137 | + name: 'hybrid', |
| 138 | + url, |
| 139 | + }); |
| 140 | + const config = await getConfigForSdk(); |
| 141 | + await sdk.configure(config); |
| 142 | + console.log('A codefresh context named hybrid was added in your $HOME folder'); |
| 143 | + } |
| 144 | + await installAgent.handler({ |
| 145 | + name, |
| 146 | + 'kube-context-name': kubeContextName, |
| 147 | + 'kube-node-selector': kubeNodeSelector, |
| 148 | + 'dry-run': dryRun, |
| 149 | + 'in-cluster': inCluster, |
| 150 | + 'kube-namespace': kubeNamespace, |
| 151 | + 'kubernetes-runner-type': kubernetesRunnerType, |
| 152 | + tolerations, |
| 153 | + 'venona-version': venonaVersion, |
| 154 | + 'kube-config-path': kubeConfigPath, |
| 155 | + 'skip-version-check': skipVersionCheck, |
| 156 | + 'install-runtime': true, |
| 157 | + verbose, |
| 158 | + 'make-default-runtime': shouldMakeDefaultRe, |
| 159 | + terminateProcess: false, |
| 160 | + createDemoPipeline: true, |
| 161 | + executeDemoPipeline: shouldExecutePipeline, |
| 162 | + }); |
| 163 | + console.log(colors.green('Agent Status:\n')); |
| 164 | + await getAgents.handler({}); |
| 165 | + console.log(colors.green(`\nDocumenation link: ${colors.blue('https://codefresh.io/docs/docs/enterprise/codefresh-runner/#codefresh-runner-preview-release')}`)); |
| 166 | + console.log(colors.green('\nTo report issues please follow this link: https://github.com/codefresh-io/cli/issues/new')); |
| 167 | + process.exit(); // TODO : This is not needed - needed to be fixed |
| 168 | + }, |
| 169 | +}); |
| 170 | + |
| 171 | +module.exports = initCmd; |
0 commit comments