Skip to content

Commit b900f90

Browse files
authored
fix(server): remove minio alias init (#1066)
1 parent 3ddb36f commit b900f90

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

server/src/storage/minio/minio.service.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,13 @@ import * as cp from 'child_process'
1414
import { promisify } from 'util'
1515
import { MinioCommandExecOutput } from './types'
1616
import { MINIO_COMMON_USER_GROUP } from 'src/constants'
17-
import { RegionService } from 'src/region/region.service'
1817

1918
const exec = promisify(cp.exec)
2019

2120
@Injectable()
2221
export class MinioService {
2322
private readonly logger = new Logger(MinioService.name)
2423

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-
3924
/**
4025
* Create s3 client
4126
* @returns
@@ -64,7 +49,7 @@ export class MinioService {
6449
const target = region.name
6550

6651
const sub_cmd = `admin user add ${target} ${username} ${password}`
67-
return await this.executeMinioClientCmd(sub_cmd)
52+
return await this.executeMinioClientCmd(region, sub_cmd)
6853
}
6954

7055
/**
@@ -75,7 +60,7 @@ export class MinioService {
7560

7661
const target = region.name
7762
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)
7964
if (res.status !== 'success') return null
8065
return res
8166
}
@@ -88,7 +73,7 @@ export class MinioService {
8873

8974
const target = region.name
9075
const sub_cmd = `admin user remove ${target} ${username}`
91-
return await this.executeMinioClientCmd(sub_cmd)
76+
return await this.executeMinioClientCmd(region, sub_cmd)
9277
}
9378

9479
/**
@@ -99,7 +84,7 @@ export class MinioService {
9984

10085
const target = region.name
10186
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)
10388
}
10489

10590
/**
@@ -186,7 +171,7 @@ export class MinioService {
186171

187172
const sub_cmd = `du ${region.name}/${bucket}`
188173

189-
const res = await this.executeMinioClientCmd(sub_cmd)
174+
const res = await this.executeMinioClientCmd(region, sub_cmd)
190175
return res as GetBucketUsedSizeOutput
191176
}
192177

@@ -198,7 +183,7 @@ export class MinioService {
198183

199184
const target = region.name
200185
const sub_cmd = `rb --force ${target}/${bucket}`
201-
return await this.executeMinioClientCmd(sub_cmd)
186+
return await this.executeMinioClientCmd(region, sub_cmd)
202187
}
203188

204189
/**
@@ -232,8 +217,12 @@ export class MinioService {
232217
* Execute minio client shell
233218
*/
234219
private async executeMinioClientCmd(
220+
region: Region,
235221
sub_cmd: string,
236222
): Promise<MinioCommandExecOutput> {
223+
const res = await this.setMinioClientTarget(region)
224+
assert(res.status === 'success', 'failed to set minio client target')
225+
237226
const mc_path = process.env.MINIO_CLIENT_PATH || 'mc'
238227
const cmd = `${mc_path} ${sub_cmd} --json`
239228

@@ -260,9 +249,20 @@ export class MinioService {
260249
const endpoint = conf.controlEndpoint
261250
const target = region.name
262251

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`
264254

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+
}
266266
}
267267

268268
/**

0 commit comments

Comments
 (0)