-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@142vip/utils): 增加
docker
相关api
方法
- Loading branch information
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { execCommand } from './exec' | ||
import { VipLogger } from './logger' | ||
import { vipSymbols } from './color' | ||
|
||
interface DockerOptions { | ||
logger?: boolean | ||
} | ||
|
||
interface BuildImageDockerOptions { | ||
imageName: string | ||
buildArgs?: [string, string][] | ||
} | ||
|
||
/** | ||
* 判断是否存在镜像 | ||
* @param imageName | ||
*/ | ||
export async function isExistImage(imageName: string) { | ||
const command = `docker images -q ${imageName}` | ||
const { code, stdout } = await execCommand(command) | ||
return code === 0 && stdout.trim() !== '' | ||
} | ||
|
||
/** | ||
* 删除Docker镜像 | ||
* @param imageName | ||
*/ | ||
export async function deleteImage(imageName: string) { | ||
const command = `docker rmi -f ${imageName}` | ||
return await execCommand(command) | ||
} | ||
|
||
/** | ||
* 删除虚悬镜像 | ||
*/ | ||
export async function deletePruneImages() { | ||
const command = 'docker image prune -f' | ||
return await execCommand(command) | ||
} | ||
|
||
export async function isExistContainer(containerName: string) { | ||
const command = `docker ps -aq -f name=${containerName}` | ||
const { code, stdout } = await execCommand(command) | ||
|
||
return code === 0 && stdout.trim() !== '' | ||
} | ||
|
||
/** | ||
* 删除容器 | ||
* @param containerName | ||
*/ | ||
export async function deleteContainer(containerName: string) { | ||
const command = `docker rm -f ${containerName}` | ||
return await execCommand(command) | ||
} | ||
|
||
/** | ||
* 是否安装docker | ||
*/ | ||
export async function isInstallDocker(args?: DockerOptions) { | ||
const command = 'docker -v' | ||
const { code, stdout, stderr } = await execCommand(command) | ||
|
||
// 打印日志 | ||
if (args?.logger) { | ||
const vipLog = new VipLogger() | ||
if (code === 0) { | ||
vipLog.log(`检测到docker,版本信息:\n`, { startLabel: vipSymbols.success }) | ||
vipLog.log(stdout) | ||
} | ||
else { | ||
vipLog.log(`未检测到docker,请先安装!!\n`, { startLabel: vipSymbols.error }) | ||
vipLog.error(stderr) | ||
} | ||
} | ||
return code === 0 && stdout.includes('Docker') | ||
} | ||
|
||
/** | ||
* 是否安装docker-compose | ||
*/ | ||
export async function isInstallDockerCompose(args?: DockerOptions) { | ||
const command = 'docker-compose -v' | ||
const { code, stdout, stderr } = await execCommand(command) | ||
|
||
// 打印日志 | ||
if (args?.logger) { | ||
const vipLog = new VipLogger() | ||
if (code === 0) { | ||
vipLog.log(`检测到docker-compose,版本信息:\n`, { startLabel: vipSymbols.success }) | ||
vipLog.log(stdout) | ||
} | ||
else { | ||
vipLog.log(`未检测到docker-compose,请先安装!!\n`, { startLabel: vipSymbols.error }) | ||
vipLog.error(stderr) | ||
} | ||
} | ||
return code === 0 && stdout.includes('Docker Compose') | ||
} | ||
|
||
/** | ||
* 推送Docker镜像到指定仓库 | ||
* @param imageName | ||
*/ | ||
export async function pushImage(imageName: string) { | ||
const command = `docker push ${imageName}` | ||
return await execCommand(command) | ||
} | ||
|
||
/** | ||
* 构建Docker镜像 | ||
* @param args | ||
*/ | ||
export async function buildImage(args: BuildImageDockerOptions) { | ||
// 构建参数 | ||
let buildArg = '' | ||
if (args.buildArgs != null) { | ||
buildArg = args.buildArgs.map(arg => `--build-arg ${arg[0]}=${arg[1]}`).join(' ') | ||
} | ||
const command = `docker build ${buildArg} -t ${args.imageName} .` | ||
console.log(111, command) | ||
return await execCommand(command) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters