From 37752ca07c4dd5d8913ca56ddef8f6196be93eb7 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 27 Dec 2022 16:02:32 +0800 Subject: [PATCH 1/6] [ server ] [ executor ] - optimize code --- .github/workflows/build-push-docker.yml | 4 +- .../wdd/agent/executor/CommandExecutor.java | 2 +- .../service/CoreExecutionServiceImpl.java | 32 +++-- .../io/wdd/rpc/init/AcceptAgentInitInfo.java | 1 + .../handler/OctopusMessageHandlerServer.java | 8 +- .../message/sender/ToAgentMessageSender.java | 6 +- .../impl/CoreServerServiceImpl.java | 2 + server/src/main/resources/bootstrap.yml | 8 +- .../main/java/io/wdd/source/shell/AgentBootUp | 0 .../java/io/wdd/source/shell/AgentUpdate.sh | 12 -- .../java/io/wdd/source/shell/agent-bootup.sh | 28 ++-- .../java/io/wdd/source/shell/agent-reboot.sh | 20 +++ .../java/io/wdd/source/shell/agent-update.sh | 25 ++++ .../io/wdd/source/shell/lib/wdd-lib-env.sh | 40 +++--- .../io/wdd/source/shell/lib/wdd-lib-os.sh | 127 +++--------------- .../io/wdd/source/shell/lib/wdd-lib-sys.sh | 40 ++++++ 16 files changed, 189 insertions(+), 166 deletions(-) delete mode 100644 source/src/main/java/io/wdd/source/shell/AgentBootUp delete mode 100644 source/src/main/java/io/wdd/source/shell/AgentUpdate.sh create mode 100644 source/src/main/java/io/wdd/source/shell/agent-reboot.sh create mode 100644 source/src/main/java/io/wdd/source/shell/agent-update.sh 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 () { From 0c38361a3b17f6f23cdd981e6999fe5080e89224 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 27 Dec 2022 16:03:20 +0800 Subject: [PATCH 2/6] [ server ] [ executor ] - optimize code -2 --- agent/src/main/resources/bootstrap.yml | 8 ++++---- source/src/main/java/io/wdd/source/shell/agent-bootup.sh | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/agent/src/main/resources/bootstrap.yml b/agent/src/main/resources/bootstrap.yml index 013d9e3..0c92b06 100644 --- a/agent/src/main/resources/bootstrap.yml +++ b/agent/src/main/resources/bootstrap.yml @@ -2,11 +2,11 @@ spring: application: name: octopus-agent profiles: - active: local + active: k3s cloud: nacos: config: - group: local + group: k3s config-retry-time: 3000 file-extension: yaml max-retry: 3 @@ -16,7 +16,7 @@ spring: timeout: 5000 config-long-poll-timeout: 5000 extension-configs: - - group: local - data-id: common-local.yaml + - group: k3s + data-id: common-k3s.yaml 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 af4053a..193ad3a 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 @@ -200,7 +200,8 @@ DownloadAllFile() { colorEcho $BLUE "start to download octopus agent !" # check for latest version # download the lasted jar - . ./lib/wdd-lib-os.sh + . ./lib/wdd-lib-os.s + h CheckAndDownloadLatestVersion FunctionSuccess From 62e6d7ed6b0f4130e56b9a6d2d79fdd97aed4be0 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 27 Dec 2022 16:48:04 +0800 Subject: [PATCH 3/6] [ server ] [ executor ] - optimize code -3 --- agent/src/main/resources/bootstrap.yml | 8 ++++---- source/src/main/java/io/wdd/source/shell/agent-bootup.sh | 6 +++--- source/src/main/java/io/wdd/source/shell/agent-update.sh | 2 +- .../src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh | 8 ++++---- .../src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh | 2 +- .../src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh | 2 +- .../src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh | 1 - 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/agent/src/main/resources/bootstrap.yml b/agent/src/main/resources/bootstrap.yml index 0c92b06..013d9e3 100644 --- a/agent/src/main/resources/bootstrap.yml +++ b/agent/src/main/resources/bootstrap.yml @@ -2,11 +2,11 @@ spring: application: name: octopus-agent profiles: - active: k3s + active: local cloud: nacos: config: - group: k3s + group: local config-retry-time: 3000 file-extension: yaml max-retry: 3 @@ -16,7 +16,7 @@ spring: timeout: 5000 config-long-poll-timeout: 5000 extension-configs: - - group: k3s - data-id: common-k3s.yaml + - group: local + data-id: common-local.yaml 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 193ad3a..e987702 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 @@ -200,8 +200,8 @@ DownloadAllFile() { colorEcho $BLUE "start to download octopus agent !" # check for latest version # download the lasted jar - . ./lib/wdd-lib-os.s - h + . ./lib/wdd-lib-os.sh + CheckAndDownloadLatestVersion FunctionSuccess @@ -268,7 +268,7 @@ InstallJDKPackage() { } systemdAgent(){ - local JAVA_OPTS="-Xms128m -Xmx512m" + local JAVA_OPTS="-Xms128m -Xmx512m -Dspring.spring.active=k3s -Dspring.cloud.nacos.config.extension-configs=[group=k3s,data-id=common-k3s.yaml]" # https://www.baeldung.com/linux/run-java-application-as-service diff --git a/source/src/main/java/io/wdd/source/shell/agent-update.sh b/source/src/main/java/io/wdd/source/shell/agent-update.sh index d66b235..ba92f8b 100644 --- a/source/src/main/java/io/wdd/source/shell/agent-update.sh +++ b/source/src/main/java/io/wdd/source/shell/agent-update.sh @@ -14,7 +14,7 @@ CheckAndDownloadLatestVersion if [[ ! -f /octopus-agent/agent-reboot.sh ]]; then\ - warn "agent-bootup.sh not exist! start to download !" + warn "agent-bootup.sh not exist! start to download !" cd /octopus-agent wget $RepoSourcePath/agent-reboot.sh fi diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh index 586514e..86e9c23 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh @@ -1,11 +1,11 @@ #!/bin/bash -#. /octopus-agent/shell/lib/wdd-lib-log.sh -#. /octopus-agent/shell/lib/wdd-lib-sys.sh +. /octopus-agent/shell/lib/wdd-lib-log.sh +. /octopus-agent/shell/lib/wdd-lib-sys.sh -. ./wdd-lib-log.sh -. ./wdd-lib-sys.sh +#. .wdd-lib-log.sh +#. .wdd-lib-sys.sh hostArchVersion="" diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh index 8d2ce2a..1e49f77 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh @@ -5,7 +5,7 @@ # shellcheck disable=SC1091 # Load Generic Libraries -. /octopus-agent/shell/lib/wdd-lib-log.sh +. /octopus-agent/shell/lib/wdd-lib-log.shwdd-lib-log.sh # Functions diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh index 54ebbd4..29b594e 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh @@ -1,7 +1,7 @@ #!/bin/bash -. ./wdd-lib-log.sh +. /octopus-agent/shell/lib/wdd-lib-log.sh OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/ 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 5767617..c7949fc 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 @@ -5,7 +5,6 @@ . /octopus-agent/shell/lib/wdd-lib-log.sh - # 判断命令是否存在 command_exists() { command -v "$@" >/dev/null 2>&1 From 3c059e3da268cfc89613dca6b42f6bb29905aff4 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 27 Dec 2022 17:21:00 +0800 Subject: [PATCH 4/6] [ server ] [ executor ] - optimize code -4 --- .../src/main/java/io/wdd/source/shell/agent-bootup.sh | 9 +++++++-- .../main/java/io/wdd/source/shell/lib/wdd-lib-env.sh | 10 +++++----- .../main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) 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 e987702..0caa3ec 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 @@ -187,6 +187,10 @@ DownloadAllFile() { FunctionStart + colorEcho $RED "[CLEAN_UP] clean the old octopus agent staff !" + rm -rf $OctopusAgentPath/lib + rm -rf $OctopusAgentPath/ + mkdir -p $OctopusAgentPath mkdir -p $OctopusAgentPath/lib @@ -200,7 +204,8 @@ DownloadAllFile() { colorEcho $BLUE "start to download octopus agent !" # check for latest version # download the lasted jar - . ./lib/wdd-lib-os.sh + cd /optopus-agent + . ./shell/lib/wdd-lib-os.sh CheckAndDownloadLatestVersion @@ -268,7 +273,7 @@ InstallJDKPackage() { } systemdAgent(){ - local JAVA_OPTS="-Xms128m -Xmx512m -Dspring.spring.active=k3s -Dspring.cloud.nacos.config.extension-configs=[group=k3s,data-id=common-k3s.yaml]" + local JAVA_OPTS="-Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s" # https://www.baeldung.com/linux/run-java-application-as-service diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh index 86e9c23..b0d75b0 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh @@ -268,20 +268,20 @@ export serverIpPbV4=$public_ipv4 export serverIpInV4= export serverIpPbV6= export serverIpInV6= -export location=$city $region $country +export location="$city $region $country" export provider=$org export managePort=$(netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2) -export cpuCore=$cores @ $freq MHz +export cpuCore="$cores @ $freq MHz" export cpuBrand=$cpuName export memoryTotal=$tram export diskTotal=$disk_total_size export diskUsage=$disk_used_size -export archInfo=$arch ($lbit Bit) -export osInfo=$opsy +export archInfo="$arch ($lbit Bit)" +export osInfo="$opsy" export osKernelInfo=$kern export tcpControl=$tcpctrl export virtualization=$virt -export ioSpeed=$ioavg MB/s +export ioSpeed="$ioavg MB/s" export machineId=$(cat /etc/machine-id) EOF 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 c7949fc..6c3db52 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 @@ -152,5 +152,7 @@ wget https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/m apt-cache madison openjdk-11-jdk | head -n 1 | awk '{print$3}' +java -jar /octopus-agent/agent.jar -Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s + } From 09119ef3012f2453757840c46a605c0bb2d9eaa0 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 28 Dec 2022 09:51:05 +0800 Subject: [PATCH 5/6] [ server ] [ executor ] - optimize code -5 --- .../src/main/resources/application-local.yml | 20 +++++++++++++++++++ server/src/main/resources/bootstrap.yml | 8 ++++---- .../java/io/wdd/source/shell/agent-bootup.sh | 7 ++++--- .../java/io/wdd/source/shell/agent-reboot.sh | 20 ------------------- .../java/io/wdd/source/shell/agent-restart.sh | 20 +++++++++++++++++++ .../java/io/wdd/source/shell/agent-update.sh | 18 ++++++++--------- .../io/wdd/source/shell/lib/wdd-lib-env.sh | 2 +- .../io/wdd/source/shell/lib/wdd-lib-os.sh | 5 ++++- .../io/wdd/source/shell/lib/wdd-lib-sys.sh | 8 +++----- 9 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 server/src/main/resources/application-local.yml delete mode 100644 source/src/main/java/io/wdd/source/shell/agent-reboot.sh create mode 100644 source/src/main/java/io/wdd/source/shell/agent-restart.sh diff --git a/server/src/main/resources/application-local.yml b/server/src/main/resources/application-local.yml new file mode 100644 index 0000000..ef43407 --- /dev/null +++ b/server/src/main/resources/application-local.yml @@ -0,0 +1,20 @@ +spring: + application: + name: octopus-server + profiles: + active: local + cloud: + nacos: + config: + group: local + config-retry-time: 3000 + file-extension: yaml + max-retry: 3 + # server-addr: 43.154.83.213:21060 + # server-addr: 140.238.52.228:21060 + server-addr: https://nacos.107421.xyz:443 + timeout: 5000 + config-long-poll-timeout: 5000 + extension-configs: + - group: local + data-id: common-local.yaml \ No newline at end of file diff --git a/server/src/main/resources/bootstrap.yml b/server/src/main/resources/bootstrap.yml index ef43407..605cba7 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: local + active: k3s cloud: nacos: config: - group: local + group: k3s 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: local - data-id: common-local.yaml \ No newline at end of file + - group: k3s + data-id: common-k3s.yaml \ No newline at end of file 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 0caa3ec..08cc715 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 @@ -204,11 +204,12 @@ DownloadAllFile() { colorEcho $BLUE "start to download octopus agent !" # check for latest version # download the lasted jar - cd /optopus-agent - . ./shell/lib/wdd-lib-os.sh - + cd $OctopusAgentPath + colorEcho $BLUE "start to load wdd-lib-os.sh" + . ./lib/wdd-lib-os.sh CheckAndDownloadLatestVersion + FunctionSuccess FunctionEnd diff --git a/source/src/main/java/io/wdd/source/shell/agent-reboot.sh b/source/src/main/java/io/wdd/source/shell/agent-reboot.sh deleted file mode 100644 index a3ef7a1..0000000 --- a/source/src/main/java/io/wdd/source/shell/agent-reboot.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - - -. ./lib/wdd-lib-log.sh -. ./lib/wdd-lib-sys.sh - - -FunctionStart - -log "prepare the env" -chmod +x ./lib/wdd-lib-env.sh -./lib/wdd-lib-env.sh - -systemctl stop octopus-agent.service -systemctl start octopus-agent.service - -FunctionSuccess -FunctionEnd - - diff --git a/source/src/main/java/io/wdd/source/shell/agent-restart.sh b/source/src/main/java/io/wdd/source/shell/agent-restart.sh new file mode 100644 index 0000000..9c0bdbc --- /dev/null +++ b/source/src/main/java/io/wdd/source/shell/agent-restart.sh @@ -0,0 +1,20 @@ +#!/bin/bash + + +. /octopus-agent/shell/lib/wdd-lib-log.sh +. /octopus-agent/shell/lib/wdd-lib-sys.sh + + +FunctionStart + +log "prepare the env" +chmod +x /octopus-agent/shell/lib/wdd-lib-env.sh +/octopus-agent/shell/lib/wdd-lib-env.sh + +systemctl stop octopus-agent.service +systemctl start octopus-agent.service + +FunctionSuccess +FunctionEnd + + diff --git a/source/src/main/java/io/wdd/source/shell/agent-update.sh b/source/src/main/java/io/wdd/source/shell/agent-update.sh index ba92f8b..896b3d5 100644 --- a/source/src/main/java/io/wdd/source/shell/agent-update.sh +++ b/source/src/main/java/io/wdd/source/shell/agent-update.sh @@ -1,25 +1,23 @@ #!/bin/bash -. ./lib/wdd-lib-log.sh -. ./lib/wdd-lib-sys.sh -. ./lib/wdd-lib-os.sh +. /octopus-agent/shell/lib/wdd-lib-log.sh +. /octopus-agent/shell/lib/wdd-lib-sys.sh +. /octopus-agent/shell/lib/wdd-lib-os.sh RepoSourcePath=https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell - CheckAndDownloadLatestVersion - -if [[ ! -f /octopus-agent/agent-reboot.sh ]]; then\ +if [[ ! -f /octopus-agent/shell/agent-reboot.sh ]]; then\ warn "agent-bootup.sh not exist! start to download !" - cd /octopus-agent - wget $RepoSourcePath/agent-reboot.sh + cd /octopus-agent/shell + wget $RepoSourcePath/agent-reboot.sh fi log "start to reboot the octopus agent !" -chmod +x /octopus-agent/agent-reboot.sh -/octopus-agent/agent-reboot.sh +chmod +x /octopus-agent/shell/agent-reboot.sh +/octopus-agent/shell/agent-reboot.sh diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh index b0d75b0..314c1a4 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh @@ -272,7 +272,7 @@ export location="$city $region $country" export provider=$org export managePort=$(netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2) export cpuCore="$cores @ $freq MHz" -export cpuBrand=$cpuName +export cpuBrand="$cpuName" export memoryTotal=$tram export diskTotal=$disk_total_size export diskUsage=$disk_used_size diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh index 29b594e..84d44b7 100644 --- a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh @@ -19,9 +19,12 @@ CheckAndDownloadLatestVersion(){ log "start to download the latest version !" wget "$OctopusAgentUrl$latestVersion" - cp "$OctopusAgentUrl$latestVersion" agent.jar + cp "$latestVersion" agent.jar log "" + echo "" + log "----------------------------------------------" ls /octopus-agent/ | grep jar + log "----------------------------------------------" } \ 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 6c3db52..43cd23c 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 @@ -1,7 +1,5 @@ #!/bin/bash - - . /octopus-agent/shell/lib/wdd-lib-log.sh @@ -140,12 +138,12 @@ tmp () { gcloud compute instances create octopus-agent-2c-4g-1 --project=compact-lacing-371804 --zone=asia-northeast1-b --machine-type=n2d-custom-2-4096 --network-interface=network-tier=PREMIUM,subnet=default --metadata=startup-script=wget\ https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell/agent-bootup.sh\ \&\&\ chmod\ \+x\ agent-bootup.sh\ \&\&\ /bin/bash\ agent-bootup.sh --can-ip-forward --maintenance-policy=MIGRATE --provisioning-model=STANDARD --service-account=172889627951-compute@developer.gserviceaccount.com --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --create-disk=auto-delete=yes,boot=yes,device-name=octopus-agent-2c-4g,image=projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20221213,mode=rw,size=20,type=projects/compact-lacing-371804/zones/us-west4-b/diskTypes/pd-ssd --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any -gcloud compute instances delete octopus-agent-2c-4g-7 --project=compact-lacing-371804 --zone=asia-northeast1-b +gcloud compute instances delete tokyo-amd64-03 --project=compact-lacing-371804 --zone=asia-northeast1-b -gcloud compute instances create octopus-agent-2c-4g-7 --project=compact-lacing-371804 --zone=asia-northeast1-b --machine-type=n2d-custom-2-4096 --network-interface=network-tier=PREMIUM,subnet=default --can-ip-forward --maintenance-policy=MIGRATE --provisioning-model=STANDARD --service-account=172889627951-compute@developer.gserviceaccount.com --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --create-disk=auto-delete=yes,boot=yes,device-name=octopus-agent-2c-4g,image=projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20221213,mode=rw,size=20,type=projects/compact-lacing-371804/zones/us-west4-b/diskTypes/pd-ssd --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any +gcloud compute instances create tokyo-amd64-03 --project=compact-lacing-371804 --zone=asia-northeast1-b --machine-type=n2d-custom-2-4096 --network-interface=network-tier=PREMIUM,subnet=default --can-ip-forward --maintenance-policy=MIGRATE --provisioning-model=STANDARD --service-account=172889627951-compute@developer.gserviceaccount.com --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --create-disk=auto-delete=yes,boot=yes,device-name=octopus-agent-2c-4g,image=projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20221213,mode=rw,size=20,type=projects/compact-lacing-371804/zones/us-west4-b/diskTypes/pd-ssd --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any -gcloud compute ssh --zone "asia-northeast1-b" "octopus-agent-2c-4g-7" --project "compact-lacing-371804" +gcloud compute ssh --zone "asia-northeast1-b" "tokyo-amd64-03" --project "compact-lacing-371804" wget https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell/agent-bootup.sh && chmod +x agent-bootup.sh && /bin/bash agent-bootup.sh From aedada18f5479cd514b50891e2bbf5a5ee8bc2dc Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 28 Dec 2022 11:21:50 +0800 Subject: [PATCH 6/6] [ server ] [ executor ] - optimize code -6 --- .../service/CoreExecutionServiceImpl.java | 2 +- .../java/io/wdd/source/shell/agent-bootup.sh | 154 ++++++++++++++++-- .../java/io/wdd/source/shell/agent-restart.sh | 8 +- .../io/wdd/source/shell/lib/wdd-lib-env.sh | 42 ++--- 4 files changed, 169 insertions(+), 37 deletions(-) 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 308c52c..80b67c3 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 @@ -94,7 +94,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService { private String generateCommandResultKey(String topicName) { - String TimeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + String TimeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")); return topicName + "-" + TimeString; } 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 08cc715..a6b2347 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 @@ -2,8 +2,9 @@ ##### 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/ + +JAVA_OPTS="-Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s" + DependLibFiles=( wdd-lib-file.sh wdd-lib-log.sh @@ -12,6 +13,10 @@ DependLibFiles=( wdd-lib-sys.sh ) + +OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/ + +RepoSourcePath=https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell OctopusAgentPath=/octopus-agent/shell ##### environment variables ###### @@ -196,8 +201,10 @@ DownloadAllFile() { echo "start to download all needed lib shell" for libfile in ${DependLibFiles[*]}; do - echo "lib file is $libfile" - wget "$RepoSourcePath/lib/$libfile" -O $OctopusAgentPath/lib/$libfile + + colorEcho $BLUE "lib file is $libfile" + wget "$RepoSourcePath/lib/$libfile" -Oq $OctopusAgentPath/lib/$libfile + FunctionSuccess done @@ -274,7 +281,6 @@ InstallJDKPackage() { } systemdAgent(){ - local JAVA_OPTS="-Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s" # https://www.baeldung.com/linux/run-java-application-as-service @@ -286,24 +292,27 @@ After=syslog.target network.target [Service] SuccessExitStatus=143 - -PermissionsStartOnly=true -LimitNOFILE=1048576 -LimitNPROC=65535 - +SyslogIdentifier=octopus-agent User=root - Type=simple WorkingDirectory=/octopus-agent - +EnvironmentFile=/etc/environment.d/octopus-agent.conf ExecStart=java -jar /octopus-agent/agent.jar ${JAVA_OPTS} -ExecStop=/bin/kill -15 $MAINPID +ExecStop=/bin/kill -15 \$MAINPID -Restart=on-failure [Install] WantedBy=multi-user.target EOF +# https://www.benzhu.xyz/linux12/ +cat >/etc/rsyslog.d/octopus-agent.conf <> $i 秒 <<-,准备切换shell,上文的日志输出将会消失!!" + sleep 2 + done + chsh -s /bin/zsh + zsh + else + colorEcho ${RED} "zsh 安装失败,大概率是已经安装!!小概率是无法连接GitHub服务器~~" + fi + FunctionEnd +} + +modifyZSH() { + FunctionStart + + ZSH_SOURCE="us" + + if [[ "$1" -ne " " ]]; then + ZSH_SOURCE="$1" + echo "zsh install source = ${ZSH_SOURCE}" + fi + + colorEcho ${GREEN} "zsh应该已经安装成功!!!" + colorEcho ${BLUE} "开始修改zsh的相关配置信息,使其更加好用…………" + echo "" + cat >~/oh-my-zsh-plugins-list.txt <>/etc/environment</etc/environment.d/octopus-agent.conf<