projects.js 5.38 KB
'use strict';

import Router from 'koa-router';
import moment from 'moment';
import Build from '../../ci/build';
import Deploy from '../../ci/deploy';


import {
    Building,
    Project,
    Server,
    DeployInfo
} from '../../models';

let r = new Router();

const colors = ['primary', 'success', 'info', 'warning', 'danger', 'success-alt', 'info-alt', 'warning-alt', 'danger-alt', 'primary-head', 'success-head', 'danger-head'];

const p = {
    /**
     * 所有项目首页
     */
    index_page: async(ctx, next) => {
        let projects = await Project.findAll();
        projects.forEach((p, i) => {
            p.color = colors[i % colors.length];
        });
        await ctx.render('action/projects', {
            projects: projects
        });
    },
    /**
     * 单个项目首页
     */
    project_index: async (ctx, next) => {
        let id = ctx.params.id;
        let project = await Project.findById(id);

        // let promises = deploy.target.map((host) => {
        //     console.log('read host :' + host);
        //     return DeployInfo.findOne({
        //         projectId: project._id,
        //         host: host
        //     }).then((info) => {
        //         return {
        //             host: host,
        //             hostFm: host.replace(/\./g, '-'),
        //             info: info
        //         };
        //     });
        // });
        // let targets = await Promise.all(promises);

        await ctx.render('action/project_index', {
            project: project
        });
    },

    new_page: async(ctx, next) => {
        let serversAll = await Server.findAll();
        let servers = {};
        serversAll.forEach(s => {
            if (servers[s.env]) {
                servers[s.env].push(s);
            } else {
                servers[s.env] = [s];
            }
        });
        await ctx.render('action/project_form', {
            servers: servers,
            project: {
                deploy: {
                    production: {},
                    test: {}
                }
            }
        });

    },
    edit_page: async(ctx, next) => {
        let id = ctx.query.id;
        let project = await Project.findById(id);

        let serversAll = await Server.findAll();
        let servers = {};
        serversAll.forEach(s => {
            if (servers[s.env]) {
                servers[s.env].push(s);
            } else {
                servers[s.env] = [s];
            }
            if (project.deploy[s.env] && project.deploy[s.env].target && project.deploy[s.env].target.indexOf(s.host) >= 0) {
                s.checked = true;
            }
        });

        await ctx.render('action/project_form', {
            project: project,
            servers: servers
        });
    },
    save: async(ctx, next) => {
        let project = ctx.request.body;
        let id = project._id;
        delete project._id;
        if (id) {
            await Project.update({
                _id: id
            }, {
                $set: project
            });
        } else {
            await Project.insert(project);
        }
        ctx.redirect('/projects');
        ctx.status = 301;
    },
    buildings_table: async(ctx, next) => {
        let pid = ctx.params.id;
        let buildings = await Building.cfind({
            projectId: pid
        }).sort({
            buildTime: -1
        }).exec();
        buildings.forEach(b => {
            b.updatedAt = moment(b.updatedAt).format('YYYY-MM-DD HH:mm:ss');
        });
        ctx.body = {
            data: buildings
        };
    },
    project_build: async(ctx, next) => {
        let pid = ctx.params.pid;
        let env = 'production';
        let branch = 'master';
        let p = await Project.findById(pid);
        let build = new Build(p);

        let {
            buildTime,
            version,
            distFile
        } = build.build(branch);
        let buildingDoc = await Building.insert({
            buildTime: buildTime,
            project: p.name,
            version: version,
            projectId: pid,
            branch: branch,
            env: env,
            distFile: distFile,
            state: 'waiting',
            createdAt: new Date(),
            updatedAt: new Date()
        });

        let id = buildingDoc[0]._id;

        build.run(id);
        ctx.body = {
            code: 200,
            id: id
        };
        await next();
    },
    project_deploy: async(ctx) => {
        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.findByName(building.project);

            let deploy = new Deploy(project, building);
            deploy.deploy();

            ctx.body = {
                code: 200,
                building: building
            };
        } else {
            ctx.body = {
                code: 201,
                msg: '该版本未构建成功,暂不能分发'
            };
        }
    }
};

r.get('/', p.index_page);
r.get('/new', p.new_page);
r.get('/edit', p.edit_page);
r.get('/:id', p.project_index);
r.get('/:id/buildings', p.buildings_table);
r.post('/save', p.save);
r.post('/build/:pid', p.project_build);
r.post('/deploy/:building', p.project_deploy);
export default r;