Skip to content

Commit

Permalink
CR-1683 - inject proxy env vars to engine container (#596)
Browse files Browse the repository at this point in the history
* inject proxy env vars to engine container

* 0.73.23
  • Loading branch information
ATGardner authored Dec 2, 2020
1 parent 3ea200e commit c9341f7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 53 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v10.15.3
3 changes: 2 additions & 1 deletion lib/interface/cli/commands/gitops/install.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
39 changes: 0 additions & 39 deletions lib/interface/cli/commands/hybrid/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,38 +950,13 @@ 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]}`);
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}`);
}
}

function serealizeToKeyValuePairs(obj) {
return _.keys(obj).reduce((acc, key) => {
if (acc) {
Expand All @@ -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]}`;
Expand Down Expand Up @@ -1088,7 +1052,6 @@ module.exports = {
getRuntimeVersion,
createTestPipeline,
getTestPipeline,
addProxyVariables,
updateTestPipelineRuntime,
executeTestPipeline,
createProgressBar,
Expand All @@ -1103,14 +1066,12 @@ module.exports = {
newRuntimeName,
newAgentName,
keyValueAsStringToObject,
keyValueArrayToObject,
objectToKeyValueArray,
downloadRelatedComponents: downloadHybridComponents,
downloadSteveDore,
downloadVeonona,
downloadProvider,
runUpgrade,
detectProxy,
serealizeToKeyValuePairs,
getRuntimeImagesWithRegistryUrl,
installAppProxy,
Expand Down
4 changes: 1 addition & 3 deletions lib/interface/cli/commands/hybrid/init.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -16,7 +17,6 @@ const installationProgress = require('./installation-process');
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
const {
createErrorHandler,
keyValueArrayToObject,
createTestPipeline,
executeTestPipeline,
updateTestPipelineRuntime,
Expand All @@ -30,10 +30,8 @@ const {
attachRuntime,
newRuntimeName,
newAgentName,
detectProxy,
keyValueAsStringToObject,
getRuntimeImagesWithRegistryUrl,
addProxyVariables,
mergeValuesFromValuesFile,
INSTALLATION_DEFAULTS,
} = require('./helper');
Expand Down
24 changes: 15 additions & 9 deletions lib/interface/cli/commands/pipeline/run.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions lib/interface/cli/helpers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -194,4 +229,7 @@ module.exports = {
watchFile,
isDebug,
ignoreHttpError,
detectProxy,
keyValueArrayToObject,
addProxyVariables,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codefresh",
"version": "0.73.22",
"version": "0.73.23",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit c9341f7

Please sign in to comment.