[READ-ONLY] Mirror of https://github.com/FoxxMD/komodo-utilities. Small utilities to enhance Komodo
gotify komodo
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: Improve find_ip execution time and search params

* Promisfy data fetching to improve execution time, thanks @mbecker20
* Accept comma-delimited list of strings for matching multiple values

FoxxMD (Jan 24, 2025, 9:22 AM EST) d572ea1d 8eab8e3e

+31 -20
+1 -1
README.md
··· 32 32 33 33 [Find one or many Containers by IP or Gateway address.](/actions/find_ip.toml) 34 34 35 - Accepts a string to find within the address so it works for finding partial IPs or subnets. 35 + Accepts a comma-delimited list of strings to find within the address so it works for finding partial IPs or subnets. 36 36 37 37 Output contains Server, Stack, Container, and Network where it was found: 38 38
+30 -19
actions/find_ip.toml
··· 2 2 name = "find_ip" 3 3 [action.config] 4 4 file_contents = """ 5 - const IP_LIKE = '172.16.10'; 6 - // set to false to find ALL that match, otherwise exits after first found 7 - const FIND_ONE = true; 5 + const IP_LIKE = '172.16.10,172.20.0.2'; 6 + 7 + const IP_LIKE_VALS = IP_LIKE.split(',').map(x => x.trim()); 8 + 9 + type ContextualContainer = {server: string, container: Types.Container }; 8 10 9 11 let foundAny = false; 10 12 11 13 const servers: Types.ListServersResponse = await komodo.read('ListServers', {}); 14 + const serverMap: Record<string, Types.ServerListItem> = servers.reduce((acc, curr) => ({...acc, [curr.id]: curr}), {}); 12 15 13 - for(const s of servers) { 16 + // could do filtering during promise execution 17 + // but for re-useability, extensibility, and completeness sake, keep all data intact and foreach after retriving everything 18 + // (even if doing filtering during promise we'd still be fetching all data so it doesn't decrease execution time significantly) 19 + const data: ContextualContainer[] = (await Promise.all(servers.map(async (s) => 20 + { 14 21 const containers = await komodo.read('ListDockerContainers', {server: s.id}); 15 - for(const c of containers) { 16 - const inspect = await komodo.read('InspectDockerContainer', {server: s.id, container: c.id}); 17 - if(inspect.NetworkSettings.Networks !== undefined) { 22 + 23 + const containerInfos = await Promise.all(containers.map(async (c) => { 24 + const container = await komodo.read('InspectDockerContainer', {server: s.id, container: c.id}); 25 + return {server: s.id, container}; 26 + })); 27 + 28 + return containerInfos; 29 + 30 + }))).flat(); 31 + 32 + for(const cInfo of data) { 33 + const {server, container} = cInfo; 34 + 35 + if(container.NetworkSettings.Networks !== undefined) { 18 36 const found: string[] = []; 19 - for(const [network, v] of Object.entries(inspect.NetworkSettings.Networks)) { 20 - if(v.Gateway.includes(IP_LIKE)) { 37 + for(const [network, v] of Object.entries(container.NetworkSettings.Networks)) { 38 + if(IP_LIKE_VALS.some(x => v.Gateway.includes(x))) { 21 39 found.push(`Gateway ${v.Gateway} in Network ${network}`); 22 40 } 23 - if(v.IPAddress.includes(IP_LIKE)) { 41 + if(IP_LIKE_VALS.some(x => v.IPAddress.includes(x))) { 24 42 found.push(`IP ${v.IPAddress} in Network ${network}`); 25 43 } 26 44 } 27 45 if(found.length > 0) { 28 - console.log(`Server ${s.name} -> Container ${c.name} has ${found.join(', ')}`); 46 + console.log(`Server ${serverMap[server].name} -> Container ${container.Name.substring(1)} has ${found.join(', ')}`); 29 47 foundAny = true; 30 48 } 31 49 } 32 - if(foundAny && FIND_ONE) { 33 - break; 34 - } 35 - } 36 - if(foundAny && FIND_ONE) { 37 - break; 38 - } 39 50 } 40 51 41 52 if(!foundAny) { 42 - console.log(`Did not find Gatways or IP Addresses containing ${IP_LIKE}`); 53 + console.log(`Did not find Gateways or IP Addresses containing ${IP_LIKE_VALS.join(' or ')}`); 43 54 } 44 55 """