diff --git a/.github/workflows/build-push-docker.yml b/.github/workflows/build-push-docker.yml index a5a6baf..dc2433d 100644 --- a/.github/workflows/build-push-docker.yml +++ b/.github/workflows/build-push-docker.yml @@ -128,9 +128,9 @@ jobs: echo "--------------------------------------" cd ./agent/target ls - date --date='TZ="Asia/Shanghai"' --rfc-3339=seconds | cut -d"+" -f1 | sed "s/ /-/g" | sed "s/:/-/g" + echo "current time is $(date --rfc-3339=seconds | cut -d"+" -f1 | sed "s/ /-/g" | sed "s/:/-/g")" echo "start to copy target jar" - cp ./agent-*.jar ./octopus-agent-$(date --date='TZ="Asia/Shanghai"' --rfc-3339=seconds | cut -d"+" -f1 | sed "s/ /-/g" | sed "s/:/-/g").jar + cp ./agent-*.jar ./octopus-agent-$(date --rfc-3339=seconds | cut -d"+" -f1 | sed "s/ /-/g" | sed "s/:/-/g").jar cd /home/runner/work/ProjectOctopus/ProjectOctopus echo "--------------------------------------" cat /proc/cpuinfo diff --git a/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java b/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java index 3ce6f9b..a91fee6 100644 --- a/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java +++ b/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java @@ -30,7 +30,7 @@ public class CommandExecutor { /** * handle command from octopus server * - * @param executionMessage get from EXECUTOR_HANDLER + * @param executionMessage get from EXECUTOR_HANDLERju */ public void execute(ExecutionMessage executionMessage) { diff --git a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java index 26e231d..308c52c 100644 --- a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java +++ b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java @@ -1,5 +1,7 @@ package io.wdd.rpc.execute.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import io.wdd.common.beans.executor.ExecutionMessage; import io.wdd.common.beans.rabbitmq.OctopusMessage; import io.wdd.common.beans.rabbitmq.OctopusMessageType; @@ -18,6 +20,8 @@ public class CoreExecutionServiceImpl implements CoreExecutionService { @Resource ToAgentMessageSender messageSender; + @Resource + ObjectMapper objectMapper; @Override public String SendCommandToAgent(String topicName, String command) { @@ -34,11 +38,22 @@ public class CoreExecutionServiceImpl implements CoreExecutionService { OctopusMessage octopusMessage = this.generateOctopusMessage(topicName, type, commandList); + ExecutionMessage executionMessage = (ExecutionMessage) octopusMessage.getContent(); + + String executionMsg; + + try { + + executionMsg = objectMapper.writeValueAsString(executionMessage); + octopusMessage.setContent(executionMsg); + + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + messageSender.send(octopusMessage); - ExecutionMessage content = (ExecutionMessage) octopusMessage.getContent(); - - return content.getResultKey(); + return executionMessage.getResultKey(); } @@ -53,15 +68,16 @@ public class CoreExecutionServiceImpl implements CoreExecutionService { private OctopusMessage generateOctopusMessage(String topicName, String type, List commandList){ + ExecutionMessage executionMessage = generateExecutionMessage( + type, + commandList, + generateCommandResultKey(topicName) + ); return OctopusMessage.builder() .type(OctopusMessageType.EXECUTOR) .init_time(LocalDateTime.now()) - .content(generateExecutionMessage( - type, - commandList, - generateCommandResultKey(topicName) - )) + .content(executionMessage) .uuid(topicName) .build(); } diff --git a/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java b/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java index e0266d7..0f4fb94 100644 --- a/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java +++ b/server/src/main/java/io/wdd/rpc/init/AcceptAgentInitInfo.java @@ -104,6 +104,7 @@ public class AcceptAgentInitInfo { if (!checkAgentAlreadyRegister(agentQueueTopic)) { // 3. save the agent info into database // backend fixed thread daemon to operate the database ensuring the operation is correct ! + log.info("[AGENT INIT] - agent not exist ! start to register !"); if (!databaseOperator.saveInitOctopusAgentInfo(serverInfoVO)) { throw new MyRuntimeException("database save agent info error !"); } diff --git a/server/src/main/java/io/wdd/rpc/message/handler/OctopusMessageHandlerServer.java b/server/src/main/java/io/wdd/rpc/message/handler/OctopusMessageHandlerServer.java index affac43..442b3da 100644 --- a/server/src/main/java/io/wdd/rpc/message/handler/OctopusMessageHandlerServer.java +++ b/server/src/main/java/io/wdd/rpc/message/handler/OctopusMessageHandlerServer.java @@ -40,8 +40,10 @@ public class OctopusMessageHandlerServer { // todo what to do after received the result - // log info ? - // update the database - // handle the result + // collect all message from agent and log to somewhere + + // 1. send some info to the specific topic name + // 2. judge from which agent the message are + // } } diff --git a/server/src/main/java/io/wdd/rpc/message/sender/ToAgentMessageSender.java b/server/src/main/java/io/wdd/rpc/message/sender/ToAgentMessageSender.java index ddf5e8f..794b0d7 100644 --- a/server/src/main/java/io/wdd/rpc/message/sender/ToAgentMessageSender.java +++ b/server/src/main/java/io/wdd/rpc/message/sender/ToAgentMessageSender.java @@ -53,9 +53,13 @@ public class ToAgentMessageSender { public void send(OctopusMessage octopusMessage) { - log.info("OctopusMessage {} send to agent {}",octopusMessage, octopusMessage.getUuid()); + log.info("OctopusMessage {} send to agent {}", octopusMessage, octopusMessage.getUuid()); + rabbitTemplate.convertAndSend( + initRabbitMQConfig.OCTOPUS_EXCHANGE, + octopusMessage.getUuid()+"*", + writeData(octopusMessage)); } diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java index b743f4b..a9f75f0 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java @@ -10,6 +10,7 @@ import io.wdd.server.beans.vo.ServerInfoVO; import io.wdd.server.coreService.CoreServerService; import io.wdd.server.service.*; import io.wdd.server.utils.EntityUtils; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; @@ -24,6 +25,7 @@ import java.util.stream.Collectors; @Service +@Slf4j public class CoreServerServiceImpl implements CoreServerService { @Resource diff --git a/server/src/main/resources/bootstrap.yml b/server/src/main/resources/bootstrap.yml index 605cba7..ef43407 100644 --- a/server/src/main/resources/bootstrap.yml +++ b/server/src/main/resources/bootstrap.yml @@ -2,11 +2,11 @@ spring: application: name: octopus-server profiles: - active: k3s + active: local cloud: nacos: config: - group: k3s + group: local config-retry-time: 3000 file-extension: yaml max-retry: 3 @@ -16,5 +16,5 @@ spring: timeout: 5000 config-long-poll-timeout: 5000 extension-configs: - - group: k3s - data-id: common-k3s.yaml \ No newline at end of file + - group: local + data-id: common-local.yaml \ No newline at end of file diff --git a/source/src/main/java/io/wdd/source/shell/AgentBootUp b/source/src/main/java/io/wdd/source/shell/AgentBootUp deleted file mode 100644 index e69de29..0000000 diff --git a/source/src/main/java/io/wdd/source/shell/AgentUpdate.sh b/source/src/main/java/io/wdd/source/shell/AgentUpdate.sh deleted file mode 100644 index 8031f97..0000000 --- a/source/src/main/java/io/wdd/source/shell/AgentUpdate.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - - -containerId=$(docker ps | grep octopus-agent | awk '{print$1}') - -docker container stop $(containerId) && docker container rm $(containerId) - -echo "y -" | docker container prune - -docker image rmi octopus-agent-ubuntu:latest -y - diff --git a/source/src/main/java/io/wdd/source/shell/agent-bootup.sh b/source/src/main/java/io/wdd/source/shell/agent-bootup.sh index 28492ea..af4053a 100644 --- a/source/src/main/java/io/wdd/source/shell/agent-bootup.sh +++ b/source/src/main/java/io/wdd/source/shell/agent-bootup.sh @@ -3,6 +3,7 @@ ##### environment variables ###### RepoSourcePath=https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell +OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/ DependLibFiles=( wdd-lib-file.sh wdd-lib-log.sh @@ -199,7 +200,8 @@ DownloadAllFile() { colorEcho $BLUE "start to download octopus agent !" # check for latest version # download the lasted jar - wget https://happybirthday.107421.xyz/octopus-agent/octopus-agent-2022-12-21-16-00-00.jar -O /octopus-agent/agent.jar + . ./lib/wdd-lib-os.sh + CheckAndDownloadLatestVersion FunctionSuccess FunctionEnd @@ -265,24 +267,32 @@ InstallJDKPackage() { } systemdAgent(){ - local JAVA_OPTS="-Xms128m -Xmx256m" + local JAVA_OPTS="-Xms128m -Xmx512m" + + # https://www.baeldung.com/linux/run-java-application-as-service cat >/etc/systemd/system/octopus-agent.service <>/etc/environment<&2 - fi -} +OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/ -######################## -# Log message -# Arguments: -# Message to log -# Returns: -# None -######################### -log() { - stderr_print "${CYAN}${MODULE:-} ${MAGENTA}$(date "+%Y-%m-%d %H:%M:%S.%2N ")${RESET}${*}" -} -######################## -# Log an 'info' message -# Arguments: -# Message to log -# Returns: -# None -######################### -info() { - log "${GREEN}INFO ${RESET} ==> ${*}" -} -######################## -# Log message -# Arguments: -# Message to log -# Returns: -# None -######################### -warn() { - log "${YELLOW}WARN ${RESET} ==> ${*}" -} -######################## -# Log an 'error' message -# Arguments: -# Message to log -# Returns: -# None -######################### -error() { - log "${RED}ERROR${RESET} ==> ${*}" -} -######################## -# Log a 'debug' message -# Globals: -# BITNAMI_DEBUG -# Arguments: -# None -# Returns: -# None -######################### -debug() { - # 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it - local bool="${BITNAMI_DEBUG:-true}" - # comparison is performed without regard to the case of alphabetic characters - shopt -s nocasematch - if [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then - log "${MAGENTA}DEBUG${RESET} ==> ${*}" - fi -} -######################## -# Indent a string -# Arguments: -# $1 - string -# $2 - number of indentation characters (default: 4) -# $3 - indentation character (default: " ") -# Returns: -# None -######################### -indent() { - local string="${1:-}" - local num="${2:?missing num}" - local char="${3:-" "}" - # Build the indentation unit string - local indent_unit="" - for ((i = 0; i < num; i++)); do - indent_unit="${indent_unit}${char}" - done - # shellcheck disable=SC2001 - # Complex regex, see https://github.com/koalaman/shellcheck/wiki/SC2001#exceptions - echo "$string" | sed "s/^/${indent_unit}/" -} +CheckAndDownloadLatestVersion(){ + + log "checking for the latest version" + local latestVersion=$(curl $OctopusAgentUrl | grep -v h1 | grep "a href=" | awk '{print$2}' |cut -d">" -f2 | cut -d"<" -f1) + + log "octopus agent latest version is => [ $latestVersion ]" + + rm -rf /octopus-agent/*.jar + cd /octopus-agent + + log "start to download the latest version !" + + wget "$OctopusAgentUrl$latestVersion" + cp "$OctopusAgentUrl$latestVersion" agent.jar + + log "" + ls /octopus-agent/ | grep jar + +} \ No newline at end of file diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh index 104f5b1..5767617 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh @@ -95,6 +95,46 @@ check_sys() { return 0 } +RED="31m" ## 姨妈红 +GREEN="32m" ## 水鸭青 +YELLOW="33m" ## 鸭屎黄 +PURPLE="35m" ## 基佬紫 +BLUE="36m" ## 天依蓝 +BlinkGreen="32;5m" ##闪烁的绿色 +BlinkRed="31;5m" ##闪烁的红色 +BackRed="41m" ## 背景红色 +SplitLine="----------------------" #会被sys函数中的方法重写 + +######## 颜色函数方法很精妙 ############ +colorEcho() { + echo -e "\033[${1}${@:2}\033[0m" 1>&2 +} + +check_root() { + if [[ $EUID != 0 ]]; then + colorEcho ${RED} "当前非root账号(或没有root权限),无法继续操作,请更换root账号!" + colorEcho ${YELLOW} "使用sudo -命令获取临时root权限(执行后可能会提示输入root密码)" + exit 1 + fi +} + +FunctionStart() { + colorEcho ${PURPLE} ${SplitLine} + colorEcho ${PURPLE} ${SplitLine} + echo "" +} + +FunctionSuccess() { + colorEcho ${GREEN} ${SplitLine} + echo "" +} + +FunctionEnd() { + echo "" + colorEcho ${BlinkGreen} ${SplitLine} + echo "" + echo "" +} tmp () {