docker/ts/classes.container.ts

100 lines
2.8 KiB
TypeScript
Raw Normal View History

import * as plugins from './plugins.js';
2022-10-17 07:36:35 +00:00
import * as interfaces from './interfaces/index.js';
2019-01-09 23:24:35 +00:00
import { DockerHost } from './classes.host.js';
import { logger } from './logger.js';
2018-07-16 21:52:50 +00:00
export class DockerContainer {
2019-01-09 23:24:35 +00:00
// STATIC
/**
* get all containers
*/
2019-08-14 12:19:45 +00:00
public static async getContainers(dockerHostArg: DockerHost): Promise<DockerContainer[]> {
2018-07-17 06:39:37 +00:00
const result: DockerContainer[] = [];
const response = await dockerHostArg.request('GET', '/containers/json');
2019-08-14 12:19:45 +00:00
// TODO: Think about getting the config by inpsecting the container
2019-01-09 23:24:35 +00:00
for (const containerResult of response.body) {
2019-08-15 16:50:13 +00:00
result.push(new DockerContainer(dockerHostArg, containerResult));
2018-07-17 06:39:37 +00:00
}
2018-07-16 21:52:50 +00:00
return result;
}
2019-01-09 23:24:35 +00:00
/**
2019-08-14 12:19:45 +00:00
* gets an container by Id
2019-01-09 23:24:35 +00:00
* @param containerId
*/
2019-08-14 21:21:54 +00:00
public static async getContainerById(containerId: string) {
// TODO: implement get container by id
}
2019-08-14 12:19:45 +00:00
/**
* create a container
*/
2019-08-15 16:50:13 +00:00
public static async create(
dockerHost: DockerHost,
containerCreationDescriptor: interfaces.IContainerCreationDescriptor
) {
// check for unique hostname
const existingContainers = await DockerContainer.getContainers(dockerHost);
2020-09-30 16:35:24 +00:00
const sameHostNameContainer = existingContainers.find((container) => {
2019-08-15 16:50:13 +00:00
// TODO implement HostName Detection;
return false;
});
const response = await dockerHost.request('POST', '/containers/create', {
Hostname: containerCreationDescriptor.Hostname,
Domainname: containerCreationDescriptor.Domainname,
2020-09-30 16:35:24 +00:00
User: 'root',
2019-08-15 16:50:13 +00:00
});
if (response.statusCode < 300) {
2020-09-30 16:27:43 +00:00
logger.log('info', 'Container created successfully');
2019-08-15 16:50:13 +00:00
} else {
2020-09-30 16:35:24 +00:00
logger.log('error', 'There has been a problem when creating the container');
2019-08-15 16:50:13 +00:00
}
2019-08-14 21:21:54 +00:00
}
2019-01-09 23:24:35 +00:00
// INSTANCE
2019-08-15 16:50:13 +00:00
// references
public dockerHost: DockerHost;
2019-01-09 23:24:35 +00:00
2019-08-15 16:50:13 +00:00
// properties
2019-08-14 21:21:54 +00:00
public Id: string;
public Names: string[];
public Image: string;
public ImageID: string;
public Command: string;
public Created: number;
public Ports: interfaces.TPorts;
public Labels: interfaces.TLabels;
public State: string;
public Status: string;
public HostConfig: any;
public NetworkSettings: {
2019-01-09 23:24:35 +00:00
Networks: {
[key: string]: {
IPAMConfig: any;
Links: any;
Aliases: any;
NetworkID: string;
EndpointID: string;
Gateway: string;
IPAddress: string;
IPPrefixLen: number;
IPv6Gateway: string;
GlobalIPv6Address: string;
GlobalIPv6PrefixLen: number;
MacAddress: string;
DriverOpts: any;
};
};
};
2019-08-14 21:21:54 +00:00
public Mounts: any;
2019-08-15 16:50:13 +00:00
constructor(dockerHostArg: DockerHost, dockerContainerObjectArg: any) {
this.dockerHost = dockerHostArg;
2020-09-30 16:35:24 +00:00
Object.keys(dockerContainerObjectArg).forEach((keyArg) => {
2019-08-15 16:50:13 +00:00
this[keyArg] = dockerContainerObjectArg[keyArg];
});
}
2018-07-16 21:52:50 +00:00
}