Skip to content

Commit

Permalink
feat(@142vip/utils): 封装VipDocker工具,修复引用错误 (#314)
Browse files Browse the repository at this point in the history
* feat(@142vip/utils): 封装`VipDocker`工具,修复引用错误

* chore: update
  • Loading branch information
mmdapl authored Jan 20, 2025
1 parent 38871d4 commit 4a9bfed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
67 changes: 42 additions & 25 deletions packages/utils/src/core/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,27 @@ interface BuildImageDockerOptions extends DockerOptions {
memory?: number
}

/**
* docker命令的通用执行器
*/
async function scriptExecutor(command: string) {
try {
const errorCode = await commandStandardExecutor(command)
if (errorCode !== 0) {
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
process.exit(1)
}
}
catch {
// 构建镜像出错时,直接退出
process.exit(1)
}
}

/**
* 判断是否存在镜像
*/
export async function isExistImage(imageName: string) {
async function isExistImage(imageName: string) {
const command = `docker images -q ${imageName}`
const { code, stdout } = await execCommand(command)
return code === 0 && stdout.trim() !== ''
Expand All @@ -31,23 +48,23 @@ export async function isExistImage(imageName: string) {
/**
* 删除Docker镜像
*/
export async function deleteImage(imageName: string) {
async function deleteImage(imageName: string) {
const command = `docker rmi -f ${imageName}`
return await execCommand(command)
}

/**
* 删除虚悬镜像
*/
export async function deletePruneImages() {
async function deletePruneImages() {
const command = 'docker image prune -f'
return await execCommand(command)
}

/**
* 判断容器是否存在
*/
export async function isExistContainer(containerName: string) {
async function isExistContainer(containerName: string) {
const command = `docker ps -aq -f name=${containerName}`
const { code, stdout } = await execCommand(command)

Expand All @@ -57,15 +74,15 @@ export async function isExistContainer(containerName: string) {
/**
* 删除容器
*/
export async function deleteContainer(containerName: string) {
async function deleteContainer(containerName: string) {
const command = `docker rm -f ${containerName}`
return await execCommand(command)
}

/**
* 是否安装docker
*/
export async function isInstallDocker(args?: DockerOptions) {
async function isExistDocker(args?: DockerOptions) {
const command = 'docker -v'
const { code, stdout, stderr } = await execCommand(command)

Expand All @@ -87,7 +104,7 @@ export async function isInstallDocker(args?: DockerOptions) {
/**
* 是否安装docker-compose
*/
export async function isInstallDockerCompose(args?: DockerOptions) {
async function isExistDockerCompose(args?: DockerOptions) {
const command = 'docker-compose -v'
const { code, stdout, stderr } = await execCommand(command)

Expand All @@ -108,17 +125,17 @@ export async function isInstallDockerCompose(args?: DockerOptions) {
/**
* 推送Docker镜像到指定仓库
*/
export async function pushImage(imageName: string) {
async function pushImage(imageName: string) {
const command = `docker push ${imageName}`
await dockerScriptExecutor(command)
await scriptExecutor(command)
}

/**
* 构建Docker镜像
* - 根据tag标记,推送到远程仓库
* - 推送完成后,删除本地镜像
*/
export async function buildImage(args: BuildImageDockerOptions) {
async function buildImage(args: BuildImageDockerOptions) {
// 构建参数
let buildArg = ''
if (args.buildArgs != null) {
Expand All @@ -144,7 +161,7 @@ export async function buildImage(args: BuildImageDockerOptions) {
}
vipLog.log(args.imageName, { startLabel: '构建镜像' })

await dockerScriptExecutor(command)
await scriptExecutor(command)

if (args.push) {
const exist = await isExistImage(args.imageName)
Expand Down Expand Up @@ -176,7 +193,7 @@ interface CreateContainerOptions extends DockerOptions {
/**
* 创建容器
*/
export async function createContainer(args: CreateContainerOptions) {
async function createContainer(args: CreateContainerOptions) {
if (args.networkName && !args.ip) {
console.log('只指定ip,没有指定容器局域网')
process.exit(1)
Expand All @@ -189,18 +206,18 @@ export async function createContainer(args: CreateContainerOptions) {
}

/**
* docker命令的通用执行器
* docker工具
*/
async function dockerScriptExecutor(command: string) {
try {
const errorCode = await commandStandardExecutor(command)
if (errorCode !== 0) {
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
process.exit(1)
}
}
catch {
// 构建镜像出错时,直接退出
process.exit(1)
}
export const VipDocker = {
isExistDocker,
isExistDockerCompose,
isExistImage,
isExistContainer,
deleteImage,
deletePruneImages,
deleteContainer,
pushImage,
buildImage,
createContainer,
scriptExecutor,
}
8 changes: 4 additions & 4 deletions packages/utils/test/docker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { buildImage, isInstallDocker, isInstallDockerCompose } from '@142vip/utils'
import { VipDocker } from '@142vip/utils';

(async () => {
await isInstallDocker({ logger: true })
const exist = await isInstallDockerCompose({ logger: true })
await VipDocker.isExistDocker({ logger: true })
const exist = await VipDocker.isExistDockerCompose({ logger: true })
console.log(111, exist)
await buildImage({
await VipDocker.buildImage({
imageName: 'aaa',
buildArgs: [
['aaa', 123],
Expand Down
4 changes: 2 additions & 2 deletions scripts/bundle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { createRequire } from 'node:module'
import process from 'node:process'
import {
OPEN_SOURCE_ADDRESS,
buildImage,
getRecentGitCommit,
VipDocker,
} from '@142vip/utils'

(async () => {
Expand All @@ -27,7 +27,7 @@ import {
const { hash: gitHash } = await getRecentGitCommit()

// 构建镜像
await buildImage({
await VipDocker.buildImage({
imageName,
buildArgs: [
// 参数中是否包含 --proxy
Expand Down

0 comments on commit 4a9bfed

Please sign in to comment.