import { Container as ContainerInterface , ContainerCreateOptions , ContainerInfo , ContainerInspectInfo , } from 'dockerode' ; import chalk from 'chalk' ; import { RunningContainerInfo , ContainerClientInterface } from './interfaces' ; import { logger } from './logger' ; export class Container implements ContainerClientInterface { client : any ; constructor ( client ) { this . client = client ; } param { ContainerCreateOptions } createOpts return { ( Promise < ContainerInterface | undefined > ) } memberof async create ( createOpts ) { let container ; try { container = await this . client . createContainer ( createOpts ) ; return container ; } catch ( err ) { console . log ( chalk . red ( `create container error: ${ err }` ) ) ; logger . error ( `create container error: ${ err . message }` ) ; return ; } } static param { ContainerInterface } container return { Promise < void > } memberof static async start ( container ) { try { await container . start ( ) ; console . log ( chalk . cyan ( `Started container ${ container . id }` ) ) ; } catch ( err ) { console . log ( chalk . red ( `start container error: ${ err }` ) ) ; logger . error ( `start container error: ${ err . message }` ) ; } } static param { ContainerInterface } container return { Promise < void > } memberof static async stop ( container ) { try { await container . stop ( ) ; console . log ( chalk . cyan ( `Stopped container ${ container . id }` ) ) ; } catch ( err ) { console . log ( chalk . red ( `stop container error: ${ err }` ) ) ; logger . error ( `stop container error: ${ err . message }` ) ; } } static param { ContainerInterface } container return { Promise < void > } memberof static async remove ( container ) { try { await container . remove ( ) ; console . log ( chalk . cyan ( `Removed container ${ container . id }` ) ) ; } catch ( err ) { console . log ( chalk . red ( `remove container error: ${ err }` ) ) ; logger . error ( `remove container error: ${ err . message }` ) ; } } return { ( Promise < RunningContainerInfo [ ] | undefined > ) } memberof async getRunningContainers ( ) { const runningContainers = [ ] ; const opts = { filters : { status : [ 'running' ] , } , } ; try { const containers = await this . client . listContainers ( opts ) ; for ( const c of containers ) { if ( c . Image . includes ( 'argus' ) ) continue ; const container = await this . client . getContainer ( c . Id ) ; const containerInspect = await container . inspect ( ) ; const containerObject = { inspectObject : containerInspect , interfaceObject : container , } ; runningContainers . push ( containerObject ) ; } return runningContainers ; } catch ( err ) { console . log ( chalk . red ( `running containers error: ${ err }` ) ) ; logger . error ( `running containers error: ${ err . message }` ) ; return ; } } static param { ( RunningContainerInfo [ ] | undefined ) } containers param { ( string [ ] | undefined ) } containerNamesToMonitor param { ( string [ ] | undefined ) } containerNamesToIgnore return { ( RunningContainerInfo [ ] | undefined ) } memberof static getRunningContainersToMonitor ( containers , containerNamesToMonitor , containerNamesToIgnore ) { if ( ! containerNamesToMonitor . length && ! containerNamesToIgnore . length ) { return containers ; } else if ( containerNamesToMonitor . length && containerNamesToIgnore . length ) { const intersection = containerNamesToMonitor . filter ( ( containerName ) => containerNamesToIgnore . includes ( containerName ) ) ; if ( intersection . length ) throw new Error ( ) ; } let containersToMonitor = [ ] ; if ( containerNamesToMonitor . length ) { containersToMonitor = containers . filter ( ( container ) => containerNamesToMonitor . includes ( container . inspectObject ?. Name . substring ( 1 ) ) ) ; } if ( containerNamesToIgnore . length ) { containersToMonitor = ( containersToMonitor || containers ) . filter ( ( container ) => ! containerNamesToIgnore . includes ( container . inspectObject ?. Name . substring ( 1 ) ) ) ; } return containersToMonitor ; } static param { ContainerInspectInfo } oldContainer param { string } newImage return { ContainerCreateOptions } memberof static newContainerConfig ( oldContainer , newImage ) { const config = { name : oldContainer . Name . replace ( '/' , '' ) , Image : newImage , Cmd : oldContainer . Config . Cmd , HostConfig : oldContainer . HostConfig , Labels : oldContainer . Config . Labels , Entrypoint : oldContainer . Config . Entrypoint , Env : oldContainer . Config . Env , } ; return config ; } }