Skip to content

Commit

Permalink
fix: issue #27, read logs
Browse files Browse the repository at this point in the history
  • Loading branch information
naimo84 committed Dec 17, 2021
1 parent 9c3f8ba commit 16747a1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 30 additions & 18 deletions src/docker-container-actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,38 @@
this.label = $('#node-input-action option:selected').text();
},
oneditprepare: function () {
var dataArray = [];
let id = this.config;
$.post('containerSearch', { id: id }, function (data) {
$.each(data, function (i, element) {
let name = element.Names[0].replace('/', '');
dataArray.push(name);
});
});



$("#node-input-container").typedInput({
types: ["str", "msg"],
typeField: "#node-input-containertype"
typeField: "#node-input-containertype",
autoComplete: function (val) {

console.log(val)
console.log(dataArray)
var matches = [];
dataArray.forEach(v => {
var i = v.toLowerCase().indexOf(val.toLowerCase());
if (i > -1) {
matches.push({
value: v,
label: v,
i: i
})
}
});
matches.sort(function (A, B) { return A.i - B.i })
return matches
}
});

$("#node-input-image").typedInput({
Expand All @@ -93,7 +121,7 @@

$('#node-input-action').change(function () {
var action = $('#node-input-action').val();
if (['list', 'prune', 'pull', 'run'].includes(action)) {
if (['list', 'prune', 'pull', 'run'].includes(action)) {
$('#node-input-container').parent().hide();
} else {
$('#node-input-container').parent().show();
Expand All @@ -119,23 +147,7 @@


});
$("#node-input-container").click(function () {
$.post('containerSearch', { id: id }, function (data) {

var dataArray = [];
$.each(data, function (i, element) {
let name = element.Names[0].replace('/', '');
dataArray.push(name);
});
$("#node-input-container").autocomplete({
source: dataArray,
minLength: 0,
close: function (event, ui) {
$("#node-input-container").autocomplete("destroy");
}
}).autocomplete("search", "");
});
});

},
})
Expand Down
49 changes: 29 additions & 20 deletions src/docker-container-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,30 @@ module.exports = function (RED: Red) {

case 'logs':
// https://docs.docker.com/engine/api/v1.40/#operation/ContainerLogs
container.logs()
.then(res => {
node.status({ fill: 'green', shape: 'dot', text: containerId + ' restarted' });
node.send(Object.assign(msg, { payload: res }));
}).catch(err => {
debug(err)
if (err.statusCode === 404) {
node.error(`No such container: [${containerId}]`);
node.send({ payload: err });
} else if (err.statusCode === 500) {
node.error(`Server Error: [${err.statusCode}] ${err.reason}`);
node.send({ payload: err });
} else {
node.error(`System Error: [${err.statusCode}] ${err.reason}`);
return;
}
});
let since = node.context().get("log_since") || 0;
node.warn("Getting logs since " + since);
container.logs({
stdout: true,
stderr: true,
since: since,
follow: false
}).then(res => {
node.status({ fill: 'green', shape: 'dot', text: containerId + ' logs read' });
node.context().set("log_since", Math.floor((new Date()).getTime() / 1000));
node.send(Object.assign(msg, { payload: res }));
}).catch(err => {
debug(err)
if (err.statusCode === 404) {
node.error(`No such container: [${containerId}]`);
node.send({ payload: err });
} else if (err.statusCode === 500) {
node.error(`Server Error: [${err.statusCode}] ${err.reason}`);
node.send({ payload: err });
} else {
node.error(`System Error: [${err.statusCode}] ${err.reason}`);
return;
}
});
break;

case 'changes':
Expand Down Expand Up @@ -629,7 +636,7 @@ module.exports = function (RED: Red) {
return;
}
});
}else{
} else {
node.send(Object.assign(msg, { payload: {} }));
}
break;
Expand All @@ -649,14 +656,16 @@ module.exports = function (RED: Red) {
discoverSonos(config, (containers) => {
RED.log.debug("GET /containerSearch: " + containers.length + " found");
res.json(containers);
}, (err) => {
res.json(err)
});
});

function discoverSonos(config, discoveryCallback) {
function discoverSonos(config, discoveryCallback, errCallback) {
let client = config.getClient();
client.listContainers({ all: true })
.then(containers => discoveryCallback(containers))
.catch(err => this.error(err));
.catch(err => errCallback(err));
}

RED.nodes.registerType('docker-container-actions', DockerContainerAction);
Expand Down

0 comments on commit 16747a1

Please sign in to comment.