diff --git a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/executor/CacheFileUpdateCommandExecutor.java b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/executor/CacheFileUpdateCommandExecutor.java index 11e3ae7c5..115286912 100644 --- a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/executor/CacheFileUpdateCommandExecutor.java +++ b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/executor/CacheFileUpdateCommandExecutor.java @@ -18,12 +18,11 @@ */ package org.apache.bigtop.manager.agent.executor; -import org.apache.bigtop.manager.common.constants.Constants; import org.apache.bigtop.manager.common.constants.MessageConstants; import org.apache.bigtop.manager.common.message.entity.payload.CacheMessagePayload; import org.apache.bigtop.manager.common.utils.JsonUtils; +import org.apache.bigtop.manager.common.utils.ProjectPathUtils; import org.apache.bigtop.manager.grpc.generated.CommandType; -import org.apache.bigtop.manager.stack.core.utils.linux.LinuxFileUtils; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; @@ -31,6 +30,9 @@ import lombok.extern.slf4j.Slf4j; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.MessageFormat; import static org.apache.bigtop.manager.common.constants.CacheFiles.CLUSTER_INFO; @@ -55,9 +57,19 @@ public CommandType getCommandType() { public void doExecute() { CacheMessagePayload cacheMessagePayload = JsonUtils.readFromString(commandRequest.getPayload(), CacheMessagePayload.class); - String cacheDir = Constants.STACK_CACHE_DIR; - - LinuxFileUtils.createDirectories(cacheDir, "root", "root", "rwxr-xr-x", false); + String cacheDir = ProjectPathUtils.getAgentCachePath(); + Path p = Paths.get(cacheDir); + if (!Files.exists(p)) { + try { + Files.createDirectories(p); + } catch (Exception e) { + log.error("Create directory failed: {}", cacheDir, e); + commandReplyBuilder.setCode(MessageConstants.FAIL_CODE); + commandReplyBuilder.setResult( + MessageFormat.format("Create directory {0}, failed: {1}", cacheDir, e.getMessage())); + return; + } + } JsonUtils.writeToFile(cacheDir + SETTINGS_INFO, cacheMessagePayload.getSettings()); JsonUtils.writeToFile(cacheDir + CONFIGURATIONS_INFO, cacheMessagePayload.getConfigurations()); diff --git a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/constants/Constants.java b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/constants/Constants.java index 14fecb1bd..77094fd89 100644 --- a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/constants/Constants.java +++ b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/constants/Constants.java @@ -18,25 +18,12 @@ */ package org.apache.bigtop.manager.common.constants; -import java.io.File; - public final class Constants { private Constants() { throw new UnsupportedOperationException("Construct Constants"); } - /** - * stack cache dir - */ - public static final String STACK_CACHE_DIR = - new File(Constants.class - .getProtectionDomain() - .getCodeSource() - .getLocation() - .getPath()) - .getParent() + "/../cache"; - /** * host key for all hosts */ diff --git a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/ProjectPathUtils.java b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/ProjectPathUtils.java index b5d614fab..779f209db 100644 --- a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/ProjectPathUtils.java +++ b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/ProjectPathUtils.java @@ -38,6 +38,10 @@ public static String getKeyStorePath() { return getProjectStoreDir() + File.separator + "keys"; } + public static String getAgentCachePath() { + return getProjectStoreDir() + File.separator + "agent-caches"; + } + private static String getProjectBaseDir() { if (Environments.isDevMode()) { return SystemUtils.getUserDir().getPath(); diff --git a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/LocalSettings.java b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/LocalSettings.java index 7de5c1468..19746fc6a 100644 --- a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/LocalSettings.java +++ b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/LocalSettings.java @@ -19,11 +19,11 @@ package org.apache.bigtop.manager.stack.core.utils; import org.apache.bigtop.manager.common.constants.CacheFiles; -import org.apache.bigtop.manager.common.constants.Constants; import org.apache.bigtop.manager.common.message.entity.pojo.ClusterInfo; import org.apache.bigtop.manager.common.message.entity.pojo.ComponentInfo; import org.apache.bigtop.manager.common.message.entity.pojo.RepoInfo; import org.apache.bigtop.manager.common.utils.JsonUtils; +import org.apache.bigtop.manager.common.utils.ProjectPathUtils; import com.fasterxml.jackson.core.type.TypeReference; import lombok.extern.slf4j.Slf4j; @@ -45,7 +45,7 @@ public static Object configurations(String service, String type, String key, Obj public static Map configurations(String service, String type) { Map configDataMap = new HashMap<>(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.CONFIGURATIONS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.CONFIGURATIONS_INFO); try { if (file.exists()) { Map> configJson = JsonUtils.readFromFile(file, new TypeReference<>() {}); @@ -69,7 +69,7 @@ public static List hosts(String componentName) { public static Map> hosts() { Map> hostJson = new HashMap<>(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.HOSTS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.HOSTS_INFO); if (file.exists()) { hostJson = JsonUtils.readFromFile(file, new TypeReference<>() {}); } @@ -79,7 +79,7 @@ public static Map> hosts() { public static Map basicInfo() { Map settings = new HashMap<>(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.SETTINGS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.SETTINGS_INFO); if (file.exists()) { settings = JsonUtils.readFromFile(file, new TypeReference<>() {}); } @@ -89,7 +89,7 @@ public static Map basicInfo() { public static Map users() { Map userMap = new HashMap<>(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.USERS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.USERS_INFO); if (file.exists()) { userMap = JsonUtils.readFromFile(file, new TypeReference<>() {}); } @@ -104,7 +104,7 @@ public static List packages() { public static List repos() { List repoInfoList = List.of(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.REPOS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.REPOS_INFO); if (file.exists()) { repoInfoList = JsonUtils.readFromFile(file, new TypeReference<>() {}); } @@ -114,7 +114,7 @@ public static List repos() { public static ClusterInfo cluster() { ClusterInfo clusterInfo = new ClusterInfo(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.CLUSTER_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.CLUSTER_INFO); if (file.exists()) { clusterInfo = JsonUtils.readFromFile(file, new TypeReference<>() {}); } @@ -124,7 +124,7 @@ public static ClusterInfo cluster() { public static Map components() { Map componentInfo = new HashMap<>(); - File file = new File(Constants.STACK_CACHE_DIR + CacheFiles.COMPONENTS_INFO); + File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.COMPONENTS_INFO); if (file.exists()) { componentInfo = JsonUtils.readFromFile(file, new TypeReference<>() {}); }