docker/ts/docker.classes.container.ts

102 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-08-15 16:50:13 +00:00
import * as plugins from './docker.plugins';
2019-01-09 23:24:35 +00:00
import * as interfaces from './interfaces';
2018-07-16 21:52:50 +00:00
import { DockerHost } from './docker.classes.host';
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);
const sameHostNameContainer = existingContainers.find(container => {
// TODO implement HostName Detection;
return false;
});
const response = await dockerHost.request('POST', '/containers/create', {
Hostname: containerCreationDescriptor.Hostname,
Domainname: containerCreationDescriptor.Domainname,
User: 'root'
});
if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log('info', 'Container created successfully');
} else {
2019-08-16 10:48:56 +00:00
plugins.smartlog.defaultLogger.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;
Object.keys(dockerContainerObjectArg).forEach(keyArg => {
this[keyArg] = dockerContainerObjectArg[keyArg];
});
}
2018-07-16 21:52:50 +00:00
}