Skip to content

Commit

Permalink
增加动态安装卸载的命令解析执行器
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Sep 12, 2023
1 parent 64d7eeb commit 7fcf5af
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 3 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,37 @@
/*
* 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;
import com.huaweicloud.sermant.core.common.LoggerFactory;

import java.util.logging.Logger;

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

@Override
public void execute(String commandContent) {
AgentCoreEntrance.unInstall();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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", "卸载agent"),
/**
* 安装插件指令
*/
INSTALL_PLUGINS("INSTALL-PLUGINS", "安装插件"),
/**
* 卸载插件指令
*/
UNINSTALL_PLUGINS("UNINSTALL-PLUGINS", "卸载插件");
private final String code;

private final String desc;

Command(String code, String desc) {
this.code = code;
this.desc = desc;
}

public String getCode() {
return code;
}

public String getDesc() {
return desc;
}
}
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 commandContent 指令的具体内容
*/
void execute(String commandContent);
}
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 java.util.HashMap;
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.getCode(), new PluginsInstallCommandExecutor());
COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_AGENT.getCode(), new AgentUnInstallCommandExecutor());
COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_PLUGINS.getCode(), new PluginsUnInstallCommandExecutor());
}

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

/**
* 处理指令
*
* @param command 指令
* @param commandContent 指令内容
*/
public static void process(String command, String commandContent) {
CommandExecutor commandExecutor = chooseExecutor(command);
if (commandExecutor == null) {
LOGGER.log(Level.SEVERE, "No corresponding command executor found");
return;
}
commandExecutor.execute(commandContent);
}

/**
* 选择命令执行器
*
* @param command 命令
* @return 命令执行器
*/
public static CommandExecutor chooseExecutor(String command) {
return COMMAND_EXECUTOR_MAP.get(command);
}
}
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 commandContent) {
if (StringUtils.isEmpty(commandContent)) {
LOGGER.log(Level.FINER, "The installed plugin information is empty");
return;
}
String[] pluginNames = commandContent.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 commandContent) {
if (StringUtils.isEmpty(commandContent)) {
LOGGER.log(Level.FINER, "The unInstalled plugin information is empty");
return;
}
String[] pluginNames = commandContent.split(",");
PluginManager.unInstall(Arrays.stream(pluginNames).collect(Collectors.toSet()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ 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) {
LOGGER.info("The following is the specific logic for plugin installation");
}

/**
* 卸载插件
*
* @param plugins 插件名称集合
*/
public static void unInstall(Set<String> plugins) {
LOGGER.info("The following is the specific logic for uninstalling plugins");
}

/**
* 卸载所有插件
*/
public static void unInstallAll() {
LOGGER.info("The following is the specific logic for uninstalling all plugins");
}

/**
* JarFile消费者
*
Expand Down
Loading

0 comments on commit 7fcf5af

Please sign in to comment.