Skip to content

Commit

Permalink
monitor installation with helm (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-denysenko-codefresh authored May 10, 2021
1 parent 3aaa800 commit 1c92eb2
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 72 deletions.
37 changes: 37 additions & 0 deletions lib/interface/cli/commands/hybrid/execute-test-pipeline.cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Command = require('../../Command');
const runnerRoot = require('../root/runner.cmd');
const colors = require('colors');
const InstallationPlan = require('./InstallationPlan');
const { addPipelineToInstallationPlan } = require('./pipeline-helper');
const { createErrorHandler } = require('./helper');

const handleError = createErrorHandler(`\nIf you had any issues with the installation please report them at:\
${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`);

const defaultDockerRegistry = 'quay.io';

const command = new Command({
command: 'execute-test-pipeline',
parent: runnerRoot,
requiresAuthentication: true,
description: 'Executes test pipeline',
webDocs: {
category: 'Runner',
title: 'Execute test pipeline',
},
builder: yargs => yargs
.option('runtime-name', {
describe: 'Runtime name to execute pipeline on',
}),
handler: async (argv) => {
const { runtimeName } = argv;

const installationPlan = new InstallationPlan({ errHandler: handleError });
installationPlan.addContext('runtimeName', runtimeName ? runtimeName.trim() : runtimeName);
await addPipelineToInstallationPlan(installationPlan, defaultDockerRegistry, true);
await installationPlan.execute();
process.exit(); // TODO : This is not needed - needed to be fixed
},
});

module.exports = command;
114 changes: 43 additions & 71 deletions lib/interface/cli/commands/hybrid/init.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const installationProgress = require('./installation-process');
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
const {
createErrorHandler,
createTestPipeline,
executeTestPipeline,
updateTestPipelineRuntime,
drawCodefreshFiglet,
getDefaultRuntime,
getRecommendedKubeNamespace,
Expand All @@ -40,6 +37,9 @@ const {
const InstallationPlan = require('./InstallationPlan');
const { produceVenonaKeys } = require('./key-helper');
const { array } = require('yargs');
const {
addPipelineToInstallationPlan,
} = require('./pipeline-helper');

const defaultDockerRegistry = 'quay.io';
const handleError = createErrorHandler(`\nIf you had any issues with the installation please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`);
Expand Down Expand Up @@ -318,10 +318,6 @@ const initCmd = new Command({
httpsProxy = httpsProxy || detectedProxyVars.httpsProxy;
noProxy = noProxy || detectedProxyVars.noProxy;

if (shouldUseHelm) {
shouldExecutePipeline = false;
}

if (noQuestions) {
// use defaults
kubeContextName = kubeContextName || getKubeContext(kubeConfigPath);
Expand Down Expand Up @@ -415,7 +411,7 @@ const initCmd = new Command({
type: 'confirm',
name: 'shouldExecutePipeline',
default: INSTALLATION_DEFAULTS.RUN_DEMO_PIPELINE,
message: 'Run demo pipeline after install?',
message: shouldUseHelm ? 'Create demo pipeline?' : 'Run demo pipeline after install?',
});
}

Expand Down Expand Up @@ -503,7 +499,7 @@ const initCmd = new Command({
bypassDownload,
},
installationEvent: installationProgress.events.ACCEPTANCE_TESTS_RAN,
condition: !skipClusterTest,
condition: !skipClusterTest && !shouldUseHelm,
});

// generate new agent name
Expand Down Expand Up @@ -834,6 +830,20 @@ const initCmd = new Command({
condition: !shouldUseHelm,
});


function shouldInstallMonitoringFn() {
if (!installMonitor) {
return false;
}

if (isInCluster() || skipClusterIntegration) {
console.log('Monitor component cannot be installed without cluster integration, you can install it seperately using: "codefresh install monitor"');
return false;
}

return true;
}

// install monitoring
installationPlan.addStep({
name: 'install cluster monitoring',
Expand All @@ -858,22 +868,7 @@ const initCmd = new Command({
successMessage: 'Successfully installed cluster monitoring',
installationEvent: installationProgress.events.MONITOR_INSTALLED,
executeOnDryRun: true,
condition: async () => {
if (shouldUseHelm) {
return false;
}

if (!installMonitor) {
return false;
}

if (isInCluster() || skipClusterIntegration) {
console.log('Monitor component cannot be installed without cluster integration, you can install it seperately using: "codefresh install monitor"');
return false;
}

return true;
},
condition: !shouldUseHelm && shouldInstallMonitoringFn,
});

// helm value files if its enabled
Expand All @@ -888,6 +883,16 @@ const initCmd = new Command({
kubeNamespace,
);

const _appProxy = {
enabled: appProxy || false,
host: appProxyHost || '',
};

const monitor = {
enabled: shouldInstallMonitoringFn(),
clusterId: kubeContextName,
};

const global = {
namespace: kubeNamespace,
codefreshHost: sdk.config.context.url,
Expand All @@ -899,7 +904,7 @@ const initCmd = new Command({
keys,
};

const content = JSON.stringify({ global }, null, 4);
const content = JSON.stringify({ appProxy: _appProxy, monitor, global }, null, 4);

fs.writeFileSync(
helmValuesFile,
Expand All @@ -914,51 +919,7 @@ const initCmd = new Command({

// Post Installation
if (shouldExecutePipeline) {
const pipelines = await sdk.pipelines.list({ id: `${INSTALLATION_DEFAULTS.PROJECT_NAME}/${INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME}` });
const testPipelineExists = !!_.get(pipelines, 'docs.length');

if (!testPipelineExists) {
installationPlan.addStep({
name: 'create test pipeline',
func: async () => {
await createTestPipeline(
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
['echo hello Codefresh Runner!'],
dockerRegistry,
);
},
installationEvent: installationProgress.events.PIPELINE_CREATED,
});
} else {
installationPlan.addStep({
name: 'update test pipeline runtime',
func: async () => {
await updateTestPipelineRuntime(
undefined,
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
dockerRegistry,
);
},
errMessage: colors.yellow('*warning* could not update test pipeline runtime, you can' +
' change it manually if you want to run it again on this runtime'),
successMessage: 'Updated test pipeline runtime',
exitOnError: false,
});
}

installationPlan.addStep({
name: 'execute test pipeline',
func: async () => {
await executeTestPipeline(
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
);
},
errMessage: 'Failed to execute test pipeline',
installationEvent: installationProgress.events.PIPELINE_EXECUTED,
});
await addPipelineToInstallationPlan(installationPlan, dockerRegistry, !shouldUseHelm);
}

await installationPlan.execute();
Expand All @@ -972,11 +933,22 @@ const initCmd = new Command({
if (installMonitor) {
console.log(`Go to ${colors.blue('https://g.codefresh.io/kubernetes/services/')} to view your cluster in Codefresh dashbaord`);
}

console.log(`Link to the new runtime: ${colors.blue(`https://g.codefresh.io/account-admin/account-conf/runtime-environments?runtime=${encodeURI(installationPlan.getContext('runtimeName'))}`)}`);
console.log(`\nDocumentation link: ${colors.blue('https://codefresh.io/docs/docs/enterprise/codefresh-runner/#codefresh-runner-preview-release')}`);
console.log(`If you had any issues with the installation please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`);
await to(progressReporter.report(installationProgress.events.FINISHED, installationProgress.status.SUCCESS));
await drawCodefreshFiglet();

if (shouldUseHelm) {
console.log('\n\nTo install helm run:');
console.log('helm repo add cf-runtime https://h.cfcr.io/codefresh-inc/runtime');
console.log(`kubectl create ns ${kubeNamespace}`);
console.log(`helm install cf-runtime cf-runtime/cf-runtime -f ${helmValuesFile} --namespace ${kubeNamespace}\n`);
console.log('In order to test your runner helm based installation please execute\n' +
'codefresh runner execute-test-pipeline');
}

process.exit(); // TODO : This is not needed - needed to be fixed
},
});
Expand Down
71 changes: 71 additions & 0 deletions lib/interface/cli/commands/hybrid/pipeline-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const sdk = require('../../../../logic/sdk');
const colors = require('colors');
const installationProgress = require('./installation-process');
const _ = require('lodash');

const {
createTestPipeline,
executeTestPipeline,
updateTestPipelineRuntime,
INSTALLATION_DEFAULTS,
} = require('./helper');

const defaultDockerRegistry = 'quay.io';

async function addPipelineToInstallationPlan(installationPlan, dockerRegistry = '', executePipeline = true) {
if (!dockerRegistry) {
// eslint-disable-next-line no-param-reassign
dockerRegistry = defaultDockerRegistry;
}

const pipelines = await sdk.pipelines.list({ id: `${INSTALLATION_DEFAULTS.PROJECT_NAME}/${INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME}` });
const testPipelineExists = !!_.get(pipelines, 'docs.length');

if (!testPipelineExists) {
installationPlan.addStep({
name: 'create test pipeline',
func: async () => {
await createTestPipeline(
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
['echo hello Codefresh Runner!'],
dockerRegistry,
);
},
installationEvent: installationProgress.events.PIPELINE_CREATED,
});
} else {
installationPlan.addStep({
name: 'update test pipeline runtime',
func: async () => {
await updateTestPipelineRuntime(
undefined,
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
dockerRegistry,
);
},
errMessage: colors.yellow('*warning* could not update test pipeline runtime, you can' +
' change it manually if you want to run it again on this runtime'),
successMessage: 'Updated test pipeline runtime',
exitOnError: false,
});
}

installationPlan.addStep({
name: 'execute test pipeline',
func: async () => {
await executeTestPipeline(
installationPlan.getContext('runtimeName'),
INSTALLATION_DEFAULTS.DEMO_PIPELINE_NAME,
);
},
errMessage: 'Failed to execute test pipeline',
installationEvent: installationProgress.events.PIPELINE_EXECUTED,
condition: executePipeline,
});
}

module.exports = {
addPipelineToInstallationPlan,
};
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.75.16",
"version": "0.75.17",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 1c92eb2

Please sign in to comment.