import { ContainerName , HealthCheckStatus , NetworkSettings , Ports } from "../../types" ; import Dockerode , { ContainerInspectInfo } from "dockerode" ; import { log } from "../../../logger" ; export type InspectResult = { name : ContainerName ; hostname : string ; ports : Ports ; healthCheckStatus : HealthCheckStatus ; networkSettings : { [ networkName ] : NetworkSettings ; } ; state : { status : string ; running : boolean ; } ; } ; export const inspectContainer = async ( container ) : Promise < InspectResult > => { try { const inspectResult = await container . inspect ( ) ; return { name : inspectResult . Name , hostname : inspectResult . Config . Hostname , ports : getPorts ( inspectResult ) , healthCheckStatus : getHealthCheckStatus ( inspectResult ) , networkSettings : getNetworkSettings ( inspectResult ) , state : { status : inspectResult . State . Status , running : inspectResult . State . Running } , } ; } catch ( err ) { log . error ( `Failed to inspect container ${ container . id }: ${ err }` ) ; throw err ; } } ; const getPorts = ( inspectInfo ) : Ports => Object . entries ( inspectInfo . NetworkSettings . Ports ) . filter ( ( [ , hostPorts ] ) => hostPorts !== null ) . map ( ( [ internalPort , hostPorts ] ) => { const hostPort = hostPorts [ 0 ] . HostPort ; return { [ parseInt ( internalPort . split ( "/" ) [ 0 ] ) ] : parseInt ( hostPort ) } ; } ) . reduce ( ( acc , curr ) => ( { ... acc , ... curr } ) , { } ) ; const getHealthCheckStatus = ( inspectResult ) : HealthCheckStatus => { const health = inspectResult . State . Health ; if ( health === undefined ) { return "none" ; } else { return health . Status as HealthCheckStatus ; } } ; const getNetworkSettings = ( inspectResult ) : { [ networkName ] : NetworkSettings ; } => Object . entries ( inspectResult . NetworkSettings . Networks ) . map ( ( [ networkName , network ] ) => ( { [ networkName ] : { networkId : network . NetworkID , ipAddress : network . IPAddress , } , } ) ) . reduce ( ( prev , next ) => ( { ... prev , ... next } ) , { } ) ;