@@ -4,13 +4,33 @@ const docker = new Docker({ socketPath: '/var/run/docker.sock' });
44// Docker container name for docker-mailserver
55const DOCKER_CONTAINER = process . env . DOCKER_CONTAINER || 'mailserver' ;
66
7+ // Debug flag
8+ const DEBUG = process . env . DEBUG_DOCKER === 'true' ;
9+
10+ /**
11+ * Debug logger that only logs if DEBUG is true
12+ * @param {string } message - Message to log
13+ * @param {any } data - Optional data to log
14+ */
15+ function debugLog ( message , data = null ) {
16+ if ( DEBUG ) {
17+ if ( data ) {
18+ console . log ( `[DOCKER-DEBUG] ${ message } ` , data ) ;
19+ } else {
20+ console . log ( `[DOCKER-DEBUG] ${ message } ` ) ;
21+ }
22+ }
23+ }
24+
725/**
826 * Executes a command in the docker-mailserver container
927 * @param {string } command Command to execute
1028 * @return {Promise<string> } stdout from the command
1129 */
1230async function execInContainer ( command ) {
1331 try {
32+ debugLog ( `Executing command in container ${ DOCKER_CONTAINER } : ${ command } ` ) ;
33+
1434 // Get container instance
1535 const container = docker . getContainer ( DOCKER_CONTAINER ) ;
1636
@@ -36,15 +56,18 @@ async function execInContainer(command) {
3656 } ) ;
3757
3858 stream . on ( 'end' , ( ) => {
59+ debugLog ( `Command completed. Output:` , stdoutData ) ;
3960 resolve ( stdoutData ) ;
4061 } ) ;
4162
4263 stream . on ( 'error' , ( err ) => {
64+ debugLog ( `Command error:` , err ) ;
4365 reject ( err ) ;
4466 } ) ;
4567 } ) ;
4668 } catch ( error ) {
4769 console . error ( `Error executing command in container: ${ command } ` , error ) ;
70+ debugLog ( `Execution error:` , error ) ;
4871 throw error ;
4972 }
5073}
@@ -56,12 +79,14 @@ async function execInContainer(command) {
5679 */
5780async function execSetup ( setupCommand ) {
5881 // The setup.sh script is usually located at /usr/local/bin/setup.sh or /usr/local/bin/setup in docker-mailserver
82+ debugLog ( `Executing setup command: ${ setupCommand } ` ) ;
5983 return execInContainer ( `/usr/local/bin/setup ${ setupCommand } ` ) ;
6084}
6185
6286// Function to retrieve email accounts
6387async function getAccounts ( ) {
6488 try {
89+ debugLog ( 'Getting email accounts list' ) ;
6590 const stdout = await execSetup ( 'email list' ) ;
6691
6792 // Parse multiline output with regex to extract email and size information
@@ -70,6 +95,7 @@ async function getAccounts() {
7095
7196 // Process each line individually
7297 const lines = stdout . split ( '\n' ) . filter ( line => line . trim ( ) . length > 0 ) ;
98+ debugLog ( 'Raw email list response:' , lines ) ;
7399
74100 for ( let i = 0 ; i < lines . length ; i ++ ) {
75101 const line = lines [ i ] . trim ( ) ;
@@ -84,6 +110,8 @@ async function getAccounts() {
84110 const totalSpace = match [ 3 ] === '~' ? 'unlimited' : match [ 3 ] ;
85111 const usagePercent = match [ 4 ] ;
86112
113+ debugLog ( `Parsed account: ${ email } , Storage: ${ usedSpace } /${ totalSpace } [${ usagePercent } %]` ) ;
114+
87115 accounts . push ( {
88116 email,
89117 storage : {
@@ -92,47 +120,59 @@ async function getAccounts() {
92120 percent : usagePercent + '%'
93121 }
94122 } ) ;
123+ } else {
124+ debugLog ( `Failed to parse account line: ${ line } ` ) ;
95125 }
96126 }
97127 }
98128
129+ debugLog ( `Found ${ accounts . length } accounts` ) ;
99130 return accounts ;
100131 } catch ( error ) {
101132 console . error ( 'Error retrieving accounts:' , error ) ;
133+ debugLog ( 'Account retrieval error:' , error ) ;
102134 throw new Error ( 'Unable to retrieve account list' ) ;
103135 }
104136}
105137
106138// Function to add a new email account
107139async function addAccount ( email , password ) {
108140 try {
141+ debugLog ( `Adding new email account: ${ email } ` ) ;
109142 await execSetup ( `email add ${ email } ${ password } ` ) ;
143+ debugLog ( `Account created: ${ email } ` ) ;
110144 return { success : true , email } ;
111145 } catch ( error ) {
112146 console . error ( 'Error adding account:' , error ) ;
147+ debugLog ( 'Account creation error:' , error ) ;
113148 throw new Error ( 'Unable to add email account' ) ;
114149 }
115150}
116151
117152// Function to delete an email account
118153async function deleteAccount ( email ) {
119154 try {
155+ debugLog ( `Deleting email account: ${ email } ` ) ;
120156 await execSetup ( `email del ${ email } ` ) ;
157+ debugLog ( `Account deleted: ${ email } ` ) ;
121158 return { success : true , email } ;
122159 } catch ( error ) {
123160 console . error ( 'Error deleting account:' , error ) ;
161+ debugLog ( 'Account deletion error:' , error ) ;
124162 throw new Error ( 'Unable to delete email account' ) ;
125163 }
126164}
127165
128166// Function to retrieve aliases
129167async function getAliases ( ) {
130168 try {
169+ debugLog ( 'Getting aliases list' ) ;
131170 const stdout = await execSetup ( 'alias list' ) ;
132171 const aliases = [ ] ;
133172
134173 // Parse each line in the format "* source destination"
135174 const lines = stdout . split ( '\n' ) . filter ( line => line . trim ( ) . length > 0 ) ;
175+ debugLog ( 'Raw alias list response:' , lines ) ;
136176 const aliasRegex = / ^ \* ( [ \w \- \. @ ] + ) ( [ \w \- \. @ ] + ) $ / ;
137177
138178 for ( let i = 0 ; i < lines . length ; i ++ ) {
@@ -141,59 +181,77 @@ async function getAliases() {
141181 if ( line . startsWith ( '*' ) ) {
142182 const match = line . match ( aliasRegex ) ;
143183 if ( match ) {
184+ const source = match [ 1 ] ;
185+ const destination = match [ 2 ] ;
186+ debugLog ( `Parsed alias: ${ source } -> ${ destination } ` ) ;
187+
144188 aliases . push ( {
145- source : match [ 1 ] ,
146- destination : match [ 2 ]
189+ source,
190+ destination
147191 } ) ;
192+ } else {
193+ debugLog ( `Failed to parse alias line: ${ line } ` ) ;
148194 }
149195 }
150196 }
151197
198+ debugLog ( `Found ${ aliases . length } aliases` ) ;
152199 return aliases ;
153200 } catch ( error ) {
154201 console . error ( 'Error retrieving aliases:' , error ) ;
202+ debugLog ( 'Alias retrieval error:' , error ) ;
155203 throw new Error ( 'Unable to retrieve alias list' ) ;
156204 }
157205}
158206
159207// Function to add an alias
160208async function addAlias ( source , destination ) {
161209 try {
210+ debugLog ( `Adding new alias: ${ source } -> ${ destination } ` ) ;
162211 await execSetup ( `alias add ${ source } ${ destination } ` ) ;
212+ debugLog ( `Alias created: ${ source } -> ${ destination } ` ) ;
163213 return { success : true , source, destination } ;
164214 } catch ( error ) {
165215 console . error ( 'Error adding alias:' , error ) ;
216+ debugLog ( 'Alias creation error:' , error ) ;
166217 throw new Error ( 'Unable to add alias' ) ;
167218 }
168219}
169220
170221// Function to delete an alias
171222async function deleteAlias ( source ) {
172223 try {
224+ debugLog ( `Deleting alias: ${ source } ` ) ;
173225 await execSetup ( `alias del ${ source } ` ) ;
226+ debugLog ( `Alias deleted: ${ source } ` ) ;
174227 return { success : true , source } ;
175228 } catch ( error ) {
176229 console . error ( 'Error deleting alias:' , error ) ;
230+ debugLog ( 'Alias deletion error:' , error ) ;
177231 throw new Error ( 'Unable to delete alias' ) ;
178232 }
179233}
180234
181235// Function to check server status
182236async function getServerStatus ( ) {
183237 try {
238+ debugLog ( 'Getting server status' ) ;
239+
184240 // Get container info
185241 const container = docker . getContainer ( DOCKER_CONTAINER ) ;
186242 const containerInfo = await container . inspect ( ) ;
187243
188244 // Check if container is running
189245 const isRunning = containerInfo . State . Running === true ;
246+ debugLog ( `Container running: ${ isRunning } ` ) ;
190247
191248 let diskUsage = '0%' ;
192249 let cpuUsage = '0%' ;
193250 let memoryUsage = '0MB' ;
194251
195252 if ( isRunning ) {
196253 // Get container stats
254+ debugLog ( 'Getting container stats' ) ;
197255 const stats = await container . stats ( { stream : false } ) ;
198256
199257 // Calculate CPU usage percentage
@@ -206,22 +264,28 @@ async function getServerStatus() {
206264 const memoryUsageBytes = stats . memory_stats . usage ;
207265 memoryUsage = formatMemorySize ( memoryUsageBytes ) ;
208266
267+ debugLog ( `Resources - CPU: ${ cpuUsage } , Memory: ${ memoryUsage } ` ) ;
268+
209269 // For disk usage, we would need to run a command inside the container
210270 // This could be a more complex operation involving checking specific directories
211271 // For simplicity, we'll set this to "N/A" or implement a basic check
212272 diskUsage = 'N/A' ;
213273 }
214274
215- return {
275+ const result = {
216276 status : isRunning ? 'running' : 'stopped' ,
217277 resources : {
218278 cpu : cpuUsage ,
219279 memory : memoryUsage ,
220280 disk : diskUsage
221281 }
222282 } ;
283+
284+ debugLog ( 'Server status result:' , result ) ;
285+ return result ;
223286 } catch ( error ) {
224287 console . error ( 'Error checking server status:' , error ) ;
288+ debugLog ( 'Server status error:' , error ) ;
225289 return {
226290 status : 'unknown' ,
227291 error : error . message
0 commit comments