deploy.js
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* 分发部署
* upload >> unzip >> startup
*
* @class Deploy
* @author jf<jeff.jiang@yoho.cn>
* @date 2016/5/21
*/
import sh from 'shelljs';
import ssh from 'ssh2';
import fs from 'fs';
import path from 'path';
import rp from 'request-promise';
import config from '../../config/config';
import ws from '../../lib/ws';
import {
DeployInfo,
Server
} from '../models';
class Deploy {
constructor(project, building) {
this.project = project;
this.building = building;
}
deploy(info) {
var self = this;
return this._deploy().then(() => {
self._state('deploying');
}).then(() => {
self._flush();
self._state('flushing');
}).then(() => {
self._state('deploy success');
}).catch(e => {
console.error(e);
self._state('fail');
});
}
_deploy() {
var self = this;
return new Promise((resolve, reject) => {
self._state('deploy code');
sh.cd(config.ci);
let child = sh.exec(`gulp upQiniu --name=${self.project.name} --time=${self.building.buildTime} --pkg=${self.building.pkgName}`, {
async: true
});
child.on('close', (code) => {
if (code == 0) {
console.log('deploy success');
resolve();
} else {
reject(new Error(`build code fail`));
}
});
})
}
_flush() {
var self = this;
return rp({
uri: 'http://flushcache.yoho.cn/flushdaction.php',
type: 'GET',
qs: {
url: `http://cdn.yoho.cn/${self.building.pkgName}/${self.building.version}/`
}
}).then(data => {
console.log(data);
});
}
async _state(state) {
ws.broadcast(`/deploy/${this.project._id}`, {
state: state
});
}
_log(msg) {
ws.broadcast(`/deploy/${this.project._id}/log`, {
host: this.info.host,
msg: msg
});
}
}
export default Deploy;