优化Harbor、Docker和Zsh安装流程,改进命令执行和文件操作
- 重构Harbor安装命令,改进容器检查和启动逻辑 - 修改Docker和DockerCompose安装方法,优化文件移动和权限设置 - 更新Zsh插件安装命令,简化git克隆过程 - 调整UnzipFile工具方法,支持更多压缩文件类型 - 修正Docker Daemon配置文件中的安装节标签 - 更新测试脚本,简化文件上传流程
This commit is contained in:
@@ -220,6 +220,7 @@ func addBaseSubcommands(cmd *cobra.Command) {
|
|||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
dockerCmd,
|
dockerCmd,
|
||||||
dockerComposeCmd,
|
dockerComposeCmd,
|
||||||
|
harborCmd,
|
||||||
swapCmd,
|
swapCmd,
|
||||||
commonToolsInstall,
|
commonToolsInstall,
|
||||||
selinuxCmd,
|
selinuxCmd,
|
||||||
@@ -246,10 +247,13 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解压harbor
|
// 解压harbor
|
||||||
utils.UnzipFile(harborLocalInstallPath, "/root/wdd/harbor")
|
utils.UnzipFile(harborLocalInstallPath, "/root/wdd/")
|
||||||
|
|
||||||
// 获取本机的内网IPv4地址
|
// 获取本机的内网IPv4地址
|
||||||
configCache := config.ConfigCache
|
configCache := config.ConfigCache
|
||||||
|
if len(configCache.Agent.Network.Interfaces) == 0 {
|
||||||
|
log.Error("没有获取到本机的ipv4地址,无法安装harbor! 请执行 info all !")
|
||||||
|
}
|
||||||
ip := configCache.Agent.Network.Interfaces[0].IPv4
|
ip := configCache.Agent.Network.Interfaces[0].IPv4
|
||||||
|
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
@@ -266,11 +270,9 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 进入harbor目录
|
|
||||||
os.Chdir("/root/wdd/harbor")
|
|
||||||
|
|
||||||
// 运行install.sh
|
// 运行install.sh
|
||||||
ok, log1 := op.HardCodeCommandExecutor("bash install.sh")
|
op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"})
|
||||||
|
ok, log1 := op.SingleLineCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"})
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("安装harbor失败: %s", log1)
|
log.Error("安装harbor失败: %s", log1)
|
||||||
return
|
return
|
||||||
@@ -314,18 +316,16 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 进入harbor目录
|
|
||||||
os.Chdir("/root/wdd/harbor")
|
|
||||||
|
|
||||||
// 启动harbor
|
// 启动harbor
|
||||||
ok, log1 := op.HardCodeCommandExecutor("docker-compose up -d")
|
op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"})
|
||||||
|
ok, log1 := op.SingleLineCommandExecutor([]string{"docker-compose", "up", "-d"})
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("启动harbor失败: %s", log1)
|
log.Error("启动harbor失败: %s", log1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待5秒
|
// 等待10秒
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
// 检查harbor是否启动成功
|
// 检查harbor是否启动成功
|
||||||
if !checkHarborIsRunning() {
|
if !checkHarborIsRunning() {
|
||||||
@@ -357,7 +357,7 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
|
|||||||
os.Chdir("/root/wdd/harbor")
|
os.Chdir("/root/wdd/harbor")
|
||||||
|
|
||||||
// 停止harbor
|
// 停止harbor
|
||||||
ok, log1 := op.HardCodeCommandExecutor("docker-compose down")
|
ok, log1 := op.SingleLineCommandExecutor([]string{"docker-compose", "down"})
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("停止harbor失败: %s", log1)
|
log.Error("停止harbor失败: %s", log1)
|
||||||
return
|
return
|
||||||
@@ -372,14 +372,30 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
|
|||||||
harborCmd.AddCommand(harborInstallCmd, harborUninstallCmd, harborStartCmd, harborStopCmd)
|
harborCmd.AddCommand(harborInstallCmd, harborUninstallCmd, harborStartCmd, harborStopCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkHarborIsRunning 检查harbor是否运行
|
// checkHarborIsRunning 检查harbor是否运行 如果存在Exit 或者 Restarting 则表示有错误!
|
||||||
func checkHarborIsRunning() bool {
|
func checkHarborIsRunning() bool {
|
||||||
ok, log2 := op.HardCodeCommandExecutor("docker ps -a | grep goharbor/ | grep -E 'Exit|Restarting' | wc -l")
|
_, resultLog := op.PipeLineCommandExecutor([][]string{
|
||||||
if !ok {
|
{
|
||||||
log.Error("查看harbor容器是否存在失败: %s", log2)
|
"docker",
|
||||||
return false
|
"ps",
|
||||||
}
|
"-a",
|
||||||
if strings.TrimSpace(log2[0]) != "0" {
|
},
|
||||||
|
{
|
||||||
|
"grep",
|
||||||
|
"goharbor/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"grep",
|
||||||
|
"-E",
|
||||||
|
"'Exit|Restarting'",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wc",
|
||||||
|
"-l",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if strings.TrimSpace(resultLog[0]) != "0" {
|
||||||
log.Error("harbor启动失败! 请检查日志!")
|
log.Error("harbor启动失败! 请检查日志!")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -605,7 +621,21 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
// apt-cache madison docker-ce | grep 20.10.20 | awk '{print$3}'
|
// apt-cache madison docker-ce | grep 20.10.20 | awk '{print$3}'
|
||||||
|
|
||||||
// get by method
|
// get by method
|
||||||
ok, log4 := op.HardCodeCommandExecutor("apt-cache madison docker-ce | grep 20.10.20 | awk '{print$3}'")
|
ok, log4 := op.PipeLineCommandExecutor([][]string{
|
||||||
|
{
|
||||||
|
"apt-cache",
|
||||||
|
"madison",
|
||||||
|
"docker-ce",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"grep",
|
||||||
|
"20.10.20",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"awk",
|
||||||
|
"'{print$3}'",
|
||||||
|
},
|
||||||
|
})
|
||||||
if ok && log4 != nil && len(log4) > 0 {
|
if ok && log4 != nil && len(log4) > 0 {
|
||||||
specificDockerVersion = strings.TrimSpace(log4[0])
|
specificDockerVersion = strings.TrimSpace(log4[0])
|
||||||
fmt.Println("get docker version from online => " + specificDockerVersion)
|
fmt.Println("get docker version from online => " + specificDockerVersion)
|
||||||
@@ -706,14 +736,7 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解压文件
|
// 解压文件
|
||||||
utils.UnzipFile(dockerLocalInstallPath, "/root/wdd")
|
utils.UnzipFile(dockerLocalInstallPath, "/root/wdd/")
|
||||||
|
|
||||||
// 安装docker
|
|
||||||
err := utils.MoveFolerToAnother("/root/wdd/docker", "/usr/bin")
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to move Docker binaries: %s", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置权限
|
// 设置权限
|
||||||
dockerBinList := []string{
|
dockerBinList := []string{
|
||||||
@@ -728,11 +751,22 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
"containerd-shim-runc-v2",
|
"containerd-shim-runc-v2",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 删除旧的docker 二进制文件
|
||||||
for _, bin := range dockerBinList {
|
for _, bin := range dockerBinList {
|
||||||
ok, resultLog := op.HardCodeCommandExecutor("chmod 777 /usr/bin/" + bin)
|
utils.RemoveFile("/usr/bin/" + bin)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 安装docker
|
||||||
|
err := utils.MoveFolerToAnother("/root/wdd/docker/", "/usr/bin")
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to move Docker binaries: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, bin := range dockerBinList {
|
||||||
|
ok, resultLog := op.SingleLineCommandExecutor([]string{"chmod", "777", "/usr/bin/" + bin})
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("Failed to set permissions for Docker binaries: %s", resultLog)
|
log.Error("Failed to set permissions for Docker binaries: %s", resultLog)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -910,7 +944,7 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
|
|||||||
op.SingleLineCommandExecutor(linkCmd)
|
op.SingleLineCommandExecutor(linkCmd)
|
||||||
|
|
||||||
// 验证安装
|
// 验证安装
|
||||||
verifyCmd := []string{"docker-compose", "--version"}
|
verifyCmd := []string{"/usr/local/bin/docker-compose", "version"}
|
||||||
ok, resultLog = op.SingleLineCommandExecutor(verifyCmd)
|
ok, resultLog = op.SingleLineCommandExecutor(verifyCmd)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("Docker Compose 安装验证失败: %s", resultLog)
|
log.Error("Docker Compose 安装验证失败: %s", resultLog)
|
||||||
@@ -950,7 +984,7 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// move file to /usr/local/bin
|
// move file to /usr/local/bin
|
||||||
err := utils.MoveFileToAnother(dockerComposeLocalInstallPath, "/usr/local/bin")
|
err := utils.MoveFileToAnother(dockerComposeLocalInstallPath, "/usr/local/bin/docker-compose")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to move Docker Compose binaries: %s", err.Error())
|
log.Error("Failed to move Docker Compose binaries: %s", err.Error())
|
||||||
return
|
return
|
||||||
@@ -964,6 +998,18 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 创建软链接
|
||||||
|
linkCmd := []string{"ln", "-sf", "/usr/local/bin/docker-compose", "/usr/bin/docker-compose"}
|
||||||
|
op.SingleLineCommandExecutor(linkCmd)
|
||||||
|
|
||||||
|
// 验证安装
|
||||||
|
verifyCmd := []string{"/usr/local/bin/docker-compose", "version"}
|
||||||
|
ok, resultLog = op.SingleLineCommandExecutor(verifyCmd)
|
||||||
|
if !ok {
|
||||||
|
log.Error("Docker Compose 安装验证失败: %s", resultLog)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Info("Docker Compose installed successfully from local file!")
|
log.Info("Docker Compose installed successfully from local file!")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,13 +76,13 @@ func addZshSubcommands(cmd *cobra.Command) {
|
|||||||
log.Info("安装zsh完成, 开始安装插件!")
|
log.Info("安装zsh完成, 开始安装插件!")
|
||||||
|
|
||||||
pluginsHardCodeUrl := []string{
|
pluginsHardCodeUrl := []string{
|
||||||
"/usr/bin/git clone https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
"https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
||||||
"/usr/bin/git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
"https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsHardCodeOutsideUrl := []string{
|
pluginsHardCodeOutsideUrl := []string{
|
||||||
"/usr/bin/git clone https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
"https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
||||||
"/usr/bin/git clone https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
"https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsListUrl := []string{
|
pluginsListUrl := []string{
|
||||||
@@ -104,13 +104,13 @@ func addZshSubcommands(cmd *cobra.Command) {
|
|||||||
if config.ConfigCache.Agent.Network.Internet == 7 {
|
if config.ConfigCache.Agent.Network.Internet == 7 {
|
||||||
// 执行硬编码命令, 安装插件
|
// 执行硬编码命令, 安装插件
|
||||||
for _, command := range pluginsHardCodeUrl {
|
for _, command := range pluginsHardCodeUrl {
|
||||||
op.HardCodeCommandExecutor(command)
|
op.SingleLineCommandExecutor([]string{"git", "clone", command})
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 执行硬编码命令, 安装插件 国外
|
// 执行硬编码命令, 安装插件 国外
|
||||||
for _, command := range pluginsHardCodeOutsideUrl {
|
for _, command := range pluginsHardCodeOutsideUrl {
|
||||||
op.HardCodeCommandExecutor(command)
|
op.SingleLineCommandExecutor([]string{"git", "clone", command})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 下载插件
|
// 下载插件
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ LimitNOFILE=infinity
|
|||||||
TasksMax=infinity
|
TasksMax=infinity
|
||||||
OOMScoreAdjust=-999
|
OOMScoreAdjust=-999
|
||||||
|
|
||||||
[installPrefix]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ SocketMode=0660
|
|||||||
SocketUser=root
|
SocketUser=root
|
||||||
SocketGroup=docker
|
SocketGroup=docker
|
||||||
|
|
||||||
[installPrefix]
|
[Install]
|
||||||
WantedBy=sockets.target
|
WantedBy=sockets.target
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -107,7 +107,7 @@ Delegate=yes
|
|||||||
KillMode=process
|
KillMode=process
|
||||||
OOMScoreAdjust=-500
|
OOMScoreAdjust=-500
|
||||||
|
|
||||||
[installPrefix]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ Set-Location "C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\tes
|
|||||||
|
|
||||||
# 删除上面存在的旧的内容
|
# 删除上面存在的旧的内容
|
||||||
mc.exe rm oracle-seoul-2/seoul-2/agent-wdd_linux_amd64
|
mc.exe rm oracle-seoul-2/seoul-2/agent-wdd_linux_amd64
|
||||||
mc.exe rm oracle-seoul-2/seoul-2/test-shell.sh
|
# mc.exe rm oracle-seoul-2/seoul-2/test-shell.sh
|
||||||
|
|
||||||
|
|
||||||
# 上传文件
|
# 上传文件
|
||||||
mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\build\agent-wdd_linux_amd64 oracle-seoul-2/seoul-2/
|
mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\build\agent-wdd_linux_amd64 oracle-seoul-2/seoul-2/
|
||||||
mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\test\test-shell.sh oracle-seoul-2/seoul-2/
|
# mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\test\test-shell.sh oracle-seoul-2/seoul-2/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnzipFile 解压文件, 支持zip和tar.gz
|
// UnzipFile 解压文件, 支持zip tgz tar.gz tar
|
||||||
func UnzipFile(zipFile string, targetFolder string) {
|
func UnzipFile(zipFile string, targetFolder string) {
|
||||||
|
log.Info("解压文件: %s, 到: %s", zipFile, targetFolder)
|
||||||
|
|
||||||
// 检查文件扩展名以确定解压缩方法
|
// 检查文件扩展名以确定解压缩方法
|
||||||
fileExt := filepath.Ext(zipFile)
|
fileExt := filepath.Ext(zipFile)
|
||||||
switch fileExt {
|
switch fileExt {
|
||||||
|
|||||||
Reference in New Issue
Block a user