Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const docs = require('../docs');

describe('docs generation', () => {
jest.setTimeout(10000);
it('should generate docs', async () => {
await docs();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ promoteCmd = promoteCmd.toCommand();

jest.mock('../../../helpers/helm');
jest.mock('../../../helpers/workflow');
jest.spyOn(process, 'exit').mockImplementation();

const request = require('requestretry');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const install = new Command({
console.log(workflowId);
return;
}
await followLogs(workflowId);
const exitCode = await followLogs(workflowId);
process.exit(exitCode);
} catch (err) {
Output.printError(err);
process.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ const promote = new Command({
console.log(workflowId);
return;
}
await followLogs(workflowId);
const exitCode = await followLogs(workflowId);
process.exit(exitCode);
} catch (err) {
Output.printError(err);
process.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const install = new Command({
console.log(workflowId);
return;
}
await followLogs(workflowId);
const exitCode = await followLogs(workflowId);
process.exit(exitCode);
} catch (err) {
Output.printError(err);
process.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ testCmd = testCmd.toCommand();

jest.mock('../../../helpers/helm');
jest.mock('../../../helpers/workflow');
jest.spyOn(process, 'exit').mockImplementation();

const request = require('requestretry');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const install = new Command({
console.log(workflowId);
return;
}
await followLogs(workflowId);
const exitCode = await followLogs(workflowId);
process.exit(exitCode);
} catch (err) {
Output.printError(err);
process.exit(1);
Expand Down
19 changes: 3 additions & 16 deletions lib/interface/cli/commands/pipeline/run.cf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const RunBaseCommand = require('./run.base');
const { sdk } = require('../../../../logic');
const Workflow = require('../../../../logic/entities/Workflow');
const { followLogs } = require('../../helpers/workflow');

function _buildBody(data) {
const body = {
Expand Down Expand Up @@ -50,22 +50,9 @@ class RunExternalCommand extends RunBaseCommand {
console.log(this.workflowId);
return 0;
}
await sdk.logs.showWorkflowLogs(this.workflowId, true);
const buildResponse = await sdk.workflows.get({ id: this.workflowId });
const workflowInstance = Workflow.fromResponse(buildResponse);
switch (workflowInstance.getStatus()) {
case 'success':
return 0;
case 'error':
return 1;
case 'terminated':
return 2;
default:
return 100;
}
} else {
console.log(this.workflowId);
return followLogs(this.workflowId);
}
console.log(this.workflowId);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/interface/cli/commands/pipeline/run.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const DEFAULTS = require('../../defaults');
const _ = require('lodash');
const { sdk } = require('../../../../logic');
const { followLogs } = require('../../helpers/workflow');
const chalk = require('chalk');

const regex = /##[0-9a-f]{24}##/i;
Expand Down Expand Up @@ -151,8 +152,7 @@ class RunLocalCommand extends RunBaseCommand {
const include = line.match(regex);
if (include) {
const workflowId = include[0].substring(2, include[0].length - 2);
sdk.logs.showWorkflowLogs(workflowId, true)
.then(() => resolve(0));
followLogs(workflowId).then(resolve);
}
});
stream.on('error', (err) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/interface/cli/commands/workflow/restart.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const restart = new Command({
console.log(workflowId);
return;
}
await followLogs(workflowId);
const exitCode = await followLogs(workflowId);
process.exit(exitCode);
},
});

Expand Down
69 changes: 60 additions & 9 deletions lib/interface/cli/helpers/workflow.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
const Promise = require('bluebird');
const _ = require('lodash');
const request = require('requestretry');

const { sdk } = require('../../../logic');
const Workflow = require('../../../logic/entities/Workflow');

const END_STATUSES = ['error', 'success', 'terminated'];

const config = require('../../../logic/cli-config/Manager').config();

async function _fallbackLogs(workflowId, interval, retriesLeft) {
try {
console.log('Retrying to retrieve logs...');
await sdk.logs.showWorkflowLogs(workflowId, true);
return;
} catch (e) {
console.log(`Could not retrieve logs due to error: ${e.message}`);
}

let failedToGetStatus = false;

try {
console.log('Checking build status...');
const workflow = await sdk.workflows.get({ id: workflowId });
const currentStatus = workflow.status;
if (END_STATUSES.includes(currentStatus)) {
console.log(`Build finished with status: '${_.upperCase(currentStatus)}'`);
return;
}
console.log(`Current build status is: '${_.upperCase(currentStatus)}'`);
} catch (e) {
if (!request.RetryStrategies.HTTPOrNetworkError(e, _.pick('statusCode'))) {
throw e;
}

failedToGetStatus = true;

if (retriesLeft === 0) {
throw e;
}
console.log(`Could not get build status: ${e.message}.`);
console.log('Retrying...');
}

await Promise.delay(interval);
await _fallbackLogs(workflowId, interval, failedToGetStatus ? retriesLeft - 1 : retriesLeft);
}

const followLogs = async (workflowId) => {
await sdk.logs.showWorkflowLogs(workflowId, true);
try {
await sdk.logs.showWorkflowLogs(workflowId, true);
} catch (e) {
if (!process.env.CF_CLI_RUN_WAIT_FALLBACK) {
throw e;
}
console.log(`Could not retrieve logs due to error: ${e.message}`);
await _fallbackLogs(workflowId, config.logs.fallback.interval, config.logs.fallback.maxAttempts);
}
const json = await sdk.workflows.get({ id: workflowId });
const workflowInstance = Workflow.fromResponse(json);
switch (workflowInstance.getStatus()) {
case 'success':
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ENUM values

process.exit(0);
break;
return 0;
case 'error':
process.exit(1);
break;
return 1;
case 'terminated':
process.exit(2);
break;
return 2;
default:
process.exit(100);
break;
return 100;
}
};

module.exports = {
followLogs,
_fallbackLogs,
};
18 changes: 18 additions & 0 deletions lib/logic/cli-config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
"description": "Defines delay between retries"
}
}
},
"logs": {
"type": "object",
"properties": {
"fallback": {
"type": "object",
"properties": {
"interval": {
"type": "integer",
"default": 5000
},
"maxAttempts": {
"type": "integer",
"default": 1000
}
}
}
}
}
}
}
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.17.8",
"version": "0.17.9",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down