dockerVolumes() added

This commit is contained in:
Sebastian Hildebrandt
2021-02-25 08:04:31 +01:00
parent ef6d0a76c4
commit 305d264376
10 changed files with 219 additions and 86 deletions
+42
View File
@@ -666,6 +666,48 @@ function dockerContainerProcesses(containerID, callback) {
exports.dockerContainerProcesses = dockerContainerProcesses;
function dockerVolumes(callback) {
let result = [];
return new Promise((resolve) => {
process.nextTick(() => {
if (!_docker_socket) {
_docker_socket = new DockerSocket();
}
_docker_socket.listVolumes(data => {
let dockerVolumes = {};
try {
dockerVolumes = data;
if (dockerVolumes && dockerVolumes.Volumes && Object.prototype.toString.call(dockerVolumes.Volumes) === '[object Array]' && dockerVolumes.Volumes.length > 0) {
dockerVolumes.Volumes.forEach(function (element) {
result.push({
name: element.Name,
driver: element.Driver,
labels: element.Labels,
mountpoint: element.Mountpoint,
options: element.Options,
scope: element.Scope,
created: element.CreatedAt ? Math.round(new Date(element.CreatedAt).getTime() / 1000) : 0,
});
});
if (callback) { callback(result); }
resolve(result);
} else {
if (callback) { callback(result); }
resolve(result);
}
} catch (err) {
if (callback) { callback(result); }
resolve(result);
}
});
});
});
}
exports.dockerVolumes = dockerVolumes;
function dockerAll(callback) {
return new Promise((resolve) => {
process.nextTick(() => {
+36 -51
View File
@@ -167,57 +167,6 @@ class DockerSocket {
}
}
listVolumes(filter, callback) {
try {
let socket = net.createConnection({ path: socketPath });
let alldata = '';
let data;
filter = filter || {};
let filterString = '';
if (filter.dangling && typeof filter.dangling === 'boolean') {
filterString += (filterString ? '&' : '') + 'dangling=true';
}
if (filter.driver && typeof filter.driver === 'string') {
filterString += (filterString ? '&' : '') + `driver=${filter.driver}`;
}
if (filter.label && typeof filter.label === 'string') {
filterString += (filterString ? '&' : '') + `label=${filter.label}`;
}
if (filter.name && typeof filter.name === 'string') {
filterString += (filterString ? '&' : '') + `name=${filter.name}`;
}
socket.on('connect', () => {
socket.write('GET http:/volumes/' + (filterString ? `?${filterString}` : '') + ' HTTP/1.0\r\n\r\n');
});
socket.on('data', data => {
alldata = alldata + data.toString();
});
socket.on('error', () => {
socket = false;
callback({});
});
socket.on('end', () => {
let startbody = alldata.indexOf('\r\n\r\n');
alldata = alldata.substring(startbody + 4);
socket = false;
try {
data = JSON.parse(alldata);
callback(data);
} catch (err) {
callback({});
}
});
} catch (err) {
callback({});
}
}
getStats(id, callback) {
id = id || '';
if (id) {
@@ -337,6 +286,42 @@ class DockerSocket {
callback({});
}
}
listVolumes(callback) {
try {
let socket = net.createConnection({ path: socketPath });
let alldata = '';
let data;
socket.on('connect', () => {
socket.write('GET http:/volumes HTTP/1.0\r\n\r\n');
});
socket.on('data', data => {
alldata = alldata + data.toString();
});
socket.on('error', () => {
socket = false;
callback({});
});
socket.on('end', () => {
let startbody = alldata.indexOf('\r\n\r\n');
alldata = alldata.substring(startbody + 4);
socket = false;
try {
data = JSON.parse(alldata);
callback(data);
} catch (err) {
callback({});
}
});
} catch (err) {
callback({});
}
}
}
module.exports = DockerSocket;
+13 -2
View File
@@ -660,7 +660,7 @@ export namespace Systeminformation {
repoTags: any;
config: any;
rootFS: any;
}
}
interface DockerContainerData {
id: string;
@@ -712,6 +712,16 @@ export namespace Systeminformation {
networks: any;
}
interface DockerVolumeData {
name: string;
driver: string;
labels: any;
mountpoint: string;
options: any;
scope: string;
created: number;
}
// 9. Virtual Box
interface VboxInfoData {
@@ -872,10 +882,11 @@ export function processLoad(processName: string, cb?: (data: Systeminformation.P
export function services(serviceName: string, cb?: (data: Systeminformation.ServicesData[]) => any): Promise<Systeminformation.ServicesData[]>;
export function dockerInfo(cb?: (data: Systeminformation.DockerInfoData) => any): Promise<Systeminformation.DockerInfoData>;
export function dockerImages(all?: boolean, cb?: (data: dockerstats.DockerImageData[]) => any): Promise<dockerstats.DockerImageData[]>;
export function dockerImages(all?: boolean, cb?: (data: Systeminformation.DockerImageData[]) => any): Promise<Systeminformation.DockerImageData[]>;
export function dockerContainers(all?: boolean, cb?: (data: Systeminformation.DockerContainerData[]) => any): Promise<Systeminformation.DockerContainerData[]>;
export function dockerContainerStats(id?: string, cb?: (data: Systeminformation.DockerContainerStatsData[]) => any): Promise<Systeminformation.DockerContainerStatsData[]>;
export function dockerContainerProcesses(id?: string, cb?: (data: any) => any): Promise<any>;
export function dockerVolumes(cb?: (data: Systeminformation.DockerVolumeData[]) => any): Promise<Systeminformation.DockerVolumeData[]>;
export function dockerAll(cb?: (data: any) => any): Promise<any>;
export function vboxInfo(cb?: (data: Systeminformation.VboxInfoData[]) => any): Promise<Systeminformation.VboxInfoData[]>;
+1
View File
@@ -486,6 +486,7 @@ exports.dockerImages = docker.dockerImages;
exports.dockerContainers = docker.dockerContainers;
exports.dockerContainerStats = docker.dockerContainerStats;
exports.dockerContainerProcesses = docker.dockerContainerProcesses;
exports.dockerVolumes = docker.dockerVolumes;
exports.dockerAll = docker.dockerAll;
exports.vboxInfo = vbox.vboxInfo;