1- import fs from 'fs' ;
1+ import { promises as fs } from 'fs' ;
22
33import camelCaseKeys from 'camelcase-keys' ;
44
@@ -9,6 +9,7 @@ import { catchHandlers } from '@app/core/utils/misc/catch-handlers.js';
99import { ContainerPortType , ContainerState } from '@app/graphql/generated/api/types.js' ;
1010import { getters , store } from '@app/store/index.js' ;
1111import { updateDockerState } from '@app/store/modules/docker.js' ;
12+ import { PathsConfig } from '../../../config/paths.config.js' ;
1213
1314export interface ContainerListingOptions {
1415 useCache ?: boolean ;
@@ -18,9 +19,7 @@ export interface ContainerListingOptions {
1819 * Get all Docker containers.
1920 * @returns All the in/active Docker containers on the system.
2021 */
21- export const getDockerContainers = async (
22- { useCache } : ContainerListingOptions = { useCache : true }
23- ) : Promise < Array < DockerContainer > > => {
22+ export const getDockerContainers = async ( { useCache = true } : ContainerListingOptions = { } ) : Promise < DockerContainer [ ] > => {
2423 const dockerState = getters . docker ( ) ;
2524 if ( useCache && dockerState . containers ) {
2625 dockerLogger . trace ( 'Using docker container cache' ) ;
@@ -29,57 +28,31 @@ export const getDockerContainers = async (
2928
3029 dockerLogger . trace ( 'Skipping docker container cache' ) ;
3130
32- /**
33- * Docker auto start file
34- *
35- * @note Doesn't exist if array is offline.
36- * @see https://github.com/limetech/webgui/issues/502#issue-480992547
37- */
38- const autoStartFile = await fs . promises
39- . readFile ( getters . paths ( ) [ 'docker-autostart' ] , 'utf8' )
40- . then ( ( file ) => file . toString ( ) )
41- . catch ( ( ) => '' ) ;
42- const autoStarts = autoStartFile . split ( '\n' ) ;
43- const rawContainers = await docker
44- . listContainers ( {
45- all : true ,
46- size : true ,
47- } )
48- // If docker throws an error return no containers
49- . catch ( catchHandlers . docker ) ;
50-
51- // Cleanup container object
52- const containers : Array < DockerContainer > = rawContainers . map ( ( container ) => {
53- const names = container . Names [ 0 ] ;
54- const containerData : DockerContainer = camelCaseKeys < DockerContainer > (
55- {
56- labels : container . Labels ?? { } ,
57- sizeRootFs : undefined ,
58- imageId : container . ImageID ,
59- state :
60- typeof container . State === 'string'
61- ? ( ContainerState [ container . State . toUpperCase ( ) ] ?? ContainerState . EXITED )
62- : ContainerState . EXITED ,
63- autoStart : autoStarts . includes ( names . split ( '/' ) [ 1 ] ) ,
64- ports : container . Ports . map < ContainerPort > ( ( port ) => ( {
65- ...port ,
66- type : ContainerPortType [ port . Type . toUpperCase ( ) ] ,
67- } ) ) ,
68- command : container . Command ,
69- created : container . Created ,
70- mounts : container . Mounts ,
71- networkSettings : container . NetworkSettings ,
72- hostConfig : {
73- networkMode : container . HostConfig . NetworkMode ,
74- } ,
75- id : container . Id ,
76- image : container . Image ,
77- status : container . Status ,
78- } ,
79- { deep : true }
80- ) ;
81- return containerData ;
82- } ) ;
31+ const paths = PathsConfig . getInstance ( ) ;
32+ const autostartFile = await fs . readFile ( paths . dockerAutostart , 'utf8' ) . catch ( ( ) => '' ) ;
33+ const autoStarts = autostartFile . split ( '\n' ) ;
34+ const rawContainers = await docker . listContainers ( { all : true } ) . catch ( catchHandlers . docker ) ;
35+
36+ const containers : DockerContainer [ ] = rawContainers . map ( ( container ) => ( {
37+ id : container . Id ,
38+ image : container . Image ,
39+ imageId : container . ImageID ,
40+ command : container . Command ,
41+ created : container . Created ,
42+ state : ContainerState [ container . State . toUpperCase ( ) as keyof typeof ContainerState ] ,
43+ status : container . Status ,
44+ ports : container . Ports . map ( ( port ) => ( {
45+ ...port ,
46+ type : ContainerPortType [ port . Type . toUpperCase ( ) as keyof typeof ContainerPortType ] ,
47+ } ) ) as ContainerPort [ ] ,
48+ autoStart : autoStarts . includes ( container . Names [ 0 ] . split ( '/' ) [ 1 ] ) ,
49+ labels : container . Labels ?? { } ,
50+ mounts : container . Mounts ,
51+ networkSettings : container . NetworkSettings ,
52+ hostConfig : {
53+ networkMode : container . HostConfig . NetworkMode ,
54+ } ,
55+ } ) ) ;
8356
8457 // Get all of the current containers
8558 const installed = containers . length ;
0 commit comments