[ server ] [ executor ] - optimize code

This commit is contained in:
zeaslity
2022-12-27 16:02:32 +08:00
parent 9bc516bdbd
commit 37752ca07c
16 changed files with 189 additions and 166 deletions

View File

@@ -128,9 +128,9 @@ jobs:
echo "--------------------------------------" echo "--------------------------------------"
cd ./agent/target cd ./agent/target
ls 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" 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 cd /home/runner/work/ProjectOctopus/ProjectOctopus
echo "--------------------------------------" echo "--------------------------------------"
cat /proc/cpuinfo cat /proc/cpuinfo

View File

@@ -30,7 +30,7 @@ public class CommandExecutor {
/** /**
* handle command from octopus server * handle command from octopus server
* *
* @param executionMessage get from EXECUTOR_HANDLER * @param executionMessage get from EXECUTOR_HANDLERju
*/ */
public void execute(ExecutionMessage executionMessage) { public void execute(ExecutionMessage executionMessage) {

View File

@@ -1,5 +1,7 @@
package io.wdd.rpc.execute.service; 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.executor.ExecutionMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessage; import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType; import io.wdd.common.beans.rabbitmq.OctopusMessageType;
@@ -18,6 +20,8 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Resource @Resource
ToAgentMessageSender messageSender; ToAgentMessageSender messageSender;
@Resource
ObjectMapper objectMapper;
@Override @Override
public String SendCommandToAgent(String topicName, String command) { public String SendCommandToAgent(String topicName, String command) {
@@ -34,11 +38,22 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
OctopusMessage octopusMessage = this.generateOctopusMessage(topicName, type, commandList); 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); messageSender.send(octopusMessage);
ExecutionMessage content = (ExecutionMessage) octopusMessage.getContent(); return executionMessage.getResultKey();
return content.getResultKey();
} }
@@ -53,15 +68,16 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
private OctopusMessage generateOctopusMessage(String topicName, String type, List<String> commandList){ private OctopusMessage generateOctopusMessage(String topicName, String type, List<String> commandList){
ExecutionMessage executionMessage = generateExecutionMessage(
type,
commandList,
generateCommandResultKey(topicName)
);
return OctopusMessage.builder() return OctopusMessage.builder()
.type(OctopusMessageType.EXECUTOR) .type(OctopusMessageType.EXECUTOR)
.init_time(LocalDateTime.now()) .init_time(LocalDateTime.now())
.content(generateExecutionMessage( .content(executionMessage)
type,
commandList,
generateCommandResultKey(topicName)
))
.uuid(topicName) .uuid(topicName)
.build(); .build();
} }

View File

@@ -104,6 +104,7 @@ public class AcceptAgentInitInfo {
if (!checkAgentAlreadyRegister(agentQueueTopic)) { if (!checkAgentAlreadyRegister(agentQueueTopic)) {
// 3. save the agent info into database // 3. save the agent info into database
// backend fixed thread daemon to operate the database ensuring the operation is correct ! // 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)) { if (!databaseOperator.saveInitOctopusAgentInfo(serverInfoVO)) {
throw new MyRuntimeException("database save agent info error !"); throw new MyRuntimeException("database save agent info error !");
} }

View File

@@ -40,8 +40,10 @@ public class OctopusMessageHandlerServer {
// todo what to do after received the result // todo what to do after received the result
// log info ? // collect all message from agent and log to somewhere
// update the database
// handle the result // 1. send some info to the specific topic name
// 2. judge from which agent the message are
//
} }
} }

View File

@@ -53,9 +53,13 @@ public class ToAgentMessageSender {
public void send(OctopusMessage octopusMessage) { 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));
} }

View File

@@ -10,6 +10,7 @@ import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.coreService.CoreServerService; import io.wdd.server.coreService.CoreServerService;
import io.wdd.server.service.*; import io.wdd.server.service.*;
import io.wdd.server.utils.EntityUtils; import io.wdd.server.utils.EntityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@@ -24,6 +25,7 @@ import java.util.stream.Collectors;
@Service @Service
@Slf4j
public class CoreServerServiceImpl implements CoreServerService { public class CoreServerServiceImpl implements CoreServerService {
@Resource @Resource

View File

@@ -2,11 +2,11 @@ spring:
application: application:
name: octopus-server name: octopus-server
profiles: profiles:
active: k3s active: local
cloud: cloud:
nacos: nacos:
config: config:
group: k3s group: local
config-retry-time: 3000 config-retry-time: 3000
file-extension: yaml file-extension: yaml
max-retry: 3 max-retry: 3
@@ -16,5 +16,5 @@ spring:
timeout: 5000 timeout: 5000
config-long-poll-timeout: 5000 config-long-poll-timeout: 5000
extension-configs: extension-configs:
- group: k3s - group: local
data-id: common-k3s.yaml data-id: common-local.yaml

View File

@@ -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

View File

@@ -3,6 +3,7 @@
##### environment variables ###### ##### environment variables ######
RepoSourcePath=https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell RepoSourcePath=https://raw.githubusercontent.com/zeaslity/ProjectOctopus/main/source/src/main/java/io/wdd/source/shell
OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/
DependLibFiles=( DependLibFiles=(
wdd-lib-file.sh wdd-lib-file.sh
wdd-lib-log.sh wdd-lib-log.sh
@@ -199,7 +200,8 @@ DownloadAllFile() {
colorEcho $BLUE "start to download octopus agent !" colorEcho $BLUE "start to download octopus agent !"
# check for latest version # check for latest version
# download the lasted jar # 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 FunctionSuccess
FunctionEnd FunctionEnd
@@ -265,24 +267,32 @@ InstallJDKPackage() {
} }
systemdAgent(){ 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 <<EOF cat >/etc/systemd/system/octopus-agent.service <<EOF
[Unit] [Unit]
Description=Octopus Agent Description=Octopus Agent Service
Documentation=https://octopus.107421.xyz/ Documentation=https://octopus.107421.xyz/
After=network.target After=syslog.target network.target
[Service] [Service]
SuccessExitStatus=143
PermissionsStartOnly=true PermissionsStartOnly=true
LimitNOFILE=1048576 LimitNOFILE=1048576
LimitNPROC=65535 LimitNPROC=65535
User=root
WorkingDirectory=/octopus-agent
ExecStart=java -jar /octopus-agent/agent.jar ${JAVA_OPTS}
ExecReload=source /etc/environment
Restart=on-failure
User=root
Type=simple
WorkingDirectory=/octopus-agent
ExecStart=java -jar /octopus-agent/agent.jar ${JAVA_OPTS}
ExecStop=/bin/kill -15 $MAINPID
Restart=on-failure
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF

View File

@@ -0,0 +1,20 @@
#!/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

View File

@@ -0,0 +1,25 @@
#!/bin/bash
. ./lib/wdd-lib-log.sh
. ./lib/wdd-lib-sys.sh
. ./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\
warn "agent-bootup.sh not exist! start to download !"
cd /octopus-agent
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

View File

@@ -263,26 +263,26 @@ else
machineNumber=99 machineNumber=99
fi fi
cat >>/etc/environment<<EOF cat >>/etc/environment<<EOF
export serverName="${city}-${hostArch}-${machineNumber}" export serverName=${city}-${hostArch}-${machineNumber}
export serverIpPbV4="$public_ipv4" export serverIpPbV4=$public_ipv4
export serverIpInV4="" export serverIpInV4=
export serverIpPbV6="" export serverIpPbV6=
export serverIpInV6="" export serverIpInV6=
export location="$city $region $country" export location=$city $region $country
export provider="$org" export provider=$org
export managePort="$(netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2)" 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 cpuBrand=$cpuName
export memoryTotal="$tram" export memoryTotal=$tram
export diskTotal="$disk_total_size" export diskTotal=$disk_total_size
export diskUsage="$disk_used_size" export diskUsage=$disk_used_size
export archInfo="$arch ($lbit Bit)" export archInfo=$arch ($lbit Bit)
export osInfo="$opsy" export osInfo=$opsy
export osKernelInfo="$kern" export osKernelInfo=$kern
export tcpControl="$tcpctrl" export tcpControl=$tcpctrl
export virtualization="$virt" export virtualization=$virt
export ioSpeed="$ioavg MB/s" export ioSpeed=$ioavg MB/s
export machineId="$(cat /etc/machine-id)" export machineId=$(cat /etc/machine-id)
EOF EOF
log "env collect complete!" log "env collect complete!"

View File

@@ -1,112 +1,27 @@
#!/bin/bash #!/bin/bash
#
# Library for logging functions
# Constants
RESET='\033[0m'
RED='\033[38;5;1m'
GREEN='\033[38;5;2m'
YELLOW='\033[38;5;3m'
MAGENTA='\033[38;5;5m'
CYAN='\033[38;5;6m'
# Functions . ./wdd-lib-log.sh
######################## OctopusAgentUrl=https://happybirthday.107421.xyz/octopus-agent/
# Print to STDERR
# Arguments:
# Message to print
# Returns:
# None
#########################
stderr_print() {
# 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it
local bool="${BITNAMI_QUIET:-false}"
# comparison is performed without regard to the case of alphabetic characters
shopt -s nocasematch
if ! [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then
printf "%b\\n" "${*}" >&2
fi
}
########################
# 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
}
######################## CheckAndDownloadLatestVersion(){
# Indent a string
# Arguments: log "checking for the latest version"
# $1 - string local latestVersion=$(curl $OctopusAgentUrl | grep -v h1 | grep "a href=" | awk '{print$2}' |cut -d">" -f2 | cut -d"<" -f1)
# $2 - number of indentation characters (default: 4)
# $3 - indentation character (default: " ") log "octopus agent latest version is => [ $latestVersion ]"
# Returns:
# None rm -rf /octopus-agent/*.jar
######################### cd /octopus-agent
indent() {
local string="${1:-}" log "start to download the latest version !"
local num="${2:?missing num}"
local char="${3:-" "}" wget "$OctopusAgentUrl$latestVersion"
# Build the indentation unit string cp "$OctopusAgentUrl$latestVersion" agent.jar
local indent_unit=""
for ((i = 0; i < num; i++)); do log ""
indent_unit="${indent_unit}${char}" ls /octopus-agent/ | grep jar
done
# shellcheck disable=SC2001 }
# Complex regex, see https://github.com/koalaman/shellcheck/wiki/SC2001#exceptions
echo "$string" | sed "s/^/${indent_unit}/"
}

View File

@@ -95,6 +95,46 @@ check_sys() {
return 0 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 () { tmp () {