Skip to content

Commit

Permalink
[INLONG-10907][Manager] Support reinstalling the installer
Browse files Browse the repository at this point in the history
  • Loading branch information
fuweng11 committed Aug 27, 2024
1 parent d90b249 commit c1d642c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.inlong.manager.common.enums.ModuleType;
import org.apache.inlong.manager.common.exceptions.BusinessException;
import org.apache.inlong.manager.common.util.AESUtils;
import org.apache.inlong.manager.common.util.CommonBeanUtils;
import org.apache.inlong.manager.common.util.Preconditions;
import org.apache.inlong.manager.dao.entity.InlongClusterEntity;
import org.apache.inlong.manager.dao.entity.InlongClusterNodeEntity;
Expand All @@ -33,6 +34,7 @@
import org.apache.inlong.manager.dao.mapper.PackageConfigEntityMapper;
import org.apache.inlong.manager.dao.mapper.UserEntityMapper;
import org.apache.inlong.manager.pojo.cluster.ClusterNodeRequest;
import org.apache.inlong.manager.pojo.cluster.agent.AgentClusterNodeDTO;
import org.apache.inlong.manager.pojo.cluster.agent.AgentClusterNodeRequest;
import org.apache.inlong.manager.service.cmd.CommandExecutor;

Expand All @@ -43,24 +45,25 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class AgentClusterNodeInstallOperator implements InlongClusterNodeInstallOperator {

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

public static final String INSTALLER_CONF_PATH = "/conf/installer.properties";
public static final String INSTALLER_START_CMD = "/bin/installer.sh start";
public static final String INSTALLER_RESTART_CMD = "/bin/installer.sh restart";
public static final String INSTALLER_STOP_CMD = "/bin/installer.sh restart";
public static final String AGENT_MANAGER_AUTH_SECRET_ID = "agent.manager.auth.secretId";
public static final String AGENT_MANAGER_AUTH_SECRET_KEY = "agent.manager.auth.secretKey";
public static final String AGENT_MANAGER_ADDR = "agent.manager.addr";
public static final String AGENT_CLUSTER_NAME = "agent.cluster.name";
public static final String AGENT_CLUSTER_TAG = "agent.cluster.tag";
public static final String AGENT_LOCAL_IP = "agent.local.ip";

private static final Logger LOGGER = LoggerFactory.getLogger(AgentClusterNodeInstallOperator.class);
@Autowired
private InlongClusterEntityMapper clusterEntityMapper;
@Autowired
Expand Down Expand Up @@ -91,45 +94,55 @@ public String getClusterNodeType() {

@Override
public boolean install(ClusterNodeRequest clusterNodeRequest, String operator) {
LOGGER.info("begin to insert agent inlong cluster node={}", clusterNodeRequest);
LOGGER.info("begin to insert agent cluster node={}", clusterNodeRequest);
try {
InlongClusterEntity clusterEntity = clusterEntityMapper.selectById(clusterNodeRequest.getParentId());
AgentClusterNodeRequest request = (AgentClusterNodeRequest) clusterNodeRequest;
commandExecutor.mkdir(request, agentInstallPath);
String downLoadUrl = getInstallerDownLoadUrl(request);
String fileName = downLoadUrl.substring(downLoadUrl.lastIndexOf('/') + 1);
commandExecutor.downLoadPackage(request, agentInstallPath, downLoadUrl);
commandExecutor.tarPackage(request, fileName, agentInstallPath);
String confFile = agentInstallPath + INSTALLER_CONF_PATH;
Map<String, String> configMap = new HashMap<>();
configMap.put(AGENT_LOCAL_IP, request.getIp());
configMap.put(AGENT_MANAGER_ADDR, managerUrl);
UserEntity userInfo = userEntityMapper.selectByName(operator);
Preconditions.expectNotNull(userInfo, "User doesn't exist");
String secretKey =
new String(AESUtils.decryptAsString(userInfo.getSecretKey(), userInfo.getEncryptVersion()));
configMap.put(AGENT_MANAGER_AUTH_SECRET_ID, operator);
configMap.put(AGENT_MANAGER_AUTH_SECRET_KEY, secretKey);
configMap.put(AGENT_CLUSTER_TAG, clusterEntity.getClusterTags());
configMap.put(AGENT_CLUSTER_NAME, clusterEntity.getName());
commandExecutor.modifyConfig(request, configMap, confFile);
deployInstaller(request, operator);
} catch (Exception e) {
clusterNodeEntityMapper.updateOperateLogById(clusterNodeRequest.getId(), e.getMessage());
String errMsg = String.format("install agent cluster node failed for ip=%s", clusterNodeRequest.getIp());
LOGGER.error(errMsg, e);
throw new BusinessException(errMsg);
}
LOGGER.info("success to install agent cluster node={}", clusterNodeRequest);
return true;
}

@Override
public boolean reInstall(ClusterNodeRequest clusterNodeRequest, String operator) {
LOGGER.info("begin to reInstall agent cluster node={}", clusterNodeRequest);
try {
AgentClusterNodeRequest request = (AgentClusterNodeRequest) clusterNodeRequest;
commandExecutor.rmDir(request, agentInstallPath.substring(0, agentInstallPath.lastIndexOf(File.separator)));
deployInstaller(request, operator);
String startCmd = agentInstallPath + INSTALLER_START_CMD;
commandExecutor.execRemote(request, startCmd);

} catch (Exception e) {
clusterNodeEntityMapper.updateOperateLogById(clusterNodeRequest.getId(), e.getMessage());
String errMsg = String.format("install installer failed for ip=%s", clusterNodeRequest.getIp());
String errMsg = String.format("reInstall agent cluster node failed for ip=%s", clusterNodeRequest.getIp());
LOGGER.error(errMsg, e);
throw new BusinessException(errMsg);
}
LOGGER.info("success to insert agent inlong cluster node={}", clusterNodeRequest);
LOGGER.info("success to re reInstall agent cluster node={}", clusterNodeRequest);
return true;
}

@Override
public boolean unload(InlongClusterNodeEntity clusterNodeEntity, String operator) {
// todo Provide agent uninstallation capability
InlongClusterEntity clusterEntity = clusterEntityMapper.selectById(clusterNodeEntity.getParentId());
try {
AgentClusterNodeRequest request = CommonBeanUtils.copyProperties(clusterNodeEntity,
AgentClusterNodeRequest::new, true);
AgentClusterNodeDTO agentClusterNodeDTO = AgentClusterNodeDTO.getFromJson(clusterNodeEntity.getExtParams());
CommonBeanUtils.copyProperties(agentClusterNodeDTO, request, true);
String stopCmd = agentInstallPath + INSTALLER_STOP_CMD;
commandExecutor.execRemote(request, stopCmd);
commandExecutor.rmDir(request, agentInstallPath.substring(0, agentInstallPath.lastIndexOf(File.separator)));
} catch (Exception e) {
clusterNodeEntityMapper.updateOperateLogById(clusterNodeEntity.getId(), e.getMessage());
String errMsg = String.format("unload agent cluster node failed for ip=%s", clusterNodeEntity.getIp());
LOGGER.error(errMsg, e);
throw new BusinessException(errMsg);
}
return true;
}

Expand All @@ -151,4 +164,26 @@ private String getInstallerDownLoadUrl(AgentClusterNodeRequest request) {
String.format("can't get installer download url for ip=%s, type=%s", request.getIp(),
request.getType()));
}

private void deployInstaller(AgentClusterNodeRequest request, String operator) throws Exception {
InlongClusterEntity clusterEntity = clusterEntityMapper.selectById(request.getParentId());
commandExecutor.mkdir(request, agentInstallPath);
String downLoadUrl = getInstallerDownLoadUrl(request);
String fileName = downLoadUrl.substring(downLoadUrl.lastIndexOf('/') + 1);
commandExecutor.downLoadPackage(request, agentInstallPath, downLoadUrl);
commandExecutor.tarPackage(request, fileName, agentInstallPath);
String confFile = agentInstallPath + INSTALLER_CONF_PATH;
Map<String, String> configMap = new HashMap<>();
configMap.put(AGENT_LOCAL_IP, request.getIp());
configMap.put(AGENT_MANAGER_ADDR, managerUrl);
UserEntity userInfo = userEntityMapper.selectByName(operator);
Preconditions.expectNotNull(userInfo, "User doesn't exist");
String secretKey =
new String(AESUtils.decryptAsString(userInfo.getSecretKey(), userInfo.getEncryptVersion()));
configMap.put(AGENT_MANAGER_AUTH_SECRET_ID, operator);
configMap.put(AGENT_MANAGER_AUTH_SECRET_KEY, secretKey);
configMap.put(AGENT_CLUSTER_TAG, clusterEntity.getClusterTags());
configMap.put(AGENT_CLUSTER_NAME, clusterEntity.getName());
commandExecutor.modifyConfig(request, configMap, confFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public interface InlongClusterNodeInstallOperator {
*/
boolean install(ClusterNodeRequest clusterNodeRequest, String operator);

/**
* ReInstalling cluster nodes.
*
* @param clusterNodeRequest cluster request
* @param operator operator
*/
boolean reInstall(ClusterNodeRequest clusterNodeRequest, String operator);

/**
* Uninstalling cluster nodes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ CommandResult downLoadPackage(AgentClusterNodeRequest clusterNodeRequest, String
throws Exception;

CommandResult mkdir(AgentClusterNodeRequest clusterNodeRequest, String path) throws Exception;

CommandResult rmDir(AgentClusterNodeRequest clusterNodeRequest, String path) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ public CommandResult mkdir(AgentClusterNodeRequest clusterNodeRequest, String pa
return execRemote(clusterNodeRequest, "mkdir " + path);
}

@Override
public CommandResult rmDir(AgentClusterNodeRequest clusterNodeRequest, String path) throws Exception {
return execRemote(clusterNodeRequest, "rm -rf " + path);
}

}

0 comments on commit c1d642c

Please sign in to comment.