[ agent ] [ status ] - app status - 3

This commit is contained in:
IceDerce
2023-01-06 17:01:41 +08:00
parent 31262d267e
commit 520d835db4
4 changed files with 77 additions and 40 deletions

View File

@@ -3,6 +3,7 @@ package io.wdd.agent.executor;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.*;
@@ -14,6 +15,7 @@ import static java.util.stream.Collectors.groupingBy;
@Service
@Slf4j
@Lazy
public class AppStatusExecutor {
private static ArrayList<ArrayList<String>> APP_STATUS_CHECK_COMMAND;
@@ -39,6 +41,7 @@ public class AppStatusExecutor {
}
public HashMap<String, Set<String>> checkAppStatus(boolean allAppStatus){
// check all app status
@@ -58,7 +61,8 @@ public class AppStatusExecutor {
// deal with the app status future result
appStatusFuture -> {
try {
return appStatusFuture.get(5, TimeUnit.SECONDS);
return appStatusFuture.get(15, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {

View File

@@ -3,6 +3,8 @@ package io.wdd.agent.executor;
import io.wdd.agent.executor.config.CommandPipelineBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -28,25 +30,38 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
public String[] call() throws Exception {
String[] result = new String[2];
// appName is fixed here
result[1] = appName;
// set the specific app service name
commandList.get(0).set(2, ALL_APP_NEED_TO_MONITOR_STATUS.get(appName));
log.debug("current app [{}] status command are => {}", appName, commandList);
/* ProcessBuilder processBuilder = new ProcessBuilder(commandList);
// judge if the app is existed !
ProcessBuilder processBuilder = new ProcessBuilder(commandList.get(0));
Process process = processBuilder.start();
boolean waitFor = process.waitFor(5, TimeUnit.SECONDS);
boolean waitFor = process.waitFor(20, TimeUnit.SECONDS);*/
if (ObjectUtils.isNotEmpty(waitFor)) {
// judge by error stream
String error = new BufferedReader(new InputStreamReader(process.getErrorStream())).readLine();
if (StringUtils.isNotEmpty(error)) {
// app not existed!
log.debug("app not installed !");
result[0] = "NotInstall";
} else {
log.debug("app existed! and then check the running status !");
// app existed! and then check the running status !
// get result from the command pipeline builder
List<String> resultList = CommandPipelineBuilder.runGetResult(commandList);
result[1] = appName;
if (ObjectUtils.isNotEmpty(resultList)) {
log.debug("app status command has accomplished !");
String appStatusCommandResult = resultList.get(0);
Assert.notNull(appStatusCommandResult, "app status command result is null !");
if (appStatusCommandResult.startsWith("1")) {
result[0] = "Healthy";
} else if (appStatusCommandResult.startsWith("0")) {
@@ -55,8 +70,10 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
result[0] = "NotInstall";
}
}
log.debug("app [ {} ] status check result is => {} ", appName, resultList);
}
}
log.debug("app status check ok result is => [ {} ]", result);
return result;
}
}

View File

@@ -2,8 +2,6 @@ package io.wdd.agent.executor.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
@@ -13,6 +11,11 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* https://stackoverflow.com/questions/3776195/using-java-processbuilder-to-execute-a-piped-command
* https://www.baeldung.com/java-lang-processbuilder-api
* https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessBuilder.html#startPipeline-java.util.List-
*/
@Slf4j
public class CommandPipelineBuilder {
@@ -27,6 +30,11 @@ public class CommandPipelineBuilder {
log.debug("command => [ {} ] , execute result is [ {} ]", commandList, resultList);
// error stream can't be got
// maybe this is the design patten of ProcessBuilder
/*List<String> error = new BufferedReader(new InputStreamReader(processList.get(lastCommandIndex).getErrorStream())).lines().collect(Collectors.toList());
log.debug("command => {} error is {}", commandList, error);*/
return resultList;
} catch (IOException e) {
@@ -71,7 +79,8 @@ public class CommandPipelineBuilder {
return ProcessBuilder.startPipeline(List.of(
new ProcessBuilder(commandList1)
.inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE),
.inheritIO()
.redirectOutput(ProcessBuilder.Redirect.PIPE),
new ProcessBuilder(commandList2)
.redirectError(ProcessBuilder.Redirect.INHERIT)
));

View File

@@ -1,30 +1,37 @@
#!/bin/bash
echo start to update !
apt-get update
command_exists() {
command -v "$@" >/dev/null 2>&1
}
command_exists "docker info"
if [[ $? -ne 0 ]] ; then
echo "no"
else
echo "yes"
fi
echo "
dockerVersion="20.10.10"
echo start to install nginx
apt-get install nginx -y
echo
echo start to uninstall nginx
apt remove nginx -y
echo $dockerVersion | cut -d"." -f-2
echo
echo start to get ip info
curl https://ipinfo.io
export JAVA_OPTS="-Xms2048m -Xmx2048m -Dfile.encoding=utf-8 -Dspring.profiles.active=k3s -Dspring.cloud.nacos.config.group=k3s -Dspring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml -Dspring.cloud.nacos.config.extension-configs[0].group=k3s -Ddebug=false -Dlogging.level.io.wdd.server=info"
export OctopusServerContainerName="octopus-server"
echo
echo --- end ---
docker container stop ${OctopusServerContainerName}
sleep 2
docker container rm ${OctopusServerContainerName}
docker image rmi docker.io/icederce/wdd-octopus-server:latest
systemctl status nginx.service | grep -c "active (running)"
docker logs --tail 500 -f ${ServerContainerName}
docker run -d \
-p 9999:9999 \
--name ${OctopusServerContainerName} \
--env JAVA_OPTS="${JAVA_OPTS}" \
docker.io/icederce/wdd-octopus-server:latest