[ Agent ] [ Base ] - nfs-server nfs
This commit is contained in:
@@ -282,12 +282,10 @@ func (op *AgentOsOperator) deployNFS(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 创建目录修改权限
|
||||
if !PureResultSingleExecuteBatch([][]string{
|
||||
{"mkdir", "-p", nfsDataPath},
|
||||
{"chmod", "777", nfsDataPath},
|
||||
}) {
|
||||
result = append(result, "目录不存在", nfsDataPath)
|
||||
return false, result
|
||||
if !BasicFolderExists(nfsDataPath) {
|
||||
return false, []string{
|
||||
fmt.Sprintf("[deployNFS] - folder of [ %s ] not exist ! nfs not installed ", nfsDataPath),
|
||||
}
|
||||
}
|
||||
|
||||
// 下载模板文件
|
||||
|
||||
@@ -77,6 +77,12 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
|
||||
case "installHelm":
|
||||
resultOk, errorLog = op.installHelmExec()
|
||||
break
|
||||
case "installNfsOnline":
|
||||
resultOk, errorLog = op.installNfsOnlineExec()
|
||||
break
|
||||
case "installNfsServerOnline":
|
||||
resultOk, errorLog = op.installNfsServerOnlineExec()
|
||||
break
|
||||
case "installHarbor":
|
||||
resultOk, errorLog = op.installHarborExec()
|
||||
break
|
||||
@@ -989,6 +995,80 @@ func (op *AgentOsOperator) modifyDockerConfigExec(args []string) (bool, []string
|
||||
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 {
|
||||
|
||||
installHarborFunc := [][]string{
|
||||
|
||||
@@ -115,6 +115,30 @@ func BasicGrepItemInFile(item string, fileName string) bool {
|
||||
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 基础替换命令
|
||||
func BasicReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
@@ -371,6 +395,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
|
||||
// BasicAppendSourceToFile 将源文件的内容添加到目标文件,使用golang标准库完成,跨平台、安全性更强
|
||||
func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
// 打开源文件
|
||||
source, err := os.Open(sourceFile)
|
||||
if err != nil {
|
||||
@@ -396,3 +421,37 @@ func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -230,3 +230,41 @@ func collectOutput(r io.Reader, resultSlice []string) []string {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user