[agent] [ executor ] - execute file

This commit is contained in:
IceDerce
2022-12-11 16:02:53 +08:00
parent 763ec56f0e
commit 2f5bb078cd
12 changed files with 190 additions and 6 deletions

View File

@@ -1,17 +1,44 @@
package io.wdd.agent.config.message.handler;
import io.wdd.agent.excuetor.shell.CommandExecutor;
import io.wdd.agent.excuetor.shell.FunctionExecutor;
import io.wdd.common.beans.executor.ExecutionMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import static io.wdd.agent.excuetor.function.CollectAllFunctionToServer.ALL_FUNCTIONS;
@Component
public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
@Resource
CommandExecutor commandExecutor;
@Resource
FunctionExecutor functionExecutor;
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.EXECUTOR)) {
return next.handle(octopusMessage);
}
ExecutionMessage executionMessage = (ExecutionMessage) octopusMessage.getContent();
String executionType = executionMessage.getType();
if (ALL_FUNCTIONS.contains(executionType)) {
// execute the exist function
functionExecutor.execute(executionMessage);
} else {
// handle command
commandExecutor.execute(executionMessage);
}
return true;
}
}

View File

@@ -0,0 +1,27 @@
package io.wdd.agent.excuetor.function;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
@Component
@Lazy
public class CollectAllFunctionToServer {
public static Set<String> ALL_FUNCTIONS = new HashSet<>(128);
/**
* store the Octopus Agent Functions and Reflection Class Path
* key: function name
* value: function class relative path
*
*/
public static HashMap<String, String> FUNCTION_REFLECTION = new HashMap<>(128);
}

View File

@@ -0,0 +1,25 @@
package io.wdd.agent.excuetor.function;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Lazy
@Slf4j
public class TestFunction {
public void execute(String streamKey) {
this.getClass().getName()
ProcessBuilder processBuilder = new ProcessBuilder();
}
}

View File

@@ -0,0 +1,3 @@
wget -O

View File

@@ -0,0 +1,11 @@
# all agent show report its functions to server at start up delay
---
1. delay 60s
2. report all functions
1. install nginx
2. install xray
3. etc
3.

View File

@@ -4,7 +4,9 @@ import com.google.common.io.ByteStreams;
import io.wdd.agent.excuetor.redis.StreamSender;
import io.wdd.agent.excuetor.thread.DaemonLogThread;
import io.wdd.agent.excuetor.thread.LogToStreamSender;
import io.wdd.common.beans.executor.ExecutionMessage;
import io.wdd.common.handler.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@@ -18,12 +20,24 @@ import java.util.concurrent.Future;
@Configuration
@Slf4j
public class CommandExecutor {
@Resource
StreamSender streamSender;
/**
*
* handle command from octopus server
*
* @param executionMessage get from EXECUTOR_HANDLER
*/
public void execute(ExecutionMessage executionMessage) {
}
public void execute(String streamKey, List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder(command);
@@ -55,7 +69,7 @@ public class CommandExecutor {
// a command shell don't understand how long it actually take
int processResult = process.waitFor();
System.out.println("processResult = " + processResult);
log.info("current shell command [{}] result is [{}]", processBuilder.command(), processResult);
DaemonLogThread.start(toStreamSender);
@@ -84,4 +98,6 @@ public class CommandExecutor {
return s;
}
}

View File

@@ -0,0 +1,34 @@
package io.wdd.agent.excuetor.shell;
import io.wdd.common.beans.executor.ExecutionMessage;
import io.wdd.common.handler.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import static io.wdd.agent.excuetor.function.CollectAllFunctionToServer.FUNCTION_REFLECTION;
@Service
@Slf4j
public class FunctionExecutor {
public void execute(ExecutionMessage executionMessage) {
String resultKey = executionMessage.getResultKey();
String functionClassPath = FUNCTION_REFLECTION.get(executionMessage.getContend());
Method execute = null;
try {
execute = Class.forName(functionClassPath).getMethod("execute", String.class);
ReflectionUtils.invokeMethod(execute, functionClassPath, resultKey);
} catch (NoSuchMethodException | ClassNotFoundException e) {
throw new MyRuntimeException(" Function Executor Reflection Error ! {} + {}", e.getCause(), e.getMessage());
}
}
}

View File

@@ -23,7 +23,6 @@ public class DaemonLogThread {
}
public static Future<?> start(Runnable logToSenderTask) {
return executorService.submit(logToSenderTask);

View File

@@ -18,10 +18,8 @@ public class LogToStreamSender implements Runnable {
this.streamKey = streamKey;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(contentInputStream)).lines()
.map(
String::valueOf
@@ -31,7 +29,5 @@ public class LogToStreamSender implements Runnable {
return lineStr;
}
).forEach(System.out::println);
}
}