Skip to content

Commit

Permalink
增加动态安装卸载的命令解析执行器
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Sep 13, 2023
1 parent 64d7eeb commit 05dd8fd
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.huaweicloud.sermant.core.notification.NotificationManager;
import com.huaweicloud.sermant.core.notification.SermantNotificationType;
import com.huaweicloud.sermant.core.operation.OperationManager;
import com.huaweicloud.sermant.core.plugin.PluginManager;
import com.huaweicloud.sermant.core.plugin.PluginSystemEntrance;
import com.huaweicloud.sermant.core.plugin.agent.ByteEnhanceManager;
import com.huaweicloud.sermant.core.plugin.agent.adviser.AdviserScheduler;
Expand Down Expand Up @@ -99,4 +100,11 @@ public static void install(String artifact, Map<String, Object> argsMap, Instrum
NotificationManager.doNotify(new NotificationInfo(SermantNotificationType.LOAD_COMPLETE, null));
}
}

/**
* agent卸载方法
*/
public static void unInstall() {
PluginManager.unInstallAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

import com.huaweicloud.sermant.core.AgentCoreEntrance;

/**
* Agent卸载命令执行器
*
* @author zhp
* @since 2023-09-09
*/
public class AgentUnInstallCommandExecutor implements CommandExecutor {
@Override
public void execute(String args) {
AgentCoreEntrance.unInstall();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

/**
* 动态安装卸载的指令枚举
*
* @author zhp
* @since 2023-09-09
*/
public enum Command {
/**
* 卸载agent指令
*/
UNINSTALL_AGENT("UNINSTALL-AGENT"),
/**
* 安装插件指令
*/
INSTALL_PLUGINS("INSTALL-PLUGINS"),
/**
* 卸载插件指令
*/
UNINSTALL_PLUGINS("UNINSTALL-PLUGINS");
private final String value;

Command(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

/**
* 指令执行器
*
* @author zhp
* @since 2023-09-09
*/
public interface CommandExecutor {

/**
* 执行指令
*
* @param args 指令参数
*/
void execute(String args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;

/**
* 命令处理器
*
* @author zhp
* @since 2023-09-09
*/
public class CommandProcessor {
/**
* 命令执行器Map
*/
private static final Map<String, CommandExecutor> COMMAND_EXECUTOR_MAP = new HashMap<>();

private static final Logger LOGGER = LoggerFactory.getLogger();

static {
COMMAND_EXECUTOR_MAP.put(Command.INSTALL_PLUGINS.getValue(), new PluginsInstallCommandExecutor());
COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_AGENT.getValue(), new AgentUnInstallCommandExecutor());
COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_PLUGINS.getValue(), new PluginsUnInstallCommandExecutor());
}

/**
* 构造函数
*/
private CommandProcessor() {
}

/**
* 处理指令
*
* @param command 指令
*/
public static void process(String command) {
if (StringUtils.isEmpty(command)) {
LOGGER.warning("Command information is empty.");
return;
}
String[] commandInfo = command.trim().split(":");
if (commandInfo.length == 0) {
LOGGER.warning("Illegal command information.");
return;
}
CommandExecutor commandExecutor = COMMAND_EXECUTOR_MAP.get(commandInfo[0].toUpperCase(Locale.ROOT));
if (commandExecutor == null) {
LOGGER.warning("No corresponding command executor found.");
return;
}
String commandArgs = commandInfo.length > 1 ? commandInfo[1] : null;
commandExecutor.execute(commandArgs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.plugin.PluginManager;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* 插件安装命令执行器
*
* @author zhp
* @since 2023-09-09
*/
public class PluginsInstallCommandExecutor implements CommandExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger();

@Override
public void execute(String args) {
if (StringUtils.isEmpty(args)) {
LOGGER.log(Level.WARNING, "The argument of command[INSTALL-PLUGINS] is empty.");
return;
}
String[] pluginNames = args.split("\\|");
PluginManager.install(Arrays.stream(pluginNames).collect(Collectors.toSet()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.command;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.plugin.PluginManager;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* 插件卸载命令执行器
*
* @author zhp
* @since 2023-09-09
*/
public class PluginsUnInstallCommandExecutor implements CommandExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger();

@Override
public void execute(String args) {
if (StringUtils.isEmpty(args)) {
LOGGER.log(Level.WARNING, "The argument of command[UNINSTALL-PLUGINS] is empty.");
return;
}
String[] pluginNames = args.split("\\|");
PluginManager.unInstall(Arrays.stream(pluginNames).collect(Collectors.toSet()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ private static File getServiceDir(String pluginPath) {
return new File(pluginPath + File.separatorChar + PluginConstant.SERVICE_DIR_NAME);
}

/**
* 安装插件
*
* @param plugins 插件名称集合
*/
public static void install(Set<String> plugins) {
}

/**
* 卸载插件
*
* @param plugins 插件名称集合
*/
public static void unInstall(Set<String> plugins) {
}

/**
* 卸载所有插件
*/
public static void unInstallAll() {
}

/**
* JarFile消费者
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.huaweicloud.sermant.core.command;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class CommandExecutorTest {
private CommandExecutor commandExecutor;

@BeforeEach
public void setUp() {
commandExecutor = Mockito.mock(CommandExecutor.class);
}

@AfterEach
public void tearDown() {
commandExecutor = null;
}

@Test
public void testExecuteWhenArgsIsValidThenNoException() {
// Arrange
String validArgs = "validArgs";

// Act & Assert
assertDoesNotThrow(() -> commandExecutor.execute(validArgs));
}

@Test
public void testExecuteWhenArgsIsNullThenNoException() {
// Arrange
String nullArgs = null;

// Act & Assert
assertDoesNotThrow(() -> commandExecutor.execute(nullArgs));
}
}
Loading

0 comments on commit 05dd8fd

Please sign in to comment.