161 lines
4.3 KiB
TypeScript
161 lines
4.3 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import * as interfaces from './interfaces/index.js';
|
|
|
|
import { DockerHost } from './classes.host.js';
|
|
import { DockerResource } from './classes.base.js';
|
|
import { DockerService } from './classes.service.js';
|
|
import { logger } from './logger.js';
|
|
|
|
export class DockerNetwork extends DockerResource {
|
|
// STATIC (Internal - prefixed with _ to indicate internal use)
|
|
|
|
/**
|
|
* Internal: Get all networks
|
|
* Public API: Use dockerHost.getNetworks() instead
|
|
*/
|
|
public static async _list(
|
|
dockerHost: DockerHost,
|
|
): Promise<DockerNetwork[]> {
|
|
const dockerNetworks: DockerNetwork[] = [];
|
|
const response = await dockerHost.request('GET', '/networks');
|
|
for (const networkObject of response.body) {
|
|
const dockerNetwork = new DockerNetwork(dockerHost);
|
|
Object.assign(dockerNetwork, networkObject);
|
|
dockerNetworks.push(dockerNetwork);
|
|
}
|
|
return dockerNetworks;
|
|
}
|
|
|
|
/**
|
|
* Internal: Get network by name
|
|
* Public API: Use dockerHost.getNetworkByName(name) instead
|
|
*/
|
|
public static async _fromName(
|
|
dockerHost: DockerHost,
|
|
dockerNetworkNameArg: string,
|
|
) {
|
|
const networks = await DockerNetwork._list(dockerHost);
|
|
return networks.find(
|
|
(dockerNetwork) => dockerNetwork.Name === dockerNetworkNameArg,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Internal: Create a network
|
|
* Public API: Use dockerHost.createNetwork(descriptor) instead
|
|
*/
|
|
public static async _create(
|
|
dockerHost: DockerHost,
|
|
networkCreationDescriptor: interfaces.INetworkCreationDescriptor,
|
|
): Promise<DockerNetwork> {
|
|
const response = await dockerHost.request('POST', '/networks/create', {
|
|
Name: networkCreationDescriptor.Name,
|
|
CheckDuplicate: true,
|
|
Driver: 'overlay',
|
|
EnableIPv6: false,
|
|
/* IPAM: {
|
|
Driver: 'default',
|
|
Config: [
|
|
{
|
|
Subnet: `172.20.${networkCreationDescriptor.NetworkNumber}.0/16`,
|
|
IPRange: `172.20.${networkCreationDescriptor.NetworkNumber}.0/24`,
|
|
Gateway: `172.20.${networkCreationDescriptor.NetworkNumber}.11`
|
|
}
|
|
]
|
|
}, */
|
|
Internal: false,
|
|
Attachable: true,
|
|
Ingress: false,
|
|
});
|
|
if (response.statusCode < 300) {
|
|
logger.log('info', 'Created network successfully');
|
|
return await DockerNetwork._fromName(
|
|
dockerHost,
|
|
networkCreationDescriptor.Name,
|
|
);
|
|
} else {
|
|
logger.log(
|
|
'error',
|
|
'There has been an error creating the wanted network',
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// INSTANCE PROPERTIES
|
|
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: {
|
|
Driver: 'default' | 'bridge' | 'overlay';
|
|
Config: [
|
|
{
|
|
Subnet: string;
|
|
IPRange: string;
|
|
Gateway: string;
|
|
},
|
|
];
|
|
};
|
|
|
|
constructor(dockerHostArg: DockerHost) {
|
|
super(dockerHostArg);
|
|
}
|
|
|
|
// INSTANCE METHODS
|
|
|
|
/**
|
|
* Refreshes this network's state from the Docker daemon
|
|
*/
|
|
public async refresh(): Promise<void> {
|
|
const updated = await DockerNetwork._fromName(this.dockerHost, this.Name);
|
|
if (updated) {
|
|
Object.assign(this, updated);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes the network
|
|
*/
|
|
public async remove() {
|
|
const response = await this.dockerHost.request(
|
|
'DELETE',
|
|
`/networks/${this.Id}`,
|
|
);
|
|
}
|
|
|
|
public async listContainersOnNetwork(): Promise<
|
|
Array<{
|
|
Name: string;
|
|
EndpointID: string;
|
|
MacAddress: string;
|
|
IPv4Address: string;
|
|
IPv6Address: string;
|
|
}>
|
|
> {
|
|
const returnArray = [];
|
|
const response = await this.dockerHost.request(
|
|
'GET',
|
|
`/networks/${this.Id}`,
|
|
);
|
|
for (const key of Object.keys(response.body.Containers)) {
|
|
returnArray.push(response.body.Containers[key]);
|
|
}
|
|
|
|
return returnArray;
|
|
}
|
|
|
|
public async getContainersOnNetworkForService(serviceArg: DockerService) {
|
|
const containersOnNetwork = await this.listContainersOnNetwork();
|
|
const containersOfService = containersOnNetwork.filter((container) => {
|
|
return container.Name.startsWith(serviceArg.Spec.Name);
|
|
});
|
|
return containersOfService;
|
|
}
|
|
}
|