-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64d7eeb
commit 7054c9b
Showing
11 changed files
with
380 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ore/src/main/java/com/huaweicloud/sermant/core/command/AgentUnInstallCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...re/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...nt-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
77 changes: 77 additions & 0 deletions
77
...t-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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.Level; | ||
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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ore/src/main/java/com/huaweicloud/sermant/core/command/PluginsInstallCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...e/src/main/java/com/huaweicloud/sermant/core/command/PluginsUnInstallCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...gentcore-core/src/test/java/com/huaweicloud/sermant/core/command/CommandExecutorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.