From c9341f70e6a3c26900aba09a6dc7b3fe752ffae8 Mon Sep 17 00:00:00 2001 From: Noam Gal Date: Wed, 2 Dec 2020 14:20:08 +0200 Subject: [PATCH] CR-1683 - inject proxy env vars to engine container (#596) * inject proxy env vars to engine container * 0.73.23 --- .nvmrc | 1 + .../cli/commands/gitops/install.cmd.js | 3 +- lib/interface/cli/commands/hybrid/helper.js | 39 ------------------- lib/interface/cli/commands/hybrid/init.cmd.js | 4 +- .../cli/commands/pipeline/run.local.js | 24 +++++++----- lib/interface/cli/helpers/general.js | 38 ++++++++++++++++++ package.json | 2 +- 7 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 000000000..cc5875fab --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v10.15.3 diff --git a/lib/interface/cli/commands/gitops/install.cmd.js b/lib/interface/cli/commands/gitops/install.cmd.js index 83c434c02..f779c8f72 100644 --- a/lib/interface/cli/commands/gitops/install.cmd.js +++ b/lib/interface/cli/commands/gitops/install.cmd.js @@ -2,7 +2,8 @@ const _ = require('lodash'); const Command = require('../../Command'); const installRoot = require('../root/install.cmd'); -const { downloadProvider, detectProxy } = require('../hybrid/helper'); +const { detectProxy } = require('../../helpers/general'); +const { downloadProvider } = require('../hybrid/helper'); const { Runner, components } = require('../../../../binary'); const installArgoCmd = new Command({ diff --git a/lib/interface/cli/commands/hybrid/helper.js b/lib/interface/cli/commands/hybrid/helper.js index fd3c26a3c..567bd2fba 100644 --- a/lib/interface/cli/commands/hybrid/helper.js +++ b/lib/interface/cli/commands/hybrid/helper.js @@ -950,15 +950,6 @@ function keyValueAsStringToObject(nodeSelectorStr) { } } -function keyValueArrayToObject(arr) { - return arr.reduce((acc, cur) => { - const parts = cur.split('='); - // eslint-disable-next-line prefer-destructuring - acc[parts[0]] = parts[1]; - return acc; - }, {}); -} - function objectToKeyValueArray(obj) { return Object.keys(obj).reduce((acc, key) => { acc.push(`${key}=${obj[key]}`); @@ -966,22 +957,6 @@ function objectToKeyValueArray(obj) { }, []); } -function addProxyVariables(envVars, { httpProxy, httpsProxy, noProxy }) { - const envVarsObj = keyValueArrayToObject(envVars); - if (httpsProxy && !envVarsObj.HTTPS_PROXY && !envVarsObj.https_proxy) { - envVars.push(`HTTPS_PROXY=${httpsProxy}`); - envVars.push(`https_proxy=${httpsProxy}`); - } - if (httpProxy && !envVarsObj.HTTP_PROXY && !envVarsObj.http_proxy) { - envVars.push(`HTTP_PROXY=${httpProxy}`); - envVars.push(`http_proxy=${httpProxy}`); - } - if (noProxy && !envVarsObj.NO_PROXY && !envVarsObj.no_proxy) { - envVars.push(`NO_PROXY=${noProxy}`); - envVars.push(`no_proxy=${noProxy}`); - } -} - function serealizeToKeyValuePairs(obj) { return _.keys(obj).reduce((acc, key) => { if (acc) { @@ -991,17 +966,6 @@ function serealizeToKeyValuePairs(obj) { }, ''); } -function detectProxy() { - const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY; - const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY; - const noProxy = process.env.no_proxy || process.env.NO_PROXY; - return { - httpProxy, - httpsProxy, - noProxy, - }; -} - function getRuntimeImagesWithRegistryUrl(registry) { return Object.keys(RUNTIME_IMAGES).reduce((acc, cur) => { acc[cur] = `${registry}/${RUNTIME_IMAGES[cur]}`; @@ -1088,7 +1052,6 @@ module.exports = { getRuntimeVersion, createTestPipeline, getTestPipeline, - addProxyVariables, updateTestPipelineRuntime, executeTestPipeline, createProgressBar, @@ -1103,14 +1066,12 @@ module.exports = { newRuntimeName, newAgentName, keyValueAsStringToObject, - keyValueArrayToObject, objectToKeyValueArray, downloadRelatedComponents: downloadHybridComponents, downloadSteveDore, downloadVeonona, downloadProvider, runUpgrade, - detectProxy, serealizeToKeyValuePairs, getRuntimeImagesWithRegistryUrl, installAppProxy, diff --git a/lib/interface/cli/commands/hybrid/init.cmd.js b/lib/interface/cli/commands/hybrid/init.cmd.js index e184aa097..c8308556c 100644 --- a/lib/interface/cli/commands/hybrid/init.cmd.js +++ b/lib/interface/cli/commands/hybrid/init.cmd.js @@ -4,6 +4,7 @@ const runnerRoot = require('../root/runner.cmd'); const inquirer = require('inquirer'); const colors = require('colors'); const _ = require('lodash'); +const { addProxyVariables, detectProxy, keyValueArrayToObject } = require('../../helpers/general'); const { getAllKubeContexts, getKubeContext } = require('../../helpers/kubernetes'); const installMonitoring = require('../monitor/install.cmd'); const createClusterCmd = require('../cluster/create.cmd'); @@ -16,7 +17,6 @@ const installationProgress = require('./installation-process'); const { to } = require('./../../../../logic/cli-config/errors/awaitTo'); const { createErrorHandler, - keyValueArrayToObject, createTestPipeline, executeTestPipeline, updateTestPipelineRuntime, @@ -30,10 +30,8 @@ const { attachRuntime, newRuntimeName, newAgentName, - detectProxy, keyValueAsStringToObject, getRuntimeImagesWithRegistryUrl, - addProxyVariables, mergeValuesFromValuesFile, INSTALLATION_DEFAULTS, } = require('./helper'); diff --git a/lib/interface/cli/commands/pipeline/run.local.js b/lib/interface/cli/commands/pipeline/run.local.js index 88a906319..37d81d5e8 100644 --- a/lib/interface/cli/commands/pipeline/run.local.js +++ b/lib/interface/cli/commands/pipeline/run.local.js @@ -7,6 +7,7 @@ const _ = require('lodash'); const { sdk } = require('../../../../logic'); const { followLogs } = require('../../helpers/logs'); const chalk = require('chalk'); +const { addProxyVariables, detectProxy } = require('../../helpers/general'); const regex = /##[0-9a-f]{24}##/i; const EngineErrorPrefix = 'EngineError'; @@ -113,15 +114,18 @@ class RunLocalCommand extends RunBaseCommand { console.log(`Running pipeline: ${pipelineName}`); return new Promise((resolve, reject) => { + const proxyEnv = detectProxy(); + const env = [ + ...(debug ? ['PIPELINE_DEBUG_MODE=true'] : []), + `ACCESS_TOKEN=${currentContext.token}`, + `PIPELINE_ID=${pipelineName}`, + `BRANCH=${branch}`, + `CF_HOST=${currentContext.url}`, + 'DOCKER_SOCKET_PATH=/var/run/docker.sock', + ]; + addProxyVariables(env, proxyEnv); const eventEmitter = this.docker.run(DEFAULTS.ENGINE_IMAGE, [], undefined, _.mergeWith({ - Env: [ - ...(debug ? ['PIPELINE_DEBUG_MODE=true'] : []), - `ACCESS_TOKEN=${currentContext.token}`, - `PIPELINE_ID=${pipelineName}`, - `BRANCH=${branch}`, - `CF_HOST=${currentContext.url}`, - 'DOCKER_SOCKET_PATH=/var/run/docker.sock', - ], + Env: env, Hostconfig: { Binds: [ '/var/run/docker.sock:/var/run/docker.sock', @@ -131,8 +135,10 @@ class RunLocalCommand extends RunBaseCommand { if (err) { console.log(chalk.red(`Error when running pipeline : ${err}`)); // eslint-disable-next-line prefer-promise-reject-errors - resolve(1); + return resolve(1); } + + resolve(0); }); if (this.argv.detach) { resolve(0); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 06c3a4068..3becbc692 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -182,6 +182,41 @@ function ignoreHttpError(e) { return undefined; } +function detectProxy() { + const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY; + const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY; + const noProxy = process.env.no_proxy || process.env.NO_PROXY; + return { + httpProxy, + httpsProxy, + noProxy, + }; +} + +function keyValueArrayToObject(arr) { + return arr.reduce((acc, cur) => { + const parts = cur.split('='); + // eslint-disable-next-line prefer-destructuring + acc[parts[0]] = parts[1]; + return acc; + }, {}); +} + +function addProxyVariables(envVars, { httpProxy, httpsProxy, noProxy }) { + const envVarsObj = keyValueArrayToObject(envVars); + if (httpsProxy && !envVarsObj.HTTPS_PROXY && !envVarsObj.https_proxy) { + envVars.push(`HTTPS_PROXY=${httpsProxy}`); + envVars.push(`https_proxy=${httpsProxy}`); + } + if (httpProxy && !envVarsObj.HTTP_PROXY && !envVarsObj.http_proxy) { + envVars.push(`HTTP_PROXY=${httpProxy}`); + envVars.push(`http_proxy=${httpProxy}`); + } + if (noProxy && !envVarsObj.NO_PROXY && !envVarsObj.no_proxy) { + envVars.push(`NO_PROXY=${noProxy}`); + envVars.push(`no_proxy=${noProxy}`); + } +} module.exports = { wrapHandler, @@ -194,4 +229,7 @@ module.exports = { watchFile, isDebug, ignoreHttpError, + detectProxy, + keyValueArrayToObject, + addProxyVariables, }; diff --git a/package.json b/package.json index e5435b354..48f81f256 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.73.22", + "version": "0.73.23", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,