Skip to content

Commit

Permalink
Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
freaker2k7 committed Jul 28, 2019
1 parent 1c9b1d4 commit 3afdda4
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 98 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## [1.8.4] - 28/07/2019
Edited some docs.

## [1.8.2] - 28/07/2019
Added logger.

## [1.8.1] - 28/07/2019
Cashed reading (in cluster mode) & fixed host issue in results (also in cluster mode).

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,6 +1,6 @@
FROM node:12

RUN curl -L https://get.docker.com | sh -
RUN npm i -g docker-server@1.8.1
RUN npm i -g docker-server@1.8.4

CMD ["docker-server"]
23 changes: 21 additions & 2 deletions README.md
Expand Up @@ -230,15 +230,30 @@ Also, you can start DockerServerwith these parameters:
<td>false</td>
</tr>
<tr>
<td><code>--folder</code></td>
<td><code>--folder [path]</code></td>
<td>Shared folder between all docker-servers. (Used only in cluster mode)</td>
<td>/tmp/docker-server</td>
</tr>
<tr>
<td><code>--cache_interval</code></td>
<td><code>--cache_interval [num]</code></td>
<td>Milliseconds between reads (of all the machines)</td>
<td>3000</td>
</tr>
<tr>
<td><code>--log_lovel [option]</code></td>
<td>Log level [trace|debug|info|warn|error|fatal]</td>
<td>info</td>
</tr>
<tr>
<td><code>--log_expiry [num]</code></td>
<td>Time for a log to live in days.</td>
<td>14</td>
</tr>
<tr>
<td><code>--log_max_size [num]</code></td>
<td>Max log size in MB</td>
<td>25</td>
</tr>
<tr>
<td><code>--help</code></td>
<td>Show he</td>
Expand Down Expand Up @@ -361,6 +376,10 @@ And/or

## Changelog

1.8.4 - Edited some docs.

1.8.2 - Added logger.

1.8.1 - Cashed reading (in cluster mode) & fixed host issue in results (also in cluster mode).

1.8.0 - **Stable Cluster mode!**
Expand Down
11 changes: 8 additions & 3 deletions index.js
Expand Up @@ -12,11 +12,16 @@ var args = yargs
.option('cluster', {describe: 'Flag to turn on the Cluster mode.', type: 'boolean', default: false})
.option('folder', {describe: 'Shared folder between all docker-servers.', type: 'string', default: '/tmp/docker-server'})
.option('cache_interval', {describe: 'Milliseconds between reads (of all the machines).', type: 'number', default: 3000})
.option('log_level', {describe: 'Log level [trace|debug|info|warn|error|fatal]', type: 'string', default: 'info'})
.option('log_expiry', {describe: 'Time for a log to live in days.', type: 'number', default: 14})
.option('log_max_size', {describe: 'Max log size in MB.', type: 'number', default: 25})
.help('help', 'Show help.\nFor more documentation see https://github.com/freaker2k7/dockerserver')
.argv;

const docker = require('./lib/docker.js');
const network = require('./lib/network.js')(args);
var log = require('./lib/logger.js')(args);

const docker = require('./lib/docker.js')(log);
const network = require('./lib/network.js')(log, args);

var app = express();

Expand Down Expand Up @@ -52,7 +57,7 @@ app.delete('/:id', mid_burst, docker.rm);
// Main listener
var server = network.protocol(app, args.https);
server.listen(args.port);
console.info('Serving on ' + network.get_protocol() + '0.0.0.0:' + args.port);
log.info('Serving on ' + network.get_protocol() + '0.0.0.0:' + args.port);


module.exports = Object.assign(docker, network, {'_app': app, '_server': server});
163 changes: 84 additions & 79 deletions lib/docker.js
Expand Up @@ -2,6 +2,7 @@ const dockerCLI = require('docker-cli-js');
const uuid = require('uuid/v4');

var docker = new dockerCLI.Docker();
var log = null;

var handle_error = function(req, res) {
return function(err) {
Expand All @@ -10,86 +11,90 @@ var handle_error = function(req, res) {
};


module.exports = {
'ps': function(req, res) {
return docker.command('ps -a').then(function(data) {
// console.debug('GET:', data);
res.json(data.containerList || []);
}).catch(handle_error(req, res));
},
'logs': function(req, res) {
return docker.command('logs ' + req.params.id).then(function(data) {
// console.debug('GET:', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
},
'run': function(req, res) {
// console.debug('PUT Request:', req.body);
if (!req.body || !req.body.image) {
return res.sendStatus(400);
}

var ports = '';
var volumes = '';
var data = '';
var detach = '';
var remove = '';

if (req.body.ports) {
for (var k in req.body.ports) {
ports += ' -p ' + k + ':' + req.body.ports[k];
module.exports = function(main_log) {
log = main_log;

return {
'ps': function(req, res) {
return docker.command('ps -a').then(function(data) {
log.debug('GET:', data);
res.json(data.containerList || []);
}).catch(handle_error(req, res));
},
'logs': function(req, res) {
return docker.command('logs ' + req.params.id).then(function(data) {
log.debug('GET:', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
},
'run': function(req, res) {
log.debug('PUT Request:', req.body);
if (!req.body || !req.body.image) {
return res.sendStatus(400);
}
}
if (req.body.volumes) {
for (var k in req.body.volumes) {
volumes += ' -v ' + k.replace(/^\.$/, context) + ':' + req.body.volumes[k];

var ports = '';
var volumes = '';
var data = '';
var detach = '';
var remove = '';

if (req.body.ports) {
for (var k in req.body.ports) {
ports += ' -p ' + k + ':' + req.body.ports[k];
}
}
if (req.body.volumes) {
for (var k in req.body.volumes) {
volumes += ' -v ' + k.replace(/^\.$/, context) + ':' + req.body.volumes[k];
}
}
if (req.body.detach) {
detach = ' --detach';
}
if (req.body.remove) {
remove = ' --rm';
}
if (req.body.data) {
data = ' ' + req.body.data;
}

return docker.command('run --name ' + (req.body.name || uuid()) + ports + volumes + remove + detach + ' ' + req.body.image + data).then(function(data) {
log.debug('PUT:', data);
res.json(data);
}).catch(handle_error(req, res));
},
'exec': function(req, res) {
log.debug('POST Request:', req.body);

var data = '';
var interactive = '';
var tty = '';

if (req.body.interactive) {
interactive = ' -i';
}
if (req.body.tty) {
tty = ' -t';
}
if (req.body.data) {
data = ' ' + req.body.data;
}

return docker.command('exec' + tty + interactive + ' ' + req.params.id + ' ' + data).then(function(data) {
log.debug('POST:', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
},
'rm': function(req, res) {
if (!req.params.id) {
return res.sendStatus(400);
}

return docker.command('rm -f ' + req.params.id).then(function(data) {
log.debug('DELETE (RM):', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
}
if (req.body.detach) {
detach = ' --detach';
}
if (req.body.remove) {
remove = ' --rm';
}
if (req.body.data) {
data = ' ' + req.body.data;
}

return docker.command('run --name ' + (req.body.name || uuid()) + ports + volumes + remove + detach + ' ' + req.body.image + data).then(function(data) {
// console.debug('PUT:', data);
res.json(data);
}).catch(handle_error(req, res));
},
'exec': function(req, res) {
console.debug('POST Request:', req.body);

var data = '';
var interactive = '';
var tty = '';

if (req.body.interactive) {
interactive = ' -i';
}
if (req.body.tty) {
tty = ' -t';
}
if (req.body.data) {
data = ' ' + req.body.data;
}

return docker.command('exec' + tty + interactive + ' ' + req.params.id + ' ' + data).then(function(data) {
console.debug('POST:', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
},
'rm': function(req, res) {
if (!req.params.id) {
return res.sendStatus(400);
}

return docker.command('rm -f ' + req.params.id).then(function(data) {
// console.debug('DELETE (RM):', req.params.id, '::', data);
res.json(data);
}).catch(handle_error(req, res));
}
};
};
28 changes: 28 additions & 0 deletions lib/logger.js
@@ -0,0 +1,28 @@
const winston = require('winston');
require('winston-daily-rotate-file');

module.exports = function(args) {
args.log_level = args.log_level.toLowerCase().replace(/trace/, 'silly');

var transport = null;

if (~['debug', 'silly'].indexOf(args.log_level)) {
transport = new winston.transports.Console({
level: args.log_level
});
} else {
transport = new (winston.transports.DailyRotateFile)({
filename: '/tmp/docker-server-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: args.log_max_size + 'm',
maxFiles: args.log_expiry + 'd',
level: args.log_level
});
}

return winston.createLogger({
name: 'ds-logger',
transports: [transport]
});
}
8 changes: 5 additions & 3 deletions lib/machine.js
Expand Up @@ -3,6 +3,7 @@ const ping = require('ping');
const si = require('systeminformation');

var args = null;
var log = null;
var last_read = null;
var cached_machines = [];

Expand Down Expand Up @@ -32,7 +33,7 @@ var get_all = function(IP) {

return fs.readdir(args.folder, function(err, files) {
if (err) {
console.error(err);
log.error(err);
return resolve({});
}

Expand Down Expand Up @@ -71,7 +72,7 @@ var get_valid_host = function(ips, IP) {
try {
fs.unlinkSync(args.folder + ip.host);
} catch(e) {
console.error('Probably someone else just removed it:', e);
log.error('Probably someone else just removed it:', e);
}

return get_valid_host(ips, IP).then(resolve);
Expand All @@ -80,7 +81,8 @@ var get_valid_host = function(ips, IP) {
};


module.exports = function(cli_args) {
module.exports = function(main_log, cli_args) {
log = main_log;
args = cli_args;

// Add a trailing slash to the folder path.
Expand Down
14 changes: 8 additions & 6 deletions lib/network.js
@@ -1,5 +1,6 @@
var IP = null;
var args = null;
var log = null;
var machine = null;
var request = null;

Expand All @@ -15,9 +16,9 @@ var get_req = function(req, host) {
body: req.body && JSON.stringify(req.body) || undefined,
method: req.method
}, function (err, res, body) {
// console.debug('request:', host, err, typeof body, body);
log.debug('request:', host, err, typeof body, body);
if (err) {
console.error('err:', host, err);
log.error('err:', host, err);
return resolve({'body': [err], 'host': host});
}

Expand All @@ -27,7 +28,7 @@ var get_req = function(req, host) {
if (body.startsWith('error')) {
body = {'error': body};
} else {
console.error('Error in get_req on', host, ':', e, typeof body, body);
log.error('Error in get_req on', host, ':', e, typeof body, body);
}

return resolve({'body': [body], 'host': host});
Expand Down Expand Up @@ -85,7 +86,8 @@ var handle_cluster = function(req, res, next) {
};


module.exports = function(cli_args) {
module.exports = function(main_log, cli_args) {
log = main_log;
args = cli_args;

return {
Expand All @@ -111,7 +113,7 @@ module.exports = function(cli_args) {
ca: fs.existsSync('/certs/chain.pem') && fs.readFileSync('/certs/chain.pem', 'utf8') || undefined
}, app);
} catch (e) {
console.error('Could not start secure server:', e);
log.error('Could not start secure server:', e);
return require('http').createServer(app);
}
}
Expand All @@ -122,7 +124,7 @@ module.exports = function(cli_args) {
'balance': function(port) {
if (args.cluster) {
IP = get_protocol() + require('ip').address() + ':' + port;
machine = require('./machine.js')(args);
machine = require('./machine.js')(log, args);
request = require('request');

machine.cpu(IP);
Expand Down

0 comments on commit 3afdda4

Please sign in to comment.