From 81363abf201446b56353d33b9b173876e197b586 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 8 Nov 2023 11:33:18 +0800 Subject: [PATCH] =?UTF-8?q?[=20Agent=20]=20[=20Executor=20]=20-=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9Echrony=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent-go/executor/BaseFunction.go | 124 ++++++++++++++++-- .../wdd/func/auto/beans/BaseFunctionEnum.java | 4 + .../func/auto/service/FuncServiceImpl.java | 3 +- 3 files changed, 122 insertions(+), 9 deletions(-) diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index 94f23be..676d9d0 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -77,6 +77,9 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) (bool, case "installHarbor": resultOk, errorLog = op.installHarborExec() break + case "installChrony": + resultOk, errorLog = op.installChronyExec() + break case "chronyToPublicNTP": resultOk, errorLog = op.chronyToPublicNTPExec() break @@ -254,7 +257,7 @@ func (op *AgentOsOperator) enableSwap() [][]string { } func (op *AgentOsOperator) enableSwapExec() (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) disableSwap() [][]string { @@ -855,7 +858,7 @@ func (op *AgentOsOperator) installHelm() [][]string { } func (op *AgentOsOperator) installHelmExec() (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { @@ -891,7 +894,7 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { } func (op *AgentOsOperator) modifyDockerConfigExec(args []string) (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) installHarbor() [][]string { @@ -961,7 +964,7 @@ func (op *AgentOsOperator) installHarbor() [][]string { } func (op *AgentOsOperator) installHarborExec() (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) chronyToPublicNTP() [][]string { @@ -1048,7 +1051,112 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string { } func (op *AgentOsOperator) chronyToPublicNTPExec() (bool, []string) { - return false, nil + + var chronyFile string + if op.IsOsTypeUbuntu { + chronyFile = "/etc/chrony/chrony.conf" + } else { + chronyFile = "/etc/chrony.conf" + } + + ok, resultLog := AllCompleteExecutor([][]string{ + { + "sed", + "-i", + "$ a allow all", + chronyFile, + }, + { + "sed", + "-i", + "s/pool ntp.ubuntu.com iburst/server ntp2.aliyun.com iburst/g", + chronyFile, + }, + { + "systemctl", + "restart", + "chronyd", + }, + { + "timedatectl", + "set-timezone", + "Asia/Shanghai", + }, + { + "sleep", + "2", + }}) + + if !ok { + return false, resultLog + } + + _, l := AllCompleteExecutor([][]string{ + { + "chronyc", + "-n", + "sources", + "-v", + }, + { + "chronyc", + "tracking", + }, + { + "chronyc", + "clients", + }, + }) + + // check chrony + return true, l +} + +// installChronyExec make sure chrony is installed +func (op *AgentOsOperator) installChronyExec() (bool, []string) { + + // uninstall systemd-timesyncd.service + ok, resultLog := BasicSystemdShutdown("systemd-timesyncd.service") + if !ok { + return false, resultLog + } + + //resultOk, l := AllCommandExecutor( + // append( + // op.RemoveCommandPrefix, + // "systemd-timesyncd.service", + // ), + //) + //if !resultOk{ + // return false, l + //} + + // install chrony + resultOk, l := AllCommandExecutor( + append( + op.InstallCommandPrefix, + "chrony", + ), + ) + if !resultOk { + return false, l + } + + // check installation + fileExistAndNotNull := BasicFileExistAndNotNull("/etc/chrony/chrony.conf") + if !fileExistAndNotNull { + return false, []string{ + "chrony config file is null !", + "chrony installation is failed !", + "please check!"} + } + + up, log2 := BasicSystemdUp("chrony.service") + if !up { + return false, log2 + } + + return true, nil } func (op *AgentOsOperator) chronyToMaster(args []string) [][]string { @@ -1089,7 +1197,7 @@ func (op *AgentOsOperator) chronyToMaster(args []string) [][]string { } func (op *AgentOsOperator) chronyToMasterExec(args []string) (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) installZSH() [][]string { @@ -1368,7 +1476,7 @@ func (op *AgentOsOperator) modifySshPort(args []string) [][]string { } func (op *AgentOsOperator) modifySshPortExec(args []string) (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) openBBR() [][]string { @@ -1377,7 +1485,7 @@ func (op *AgentOsOperator) openBBR() [][]string { } func (op *AgentOsOperator) openBBRExec() (bool, []string) { - return false, nil + return true, nil } func (op *AgentOsOperator) ok(args []string) [][]string { diff --git a/server/src/main/java/io/wdd/func/auto/beans/BaseFunctionEnum.java b/server/src/main/java/io/wdd/func/auto/beans/BaseFunctionEnum.java index bd08fed..67930db 100644 --- a/server/src/main/java/io/wdd/func/auto/beans/BaseFunctionEnum.java +++ b/server/src/main/java/io/wdd/func/auto/beans/BaseFunctionEnum.java @@ -44,6 +44,10 @@ public enum BaseFunctionEnum { "安装Harbor, 默认为2.1.0版本" ), + INSTALL_CHRONY( + "installChrony", + "安装Chrony服务器" + ), CHRONY_TO_PUBLIC_NTP( "chronyToPublicNTP", diff --git a/server/src/main/java/io/wdd/func/auto/service/FuncServiceImpl.java b/server/src/main/java/io/wdd/func/auto/service/FuncServiceImpl.java index 7134c7f..bb3dfce 100644 --- a/server/src/main/java/io/wdd/func/auto/service/FuncServiceImpl.java +++ b/server/src/main/java/io/wdd/func/auto/service/FuncServiceImpl.java @@ -62,8 +62,9 @@ public class FuncServiceImpl implements FuncService { } if (syncResultLog - .get(0) + .get(syncResultLog.size() - 1) .endsWith("true")) { + log.debug( "基本脚本调用成功 => {}", syncResultLog