Skip to content

Commit

Permalink
Saas 489 cli helm promotion (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh authored Oct 16, 2018
1 parent b91363f commit 36c999d
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Commands:
codefresh run <name> Run a pipeline by id or name and attach the created workflow logs.
codefresh delete-release [name] Delete a helm release from a kubernetes cluster.
codefresh install-chart Install or upgrade a Helm chart Repository flag can be either absolute url or saved repository in Codefresh.
codefresh helm-promotion Promote a Helm release in another environment.
codefresh test-release [name] Test a helm release.
Options:
Expand Down
120 changes: 120 additions & 0 deletions lib/interface/cli/commands/pipeline/dynamic/promote-chart.cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const Command = require('../../../Command');
const {
promoteChart,
} = require('./../../../../../logic/api/helm');
const { printError } = require('./../../../helpers/general');
const { log, workflow } = require('../../../../../logic').api;

const promote = new Command({
root: true,
command: 'helm-promotion',
description: 'Promote a Helm release in another environment',
webDocs: {
category: 'Predefined Pipelines',
title: 'Promote Helm Release',
weight: 20,
},
builder: (yargs) => {
return yargs
.option('board', {
description: 'Board for promotion',
type: 'string',
required: false,
})
.option('source', {
description: 'Source column',
type: 'string',
required: false,
})
.option('target', {
description: 'Target column',
type: 'string',
required: false,
})
.option('namespace', {
description: 'Promote to namespace',
default: 'default',
type: 'string',
})
.option('source-tiller-namespace', {
description: 'Where tiller has been installed in source cluster',
default: 'kube-system',
type: 'string',
})
.option('target-tiller-namespace', {
description: 'Where tiller has been installed in target cluster',
default: 'kube-system',
type: 'string',
})
.option('source-release', {
description: 'The name of source release',
})
.option('revision', {
description: 'Revision of source release',
type: 'string',
required: false,
})
.option('target-release', {
description: 'The name to set to the release',
})
.option('context', {
description: 'Contexts (yaml || secret-yaml) to be passed to the install',
array: true,
})
.option('set', {
description: 'set of KEY=VALUE to be passed to the install',
array: true,
})
.option('detach', {
alias: 'd',
describe: 'Run pipeline and print build ID',
})
.example(
'codefresh helm-promotion --board app --source dev --target test --source-release application',
`Promote 'application' release on board 'app' from 'dev' to 'test' environment`);
},
handler: async (argv) => {
try {
const workflowId = await promoteChart({
board: argv.board,
sourceSection: argv.source,
targetSection: argv.target,
sourceCluster: argv.sourceCluster,
tillerNamespace: argv.sourceTillerNamespace,
releaseName: argv.sourceRelease,
revision: argv.revision,
targetCluster: argv.targetCluster,
targetTillerNamespace: argv.targetTillerNamespace,
targetNamespace: argv.namespace,
targetReleaseName: argv.targetRelease,
values: argv.context,
setValues: argv.set,
});
if (argv.detach) {
console.log(workflowId);
} else {
await log.showWorkflowLogs(workflowId, true);
const workflowInstance = await workflow.getWorkflowById(workflowId);
switch (workflowInstance.getStatus()) {
case 'success':
process.exit(0);
break;
case 'error':
process.exit(1);
break;
case 'terminated':
process.exit(2);
break;
default:
process.exit(100);
break;
}
}
} catch (err) {
printError(err);
process.exit(1);
}
},
});

module.exports = promote;
49 changes: 49 additions & 0 deletions lib/logic/api/helm.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,54 @@ const installChart = async ({
return res.id;
};

const promoteChart = async ({
board,
sourceSection,
targetSection,
sourceCluster,
tillerNamespace,
releaseName,
revision,
targetCluster,
targetTillerNamespace,
targetNamespace,
targetReleaseName,
values,
setValues,
}) => {
let normalizedValues = [];
if (values && _.isArray(values) && values.length > 0) {
normalizedValues = await _normalizeValues(values);
}

const normalizedSetValues = await _normalizeSetValues(setValues);
const options = {
url: '/api/kubernetes/chart/promote',
method: 'POST',
qs: {
selector: sourceCluster,
tillerNamespace,
},
body: {
board,
sourceSection,
targetSection,
sourceCluster,
releaseName,
revision,
targetSelector: targetCluster,
targetTillerNamespace,
targetNamespace,
targetReleaseName,
values: normalizedValues,
set: normalizedSetValues,
},
};

const res = await sendHttpRequest(options);
return res.id;
};

const testRelease = async ({
releaseName,
cluster,
Expand Down Expand Up @@ -198,6 +246,7 @@ const deleteRepo = async (name) => {

module.exports = {
installChart,
promoteChart,
testRelease,
deleteRelease,
getAllRepos,
Expand Down
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.8.77",
"version": "0.8.78",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 36c999d

Please sign in to comment.