···32323333[Find one or many Containers by IP or Gateway address.](/actions/find_ip.toml)
34343535-Accepts a string to find within the address so it works for finding partial IPs or subnets.
3535+Accepts a comma-delimited list of strings to find within the address so it works for finding partial IPs or subnets.
36363737Output contains Server, Stack, Container, and Network where it was found:
3838
+30-19
actions/find_ip.toml
···22name = "find_ip"
33[action.config]
44file_contents = """
55-const IP_LIKE = '172.16.10';
66-// set to false to find ALL that match, otherwise exits after first found
77-const FIND_ONE = true;
55+const IP_LIKE = '172.16.10,172.20.0.2';
66+77+const IP_LIKE_VALS = IP_LIKE.split(',').map(x => x.trim());
88+99+type ContextualContainer = {server: string, container: Types.Container };
810911let foundAny = false;
10121113const servers: Types.ListServersResponse = await komodo.read('ListServers', {});
1414+const serverMap: Record<string, Types.ServerListItem> = servers.reduce((acc, curr) => ({...acc, [curr.id]: curr}), {});
12151313-for(const s of servers) {
1616+// could do filtering during promise execution
1717+// but for re-useability, extensibility, and completeness sake, keep all data intact and foreach after retriving everything
1818+// (even if doing filtering during promise we'd still be fetching all data so it doesn't decrease execution time significantly)
1919+const data: ContextualContainer[] = (await Promise.all(servers.map(async (s) =>
2020+{
1421 const containers = await komodo.read('ListDockerContainers', {server: s.id});
1515- for(const c of containers) {
1616- const inspect = await komodo.read('InspectDockerContainer', {server: s.id, container: c.id});
1717- if(inspect.NetworkSettings.Networks !== undefined) {
2222+2323+ const containerInfos = await Promise.all(containers.map(async (c) => {
2424+ const container = await komodo.read('InspectDockerContainer', {server: s.id, container: c.id});
2525+ return {server: s.id, container};
2626+ }));
2727+2828+ return containerInfos;
2929+3030+}))).flat();
3131+3232+for(const cInfo of data) {
3333+ const {server, container} = cInfo;
3434+3535+ if(container.NetworkSettings.Networks !== undefined) {
1836 const found: string[] = [];
1919- for(const [network, v] of Object.entries(inspect.NetworkSettings.Networks)) {
2020- if(v.Gateway.includes(IP_LIKE)) {
3737+ for(const [network, v] of Object.entries(container.NetworkSettings.Networks)) {
3838+ if(IP_LIKE_VALS.some(x => v.Gateway.includes(x))) {
2139 found.push(`Gateway ${v.Gateway} in Network ${network}`);
2240 }
2323- if(v.IPAddress.includes(IP_LIKE)) {
4141+ if(IP_LIKE_VALS.some(x => v.IPAddress.includes(x))) {
2442 found.push(`IP ${v.IPAddress} in Network ${network}`);
2543 }
2644 }
2745 if(found.length > 0) {
2828- console.log(`Server ${s.name} -> Container ${c.name} has ${found.join(', ')}`);
4646+ console.log(`Server ${serverMap[server].name} -> Container ${container.Name.substring(1)} has ${found.join(', ')}`);
2947 foundAny = true;
3048 }
3149 }
3232- if(foundAny && FIND_ONE) {
3333- break;
3434- }
3535- }
3636- if(foundAny && FIND_ONE) {
3737- break;
3838- }
3950}
40514152if(!foundAny) {
4242- console.log(`Did not find Gatways or IP Addresses containing ${IP_LIKE}`);
5353+ console.log(`Did not find Gateways or IP Addresses containing ${IP_LIKE_VALS.join(' or ')}`);
4354}
4455"""