@@ -19,10 +19,27 @@ interface BuildImageDockerOptions extends DockerOptions {
1919 memory ?: number
2020}
2121
22+ /**
23+ * docker命令的通用执行器
24+ */
25+ async function scriptExecutor ( command : string ) {
26+ try {
27+ const errorCode = await commandStandardExecutor ( command )
28+ if ( errorCode !== 0 ) {
29+ vipLog . error ( `Error Code: ${ errorCode } ` , { startLabel : 'commandStandardExecutor' } )
30+ process . exit ( 1 )
31+ }
32+ }
33+ catch {
34+ // 构建镜像出错时,直接退出
35+ process . exit ( 1 )
36+ }
37+ }
38+
2239/**
2340 * 判断是否存在镜像
2441 */
25- export async function isExistImage ( imageName : string ) {
42+ async function isExistImage ( imageName : string ) {
2643 const command = `docker images -q ${ imageName } `
2744 const { code, stdout } = await execCommand ( command )
2845 return code === 0 && stdout . trim ( ) !== ''
@@ -31,23 +48,23 @@ export async function isExistImage(imageName: string) {
3148/**
3249 * 删除Docker镜像
3350 */
34- export async function deleteImage ( imageName : string ) {
51+ async function deleteImage ( imageName : string ) {
3552 const command = `docker rmi -f ${ imageName } `
3653 return await execCommand ( command )
3754}
3855
3956/**
4057 * 删除虚悬镜像
4158 */
42- export async function deletePruneImages ( ) {
59+ async function deletePruneImages ( ) {
4360 const command = 'docker image prune -f'
4461 return await execCommand ( command )
4562}
4663
4764/**
4865 * 判断容器是否存在
4966 */
50- export async function isExistContainer ( containerName : string ) {
67+ async function isExistContainer ( containerName : string ) {
5168 const command = `docker ps -aq -f name=${ containerName } `
5269 const { code, stdout } = await execCommand ( command )
5370
@@ -57,15 +74,15 @@ export async function isExistContainer(containerName: string) {
5774/**
5875 * 删除容器
5976 */
60- export async function deleteContainer ( containerName : string ) {
77+ async function deleteContainer ( containerName : string ) {
6178 const command = `docker rm -f ${ containerName } `
6279 return await execCommand ( command )
6380}
6481
6582/**
6683 * 是否安装docker
6784 */
68- export async function isInstallDocker ( args ?: DockerOptions ) {
85+ async function isExistDocker ( args ?: DockerOptions ) {
6986 const command = 'docker -v'
7087 const { code, stdout, stderr } = await execCommand ( command )
7188
@@ -87,7 +104,7 @@ export async function isInstallDocker(args?: DockerOptions) {
87104/**
88105 * 是否安装docker-compose
89106 */
90- export async function isInstallDockerCompose ( args ?: DockerOptions ) {
107+ async function isExistDockerCompose ( args ?: DockerOptions ) {
91108 const command = 'docker-compose -v'
92109 const { code, stdout, stderr } = await execCommand ( command )
93110
@@ -108,17 +125,17 @@ export async function isInstallDockerCompose(args?: DockerOptions) {
108125/**
109126 * 推送Docker镜像到指定仓库
110127 */
111- export async function pushImage ( imageName : string ) {
128+ async function pushImage ( imageName : string ) {
112129 const command = `docker push ${ imageName } `
113- await dockerScriptExecutor ( command )
130+ await scriptExecutor ( command )
114131}
115132
116133/**
117134 * 构建Docker镜像
118135 * - 根据tag标记,推送到远程仓库
119136 * - 推送完成后,删除本地镜像
120137 */
121- export async function buildImage ( args : BuildImageDockerOptions ) {
138+ async function buildImage ( args : BuildImageDockerOptions ) {
122139 // 构建参数
123140 let buildArg = ''
124141 if ( args . buildArgs != null ) {
@@ -144,7 +161,7 @@ export async function buildImage(args: BuildImageDockerOptions) {
144161 }
145162 vipLog . log ( args . imageName , { startLabel : '构建镜像' } )
146163
147- await dockerScriptExecutor ( command )
164+ await scriptExecutor ( command )
148165
149166 if ( args . push ) {
150167 const exist = await isExistImage ( args . imageName )
@@ -176,7 +193,7 @@ interface CreateContainerOptions extends DockerOptions {
176193/**
177194 * 创建容器
178195 */
179- export async function createContainer ( args : CreateContainerOptions ) {
196+ async function createContainer ( args : CreateContainerOptions ) {
180197 if ( args . networkName && ! args . ip ) {
181198 console . log ( '只指定ip,没有指定容器局域网' )
182199 process . exit ( 1 )
@@ -189,18 +206,18 @@ export async function createContainer(args: CreateContainerOptions) {
189206}
190207
191208/**
192- * docker命令的通用执行器
209+ * docker工具
193210 */
194- async function dockerScriptExecutor ( command : string ) {
195- try {
196- const errorCode = await commandStandardExecutor ( command )
197- if ( errorCode !== 0 ) {
198- vipLog . error ( `Error Code: ${ errorCode } ` , { startLabel : 'commandStandardExecutor' } )
199- process . exit ( 1 )
200- }
201- }
202- catch {
203- // 构建镜像出错时,直接退出
204- process . exit ( 1 )
205- }
211+ export const VipDocker = {
212+ isExistDocker ,
213+ isExistDockerCompose ,
214+ isExistImage ,
215+ isExistContainer ,
216+ deleteImage ,
217+ deletePruneImages ,
218+ deleteContainer ,
219+ pushImage ,
220+ buildImage ,
221+ createContainer ,
222+ scriptExecutor ,
206223}
0 commit comments