...
|
...
|
@@ -9,7 +9,7 @@ |
|
|
|
|
|
import ssh from 'ssh2';
|
|
|
import path from 'path';
|
|
|
|
|
|
import fs from 'fs';
|
|
|
import config from '../../config/config';
|
|
|
import ws from '../../lib/ws';
|
|
|
import {
|
...
|
...
|
@@ -35,19 +35,14 @@ class Deploy { |
|
|
}
|
|
|
|
|
|
async deploy(cb) {
|
|
|
|
|
|
let server = await Server.findByHost(this.host);
|
|
|
this.id = await DeployInfo.insertOrUpdate(this.info);
|
|
|
this.server = server;
|
|
|
this.callback = cb;
|
|
|
setImmediate(function(){
|
|
|
this.sshDeploy({
|
|
|
host: server.host,
|
|
|
username: server.username,
|
|
|
password: server.password,
|
|
|
port: server.port
|
|
|
})
|
|
|
}.bind(this));
|
|
|
// setImmediate(async function(){
|
|
|
// this.sshDeploy(this._getSshInfo());
|
|
|
// }.bind(this));
|
|
|
|
|
|
this.server = await this._getSshInfo();
|
|
|
this.sshDeploy(this.server);
|
|
|
|
|
|
return this.id;
|
|
|
}
|
...
|
...
|
@@ -58,11 +53,34 @@ class Deploy { |
|
|
this.state = 'closed';
|
|
|
}
|
|
|
|
|
|
async _getSshInfo() {
|
|
|
let server = await Server.findByHost(this.host);
|
|
|
|
|
|
if (this.project.type === 'php') {
|
|
|
return {
|
|
|
host: server.host,
|
|
|
username: 'om',
|
|
|
privateKey: fs.readFileSync(path.join(__dirname, './id_rsa_ssh')),
|
|
|
port: server.port,
|
|
|
deployDir: server.deployDir
|
|
|
};
|
|
|
} else {
|
|
|
return {
|
|
|
host: server.host,
|
|
|
username: server.username,
|
|
|
password: server.password,
|
|
|
port: server.port,
|
|
|
deployDir: server.deployDir
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sshDeploy(serverInfo) {
|
|
|
console.log('ssh connecting', JSON.stringify(serverInfo));
|
|
|
let conn = new ssh.Client();
|
|
|
let self = this;
|
|
|
|
|
|
this._state('connecting');
|
|
|
this.conn = conn;
|
|
|
conn.on('ready', async() => {
|
|
|
console.log(`connected ${serverInfo.host}`);
|
...
|
...
|
@@ -79,7 +97,7 @@ class Deploy { |
|
|
} catch (e) {
|
|
|
self._state('fail');
|
|
|
self._log(e);
|
|
|
self.callback(err, null);
|
|
|
self.callback(e, null);
|
|
|
} finally {
|
|
|
conn.end();
|
|
|
}
|
...
|
...
|
@@ -167,35 +185,41 @@ class Deploy { |
|
|
_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');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (startup) {
|
|
|
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');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
});
|
|
|
} else {
|
|
|
return Promise.resolve();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
async _state(state) {
|
|
|
ws.broadcast(`/deploy/${this.project._id}`, {
|
|
|
host: this.info.host,
|
|
|
host: this.host,
|
|
|
state: state
|
|
|
});
|
|
|
await DeployInfo.updateState(this.id, state);
|
...
|
...
|
@@ -203,7 +227,7 @@ class Deploy { |
|
|
|
|
|
_log(msg) {
|
|
|
ws.broadcast(`/deploy/${this.project._id}/log`, {
|
|
|
host: this.info.host,
|
|
|
host: this.host,
|
|
|
msg: msg
|
|
|
});
|
|
|
}
|
...
|
...
|
|