[ Agent ] [ Base ] - nfs-server nfs

This commit is contained in:
zeaslity
2023-11-21 17:13:31 +08:00
parent 48e8a6c951
commit 55d0772d50
5 changed files with 191 additions and 6 deletions

View File

@@ -282,12 +282,10 @@ func (op *AgentOsOperator) deployNFS(funcArgs []string) (bool, []string) {
return false, result return false, result
} }
// 创建目录修改权限 // 创建目录修改权限
if !PureResultSingleExecuteBatch([][]string{ if !BasicFolderExists(nfsDataPath) {
{"mkdir", "-p", nfsDataPath}, return false, []string{
{"chmod", "777", nfsDataPath}, fmt.Sprintf("[deployNFS] - folder of [ %s ] not exist ! nfs not installed ", nfsDataPath),
}) { }
result = append(result, "目录不存在", nfsDataPath)
return false, result
} }
// 下载模板文件 // 下载模板文件

View File

@@ -77,6 +77,12 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
case "installHelm": case "installHelm":
resultOk, errorLog = op.installHelmExec() resultOk, errorLog = op.installHelmExec()
break break
case "installNfsOnline":
resultOk, errorLog = op.installNfsOnlineExec()
break
case "installNfsServerOnline":
resultOk, errorLog = op.installNfsServerOnlineExec()
break
case "installHarbor": case "installHarbor":
resultOk, errorLog = op.installHarborExec() resultOk, errorLog = op.installHarborExec()
break break
@@ -989,6 +995,80 @@ func (op *AgentOsOperator) modifyDockerConfigExec(args []string) (bool, []string
return true, nil return true, nil
} }
func (op *AgentOsOperator) installNfsOnlineExec() (bool, []string) {
if op.IsOsTypeUbuntu {
// ubuntu
installOk, installLog := BasicInstallSoftwares(op.InstallCommandPrefix, false,
"nfs-common", "nfs-client", "nfs")
if !installOk {
return false, installLog
}
ok, resultLog := BasicSystemdUp("nfs")
if !ok {
return false, append(resultLog, "[installNfsOnlineExec] - start nfs-common.service failed !")
}
}
return true, nil
}
func (op *AgentOsOperator) installNfsServerOnlineExec() (bool, []string) {
// os
if op.IsOsTypeUbuntu {
// ubuntu
installOk, installLog := BasicInstallSoftwares(op.InstallCommandPrefix, true, "nfs-kernel-server",
"nfs-common")
if !installOk {
return false, installLog
}
if !PureResultSingleExecuteBatch([][]string{
{"mkdir", "-p", nfsDataPath},
{"chmod", "777", nfsDataPath},
}) {
return false, []string{
"[installNfsServerOnlineExec]- create nfs data folder failed !",
}
}
if !BasicGrepItemInFile(nfsDataPath, "/etc/exports") {
log.DebugF("[installNfsServerOnlineExec]- add nfs path to /etc/exports !")
nfsExport := nfsDataPath + " *(rw,no_root_squash,no_all_squash,sync)"
if !BasicAppendContentToFile(nfsExport, "/etc/exports") {
return false, []string{
"[installNfsServerOnlineExec]- add nfs path to /etc/exports failed !",
}
}
}
// restart nfs-server
AllCompleteExecutor([][]string{
{
"systemctl",
"restart",
"nfs-server",
},
{
"systemctl",
"restart",
"nf",
},
})
ok, i := HardCodeCommandExecutor("rpcinfo -p localhost")
if !ok {
return false, append(i,
"installNfsServerOnlineExec] - rpc info error !",
"please check nfs server installation")
}
}
return true, nil
}
func (op *AgentOsOperator) installHarbor() [][]string { func (op *AgentOsOperator) installHarbor() [][]string {
installHarborFunc := [][]string{ installHarborFunc := [][]string{

View File

@@ -115,6 +115,30 @@ func BasicGrepItemInFile(item string, fileName string) bool {
return false return false
} }
func BasicInstallSoftwares(installPrefix []string, isStrict bool, softwares ...string) (bool, []string) {
var installLog []string
for _, software := range softwares {
log.DebugF("[BasicInstallSoftwares] - going to install [ %s ]", software)
if !PureResultSingleExecute(append(installPrefix, software)) {
failedInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install failed !", software)
installLog = append(installLog, failedInstall)
if isStrict {
return false, installLog
}
}
successInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install success !", software)
installLog = append(installLog, successInstall)
}
return true, installLog
}
// BasicReplace 基础替换命令 // BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool { func BasicReplace(filename string, origin string, replace string) bool {
@@ -371,6 +395,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
// BasicAppendSourceToFile 将源文件的内容添加到目标文件使用golang标准库完成跨平台、安全性更强 // BasicAppendSourceToFile 将源文件的内容添加到目标文件使用golang标准库完成跨平台、安全性更强
func BasicAppendSourceToFile(sourceFile, targetFile string) bool { func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
// 打开源文件 // 打开源文件
source, err := os.Open(sourceFile) source, err := os.Open(sourceFile)
if err != nil { if err != nil {
@@ -396,3 +421,37 @@ func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
return true return true
} }
// BasicAppendContentToFile 向目标文件中追加写入一些内容
func BasicAppendContentToFile(content string, targetFile string) bool {
// 打开文件用于追加。如果文件不存在,将会创建一个新文件。
file, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[BasicAppendContentToFile] - Error opening file: %s", err.Error())
return false
}
defer file.Close() // 确保文件最终被关闭
// 写入内容到文件
if _, err := file.WriteString(content); err != nil {
log.ErrorF("[BasicAppendContentToFile] - Error writing to file: %s", err.Error())
return false
}
return true
}
// BasicAppendNullToFile 清空一个文件
func BasicAppendNullToFile(targetFile string) bool {
// 使用os.O_TRUNC清空文件内容
file, err := os.OpenFile(targetFile, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[BasicAppendNullToFile] - Error opening file: %s", err.Error())
return false
}
defer file.Close() // 确保在函数退出前关闭文件
return true
}

View File

@@ -230,3 +230,41 @@ func collectOutput(r io.Reader, resultSlice []string) []string {
return resultSlice return resultSlice
} }
// HardCodeCommandExecutor 执行硬编码的shell命令如 echo sda > sdasd ; rpcinfo -p localhost 等
func HardCodeCommandExecutor(hardCodeCommand string) (bool, []string) {
// result
var resultSlice []string
resultOk := true
cmd := exec.Command("sh", "-c", hardCodeCommand)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.DebugF("hard code command %v stdout error => %s", hardCodeCommand, err)
resultOk = false
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.DebugF("hard code command %v stderr error => %s", hardCodeCommand, err)
resultOk = false
}
if err := cmd.Start(); err != nil {
log.DebugF("hard code command %v runtime error => %v", hardCodeCommand, err)
resultOk = false
}
// 收集错误或者
resultSlice = collectOutput(stdout, resultSlice)
resultSlice = collectOutput(stderr, resultSlice)
if err := cmd.Wait(); err != nil {
log.DebugF("hard code command %v result error => %v", hardCodeCommand, err)
resultOk = false
}
log.DebugF("hard code command of %v result are => %v", hardCodeCommand, resultSlice)
return resultOk, resultSlice
}

View File

@@ -49,6 +49,16 @@ public enum BaseFunctionEnum {
"安装Harbor, 默认为2.9.0版本" "安装Harbor, 默认为2.9.0版本"
), ),
INSTALL_NFS_ONLINE(
"installNfsOnline",
"安装nfs-client online"
),
INSTALL_NFS_SERVER_ONLINE(
"installNfsServerOnline",
"安装nfs-server online"
),
INSTALL_CHRONY( INSTALL_CHRONY(
"installChrony", "installChrony",
"安装Chrony服务器" "安装Chrony服务器"