|
| 1 | +const colors = require('colors'); |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const Command = require('../../Command'); |
| 5 | +const installRoot = require('../root/install.cmd'); |
| 6 | +const { |
| 7 | + INSTALLATION_DEFAULTS, |
| 8 | + installAppProxy, |
| 9 | + createErrorHandler, |
| 10 | + drawCodefreshFiglet, |
| 11 | +} = require('../hybrid/helper'); |
| 12 | +const { getKubeContext } = require('../../helpers/kubernetes'); |
| 13 | +const sdk = require('../../../../logic/sdk'); |
| 14 | +const { to } = require('../../../../logic/cli-config/errors/awaitTo'); |
| 15 | +const { mergeWithValues, selectRuntime } = require('./helper'); |
| 16 | + |
| 17 | +const openIssueMessage = 'If you had any issues with this installation process please report them at:' |
| 18 | + + ` ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`; |
| 19 | +const handleError = createErrorHandler(openIssueMessage); |
| 20 | + |
| 21 | +function printInstallationOptionsSummary({ |
| 22 | + kubeContextName, |
| 23 | + kubeNamespace, |
| 24 | + host, |
| 25 | + runtimeEnvironment, |
| 26 | +}) { |
| 27 | + const summary = `\n${colors.green('Installation options summary:')} |
| 28 | + 1. Kubernetes Context: ${colors.cyan(kubeContextName)} |
| 29 | + 2. Kubernetes Namespace: ${colors.cyan(kubeNamespace)} |
| 30 | + 3. App-Proxy hostname: ${colors.cyan(host)} |
| 31 | + 4. Runtime-Environment: ${colors.cyan(runtimeEnvironment)} |
| 32 | + `; |
| 33 | + console.log(summary); |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +const installAppProxyHandler = new Command({ |
| 38 | + root: false, |
| 39 | + parent: installRoot, |
| 40 | + command: 'app-proxy', |
| 41 | + description: 'Install the App-Proxy component on your Kubernetes cluster', |
| 42 | + webDocs: { |
| 43 | + category: 'App-Proxy', |
| 44 | + title: 'Install', |
| 45 | + weight: 100, |
| 46 | + }, |
| 47 | + |
| 48 | + builder: yargs => yargs |
| 49 | + .env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv |
| 50 | + .option('kube-config-path', { |
| 51 | + describe: 'Path to kubeconfig file [$KUBECONFIG]', |
| 52 | + default: INSTALLATION_DEFAULTS.KUBECONFIG_PATH, |
| 53 | + type: 'string', |
| 54 | + }) |
| 55 | + .option('kube-context-name', { |
| 56 | + describe: 'Name of the kubernetes context on which the app-proxy should be installed' |
| 57 | + + ` (default: ${getKubeContext()}) [$CF_ARG_KUBE_CONTEXT_NAME]`, |
| 58 | + }) |
| 59 | + .option('kube-namespace', { |
| 60 | + describe: 'Name of the namespace on which app-proxy should be installed (default:' |
| 61 | + + ` ${INSTALLATION_DEFAULTS.NAMESPACE}) [$CF_ARG_KUBE_NAMESPACE]`, |
| 62 | + type: 'string', |
| 63 | + }) |
| 64 | + .option('runtime-environment', { |
| 65 | + describe: 'The Codefresh runtime-environment that this app-proxy will be associated with', |
| 66 | + type: 'string', |
| 67 | + }) |
| 68 | + .option('docker-registry', { |
| 69 | + describe: 'The prefix for the container registry that will be used for pulling the app-proxy component image', |
| 70 | + default: 'docker.io', |
| 71 | + type: 'string', |
| 72 | + }) |
| 73 | + .option('values', { |
| 74 | + describe: 'specify values in a YAML file', |
| 75 | + }) |
| 76 | + .option('set-value', { |
| 77 | + describe: 'Set values for templates, example: --set-value LocalVolumesDir=/mnt/disks/ssd0/codefresh-volumes', |
| 78 | + type: 'array', |
| 79 | + }) |
| 80 | + .option('verbose', { |
| 81 | + describe: 'Print logs', |
| 82 | + }) |
| 83 | + .option('host', { |
| 84 | + describe: 'the hostname that will be used by the app-proxy ingress', |
| 85 | + type: 'string', |
| 86 | + }) |
| 87 | + .option('ingress-class', { |
| 88 | + describe: 'the ingress class that will be used by the app-proxy ingress', |
| 89 | + type: 'string', |
| 90 | + }), |
| 91 | + |
| 92 | + handler: async (_argv) => { |
| 93 | + const argv = mergeWithValues(_argv); |
| 94 | + const { |
| 95 | + 'kube-config-path': kubeConfigPath, |
| 96 | + 'kube-context-name': kubeContextName, |
| 97 | + 'kube-namespace': kubeNamespace, |
| 98 | + 'docker-registry': dockerRegistry, |
| 99 | + verbose, |
| 100 | + values, |
| 101 | + 'set-value': setValues, |
| 102 | + host, |
| 103 | + 'ingress-class': ingressClass, |
| 104 | + noExit, |
| 105 | + } = argv; |
| 106 | + let { |
| 107 | + 'runtime-environment': runtimeEnvironment, |
| 108 | + } = argv; |
| 109 | + |
| 110 | + const [listReErr, runtimes] = await to(sdk.runtimeEnvs.list({ })); |
| 111 | + await handleError(listReErr, 'Failed to get account\'s runtime environments'); |
| 112 | + const runtimeNames = runtimes.reduce((acc, re) => { |
| 113 | + if (_.get(re, 'metadata.agent')) { |
| 114 | + acc.push(_.get(re, 'metadata.name')); |
| 115 | + } |
| 116 | + return acc; |
| 117 | + }, []); |
| 118 | + |
| 119 | + if (_.isEmpty(runtimeNames)) { |
| 120 | + await handleError( |
| 121 | + new Error('no runtime environments found'), |
| 122 | + 'Cannot install app-proxy without a Codefresh runtime-environment', |
| 123 | + ); |
| 124 | + } |
| 125 | + if (!runtimeEnvironment || !runtimeNames.find(re => re === runtimeEnvironment)) { |
| 126 | + if (runtimeEnvironment) { |
| 127 | + console.log(colors.bold(`Runtime-environment "${colors.cyan(runtimeEnvironment)}" ` |
| 128 | + + 'was not found, please choose on of the following:')); |
| 129 | + } |
| 130 | + runtimeEnvironment = await selectRuntime(runtimeNames); |
| 131 | + } |
| 132 | + |
| 133 | + printInstallationOptionsSummary({ |
| 134 | + kubeContextName, |
| 135 | + kubeNamespace, |
| 136 | + host, |
| 137 | + runtimeEnvironment, |
| 138 | + }); |
| 139 | + |
| 140 | + console.log('installing app-proxy...'); |
| 141 | + const appProxyUrl = await installAppProxy({ |
| 142 | + apiHost: sdk.config.context.url, |
| 143 | + appProxyHost: host, |
| 144 | + appProxyIngressClass: ingressClass, |
| 145 | + kubeConfigPath, |
| 146 | + kubeContextName, |
| 147 | + kubeNamespace, |
| 148 | + dockerRegistry, |
| 149 | + valuesFile: values, |
| 150 | + setValue: setValues, |
| 151 | + verbose, |
| 152 | + }); |
| 153 | + |
| 154 | + const re = runtimes.find(runtime => runtimeEnvironment === _.get(runtime, 'metadata.name')); |
| 155 | + const body = { |
| 156 | + appProxy: { |
| 157 | + externalIP: appProxyUrl, |
| 158 | + }, |
| 159 | + }; |
| 160 | + console.log(`updating runtime-environment ${colors.cyan(runtimeEnvironment)} with app-proxy url`); |
| 161 | + await sdk.runtimeEnvs.update({ name: runtimeEnvironment }, _.merge(re, body)); |
| 162 | + console.log(`runtime-environment ${colors.cyan(runtimeEnvironment)} updated`); |
| 163 | + |
| 164 | + console.log(openIssueMessage); |
| 165 | + await drawCodefreshFiglet(); |
| 166 | + if (!noExit) { |
| 167 | + process.exit(); |
| 168 | + } |
| 169 | + }, |
| 170 | + |
| 171 | +}); |
| 172 | + |
| 173 | +module.exports = installAppProxyHandler; |
0 commit comments