Authored by 姜枫

add influxdb

... ... @@ -84,7 +84,7 @@ class Build {
_cloneCode(branch) {
var self = this;
this._state('cloning_code');
let clone_script = `git clone -b ${branch} --progress ${this.project.gitlab}`;
let clone_script = `git clone -b ${branch} ${this.project.gitlab}`;
this._log(`>>>>>>>>> ${clone_script} >>>>>>>>>>>`);
return new Promise((reslove, reject) => {
... ...
... ... @@ -52,18 +52,16 @@ class Deploy {
} catch (e) {
self._state('fail');
self._log(e);
console.error(e);
}
}).on('error', (err) => {
self._state('fail');
self._log(err);
console.log(err);
}).connect(serverInfo);
}
_preDeploy(conn) {
let self = this;
return new Promise((reslove, reject) => {
return new Promise((resolve, reject) => {
let script = `mkdir -p ${self.remoteWorkDir} && mkdir -p ${self.remoteDist}`;
self._state('preparing');
self._log(`>>>>>>>>> ${script} >>>>>>>>`);
... ... @@ -72,8 +70,7 @@ class Deploy {
reject(err);
} else {
stream.on('exit', (code) => {
console.log(`mkdir code = ${code}`);
reslove();
resolve();
});
}
... ... @@ -83,14 +80,11 @@ class Deploy {
_scp(conn) {
let self = this;
console.log(`localFile : ${this.localFile}`);
console.log(`remoteFIle : ${this.remoteFile}`);
return new Promise((reslove, reject) => {
return new Promise((resolve, reject) => {
self._state('uploading');
self._log(`>>>> uploading ${self.localFile} ==> ${self.remoteFile}`);
conn.sftp((err, sftp) => {
console.log('open sftp');
if (err) {
reject(err);
} else {
... ... @@ -102,7 +96,7 @@ class Deploy {
} else {
self._log(' uploaded success!');
self._state('uploaded');
reslove();
resolve();
}
});
}
... ... @@ -112,9 +106,9 @@ class Deploy {
_unzip(conn) {
let self = this;
return new Promise((reslove, reject) => {
return new Promise((resolve, reject) => {
self._state('unziping');
let script = `tar -zxvf ${self.remoteFile} -C ${self.remoteWorkDir}`;
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) {
... ... @@ -128,9 +122,8 @@ class Deploy {
});
stream.on('exit', (code) => {
if (code === 0) {
console.log('unzip success');
self._state('unziped');
reslove();
resolve();
} else {
reject('unzip fail: ' + script);
}
... ... @@ -143,7 +136,7 @@ class Deploy {
_startup(conn) {
let self = this;
let startup = this.project.scripts.start;
return new Promise((reslove, reject) => {
return new Promise((resolve, reject) => {
self._state('starting');
self._log(`>>>> ${startup}`);
conn.exec(`cd ${self.remoteRunningDir} && ${startup}`, (err, stream) => {
... ... @@ -158,9 +151,8 @@ class Deploy {
});
stream.on('exit', (code) => {
if (code === 0) {
console.log('startup!');
self._state('running');
reslove();
resolve();
} else {
reject('startup fail');
}
... ... @@ -169,7 +161,7 @@ class Deploy {
});
});
}
async _state(state) {
ws.broadcast(`/deploy/${this.project._id}`, {
host: this.info.host,
... ...
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/7/29
*/
const influx = require('influx');
let client = influx({
hosts: [{
host: '54.222.219.223',
port: 8086,
protocol: 'http'
}],
database: 'udp'
});
client.getSeriesNames((err, dbs) => {
console.log(err);
console.log(JSON.stringify(dbs));
});
client.query('select * from test_point limit 10', function(err, results) {
console.log(results);
});
const db = {
client: client,
query: (query) => {
return new Promise((resolve, reject) => {
client.query(query, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
};
module.exports = db;
... ...
/**
* 跟踪监控工具, 将监控数据写入influxdb
*
* @usage:
* <code>
* let trace = new Trace({
* host: '54.222.219.223',
* port: 4444
* });
*
* let testTrace = trace.trace('test_key'); // createOrChoose a measurement
*
* // testTrace(someTags, someFields);
* testTrace({ foo: 'bar', foobar: 'baz2'}, {value: 123, value2: 'aaa 123', value3: 1.3, value4: false});
*
* </code>
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/8/1
*/
'use strict';
const dgram = require('dgram');
const influx = require('influx');
const _ = require('lodash');
class Trace {
/**
* influxdb connection config
*
* @param options
*/
constructor(options) {
this.options = options;
}
/**
* create or choose a measurement to write point in.
* @param name {string} the measurement name
* @param options {object} some options. the protocol of influxdb connection.
* @returns {function()} a point write function
*/
trace(name, options) {
options = _.assign({
protocol: 'udp'
}, options);
let self = this;
if (options.protocol === 'udp') {
return (key, fields) => {
return self.udpTrace(name, options, key, fields);
};
} else if (options.protocol === 'http') {
return (key, fields) => {
return self.httpTrace(name, options, key, fields);
};
}
}
/**
* write point into influxdb by UDP
*
* @param name {string} the measurement name
* @param options {object}
* @param key {object} some keys of data
* @param fields {object} some fields of data
* @returns {Promise}
*/
udpTrace(name, options, key, fields) {
if (_.isArray(key)) {
key.forEach(p => {
let line = `${this._escape(name)},${this._makeLine(p)}`;
return this._updPostLine(line);
});
} else {
let line = `${this._escape(name)},${this._makeLine(key, false)} ${this._makeLine(fields, true)}`;
return this._updPostLine(line);
}
}
/**
* upd send.
* @param line {string} @see infulxdb's line protocol
* @returns {Promise}
* @private
*/
_updPostLine(line) {
let self = this;
return new Promise((resolve, reject) => {
let socket = dgram.createSocket("udp4");
let buff = new Buffer(line);
socket.send(buff, 0, buff.length, self.options.port, self.options.host, (err, rp) => {
socket.close();
console.log(rp);
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
/**
* make the data with influxdb's line protocol.
* @see https://docs.influxdata.com/influxdb/v0.13/write_protocols/line/
*
* @param data {object}
* @param withNoQuote {boolean}
* @returns {string}
* @private
*/
_makeLine(data, withNoQuote) {
if (_.isObject(data)) {
return Object.keys(data).map(key => {
return key + '=' + this._escape(data[key], withNoQuote);
}).join(',');
} else if (_.isArray(data)) {
return data.map(d => {
return this._makeLine(d, escape);
}).join(' ');
} else if (!_.isNil(data)) {
return 'value=' + this._escape(data, withNoQuote);
} else {
return '';
}
}
/**
* data escape with influxdb's line protocol.
*
* @param value {*}
* @param withQuote {boolean}
* @returns {*}
* @private
*/
_escape(value, withQuote) {
if (_.isString(value)) {
value = _.replace(value, /,/g, '\\,');
value = _.replace(value, /=/g, '\\=');
if (withQuote) {
value = '"' + value + '"';
} else {
value = _.replace(value, /\s/g, '\\ ');
}
} else if (_.isInteger(value)) {
if (withQuote) {
value = value + 'i';
}
}
return value;
}
/**
* write point into influxdb by HTTP. use the open source node-influx module.
* @see https://github.com/node-influx/node-influx
*
* @param name {string} the measurement name
* @param options {object}
* @param key {object} some keys of data
* @param fields {object} some fields of data
* @returns {Promise}
*/
httpTrace(name, options, key, fields) {
let client = this.getHttpClient();
return new Promise((resolve, reject) => {
if (_.isArray(key)) {
client.writePoints(name, key, options, (err, rp) => {
if (err) {
reject(err);
} else {
resolve(rp)
}
});
} else {
client.writePoint(name, fields, key, options, (err, rp) => {
if (err) {
reject(err);
} else {
resolve(rp)
}
});
}
});
}
/**
* the singleton http client.
*
* @returns {*}
*/
getHttpClient() {
if (!this.httpClient) {
this.httpClient = influx(this.options);
}
return this.httpClient;
}
}
module.exports = Trace;
... ...
... ... @@ -6,9 +6,6 @@ import {
Server
} from '../../models';
const r = new Router;
const envs = {
... ... @@ -43,7 +40,7 @@ const servers = {
port: port,
env: env,
deployDir: deployDir
}
};
if (_id) {
await Server.update({
_id: _id
... ...
... ... @@ -38,6 +38,7 @@
"fs-promise": "^0.5.0",
"fstream": "^1.0.9",
"handlebars": "^4.0.5",
"influx": "^4.2.1",
"koa": "^2.0.0",
"koa-body": "^1.4.0",
"koa-convert": "^1.2.0",
... ...