2024-06-05 14:10:44 +02:00
|
|
|
import * as plugins from './plugins.js';
|
2022-10-17 09:36:35 +02:00
|
|
|
import * as interfaces from './interfaces/index.js';
|
2019-08-14 14:19:45 +02:00
|
|
|
|
2024-06-05 14:10:44 +02:00
|
|
|
import { DockerHost } from './classes.host.js';
|
2025-11-24 12:20:30 +00:00
|
|
|
import { DockerResource } from './classes.base.js';
|
2024-06-05 14:10:44 +02:00
|
|
|
import { DockerService } from './classes.service.js';
|
2024-06-08 15:03:19 +02:00
|
|
|
import { logger } from './logger.js';
|
2019-08-14 14:19:45 +02:00
|
|
|
|
2025-11-24 12:20:30 +00:00
|
|
|
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(
|
2025-08-19 01:46:37 +00:00
|
|
|
dockerHost: DockerHost,
|
|
|
|
|
): Promise<DockerNetwork[]> {
|
2019-08-14 23:21:54 +02:00
|
|
|
const dockerNetworks: DockerNetwork[] = [];
|
2019-08-15 18:50:13 +02:00
|
|
|
const response = await dockerHost.request('GET', '/networks');
|
|
|
|
|
for (const networkObject of response.body) {
|
2019-09-08 19:22:20 +02:00
|
|
|
const dockerNetwork = new DockerNetwork(dockerHost);
|
|
|
|
|
Object.assign(dockerNetwork, networkObject);
|
|
|
|
|
dockerNetworks.push(dockerNetwork);
|
2019-08-15 18:50:13 +02:00
|
|
|
}
|
2019-08-14 23:21:54 +02:00
|
|
|
return dockerNetworks;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 12:20:30 +00:00
|
|
|
/**
|
|
|
|
|
* Internal: Get network by name
|
|
|
|
|
* Public API: Use dockerHost.getNetworkByName(name) instead
|
|
|
|
|
*/
|
|
|
|
|
public static async _fromName(
|
2025-08-19 01:46:37 +00:00
|
|
|
dockerHost: DockerHost,
|
|
|
|
|
dockerNetworkNameArg: string,
|
|
|
|
|
) {
|
2025-11-24 12:20:30 +00:00
|
|
|
const networks = await DockerNetwork._list(dockerHost);
|
2025-08-19 01:46:37 +00:00
|
|
|
return networks.find(
|
|
|
|
|
(dockerNetwork) => dockerNetwork.Name === dockerNetworkNameArg,
|
|
|
|
|
);
|
2019-08-14 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 12:20:30 +00:00
|
|
|
/**
|
|
|
|
|
* Internal: Create a network
|
|
|
|
|
* Public API: Use dockerHost.createNetwork(descriptor) instead
|
|
|
|
|
*/
|
|
|
|
|
public static async _create(
|
2019-08-15 18:50:13 +02:00
|
|
|
dockerHost: DockerHost,
|
2025-08-19 01:46:37 +00:00
|
|
|
networkCreationDescriptor: interfaces.INetworkCreationDescriptor,
|
2019-08-15 18:50:13 +02:00
|
|
|
): Promise<DockerNetwork> {
|
|
|
|
|
const response = await dockerHost.request('POST', '/networks/create', {
|
|
|
|
|
Name: networkCreationDescriptor.Name,
|
|
|
|
|
CheckDuplicate: true,
|
2025-11-25 05:18:48 +00:00
|
|
|
Driver: networkCreationDescriptor.Driver || 'overlay',
|
|
|
|
|
EnableIPv6: networkCreationDescriptor.EnableIPv6 || false,
|
|
|
|
|
IPAM: networkCreationDescriptor.IPAM,
|
|
|
|
|
Internal: networkCreationDescriptor.Internal || false,
|
|
|
|
|
Attachable: networkCreationDescriptor.Attachable !== undefined ? networkCreationDescriptor.Attachable : true,
|
|
|
|
|
Labels: networkCreationDescriptor.Labels,
|
2020-09-30 16:35:24 +00:00
|
|
|
Ingress: false,
|
2019-08-14 23:21:54 +02:00
|
|
|
});
|
2019-08-16 12:48:56 +02:00
|
|
|
if (response.statusCode < 300) {
|
2020-09-30 16:27:43 +00:00
|
|
|
logger.log('info', 'Created network successfully');
|
2025-11-24 12:20:30 +00:00
|
|
|
return await DockerNetwork._fromName(
|
2025-08-19 01:46:37 +00:00
|
|
|
dockerHost,
|
|
|
|
|
networkCreationDescriptor.Name,
|
|
|
|
|
);
|
2019-08-15 18:50:13 +02:00
|
|
|
} else {
|
2025-08-19 01:46:37 +00:00
|
|
|
logger.log(
|
|
|
|
|
'error',
|
|
|
|
|
'There has been an error creating the wanted network',
|
|
|
|
|
);
|
2019-08-16 12:48:56 +02:00
|
|
|
return null;
|
2019-08-15 18:50:13 +02:00
|
|
|
}
|
2019-08-14 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 12:20:30 +00:00
|
|
|
// INSTANCE PROPERTIES
|
2019-08-14 23:21:54 +02: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 18:50:13 +02:00
|
|
|
Driver: 'default' | 'bridge' | 'overlay';
|
2019-08-14 23:21:54 +02:00
|
|
|
Config: [
|
|
|
|
|
{
|
2019-08-15 18:50:13 +02:00
|
|
|
Subnet: string;
|
|
|
|
|
IPRange: string;
|
|
|
|
|
Gateway: string;
|
2025-08-19 01:46:37 +00:00
|
|
|
},
|
2019-08-15 18:50:13 +02:00
|
|
|
];
|
2019-08-14 23:21:54 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-08 19:22:20 +02:00
|
|
|
constructor(dockerHostArg: DockerHost) {
|
2025-11-24 12:20:30 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2019-08-15 18:50:13 +02:00
|
|
|
}
|
2019-08-14 23:21:54 +02:00
|
|
|
|
2019-08-15 18:50:13 +02:00
|
|
|
/**
|
2025-11-24 12:20:30 +00:00
|
|
|
* Removes the network
|
2019-08-15 18:50:13 +02:00
|
|
|
*/
|
|
|
|
|
public async remove() {
|
2025-08-19 01:46:37 +00:00
|
|
|
const response = await this.dockerHost.request(
|
|
|
|
|
'DELETE',
|
|
|
|
|
`/networks/${this.Id}`,
|
|
|
|
|
);
|
2019-08-15 18:50:13 +02:00
|
|
|
}
|
2019-09-21 21:57:57 +02:00
|
|
|
|
2025-11-24 13:27:10 +00:00
|
|
|
public async listContainersOnNetwork(): Promise<
|
2019-11-20 13:36:03 +00:00
|
|
|
Array<{
|
2019-09-21 21:57:57 +02:00
|
|
|
Name: string;
|
|
|
|
|
EndpointID: string;
|
|
|
|
|
MacAddress: string;
|
|
|
|
|
IPv4Address: string;
|
|
|
|
|
IPv6Address: string;
|
2019-11-20 13:36:03 +00:00
|
|
|
}>
|
|
|
|
|
> {
|
2019-09-22 14:32:48 +02:00
|
|
|
const returnArray = [];
|
2025-08-19 01:46:37 +00:00
|
|
|
const response = await this.dockerHost.request(
|
|
|
|
|
'GET',
|
|
|
|
|
`/networks/${this.Id}`,
|
|
|
|
|
);
|
2019-09-22 14:32:48 +02:00
|
|
|
for (const key of Object.keys(response.body.Containers)) {
|
|
|
|
|
returnArray.push(response.body.Containers[key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnArray;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-22 15:02:29 +02:00
|
|
|
public async getContainersOnNetworkForService(serviceArg: DockerService) {
|
2025-11-24 13:27:10 +00:00
|
|
|
const containersOnNetwork = await this.listContainersOnNetwork();
|
2020-09-30 16:35:24 +00:00
|
|
|
const containersOfService = containersOnNetwork.filter((container) => {
|
2019-09-22 14:32:48 +02:00
|
|
|
return container.Name.startsWith(serviceArg.Spec.Name);
|
|
|
|
|
});
|
2019-09-22 15:11:57 +02:00
|
|
|
return containersOfService;
|
2019-09-21 21:57:57 +02:00
|
|
|
}
|
2019-08-14 23:21:54 +02:00
|
|
|
}
|