fuweng11 commented on code in PR #10098: URL: https://github.com/apache/inlong/pull/10098#discussion_r1598230422
########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/cmd/CommandExecutorImpl.java: ########## @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.inlong.manager.service.cmd; + +import org.apache.inlong.manager.common.consts.InlongConstants; +import org.apache.inlong.manager.pojo.cluster.agent.AgentClusterNodeRequest; +import org.apache.inlong.manager.service.cmd.shell.ShellExecutorImpl; +import org.apache.inlong.manager.service.cmd.shell.ShellTrackerImpl; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Service +public class CommandExecutorImpl implements CommandExecutor { + + private static final Logger LOG = LoggerFactory.getLogger(CommandExecutorImpl.class); + + private String join(List<String> strings) { + StringBuilder buffer = new StringBuilder(); + for (String string : strings) { + buffer.append(string).append(InlongConstants.NEW_LINE); + } + return buffer.toString(); + } + + private String join(String... strings) { + StringBuffer buffer = new StringBuffer(); + for (String string : strings) { + buffer.append(string).append(InlongConstants.BLANK); + } + return buffer.toString(); + } + + @Override + public CommandResult exec(String cmd) throws Exception { + ShellTrackerImpl shellTracker = new ShellTrackerImpl(); + ShellExecutorImpl shellExecutor = new ShellExecutorImpl(shellTracker); + shellExecutor.syncExec("sh", "-c", cmd); + String cmdMsg = join("sh", "-c", cmd); + LOG.debug("run command : " + cmdMsg); + CommandResult commandResult = new CommandResult(); + commandResult.setCode(shellTracker.getCode()); + commandResult.setResult(join(shellTracker.getResult())); + if (commandResult.getCode() != 0) { + throw new Exception("command " + cmdMsg + " exec failed, code = " + + commandResult.getCode() + ", output = " + commandResult.getResult()); + } + LOG.debug(commandResult.getResult()); + return commandResult; + } + + @Override + public CommandResult execRemote(AgentClusterNodeRequest clusterNodeRequest, String cmd) throws Exception { + String cmdShell = "./conf/exec_cmd.exp"; + String ip = clusterNodeRequest.getIp(); + String port = String.valueOf(clusterNodeRequest.getSshPort()); + String user = clusterNodeRequest.getUsername(); + String password = clusterNodeRequest.getPassword(); + String remoteCommandTimeout = "20000"; + + cmd = "sh -c \"" + cmd + "\""; + String cmdMsg = join(cmdShell, ip, user, password, remoteCommandTimeout, cmd, port); + LOG.info("run remote command : " + cmdMsg); + + ShellTrackerImpl shellTracker = new ShellTrackerImpl(); + ShellExecutorImpl shellExecutor = new ShellExecutorImpl(shellTracker); + shellExecutor.syncExec(cmdShell, ip, user, password, remoteCommandTimeout, cmd, port); + + CommandResult commandResult = new CommandResult(); + commandResult.setCode(shellTracker.getCode()); + commandResult.setResult(join(shellTracker.getResult())); + + LOG.debug(commandResult.getResult()); + if (commandResult.getCode() != 0) { + throw new Exception( + "remote command " + cmdMsg + " exec failed, code = " + commandResult.getCode() + ", output = " + + commandResult.getResult()); + } + return commandResult; + } + + @Override + public CommandResult modifyConfig(AgentClusterNodeRequest clusterNodeRequest, Map<String, String> configMap, + String confPath) + throws Exception { + List<String> configList = configMap.entrySet().stream() + .map(entry -> "grep " + entry.getKey() + " " + confPath + " && sed -i 's%^" + entry.getKey() + ".*%" Review Comment: No need. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
