deploy.js
5.84 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* 分发部署
* upload >> unzip >> startup
*
* @class Deploy
* @author jf<jeff.jiang@yoho.cn>
* @date 2016/5/21
*/
import ssh from 'ssh2';
import path from 'path';
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;
}
async deploy(info) {
let server = await Server.findByHost(info.host);
this.server = server;
this.info = info;
this.sshDeploy({
host: server.host,
username: server.username,
password: server.password,
port: server.port
});
}
sshDeploy(serverInfo) {
console.log('ssh connecting', serverInfo);
let conn = new ssh.Client();
let self = this;
conn.on('ready', async() => {
console.log(`connected ${serverInfo.host}`);
try {
await self._preDeploy(conn);
await self._scp(conn);
await self._unzip(conn);
await self._startup(conn);
conn.end();
} catch (e) {
self._state('fail');
self._log(e);
}
}).on('error', (err) => {
self._state('fail');
self._log(err);
}).connect(serverInfo);
}
_preDeploy(conn) {
let self = this;
return new Promise((resolve, reject) => {
let script = `mkdir -p ${self.remoteWorkDir} && mkdir -p ${self.remoteDist}`;
self._state('preparing');
self._log(`>>>>>>>>> ${script} >>>>>>>>`);
conn.exec(script, (err, stream) => {
if (err) {
reject(err);
} else {
stream.on('exit', (code) => {
resolve();
});
}
});
});
}
_scp(conn) {
let self = this;
return new Promise((resolve, reject) => {
self._state('uploading');
self._log(`>>>> uploading ${self.localFile} ==> ${self.remoteFile}`);
conn.sftp((err, sftp) => {
if (err) {
reject(err);
} else {
sftp.fastPut(self.localFile, self.remoteFile, {
chunkSize: 10240
}, (err) => {
if (err) {
reject(err);
} else {
self._log(' uploaded success!');
self._state('uploaded');
resolve();
}
});
}
});
});
}
_unzip(conn) {
let self = this;
return new Promise((resolve, reject) => {
self._state('unziping');
let script = `tar -zxvf ${self.remoteFile} -C ${self.remoteWorkDir} && rm -rf ${self.remoteDist}`;
self._log(`>>>> unziping ${self.remoteFile} ==> ${self.remoteWorkDir}`);
conn.exec(script, (err, stream) => {
if (err) {
reject(err);
} else {
stream.stdout.on('data', (data) => {
//self._log(data.toString());
});
stream.stderr.on('data', (data) => {
//self._log(data.toString());
});
stream.on('exit', (code) => {
if (code === 0) {
self._state('unziped');
resolve();
} else {
reject('unzip fail: ' + script);
}
});
}
})
});
}
_startup(conn) {
let self = this;
let startup = this.project.scripts.start;
return new Promise((resolve, reject) => {
self._state('starting');
self._log(`>>>> ${startup}`);
conn.exec(`cd ${self.remoteRunningDir} && ${startup}`, (err, stream) => {
if (err) {
reject(err);
} else {
stream.stdout.on('data', (data) => {
self._log(data.toString());
});
stream.stderr.on('data', (data) => {
self._log(data.toString());
});
stream.on('exit', (code) => {
if (code === 0) {
self._state('running');
resolve();
} else {
reject('startup fail');
}
});
}
});
});
}
async _state(state) {
ws.broadcast(`/deploy/${this.project._id}`, {
host: this.info.host,
state: state
});
await DeployInfo.updateState(this.info._id, state);
}
_log(msg) {
ws.broadcast(`/deploy/${this.project._id}/log`, {
host: this.info.host,
msg: msg
});
}
get remoteWorkDir() {
return path.join(this.server.deployDir, this.project.name, 'current');
}
get remoteRunningDir() {
return path.join(this.server.deployDir, this.project.name, 'current', this.project.name);
}
get remoteDist() {
return path.join(this.server.deployDir, this.project.name, this.building.buildTime);
}
get remoteFile() {
return path.join(this.server.deployDir, this.building.distFile);
}
get localFile() {
return path.join(config.buildDir, this.building.distFile);
}
}
export default Deploy;