docker/ts/docker.classes.network.ts

120 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-08-15 16:50:13 +00:00
import * as plugins from './docker.plugins';
2019-08-14 12:19:45 +00:00
import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host';
2019-09-22 12:32:48 +00:00
import { DockerService } from './docker.classes.service';
2019-08-14 12:19:45 +00:00
2019-08-14 21:21:54 +00:00
export class DockerNetwork {
public static async getNetworks(dockerHost: DockerHost): Promise<DockerNetwork[]> {
const dockerNetworks: DockerNetwork[] = [];
2019-08-15 16:50:13 +00:00
const response = await dockerHost.request('GET', '/networks');
for (const networkObject of response.body) {
2019-09-08 17:22:20 +00:00
const dockerNetwork = new DockerNetwork(dockerHost);
Object.assign(dockerNetwork, networkObject);
dockerNetworks.push(dockerNetwork);
2019-08-15 16:50:13 +00:00
}
2019-08-14 21:21:54 +00:00
return dockerNetworks;
}
2019-08-15 16:50:13 +00:00
public static async getNetworkByName(dockerHost: DockerHost, dockerNetworkNameArg: string) {
const networks = await DockerNetwork.getNetworks(dockerHost);
return networks.find(dockerNetwork => dockerNetwork.Name === dockerNetworkNameArg);
2019-08-14 21:21:54 +00:00
}
2019-08-15 16:50:13 +00:00
public static async createNetwork(
dockerHost: DockerHost,
networkCreationDescriptor: interfaces.INetworkCreationDescriptor
): Promise<DockerNetwork> {
const response = await dockerHost.request('POST', '/networks/create', {
Name: networkCreationDescriptor.Name,
CheckDuplicate: true,
Driver: 'overlay',
2019-09-23 11:52:52 +00:00
EnableIPv6: false,
2019-09-23 11:41:06 +00:00
/* IPAM: {
2019-09-22 21:42:29 +00:00
Driver: 'default',
Config: [
{
Subnet: `172.20.${networkCreationDescriptor.NetworkNumber}.0/16`,
IPRange: `172.20.${networkCreationDescriptor.NetworkNumber}.0/24`,
Gateway: `172.20.${networkCreationDescriptor.NetworkNumber}.11`
}
]
2019-09-23 11:41:06 +00:00
}, */
Internal: false,
2019-08-15 16:50:13 +00:00
Attachable: true,
Ingress: false
2019-08-14 21:21:54 +00:00
});
2019-08-16 10:48:56 +00:00
if (response.statusCode < 300) {
2019-08-15 16:50:13 +00:00
plugins.smartlog.defaultLogger.log('info', 'Created network successfully');
return await DockerNetwork.getNetworkByName(dockerHost, networkCreationDescriptor.Name);
} else {
2019-08-16 10:48:56 +00:00
plugins.smartlog.defaultLogger.log(
'error',
'There has been an error creating the wanted network'
);
return null;
2019-08-15 16:50:13 +00:00
}
2019-08-14 21:21:54 +00:00
}
2019-08-15 16:50:13 +00:00
// INSTANCE
// references
public dockerHost: DockerHost;
// properties
2019-08-14 21:21:54 +00:00
public Name: string;
public Id: string;
public Created: string;
public Scope: string;
public Driver: string;
public EnableIPv6: boolean;
public Internal: boolean;
public Attachable: boolean;
public Ingress: false;
public IPAM: {
2019-08-15 16:50:13 +00:00
Driver: 'default' | 'bridge' | 'overlay';
2019-08-14 21:21:54 +00:00
Config: [
{
2019-08-15 16:50:13 +00:00
Subnet: string;
IPRange: string;
Gateway: string;
2019-08-14 21:21:54 +00:00
}
2019-08-15 16:50:13 +00:00
];
2019-08-14 21:21:54 +00:00
};
2019-09-08 17:22:20 +00:00
constructor(dockerHostArg: DockerHost) {
2019-08-15 16:50:13 +00:00
this.dockerHost = dockerHostArg;
}
2019-08-14 21:21:54 +00:00
2019-08-15 16:50:13 +00:00
/**
* removes the network
*/
public async remove() {
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
}
2019-09-21 19:57:57 +00:00
2019-09-22 12:32:48 +00:00
public async getContainersOnNetwork(): Promise<Array<{
2019-09-21 19:57:57 +00:00
Name: string;
EndpointID: string;
MacAddress: string;
IPv4Address: string;
IPv6Address: string;
2019-09-22 12:32:48 +00:00
}>> {
const returnArray = [];
2019-09-21 19:57:57 +00:00
const response = await this.dockerHost.request('GET', `/networks/${this.Id}`);
2019-09-22 12:32:48 +00:00
for (const key of Object.keys(response.body.Containers)) {
returnArray.push(response.body.Containers[key]);
}
return returnArray;
}
2019-09-22 13:02:29 +00:00
public async getContainersOnNetworkForService(serviceArg: DockerService) {
2019-09-22 12:32:48 +00:00
const containersOnNetwork = await this.getContainersOnNetwork();
const containersOfService = containersOnNetwork.filter(container => {
return container.Name.startsWith(serviceArg.Spec.Name);
});
2019-09-22 13:11:57 +00:00
return containersOfService;
2019-09-21 19:57:57 +00:00
}
2019-08-14 21:21:54 +00:00
}