This is an automated email from the ASF dual-hosted git repository.

wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git


The following commit(s) were added to refs/heads/main by this push:
     new 9836449  BIGTOP-4254: Move agent cache dir to project store dir (#93)
9836449 is described below

commit 9836449d1934be8d429d3011ba2d1654bfa29620
Author: Zhiguo Wu <[email protected]>
AuthorDate: Thu Oct 24 15:07:32 2024 +0800

    BIGTOP-4254: Move agent cache dir to project store dir (#93)
---
 .../executor/CacheFileUpdateCommandExecutor.java   | 22 +++++++++++++++++-----
 .../bigtop/manager/common/constants/Constants.java | 13 -------------
 .../manager/common/utils/ProjectPathUtils.java     |  4 ++++
 .../manager/stack/core/utils/LocalSettings.java    | 16 ++++++++--------
 4 files changed, 29 insertions(+), 26 deletions(-)

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 11e3ae7..1152869 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 org.springframework.stereotype.Component;
 
 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 class CacheFileUpdateCommandExecutor extends 
AbstractCommandExecutor {
     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 14fecb1..77094fd 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 b5d614f..779f209 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 class ProjectPathUtils {
         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 7de5c14..19746fc 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 class LocalSettings {
     public static Map<String, Object> configurations(String service, String 
type) {
 
         Map<String, Object> 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<String, Map<String, Object>> configJson = 
JsonUtils.readFromFile(file, new TypeReference<>() {});
@@ -69,7 +69,7 @@ public class LocalSettings {
     public static Map<String, List<String>> hosts() {
 
         Map<String, List<String>> 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 class LocalSettings {
     public static Map<String, Object> basicInfo() {
 
         Map<String, Object> 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 class LocalSettings {
     public static Map<String, String> users() {
 
         Map<String, String> 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 class LocalSettings {
     public static List<RepoInfo> repos() {
 
         List<RepoInfo> 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 class LocalSettings {
     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 class LocalSettings {
     public static Map<String, ComponentInfo> components() {
 
         Map<String, ComponentInfo> 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<>() 
{});
         }

Reply via email to