Fix and improve test docker host

This commit is contained in:
Louis Lam 2022-07-29 20:57:13 +08:00
parent 1062e629c5
commit f3322398e5
2 changed files with 35 additions and 9 deletions

View file

@ -59,7 +59,7 @@ class DockerHost {
* @param {Object} dockerHost Docker host to check for * @param {Object} dockerHost Docker host to check for
* @returns {number} Total amount of containers on the host * @returns {number} Total amount of containers on the host
*/ */
static async getAmountContainer(dockerHost) { static async testDockerHost(dockerHost) {
const options = { const options = {
url: "/containers/json?all=true", url: "/containers/json?all=true",
headers: { headers: {
@ -72,14 +72,32 @@ class DockerHost {
}), }),
}; };
if (dockerHost.docker_type === "socket") { if (dockerHost.dockerType === "socket") {
options.socketPath = dockerHost.docker_daemon; options.socketPath = dockerHost.dockerDaemon;
} else if (dockerHost.docker_type === "tcp") { } else if (dockerHost.dockerType === "tcp") {
options.baseURL = dockerHost.docker_daemon; options.baseURL = dockerHost.dockerDaemon;
} }
let res = await axios.request(options); let res = await axios.request(options);
if (Array.isArray(res.data)) {
if (res.data.length > 1) {
if ("ImageID" in res.data[0]) {
return res.data.length; return res.data.length;
} else {
throw new Error("Invalid Docker response, is it Docker really a daemon?");
}
} else {
return res.data.length;
}
} else {
throw new Error("Invalid Docker response, is it Docker really a daemon?");
}
} }
} }

View file

@ -1,6 +1,7 @@
const { sendDockerHostList } = require("../client"); const { sendDockerHostList } = require("../client");
const { checkLogin } = require("../util-server"); const { checkLogin } = require("../util-server");
const { DockerHost } = require("../docker"); const { DockerHost } = require("../docker");
const { log } = require("../../src/util");
/** /**
* Handlers for docker hosts * Handlers for docker hosts
@ -52,15 +53,22 @@ module.exports.dockerSocketHandler = (socket) => {
try { try {
checkLogin(socket); checkLogin(socket);
let amount = await DockerHost.getAmountContainer(dockerHost); let amount = await DockerHost.testDockerHost(dockerHost);
let msg;
if (amount > 1) {
msg = "Connected Successfully. Amount of containers: " + amount;
} else {
msg = "Connected Successfully, but there are no containers?";
}
callback({ callback({
ok: true, ok: true,
msg: "Amount of containers: " + amount, msg,
}); });
} catch (e) { } catch (e) {
console.error(e); log.error("docker", e);
callback({ callback({
ok: false, ok: false,