Authored by 姜枫

add deploy api

... ... @@ -16,6 +16,7 @@ const ws = require('../../lib/ws');
const {
Building
} = require('../models');
const _ = require('lodash');
class Build {
... ... @@ -171,7 +172,7 @@ class Build {
sh.cd(this.buildPath);
files.forEach(f => {
_.forEach(files, f => {
if (f) {
let fDir = path.join(this.buildPath, f);
... ...
... ... @@ -184,6 +184,56 @@ r.get('/rollback', async (ctx) => {
}
});
r.get('/deploy', async (ctx) => {
let q = ctx.request.query;
let buildingId = ctx.params.building;
let building = await Building.findById(buildingId);
if (!building) {
ctx.body = {
code: 201,
msg: '该版本不存在'
};
} else if (building.state == 'success') {
let project = await Project.findById(building.projectId);
let targets = project.deploy[building.env].target;
if (typeof targets === 'string') {
targets = [targets];
}
targets.forEach(async(host) => {
let doc = await DeployInfo.findOne({
projectId: project._id,
host: host,
env: building.env
});
if (!doc || doc.state !== 'running' || doc.building !== building.buildTime) {
let deploy = new Deploy(project, building, host);
DeployPool.deploy(deploy);
}
});
await Operation.action(ctx.session.user, 'PROJECT_DEPLOY', '项目分发部署', {
_id: buildingId,
project: project.name,
branch: building.branch,
env: building.env
});
ctx.body = {
code: 200,
building: building
};
} else {
ctx.body = {
code: 201,
msg: '该版本未构建成功,暂不能分发'
};
}
});
r.get('/state', async (ctx) => {
let q = ctx.request.query;
... ... @@ -194,7 +244,18 @@ r.get('/state', async (ctx) => {
building: building.buildTime
});
let finish = true;
let redeploy = false;
let deploys = _.map(doc, d => {
if (d.state !== 'success' && d.state !== 'fail') {
finish = false;
}
if (d.state === 'fail') {
redeploy = true;
}
return {
host: d.host,
state: d.state
... ... @@ -203,7 +264,9 @@ r.get('/state', async (ctx) => {
ctx.body = {
build: building.state,
deploy: deploys
deploy: deploys,
finish: finish,
redeploy: redeploy && finish
};
});
... ...