-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1183 from c0derMo/master
Adding option to monitor other docker containers
- Loading branch information
Showing
16 changed files
with
579 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. | ||
BEGIN TRANSACTION; | ||
|
||
CREATE TABLE docker_host ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
user_id INT NOT NULL, | ||
docker_daemon VARCHAR(255), | ||
docker_type VARCHAR(255), | ||
name VARCHAR(255) | ||
); | ||
|
||
ALTER TABLE monitor | ||
ADD docker_host INTEGER REFERENCES docker_host(id); | ||
|
||
ALTER TABLE monitor | ||
ADD docker_container VARCHAR(255); | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const axios = require("axios"); | ||
const { R } = require("redbean-node"); | ||
const version = require("../package.json").version; | ||
const https = require("https"); | ||
|
||
class DockerHost { | ||
/** | ||
* Save a docker host | ||
* @param {Object} dockerHost Docker host to save | ||
* @param {?number} dockerHostID ID of the docker host to update | ||
* @param {number} userID ID of the user who adds the docker host | ||
* @returns {Promise<Bean>} | ||
*/ | ||
static async save(dockerHost, dockerHostID, userID) { | ||
let bean; | ||
|
||
if (dockerHostID) { | ||
bean = await R.findOne("docker_host", " id = ? AND user_id = ? ", [ dockerHostID, userID ]); | ||
|
||
if (!bean) { | ||
throw new Error("docker host not found"); | ||
} | ||
|
||
} else { | ||
bean = R.dispense("docker_host"); | ||
} | ||
|
||
bean.user_id = userID; | ||
bean.docker_daemon = dockerHost.dockerDaemon; | ||
bean.docker_type = dockerHost.dockerType; | ||
bean.name = dockerHost.name; | ||
|
||
await R.store(bean); | ||
|
||
return bean; | ||
} | ||
|
||
/** | ||
* Delete a Docker host | ||
* @param {number} dockerHostID ID of the Docker host to delete | ||
* @param {number} userID ID of the user who created the Docker host | ||
* @returns {Promise<void>} | ||
*/ | ||
static async delete(dockerHostID, userID) { | ||
let bean = await R.findOne("docker_host", " id = ? AND user_id = ? ", [ dockerHostID, userID ]); | ||
|
||
if (!bean) { | ||
throw new Error("docker host not found"); | ||
} | ||
|
||
// Delete removed proxy from monitors if exists | ||
await R.exec("UPDATE monitor SET docker_host = null WHERE docker_host = ?", [ dockerHostID ]); | ||
|
||
await R.trash(bean); | ||
} | ||
|
||
/** | ||
* Fetches the amount of containers on the Docker host | ||
* @param {Object} dockerHost Docker host to check for | ||
* @returns {number} Total amount of containers on the host | ||
*/ | ||
static async testDockerHost(dockerHost) { | ||
const options = { | ||
url: "/containers/json?all=true", | ||
headers: { | ||
"Accept": "*/*", | ||
"User-Agent": "Uptime-Kuma/" + version | ||
}, | ||
httpsAgent: new https.Agent({ | ||
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) | ||
rejectUnauthorized: false, | ||
}), | ||
}; | ||
|
||
if (dockerHost.dockerType === "socket") { | ||
options.socketPath = dockerHost.dockerDaemon; | ||
} else if (dockerHost.dockerType === "tcp") { | ||
options.baseURL = dockerHost.dockerDaemon; | ||
} | ||
|
||
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; | ||
} 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?"); | ||
} | ||
|
||
} | ||
} | ||
|
||
module.exports = { | ||
DockerHost, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { BeanModel } = require("redbean-node/dist/bean-model"); | ||
|
||
class DockerHost extends BeanModel { | ||
/** | ||
* Returns an object that ready to parse to JSON | ||
* @returns {Object} | ||
*/ | ||
toJSON() { | ||
return { | ||
id: this.id, | ||
userID: this.user_id, | ||
dockerDaemon: this.docker_daemon, | ||
dockerType: this.docker_type, | ||
name: this.name, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = DockerHost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const { sendDockerHostList } = require("../client"); | ||
const { checkLogin } = require("../util-server"); | ||
const { DockerHost } = require("../docker"); | ||
const { log } = require("../../src/util"); | ||
|
||
/** | ||
* Handlers for docker hosts | ||
* @param {Socket} socket Socket.io instance | ||
*/ | ||
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, | ||
}); | ||
} | ||
}); | ||
|
||
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, | ||
}); | ||
} | ||
}); | ||
|
||
socket.on("testDockerHost", async (dockerHost, callback) => { | ||
try { | ||
checkLogin(socket); | ||
|
||
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({ | ||
ok: true, | ||
msg, | ||
}); | ||
|
||
} catch (e) { | ||
log.error("docker", e); | ||
|
||
callback({ | ||
ok: false, | ||
msg: e.message, | ||
}); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.