[ Agent ] [ Initialization ] - add harbor func - 1

This commit is contained in:
zeaslity
2023-11-09 13:58:58 +08:00
parent aae4b7a286
commit cdfa4b5d04
8 changed files with 120 additions and 101 deletions

View File

@@ -1,7 +1,6 @@
package executor package executor
import ( import (
"agent-go/g"
"agent-go/register" "agent-go/register"
"fmt" "fmt"
"strings" "strings"
@@ -802,7 +801,7 @@ func (op *AgentOsOperator) installHelm() [][]string {
{ {
"wget", "wget",
"--no-check-certificate", "--no-check-certificate",
g.BaseFuncOssUrlPrefix + "helm-v3.12.1-linux-amd64.tar.gz", op.OssOfflinePrefix + "helm-v3.12.1-linux-amd64.tar.gz",
"-O", "-O",
"/root/wdd/helm-v3.12.1-linux-amd64.tar.gz", "/root/wdd/helm-v3.12.1-linux-amd64.tar.gz",
}, },
@@ -873,7 +872,7 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string {
}, },
{ {
"wget", "wget",
g.BaseFuncOssUrlPrefix + "daemon-config.json", op.OssOfflinePrefix + "daemon-config.json",
"-O", "-O",
"/etc/docker/daemon.json", "/etc/docker/daemon.json",
}, },
@@ -913,7 +912,7 @@ func (op *AgentOsOperator) installHarbor() [][]string {
//{ //{
// "wget", // "wget",
// "--no-check-certificate", // "--no-check-certificate",
// g.BaseFuncOssUrlPrefix + "harbor-offline-installer-v2.1.0.tgz", // op.OssOfflinePrefix + "harbor-offline-installer-v2.1.0.tgz",
// "-O", // "-O",
// "/root/wdd/harbor-offline-installer-v2.1.0.tgz", // "/root/wdd/harbor-offline-installer-v2.1.0.tgz",
//}, //},
@@ -932,7 +931,7 @@ func (op *AgentOsOperator) installHarbor() [][]string {
{ {
"wget", "wget",
"--no-check-certificate", "--no-check-certificate",
g.BaseFuncOssUrlPrefix + "harbor-config-template.yml", op.OssOfflinePrefix + "harbor-config-template.yml",
"-O", "-O",
"/root/wdd/harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
@@ -1009,7 +1008,8 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
} }
// download offline file // download offline file
log.Info("[install harbor] - start to download harbor offline installer !") harborOfflineFileURL := op.OssOfflinePrefix + "harbor-offline-installer-v2.1.0.tgz"
log.InfoF("[install harbor] - start to download harbor offline installer from => %s !", harborOfflineFileURL)
resultOk, resultLog := AllCompleteExecutor([][]string{ resultOk, resultLog := AllCompleteExecutor([][]string{
{ {
"mkdir", "mkdir",
@@ -1029,7 +1029,7 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
{ {
"wget", "wget",
"--no-check-certificate", "--no-check-certificate",
g.BaseFuncOssUrlPrefix + "harbor-offline-installer-v2.1.0.tgz", harborOfflineFileURL,
"-qO", "-qO",
"/root/wdd/harbor-offline-installer-v2.1.0.tgz", "/root/wdd/harbor-offline-installer-v2.1.0.tgz",
}}, }},
@@ -1063,7 +1063,7 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
AllCommandExecutor([]string{ AllCommandExecutor([]string{
"wget", "wget",
"--no-check-certificate", "--no-check-certificate",
g.BaseFuncOssUrlPrefix + "harbor-config-template.yml", op.OssOfflinePrefix + "harbor-config-template.yml",
"-qO", "-qO",
"/root/wdd/harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}) })

View File

@@ -1,6 +1,9 @@
package executor package executor
import "strings" import (
"strconv"
"strings"
)
func BuildAgentOsOperator(osInfo string, ossOfflinePrefix string) *AgentOsOperator { func BuildAgentOsOperator(osInfo string, ossOfflinePrefix string) *AgentOsOperator {
@@ -84,8 +87,6 @@ func detectByOsType(os *AgentOsOperator, osInfo string) {
func detectByInternet(os *AgentOsOperator) { func detectByInternet(os *AgentOsOperator) {
log.Info("开始检测Agent的网络连通情况!")
outsideTestUrl := "www.google.com" outsideTestUrl := "www.google.com"
innerTestUrl := "www.baidu.com" innerTestUrl := "www.baidu.com"
@@ -109,4 +110,6 @@ func detectByInternet(os *AgentOsOperator) {
os.IsAgentInnerWall = true os.IsAgentInnerWall = true
} }
log.InfoF("[Agent Network Status] - Can Access Internet => %s Inner CN => %s", strconv.FormatBool(os.CanAccessInternet), strconv.FormatBool(os.IsAgentInnerWall))
} }

View File

@@ -92,10 +92,9 @@ public class BaseFuncScheduler {
// BaseFunctionEnum.CHRONY_TO_PUBLIC_NTP, // BaseFunctionEnum.CHRONY_TO_PUBLIC_NTP,
// BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY, // BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY,
// BaseFunctionEnum.INSTALL_DOCKER, // BaseFunctionEnum.INSTALL_DOCKER,
// BaseFunctionEnum.INSTALL_DOCKER_COMPOSE, // BaseFunctionEnum.INSTALL_DOCKER_COMPOSE
// BaseFunctionEnum.INSTALL_HARBOR, // BaseFunctionEnum.INSTALL_HARBOR,
// BaseFunctionEnum.INSTALL_ZSH // BaseFunctionEnum.INSTALL_ZSH
BaseFunctionEnum.INSTALL_HARBOR
); );
@@ -103,7 +102,8 @@ public class BaseFuncScheduler {
if (!funcService.callBaseFuncAndJudge( if (!funcService.callBaseFuncAndJudge(
masterTopicName, masterTopicName,
procedure, procedure,
null null,
false
)) { )) {
log.error( log.error(
@@ -115,6 +115,17 @@ public class BaseFuncScheduler {
} }
} }
if (!funcService.callBaseFuncAndJudge(
masterTopicName,
BaseFunctionEnum.INSTALL_HARBOR,
null,
true
)) {
log.error("Master Install Harbor Failed !");
return false;
}
return true; return true;
} }

View File

@@ -18,8 +18,9 @@ public interface FuncService {
* *
* @param baseFunctionEnum * @param baseFunctionEnum
* @param funcArgs * @param funcArgs
* @param durationTask
* @return * @return
*/ */
boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs); boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs, boolean durationTask);
} }

View File

@@ -26,7 +26,8 @@ public class FuncServiceImpl implements FuncService {
agentTopicName, agentTopicName,
ExecutionMessageType.BASE, ExecutionMessageType.BASE,
baseFunctionName, baseFunctionName,
funcArgs funcArgs,
false
); );
} }
@@ -37,12 +38,13 @@ public class FuncServiceImpl implements FuncService {
agentTopicName, agentTopicName,
ExecutionMessageType.APP, ExecutionMessageType.APP,
appFunctionName, appFunctionName,
funcArgs funcArgs,
false
); );
} }
@Override @Override
public boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs) { public boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs, boolean durationTask) {
log.debug( log.debug(
"调用基本脚本 => {}", "调用基本脚本 => {}",
@@ -53,7 +55,8 @@ public class FuncServiceImpl implements FuncService {
agentTopicName, agentTopicName,
ExecutionMessageType.BASE, ExecutionMessageType.BASE,
baseFunctionEnum.getFuncName(), baseFunctionEnum.getFuncName(),
funcArgs funcArgs,
durationTask
); );
if (CollectionUtils.isEmpty(syncResultLog)) { if (CollectionUtils.isEmpty(syncResultLog)) {
@@ -83,7 +86,7 @@ public class FuncServiceImpl implements FuncService {
// 归一化调用 // 归一化调用
private List<String> syncCallFunction(String agentTopicName, ExecutionMessageType emType, String funcName, List<String> funcArgs) { private List<String> syncCallFunction(String agentTopicName, ExecutionMessageType emType, String funcName, List<String> funcArgs, boolean durationTask) {
// 重新构造内容增加Function Name // 重新构造内容增加Function Name
if (CollectionUtils.isEmpty(funcArgs)) { if (CollectionUtils.isEmpty(funcArgs)) {
@@ -103,7 +106,7 @@ public class FuncServiceImpl implements FuncService {
null, null,
null, null,
true, true,
false durationTask
); );
return resultLog; return resultLog;

View File

@@ -76,66 +76,69 @@ public class ExecutionServiceImpl implements ExecutionService {
); );
// 需要返回结果 // 需要返回结果
if (!durationTask) {
// 等待结果
// countDownLatch的个数约定为1, agent会合并多行命令的结果一并返回
OMessageReplayContent replayContent = WaitFromAgent(
octopusMessage,
1
);
CountDownLatch replayLatch = replayContent.getCountDownLatch(); // 超时等待时间,默认为 COMMAND_MAX_WAIT_TIMEOUT
boolean waitOK = false; int commandMaxWaitTimeout = COMMAND_MAX_WAIT_TIMEOUT;
if (durationTask) {
// 2023年11月9日 超长的任务时间设置为5分钟
commandMaxWaitTimeout = COMMAND_MAX_WAIT_TIMEOUT * 5;
}
// 等待结果
// countDownLatch的个数约定为1, agent会合并多行命令的结果一并返回
OMessageReplayContent replayContent = WaitFromAgent(
octopusMessage,
1
);
try { CountDownLatch replayLatch = replayContent.getCountDownLatch();
boolean waitOK = false;
// 2023年10月8日 根据执行的命令数量设定超时等待时间 try {
int commandMaxWaitTimeout = COMMAND_MAX_WAIT_TIMEOUT;
if (!CollectionUtils.isEmpty(commandListComplete)) {
commandMaxWaitTimeout = COMMAND_MAX_WAIT_TIMEOUT * commandListComplete.size();
}
waitOK = replayLatch.await(
commandMaxWaitTimeout,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
// 释放等待队列
StopWaitingResult(octopusMessage);
// 转换结果
commandResultLog = (ArrayList<String>) octopusMessage.getResult();
Object executionContent;
if (executionMessage
.getExecutionType()
.equals("BASE")) {
executionContent = executionMessage.getExecutionType() + "==" + executionMessage.getFuncContent();
} else if (executionMessage.getSingleLineCommand() != null) {
executionContent = executionMessage.getSingleLineCommand();
} else {
executionContent = executionMessage.getMultiLineCommand();
}
// debug
log.debug(
"执行命令 {} {} 在规定时间内结束, 结果为 {} 返回内容为 {}",
executionContent,
waitOK ? "已经" : "",
octopusMessage.getResultCode(),
commandResultLog
);
// 2023年10月8日 根据执行的命令数量设定超时等待时间
if (!CollectionUtils.isEmpty(commandListComplete)) {
commandMaxWaitTimeout = COMMAND_MAX_WAIT_TIMEOUT * commandListComplete.size();
} }
waitOK = replayLatch.await(
commandMaxWaitTimeout,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
// 释放等待队列
StopWaitingResult(octopusMessage);
// 转换结果
commandResultLog = (ArrayList<String>) octopusMessage.getResult();
Object executionContent;
if (executionMessage
.getExecutionType()
.equals("BASE")) {
executionContent = executionMessage.getExecutionType() + "==" + executionMessage.getFuncContent();
} else if (executionMessage.getSingleLineCommand() != null) {
executionContent = executionMessage.getSingleLineCommand();
} else {
executionContent = executionMessage.getMultiLineCommand();
}
// debug
log.debug(
"执行命令 {} {} 在规定时间内结束, 结果为 {} 返回内容为 {}",
executionContent,
waitOK ? "已经" : "",
octopusMessage.getResultCode(),
commandResultLog
);
} }
return commandResultLog; return commandResultLog;
} }

View File

@@ -1,6 +1,5 @@
package io.wdd.server.coreService.impl; package io.wdd.server.coreService.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -88,14 +87,17 @@ public class CoreServerServiceImpl implements CoreServerService {
public Page<ServerInfoPO> serverGetByPage(ServerQueryEntity serverQueryEntity) { public Page<ServerInfoPO> serverGetByPage(ServerQueryEntity serverQueryEntity) {
// 默认数值处理 // 默认数值处理
if (serverQueryEntity == null || serverQueryEntity.getPageSize() == null || serverQueryEntity.getPageNumber() == null) { if (serverQueryEntity == null) {
serverQueryEntity = ServerQueryEntity serverQueryEntity = ServerQueryEntity
.builder() .builder()
.pageNumber(1) .pageNumber(1)
.pageSize(10) .pageSize(10)
.build(); .build();
} else { }
if (serverQueryEntity.getPageSize() == null || serverQueryEntity.getPageNumber() == null) {
serverQueryEntity.setPageSize(10);
serverQueryEntity.setPageNumber(1);
} }
Page<ServerInfoPO> serverInfoPOPage = new Page<>( Page<ServerInfoPO> serverInfoPOPage = new Page<>(
@@ -104,29 +106,23 @@ public class CoreServerServiceImpl implements CoreServerService {
); );
// 查询Page会直接写入到serverInfoPOPage中 // 查询Page会直接写入到serverInfoPOPage中
serverInfoService new LambdaQueryChainWrapper<ServerInfoPO>(serverInfoService.getBaseMapper())
.getBaseMapper() .likeRight(
.selectPage( StringUtils.isNotEmpty(serverQueryEntity.getServerName()),
serverInfoPOPage, ServerInfoPO::getServerName,
new QueryWrapper<ServerInfoPO>() serverQueryEntity.getServerName()
.like( )
StringUtils.isNoneEmpty(serverQueryEntity.getServerName()), .likeRight(
"server_name", StringUtils.isNotEmpty(serverQueryEntity.getServerIPv4()),
serverQueryEntity.getServerName() ServerInfoPO::getServerIpPbV4,
) serverQueryEntity.getServerIPv4()
.likeRight( )
StringUtils.isNoneEmpty(serverQueryEntity.getServerIPv4()), .likeRight(
"server_ip_pb_v4", StringUtils.isNotEmpty(serverQueryEntity.getServerLocation()),
serverQueryEntity.getServerIPv4() ServerInfoPO::getLocation,
) serverQueryEntity.getServerLocation()
.likeRight( )
StringUtils.isNoneEmpty(serverQueryEntity.getServerLocation()), .page(serverInfoPOPage);
"location",
serverQueryEntity.getServerLocation()
)
.orderByAsc("server_id")
);
// 将查询结果中的User对象转换为UserVO对象 // 将查询结果中的User对象转换为UserVO对象
/*Page<ServerInfoVO> serverInfoVOPage = new Page<>( /*Page<ServerInfoVO> serverInfoVOPage = new Page<>(

View File

@@ -24,10 +24,12 @@ public class TestBaseFuncScheduler {
ProjectDeployContext projectDeployContext = new ProjectDeployContext(); ProjectDeployContext projectDeployContext = new ProjectDeployContext();
projectDeployContext.setProjectId(1716372290994155522L); // projectDeployContext.setProjectId(1716372290994155522L); // vultr
projectDeployContext.setProjectId(1722453318596550657L); // lappro
ServerQueryEntity serverQueryEntity = new ServerQueryEntity(); ServerQueryEntity serverQueryEntity = new ServerQueryEntity();
serverQueryEntity.setServerName("Paripark"); serverQueryEntity.setServerName("Chengdu");
ServerInfoPO serverInfoPO = serverService ServerInfoPO serverInfoPO = serverService
.serverGetByPage(serverQueryEntity) .serverGetByPage(serverQueryEntity)
.getRecords() .getRecords()