2022-07-22 08:47:04 -07:00
|
|
|
const { sendDockerHostList } = require("../client");
|
|
|
|
const { checkLogin } = require("../util-server");
|
|
|
|
const { DockerHost } = require("../docker");
|
2022-07-29 05:57:13 -07:00
|
|
|
const { log } = require("../../src/util");
|
2022-07-22 08:47:04 -07:00
|
|
|
|
2022-07-22 08:57:40 -07:00
|
|
|
/**
|
|
|
|
* Handlers for docker hosts
|
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
*/
|
2022-07-22 08:47:04 -07:00
|
|
|
module.exports.dockerSocketHandler = (socket) => {
|
|
|
|
socket.on("addDockerHost", async (dockerHost, dockerHostID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
let dockerHostBean = await DockerHost.save(dockerHost, dockerHostID, socket.userID);
|
|
|
|
await sendDockerHostList(socket);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "Saved",
|
|
|
|
id: dockerHostBean.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
2022-07-22 08:57:40 -07:00
|
|
|
});
|
2022-07-22 08:47:04 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("deleteDockerHost", async (dockerHostID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await DockerHost.delete(dockerHostID, socket.userID);
|
|
|
|
await sendDockerHostList(socket);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "Deleted",
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
2022-07-22 08:57:40 -07:00
|
|
|
});
|
2022-07-22 08:47:04 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("testDockerHost", async (dockerHost, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-07-29 05:57:13 -07:00
|
|
|
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?";
|
|
|
|
}
|
2022-07-22 08:47:04 -07:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2022-07-29 05:57:13 -07:00
|
|
|
msg,
|
2022-07-22 08:47:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
2022-07-29 05:57:13 -07:00
|
|
|
log.error("docker", e);
|
2022-07-22 08:47:04 -07:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
2022-07-22 08:57:40 -07:00
|
|
|
});
|
2022-07-22 08:47:04 -07:00
|
|
|
}
|
2022-07-22 08:57:40 -07:00
|
|
|
});
|
|
|
|
};
|