[agent] [ executor ] - execute file
This commit is contained in:
@@ -1,17 +1,44 @@
|
|||||||
package io.wdd.agent.config.message.handler;
|
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.OctopusMessage;
|
||||||
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
|
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import static io.wdd.agent.excuetor.function.CollectAllFunctionToServer.ALL_FUNCTIONS;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
|
public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
CommandExecutor commandExecutor;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
FunctionExecutor functionExecutor;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handle(OctopusMessage octopusMessage) {
|
public boolean handle(OctopusMessage octopusMessage) {
|
||||||
|
|
||||||
if (!octopusMessage.getType().equals(OctopusMessageType.EXECUTOR)) {
|
if (!octopusMessage.getType().equals(OctopusMessageType.EXECUTOR)) {
|
||||||
return next.handle(octopusMessage);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
|
wget -O
|
||||||
@@ -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.
|
||||||
@@ -4,7 +4,9 @@ import com.google.common.io.ByteStreams;
|
|||||||
import io.wdd.agent.excuetor.redis.StreamSender;
|
import io.wdd.agent.excuetor.redis.StreamSender;
|
||||||
import io.wdd.agent.excuetor.thread.DaemonLogThread;
|
import io.wdd.agent.excuetor.thread.DaemonLogThread;
|
||||||
import io.wdd.agent.excuetor.thread.LogToStreamSender;
|
import io.wdd.agent.excuetor.thread.LogToStreamSender;
|
||||||
|
import io.wdd.common.beans.executor.ExecutionMessage;
|
||||||
import io.wdd.common.handler.MyRuntimeException;
|
import io.wdd.common.handler.MyRuntimeException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -18,12 +20,24 @@ import java.util.concurrent.Future;
|
|||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
public class CommandExecutor {
|
public class CommandExecutor {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
StreamSender streamSender;
|
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) {
|
public void execute(String streamKey, List<String> command) {
|
||||||
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||||
@@ -55,7 +69,7 @@ public class CommandExecutor {
|
|||||||
// a command shell don't understand how long it actually take
|
// a command shell don't understand how long it actually take
|
||||||
int processResult = process.waitFor();
|
int processResult = process.waitFor();
|
||||||
|
|
||||||
System.out.println("processResult = " + processResult);
|
log.info("current shell command [{}] result is [{}]", processBuilder.command(), processResult);
|
||||||
|
|
||||||
DaemonLogThread.start(toStreamSender);
|
DaemonLogThread.start(toStreamSender);
|
||||||
|
|
||||||
@@ -84,4 +98,6 @@ public class CommandExecutor {
|
|||||||
return s;
|
return s;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,6 @@ public class DaemonLogThread {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Future<?> start(Runnable logToSenderTask) {
|
public static Future<?> start(Runnable logToSenderTask) {
|
||||||
|
|
||||||
return executorService.submit(logToSenderTask);
|
return executorService.submit(logToSenderTask);
|
||||||
|
|||||||
@@ -18,10 +18,8 @@ public class LogToStreamSender implements Runnable {
|
|||||||
this.streamKey = streamKey;
|
this.streamKey = streamKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
new BufferedReader(new InputStreamReader(contentInputStream)).lines()
|
new BufferedReader(new InputStreamReader(contentInputStream)).lines()
|
||||||
.map(
|
.map(
|
||||||
String::valueOf
|
String::valueOf
|
||||||
@@ -31,7 +29,5 @@ public class LogToStreamSender implements Runnable {
|
|||||||
return lineStr;
|
return lineStr;
|
||||||
}
|
}
|
||||||
).forEach(System.out::println);
|
).forEach(System.out::println);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package io.wdd.common.beans.executor;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@SuperBuilder(toBuilder = true)
|
||||||
|
public class ExecutionMessage {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
private String contend;
|
||||||
|
|
||||||
|
private String resultKey;
|
||||||
|
|
||||||
|
}
|
||||||
26
source/src/main/java/io/wdd/source/shell/test.sh
Normal file
26
source/src/main/java/io/wdd/source/shell/test.sh
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
echo "start to update !"
|
||||||
|
apt-get update
|
||||||
|
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "start to install nginx"
|
||||||
|
apt-get install nginx -y
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "start to uninstall nginx"
|
||||||
|
apt remove nginx -y
|
||||||
|
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "start to get ip info"
|
||||||
|
curl https://ipinfo.io
|
||||||
|
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "--- end ---"
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user