Skip to content

Commit

Permalink
Saas 7447 - fix migration (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
roi-codefresh authored Jun 11, 2020
1 parent e4939d3 commit f8a9a61
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
4 changes: 4 additions & 0 deletions lib/interface/cli/commands/agent/install.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ const installAgentCmd = new Command({
} = argv;
const {
'runtime-name': reName,
'skip-re-creation': skipRuntimeCreation,
'kube-node-selector': kubeNodeSelector,
'build-node-selector': buildNodeSelector,
'dry-run': dryRun,
'in-cluster': inCluster,
'kubernetes-runner-type': kubernetesRunnerType,
Expand Down Expand Up @@ -182,13 +184,15 @@ const installAgentCmd = new Command({
if (installRuntime) {
return installRuntimeCmd.handler({
'runtime-name': reName,
'skip-re-creation': skipRuntimeCreation,
'runtime-kube-context-name': kubeContextName,
'runtime-kube-namespace': kubeNamespace,
'agent-name': name,
'runtime-kube-config-path': kubeConfigPath,
'attach-runtime': true,
'restart-agent': true,
'make-default-runtime': shouldMakeDefaultRe,
'kube-node-selector': buildNodeSelector,
'storage-class-name': storageClassName,
'set-value': setValue,
'set-file': setFile,
Expand Down
49 changes: 24 additions & 25 deletions lib/interface/cli/commands/hybrid/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const _ = require('lodash');
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
const { sdk } = require('../../../../logic');
const installAgent = require('../agent/install.cmd');
const attachRuntime = require('../runtimeEnvironments/attach.cmd');
const installMonitoring = require('../monitor/install.cmd');
const colors = require('colors');
const inquirer = require('inquirer');
Expand Down Expand Up @@ -61,6 +60,10 @@ async function migrate({
agents,
}) {
const newAgentName = agentName || await getNewAgentName(kubeContextName, kubeNamespace, agents);
const [getRuntimeErr, runtime] = await to(sdk.runtimeEnvs.get({ name: runtimeName }));
handleError(getRuntimeErr, `Failed to get runtime with name "${runtimeName}"`);
const oldNodeSelector = _.get(runtime, 'runtimeScheduler.cluster.nodeSelector');
const oldStorageClassName = _.get(runtime, 'dockerDaemonScheduler.pvcs.dind.storageClassName');

// prompt migration process confirmation
console.log(`${colors.red('This migration process will do the following:')}`);
Expand All @@ -81,7 +84,15 @@ async function migrate({
process.exit(1);
}

// delete old agent
// prepare old runtime
if (oldStorageClassName && oldStorageClassName.startsWith('dind-local-volumes-venona')) {
// need to replace to start with 'dind-local-volumes-runner'
const newRe = _.set(runtime, 'dockerDaemonScheduler.pvcs.dind.storageClassName', oldStorageClassName.replace('venona', 'runner'));
const [err] = await to(sdk.runtimeEnvs.update({ name: runtimeName }, newRe));
handleError(err, 'Failed to update runtime storage class name');
}

// delete old agent and runtime
console.log(`Running migration script on runtime: ${colors.cyan(runtimeName)}`);
const [migrateScriptErr, migrateScriptExitCode] = await to(sdk.agents.migrate({
kubeContextName,
Expand Down Expand Up @@ -113,43 +124,31 @@ async function migrate({
oldConfig.nodeSelector = `${key}=${oldConfig.nodeSelector[key]}`;
}

// install new agent
console.log(`Creating new codefresh agent with name: ${colors.cyan(newAgentName)}`);
// install new agent and runtime
console.log(`Creating new codefresh runner with name: ${colors.cyan(newAgentName)}`);
const agentInstallOptions = {
name: newAgentName,
'kube-context-name': kubeContextName,
'kube-node-selector': oldConfig.nodeSelector,
'build-node-selector': oldNodeSelector,
'kube-namespace': kubeNamespace,
'agent-kube-namespace': kubeNamespace,
'agent-kube-context-name': kubeContextName,
'agent-kube-config-path': kubeConfigPath,
tolerations: JSON.stringify(oldConfig.tolerations),
'kube-config-path': kubeConfigPath,
'install-runtime': false,
'install-runtime': true,
'runtime-name': runtimeName,
'skip-re-creation': true,
verbose,
'make-default-runtime': shouldMakeDefaultRe,
'storage-class-name': storageClassName,
'storage-class-name': storageClassName || oldStorageClassName,
terminateProcess: false,
'set-value': setValue,
'set-file': setFile,
};
const [agentInstallErr] = await to(installAgent.handler(agentInstallOptions));
handleError(agentInstallErr, 'Failed to install new agent');

// attach old runtime to new agent
console.log(`Attaching runtime: ${colors.cyan(runtimeName)} to agent: ${colors.cyan(newAgentName)}`);
const [attachRuntimeErr] = await to(attachRuntime.handler({
'agent-name': newAgentName,
'runtime-name': runtimeName,
'runtime-kube-context-name': kubeContextName,
'runtime-kube-namespace': kubeNamespace,
'runtime-kube-serviceaccount': 'venona',
'runtime-kube-config-path': kubeConfigPath,
'agent-kube-context-name': kubeContextName,
'agent-kube-namespace': kubeNamespace,
'agent-kube-config-path': kubeConfigPath,
'restart-agent': true,
terminateProcess: false,
verbose,
}));
handleError(attachRuntimeErr, 'Failed to attach the old runtime to the new agent');
handleError(agentInstallErr, 'Failed to install new agent and runtime');

// Install new monitoring components
console.log('Installing monitoring components');
Expand Down
38 changes: 20 additions & 18 deletions lib/interface/cli/commands/runtimeEnvironments/install.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const installRuntimeCmd = new Command({
'storage-class-name': storageClassName,
'agent-name': agentName,
'runtime-name': reName,
'skip-re-creation': skipRuntimeCreation,
'dry-run': dryRun,
'in-cluster': inCluster,
'kube-node-selector': kubeNodeSelector,
Expand Down Expand Up @@ -176,23 +177,25 @@ const installRuntimeCmd = new Command({
}

// create RE in codefresh
await sdk.cluster.create({
namespace: kubeNamespace,
storageClassName: storageClassName || `${defaultStorageClassPrefix}-${kubeNamespace}`,
runnerType: kubernetesRunnerType,
nodeSelector: kubeNodeSelectorObj,
annotations: buildAnnotations,
clusterName,
runtimeEnvironmentName: runtimeName,
agent: true,
});
console.log(`Runtime environment "${colors.cyan(runtimeName)}" has been created`);
if (shouldMakeDefaultRe) {
const re = await sdk.runtimeEnvs.get({
name: runtimeName,
if (!skipRuntimeCreation) {
await sdk.cluster.create({
namespace: kubeNamespace,
storageClassName: storageClassName || `${defaultStorageClassPrefix}-${kubeNamespace}`,
runnerType: kubernetesRunnerType,
nodeSelector: kubeNodeSelectorObj,
annotations: buildAnnotations,
clusterName,
runtimeEnvironmentName: runtimeName,
agent: true,
});
await sdk.runtimeEnvs.setDefault({ account: re.accountId, name: re.metadata.name });
console.log(`Runtime environment "${colors.cyan(runtimeName)}" has been set as the default runtime`);
console.log(`Runtime environment "${colors.cyan(runtimeName)}" has been created`);
if (shouldMakeDefaultRe) {
const re = await sdk.runtimeEnvs.get({
name: runtimeName,
});
await sdk.runtimeEnvs.setDefault({ account: re.accountId, name: re.metadata.name });
console.log(`Runtime environment "${colors.cyan(runtimeName)}" has been set as the default runtime`);
}
}

// check if cluster already exists
Expand Down Expand Up @@ -223,7 +226,6 @@ const installRuntimeCmd = new Command({
}

// install RE on cluster

const runtimeEvents = new ProgressEvents();
const runtimeFormat = 'downloading runtime installer [{bar}] {percentage}% | {value}/{total}';
const runtimmrProgressBar = new cliProgress.SingleBar({ stopOnComplete: true, format: runtimeFormat }, cliProgress.Presets.shades_classic);
Expand Down Expand Up @@ -256,7 +258,7 @@ const installRuntimeCmd = new Command({
setFile,
terminateProcess: !attachRuntime,
events: runtimeEvents,
storageClassName,
storageClassName: storageClassName && storageClassName.startsWith('dind-local-volumes') ? undefined : storageClassName,
logFormatting: DefaultLogFormatter,
});
// attach RE to agent in codefresh
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.68.4",
"version": "0.68.5",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit f8a9a61

Please sign in to comment.