2014-12-02 21:21:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-12 22:19:42 +00:00
|
|
|
var Steppy = require('twostep').Steppy,
|
2014-12-02 21:21:32 +00:00
|
|
|
fs = require('fs'),
|
2015-04-12 22:19:42 +00:00
|
|
|
path = require('path'),
|
2015-05-18 17:52:01 +00:00
|
|
|
_ = require('underscore'),
|
2015-05-20 20:20:51 +00:00
|
|
|
reader = require('./reader'),
|
2015-07-26 13:05:54 +00:00
|
|
|
db = require('../db'),
|
2015-12-02 20:24:45 +00:00
|
|
|
utils = require('./utils'),
|
2015-12-28 20:34:08 +00:00
|
|
|
SpawnCommand = require('./command/spawn').Command,
|
|
|
|
validateParams = require('./validateParams');
|
2014-12-02 21:21:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and returns given `config` to the `callback`(err, config)
|
|
|
|
*/
|
|
|
|
exports.validateConfig = function(config, callback) {
|
2015-12-28 20:34:08 +00:00
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
validateParams(config, {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
scm: {
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
properties: {
|
|
|
|
type: {enum: ['git', 'mercurial'], required: true},
|
|
|
|
repository: {type: 'string', required: true},
|
|
|
|
rev: {type: 'string', required: true}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
steps: {
|
|
|
|
type: 'array',
|
|
|
|
required: true,
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
cmd: {type: 'string', required: true},
|
2015-12-31 20:58:39 +00:00
|
|
|
name: {type: 'string'},
|
|
|
|
type: {enum: ['shell']},
|
2015-12-28 20:34:08 +00:00
|
|
|
shell: {type: 'string'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
additionalProperties: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.pass(null);
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
err.message = (
|
|
|
|
'Error during validation of project "' + config.name +
|
|
|
|
'": ' + err.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
callback(err, config);
|
|
|
|
}
|
|
|
|
);
|
2014-12-02 21:21:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-13 19:25:18 +00:00
|
|
|
* Loads and returns project
|
2014-12-02 21:21:32 +00:00
|
|
|
*/
|
|
|
|
exports.load = function(baseDir, name, callback) {
|
|
|
|
var dir = path.join(baseDir, name);
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
fs.readdir(dir, this.slot());
|
|
|
|
},
|
|
|
|
function(err, dirContent) {
|
|
|
|
exports.loadConfig(dir, this.slot());
|
|
|
|
},
|
|
|
|
function(err, config) {
|
2015-06-14 20:31:17 +00:00
|
|
|
config.name = name;
|
2015-05-01 11:11:29 +00:00
|
|
|
config.dir = dir;
|
2015-12-28 20:34:08 +00:00
|
|
|
|
|
|
|
exports.validateConfig(config, this.slot());
|
2015-04-12 22:19:42 +00:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-13 19:25:18 +00:00
|
|
|
* Loads all projects from `baseDir` and returns array of projects
|
2015-04-12 22:19:42 +00:00
|
|
|
*/
|
|
|
|
exports.loadAll = function(baseDir, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
fs.readdir(baseDir, this.slot());
|
|
|
|
},
|
|
|
|
function(err, dirs) {
|
|
|
|
var loadGroup = this.makeGroup();
|
|
|
|
_(dirs).each(function(dir) {
|
|
|
|
exports.load(baseDir, dir, loadGroup.slot());
|
|
|
|
});
|
2014-12-02 21:21:32 +00:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.loadConfig = function(dir, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
2015-05-18 17:52:01 +00:00
|
|
|
reader.load(dir, 'config', this.slot());
|
2014-12-02 21:21:32 +00:00
|
|
|
},
|
2015-05-19 20:48:20 +00:00
|
|
|
function(err, config) {
|
2015-08-21 20:38:20 +00:00
|
|
|
// convert steps object to array
|
|
|
|
if (!_(config.steps).isArray() && _(config.steps).isObject()) {
|
|
|
|
config.steps = _(config.steps).map(function(val, name) {
|
|
|
|
var step;
|
|
|
|
if (_(val).isObject()) {
|
|
|
|
step = val;
|
|
|
|
} else {
|
|
|
|
step = {cmd: val};
|
|
|
|
}
|
|
|
|
step.name = name;
|
|
|
|
return step;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:34:08 +00:00
|
|
|
// apply defaults to not yet validated config
|
2015-05-19 20:48:20 +00:00
|
|
|
_(config.steps).each(function(step) {
|
2015-05-20 20:20:51 +00:00
|
|
|
if (!step.type) step.type = 'shell';
|
2015-12-28 20:34:08 +00:00
|
|
|
if (!step.name && step.cmd) step.name = utils.prune(step.cmd, 40);
|
2015-05-19 20:48:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.pass(config);
|
|
|
|
},
|
2014-12-02 21:21:32 +00:00
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.saveConfig = function(config, dir, callback) {
|
|
|
|
fs.writeFile(
|
|
|
|
path.join(dir, 'config.json'),
|
|
|
|
JSON.stringify(config, null, 4),
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.create = function(baseDir, config, callback) {
|
|
|
|
var dir;
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
dir = path.join(baseDir, config.name);
|
|
|
|
fs.mkdir(dir, this.slot());
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
exports.saveConfig(config, baseDir, this.slot());
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
exports.load(dir, this.slot());
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
2015-07-26 13:05:54 +00:00
|
|
|
|
|
|
|
exports.getAvgProjectBuildDuration = function(projectName, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
// get last done builds to calc avg build time
|
|
|
|
db.builds.find({
|
|
|
|
start: {
|
|
|
|
projectName: projectName,
|
|
|
|
status: 'done',
|
|
|
|
descCreateDate: ''
|
|
|
|
},
|
|
|
|
limit: 10
|
|
|
|
}, this.slot());
|
|
|
|
},
|
|
|
|
function(err, doneBuilds) {
|
|
|
|
var durationsSum = _(doneBuilds).reduce(function(memo, build) {
|
|
|
|
return memo + (build.endDate - build.startDate);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
this.pass(Math.round(durationsSum / doneBuilds.length));
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
2015-12-02 20:24:45 +00:00
|
|
|
|
|
|
|
exports.remove = function(params, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
db.builds.find({
|
|
|
|
start: {projectName: params.name, descCreateDate: ''}
|
|
|
|
}, this.slot());
|
|
|
|
|
|
|
|
new SpawnCommand().run({cmd: 'rm', args: [
|
|
|
|
'-Rf', path.join(params.baseDir, params.name)
|
|
|
|
]}, this.slot());
|
|
|
|
},
|
|
|
|
function(err, builds) {
|
|
|
|
if (builds.length) {
|
|
|
|
db.builds.del(builds, this.slot());
|
|
|
|
|
|
|
|
var logLinesRemoveGroup = this.makeGroup();
|
|
|
|
_(builds).each(function(build) {
|
|
|
|
db.logLines.remove({
|
|
|
|
start: {buildId: build.id}
|
|
|
|
}, logLinesRemoveGroup.slot());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.pass(null, null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
2015-12-04 19:54:21 +00:00
|
|
|
|
|
|
|
exports.rename = function(params, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
fs.rename(
|
|
|
|
path.join(params.baseDir, params.name),
|
|
|
|
path.join(params.baseDir, params.newName),
|
|
|
|
this.slot()
|
|
|
|
);
|
|
|
|
|
|
|
|
db.builds.multiUpdate(
|
|
|
|
{start: {projectName: params.name, descCreateDate: ''}},
|
|
|
|
function(build) {
|
|
|
|
build.project.name = params.newName;
|
|
|
|
return build;
|
|
|
|
},
|
|
|
|
this.slot()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|