@@ -14,28 +14,13 @@ import * as cp from 'child_process'
14
14
import { promisify } from 'util'
15
15
import { MinioCommandExecOutput } from './types'
16
16
import { MINIO_COMMON_USER_GROUP } from 'src/constants'
17
- import { RegionService } from 'src/region/region.service'
18
17
19
18
const exec = promisify ( cp . exec )
20
19
21
20
@Injectable ( )
22
21
export class MinioService {
23
22
private readonly logger = new Logger ( MinioService . name )
24
23
25
- constructor ( private readonly regionService : RegionService ) {
26
- this . regionService . findAll ( ) . then ( async ( regions ) => {
27
- for ( const region of regions ) {
28
- const res = await this . setMinioClientTarget ( region )
29
- if ( res . status === 'success' ) {
30
- this . logger . log ( 'minio alias init - ' + region . name + ' success' )
31
- } else {
32
- this . logger . log ( 'minio alias init ' + region . name + ' failed' )
33
- this . logger . debug ( res )
34
- }
35
- }
36
- } )
37
- }
38
-
39
24
/**
40
25
* Create s3 client
41
26
* @returns
@@ -64,7 +49,7 @@ export class MinioService {
64
49
const target = region . name
65
50
66
51
const sub_cmd = `admin user add ${ target } ${ username } ${ password } `
67
- return await this . executeMinioClientCmd ( sub_cmd )
52
+ return await this . executeMinioClientCmd ( region , sub_cmd )
68
53
}
69
54
70
55
/**
@@ -75,7 +60,7 @@ export class MinioService {
75
60
76
61
const target = region . name
77
62
const sub_cmd = `admin user info ${ target } ${ username } `
78
- const res = await this . executeMinioClientCmd ( sub_cmd )
63
+ const res = await this . executeMinioClientCmd ( region , sub_cmd )
79
64
if ( res . status !== 'success' ) return null
80
65
return res
81
66
}
@@ -88,7 +73,7 @@ export class MinioService {
88
73
89
74
const target = region . name
90
75
const sub_cmd = `admin user remove ${ target } ${ username } `
91
- return await this . executeMinioClientCmd ( sub_cmd )
76
+ return await this . executeMinioClientCmd ( region , sub_cmd )
92
77
}
93
78
94
79
/**
@@ -99,7 +84,7 @@ export class MinioService {
99
84
100
85
const target = region . name
101
86
const sub_cmd = `admin group add ${ target } ${ MINIO_COMMON_USER_GROUP } ${ username } `
102
- return await this . executeMinioClientCmd ( sub_cmd )
87
+ return await this . executeMinioClientCmd ( region , sub_cmd )
103
88
}
104
89
105
90
/**
@@ -186,7 +171,7 @@ export class MinioService {
186
171
187
172
const sub_cmd = `du ${ region . name } /${ bucket } `
188
173
189
- const res = await this . executeMinioClientCmd ( sub_cmd )
174
+ const res = await this . executeMinioClientCmd ( region , sub_cmd )
190
175
return res as GetBucketUsedSizeOutput
191
176
}
192
177
@@ -198,7 +183,7 @@ export class MinioService {
198
183
199
184
const target = region . name
200
185
const sub_cmd = `rb --force ${ target } /${ bucket } `
201
- return await this . executeMinioClientCmd ( sub_cmd )
186
+ return await this . executeMinioClientCmd ( region , sub_cmd )
202
187
}
203
188
204
189
/**
@@ -232,8 +217,12 @@ export class MinioService {
232
217
* Execute minio client shell
233
218
*/
234
219
private async executeMinioClientCmd (
220
+ region : Region ,
235
221
sub_cmd : string ,
236
222
) : Promise < MinioCommandExecOutput > {
223
+ const res = await this . setMinioClientTarget ( region )
224
+ assert ( res . status === 'success' , 'failed to set minio client target' )
225
+
237
226
const mc_path = process . env . MINIO_CLIENT_PATH || 'mc'
238
227
const cmd = `${ mc_path } ${ sub_cmd } --json`
239
228
@@ -260,9 +249,20 @@ export class MinioService {
260
249
const endpoint = conf . controlEndpoint
261
250
const target = region . name
262
251
263
- const cmd = `alias set ${ target } ${ endpoint } ${ access_key } ${ access_secret } `
252
+ const mc_path = process . env . MINIO_CLIENT_PATH || 'mc'
253
+ const cmd = `${ mc_path } alias set ${ target } ${ endpoint } ${ access_key } ${ access_secret } --json`
264
254
265
- return await this . executeMinioClientCmd ( cmd )
255
+ try {
256
+ const { stdout } = await exec ( cmd )
257
+ const json : MinioCommandExecOutput = JSON . parse ( stdout )
258
+ return json
259
+ } catch ( error ) {
260
+ this . logger . error ( `failed to exec command: {${ cmd } }` , error )
261
+ return {
262
+ status : 'error' ,
263
+ error : error ,
264
+ }
265
+ }
266
266
}
267
267
268
268
/**
0 commit comments