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 f200b06b BIGTOP-4291: Fix issues when Agent runs by non-root user 
(#114)
f200b06b is described below

commit f200b06b693be427a168a56bbf468e55d56b8da2
Author: Zhiguo Wu <[email protected]>
AuthorDate: Tue Dec 3 11:07:05 2024 +0800

    BIGTOP-4291: Fix issues when Agent runs by non-root user (#114)
---
 .../bigtop/manager/common/constants/Constants.java |   8 +-
 .../server/service/impl/HostServiceImpl.java       |   4 +-
 .../bigtop/manager/server/utils/StackUtils.java    |   2 +-
 .../bigtop/3.3.0/services/zookeeper/metainfo.xml   |   4 +-
 .../manager/stack/core/param/BaseParams.java       |  12 +-
 .../spi/hook/{InstallHook.java => AddHook.java}    |   4 +-
 .../stack/core/spi/script/AbstractScript.java      |  17 ++
 .../manager/stack/core/utils/TarballUtils.java     |   8 +
 .../stack/core/utils/linux/LinuxAccountUtils.java  |  64 ++++++-
 .../stack/core/utils/linux/LinuxFileUtils.java     | 213 +++++++++++----------
 .../v1_0_0/prometheus/PrometheusServerScript.java  |   5 +-
 .../infra/v1_0_0/prometheus/PrometheusSetup.java   |   1 -
 12 files changed, 218 insertions(+), 124 deletions(-)

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 77094fd8..628929c0 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
@@ -32,22 +32,22 @@ public final class Constants {
     /**
      * permission 644
      */
-    public static final String PERMISSION_644 = "rw-r--r--";
+    public static final String PERMISSION_644 = "644";
 
     /**
      * permission 755
      */
-    public static final String PERMISSION_755 = "rwxr-xr-x";
+    public static final String PERMISSION_755 = "755";
 
     /**
      * permission 775
      */
-    public static final String PERMISSION_775 = "rwxrwxr-x";
+    public static final String PERMISSION_775 = "775";
 
     /**
      * permission 777
      */
-    public static final String PERMISSION_777 = "rwwrwxrwx";
+    public static final String PERMISSION_777 = "777";
 
     /**
      * root user
diff --git 
a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/HostServiceImpl.java
 
b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/HostServiceImpl.java
index 319b5894..c8951f6d 100644
--- 
a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/HostServiceImpl.java
+++ 
b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/HostServiceImpl.java
@@ -184,7 +184,9 @@ public class HostServiceImpl implements HostService {
             // Download & Extract agent tarball
             String repoUrl = archRepoMap.get(arch).getBaseUrl();
             String tarballUrl = repoUrl + "/bigtop-manager-agent.tar.gz";
-            String command = "curl -L " + tarballUrl + " | tar -xz -C " + path;
+            String command = "sudo mkdir -p " + path + " &&"
+                    + " sudo chown -R " + hostDTO.getSshUser() + ":" + 
hostDTO.getSshUser() + " " + path
+                    + " && curl -L " + tarballUrl + " | tar -xz -C " + path;
             ShellResult result = execCommandOnRemoteHost(hostDTO, 
hostDTO.getHostname(), command);
             if (result.getExitCode() != MessageConstants.SUCCESS_CODE) {
                 hostPO.setErrInfo(result.getErrMsg());
diff --git 
a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/StackUtils.java
 
b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/StackUtils.java
index 3b47310e..b8e7fba4 100644
--- 
a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/StackUtils.java
+++ 
b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/StackUtils.java
@@ -166,7 +166,7 @@ public class StackUtils {
 
                 DAG.addNodeIfAbsent(blocked, getCommandWrapper(blocked));
                 for (String blocker : blockers) {
-                    DAG.addNodeIfAbsent(blocked, getCommandWrapper(blocked));
+                    DAG.addNodeIfAbsent(blocker, getCommandWrapper(blocker));
                     DAG.addEdge(blocker, blocked, new DagGraphEdge(blocker, 
blocked), false);
                 }
             }
diff --git 
a/bigtop-manager-server/src/main/resources/stacks/bigtop/3.3.0/services/zookeeper/metainfo.xml
 
b/bigtop-manager-server/src/main/resources/stacks/bigtop/3.3.0/services/zookeeper/metainfo.xml
index 795bdb41..29a95f92 100644
--- 
a/bigtop-manager-server/src/main/resources/stacks/bigtop/3.3.0/services/zookeeper/metainfo.xml
+++ 
b/bigtop-manager-server/src/main/resources/stacks/bigtop/3.3.0/services/zookeeper/metainfo.xml
@@ -49,10 +49,12 @@
             <package-specific>
                 <architectures>
                     <arch>x86_64</arch>
+                    <arch>aarch64</arch>
                 </architectures>
                 <packages>
                     <package>
-                        <name>zookeeper_3_3_0</name>
+                        <name>zookeeper-3.7.2-1.tar.gz</name>
+                        
<checksum>380f15d55c0282e33fdc7c2ec551bc5586f0ac126c243cd84347ccb775b846f3</checksum>
                     </package>
                 </packages>
             </package-specific>
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/param/BaseParams.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/param/BaseParams.java
index ff3eb7f0..b0e689c6 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/param/BaseParams.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/param/BaseParams.java
@@ -29,8 +29,6 @@ import 
org.apache.bigtop.manager.stack.core.annotations.GlobalParams;
 import org.apache.bigtop.manager.stack.core.exception.StackException;
 import org.apache.bigtop.manager.stack.core.utils.LocalSettings;
 
-import org.apache.commons.lang3.StringUtils;
-
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 
@@ -41,8 +39,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static org.apache.bigtop.manager.common.constants.Constants.ROOT_USER;
-
 @Slf4j
 public abstract class BaseParams implements Params {
 
@@ -90,9 +86,7 @@ public abstract class BaseParams implements Params {
 
     @Override
     public String user() {
-        return StringUtils.isNotBlank(this.commandPayload.getServiceUser())
-                ? this.commandPayload.getServiceUser()
-                : ROOT_USER;
+        return this.commandPayload.getServiceUser();
     }
 
     @Override
@@ -132,10 +126,8 @@ public abstract class BaseParams implements Params {
 
     @Override
     public String stackHome() {
-        String stackName = this.commandPayload.getStackName();
-        String stackVersion = this.commandPayload.getStackVersion();
         String root = LocalSettings.cluster().getRootDir();
-        return MessageFormat.format("{0}/{1}/{2}", root, 
stackName.toLowerCase(), stackVersion);
+        return MessageFormat.format("{0}/services", root);
     }
 
     @Override
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/InstallHook.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/AddHook.java
similarity index 94%
rename from 
bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/InstallHook.java
rename to 
bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/AddHook.java
index 2ef0c28b..4ec0cd6a 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/InstallHook.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/AddHook.java
@@ -29,9 +29,9 @@ import lombok.extern.slf4j.Slf4j;
  */
 @Slf4j
 @AutoService(Hook.class)
-public class InstallHook extends AbstractHook {
+public class AddHook extends AbstractHook {
 
-    public static final String NAME = "install";
+    public static final String NAME = "add";
 
     @Override
     public void doBefore(Params params) {
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/script/AbstractScript.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/script/AbstractScript.java
index f0554857..42f4c39f 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/script/AbstractScript.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/script/AbstractScript.java
@@ -18,16 +18,21 @@
  */
 package org.apache.bigtop.manager.stack.core.spi.script;
 
+import org.apache.bigtop.manager.common.constants.Constants;
 import org.apache.bigtop.manager.common.message.entity.pojo.PackageInfo;
 import org.apache.bigtop.manager.common.message.entity.pojo.RepoInfo;
 import org.apache.bigtop.manager.common.shell.ShellResult;
 import org.apache.bigtop.manager.stack.core.param.Params;
 import org.apache.bigtop.manager.stack.core.utils.TarballUtils;
+import org.apache.bigtop.manager.stack.core.utils.linux.LinuxAccountUtils;
+import org.apache.bigtop.manager.stack.core.utils.linux.LinuxFileUtils;
 
 import org.apache.commons.lang3.StringUtils;
 
 import lombok.extern.slf4j.Slf4j;
 
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.List;
 import java.util.Properties;
 
@@ -47,9 +52,21 @@ public abstract class AbstractScript implements Script {
         String stackHome = params.stackHome();
         String serviceHome = params.serviceHome();
 
+        if (!Files.exists(Path.of(stackHome))) {
+            String user = System.getProperty("user.name");
+            String group = params.group();
+
+            LinuxAccountUtils.assignUserToSupGroups(user, List.of(group));
+            LinuxFileUtils.createDirectories(stackHome, user, group, 
Constants.PERMISSION_755, true);
+        }
+
         for (PackageInfo packageInfo : packages) {
             Integer skipLevels = 
Integer.parseInt(properties.getProperty(PROPERTY_KEY_SKIP_LEVELS, "0"));
             TarballUtils.installPackage(repo.getBaseUrl(), stackHome, 
serviceHome, packageInfo, skipLevels);
+
+            // Dir already created by TarballUtils, this changes the owner and 
permission for the service
+            LinuxFileUtils.createDirectories(
+                    serviceHome, params.user(), params.group(), 
Constants.PERMISSION_755, true);
         }
 
         return ShellResult.success();
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/TarballUtils.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/TarballUtils.java
index cb6bf087..b05c3c9f 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/TarballUtils.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/TarballUtils.java
@@ -23,10 +23,13 @@ import 
org.apache.bigtop.manager.stack.core.exception.StackException;
 import org.apache.bigtop.manager.stack.core.tarball.ChecksumValidator;
 import org.apache.bigtop.manager.stack.core.tarball.TarballDownloader;
 import org.apache.bigtop.manager.stack.core.tarball.TarballExtractor;
+import org.apache.bigtop.manager.stack.core.utils.linux.LinuxFileUtils;
 
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 @Slf4j
 public class TarballUtils {
@@ -34,6 +37,11 @@ public class TarballUtils {
     @SuppressWarnings("ResultOfMethodCallIgnored")
     public static void installPackage(
             String repoUrl, String stackHome, String serviceHome, PackageInfo 
packageInfo, Integer skipLevels) {
+        if (Files.exists(Path.of(serviceHome))) {
+            log.info("Service home [{}] exists, deleting...", serviceHome);
+            LinuxFileUtils.removeDirectories(serviceHome);
+        }
+
         String remoteUrl = repoUrl + File.separator + packageInfo.getName();
         File localFile = new File(stackHome + File.separator + 
packageInfo.getName());
         String algorithm = packageInfo.getChecksum().split(":")[0];
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxAccountUtils.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxAccountUtils.java
index 99de7d82..0144d134 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxAccountUtils.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxAccountUtils.java
@@ -18,6 +18,7 @@
  */
 package org.apache.bigtop.manager.stack.core.utils.linux;
 
+import org.apache.bigtop.manager.common.constants.MessageConstants;
 import org.apache.bigtop.manager.common.shell.ShellExecutor;
 import org.apache.bigtop.manager.common.shell.ShellResult;
 import org.apache.bigtop.manager.common.utils.FileUtils;
@@ -67,7 +68,10 @@ public class LinuxAccountUtils {
         builderParameters.add(user);
 
         try {
-            ShellExecutor.execCommand(builderParameters);
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
         } catch (IOException e) {
             throw new StackException(e);
         }
@@ -154,7 +158,10 @@ public class LinuxAccountUtils {
         builderParameters.add(user);
 
         try {
-            ShellExecutor.execCommand(builderParameters);
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
         } catch (IOException e) {
             throw new StackException(e);
         }
@@ -179,7 +186,10 @@ public class LinuxAccountUtils {
         builderParameters.add(group);
 
         try {
-            ShellExecutor.execCommand(builderParameters);
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
         } catch (IOException e) {
             throw new StackException(e);
         }
@@ -227,7 +237,37 @@ public class LinuxAccountUtils {
         builderParameters.add(group);
 
         try {
-            ShellExecutor.execCommand(builderParameters);
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
+        } catch (IOException e) {
+            throw new StackException(e);
+        }
+    }
+
+    /**
+     * Assign user to supplementary groups
+     *
+     * @param user user
+     * @param groups groups
+     */
+    public static void assignUserToSupGroups(String user, List<String> groups) 
{
+        Objects.requireNonNull(user);
+        Objects.requireNonNull(groups);
+
+        List<String> builderParameters = new ArrayList<>();
+
+        builderParameters.add("usermod");
+        builderParameters.add("-aG");
+        builderParameters.add(String.join(",", groups));
+        builderParameters.add(user);
+
+        try {
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
         } catch (IOException e) {
             throw new StackException(e);
         }
@@ -249,7 +289,7 @@ public class LinuxAccountUtils {
         builderParameters.add("awk -F':' '{print $1}' /etc/group | grep  " + 
group);
 
         try {
-            ShellResult output = ShellExecutor.execCommand(builderParameters);
+            ShellResult output = sudoExecCmd(builderParameters);
             return output.getExitCode() == 0;
         } catch (IOException e) {
             throw new StackException(e);
@@ -271,7 +311,7 @@ public class LinuxAccountUtils {
         builderParameters.add("awk -F':' '{print $1}' /etc/passwd | grep  " + 
user);
 
         try {
-            ShellResult output = ShellExecutor.execCommand(builderParameters);
+            ShellResult output = sudoExecCmd(builderParameters);
             return output.getExitCode() == 0;
         } catch (IOException e) {
             throw new StackException(e);
@@ -314,4 +354,16 @@ public class LinuxAccountUtils {
 
         return null;
     }
+
+    private static ShellResult sudoExecCmd(List<String> params) throws 
IOException {
+        if ("root".equals(System.getProperty("user.name"))) {
+            return ShellExecutor.execCommand(params);
+        } else {
+            List<String> sudoParams = new ArrayList<>();
+            sudoParams.add("sudo");
+            sudoParams.addAll(params);
+
+            return ShellExecutor.execCommand(sudoParams);
+        }
+    }
 }
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxFileUtils.java
 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxFileUtils.java
index a143afae..65b48949 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxFileUtils.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxFileUtils.java
@@ -19,6 +19,9 @@
 package org.apache.bigtop.manager.stack.core.utils.linux;
 
 import org.apache.bigtop.manager.common.constants.Constants;
+import org.apache.bigtop.manager.common.constants.MessageConstants;
+import org.apache.bigtop.manager.common.shell.ShellExecutor;
+import org.apache.bigtop.manager.common.shell.ShellResult;
 import org.apache.bigtop.manager.common.utils.JsonUtils;
 import org.apache.bigtop.manager.common.utils.YamlUtils;
 import org.apache.bigtop.manager.stack.core.enums.ConfigType;
@@ -29,18 +32,12 @@ import org.apache.commons.lang3.StringUtils;
 
 import lombok.extern.slf4j.Slf4j;
 
-import java.io.File;
 import java.io.IOException;
-import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.nio.file.attribute.GroupPrincipal;
-import java.nio.file.attribute.PosixFileAttributeView;
-import java.nio.file.attribute.PosixFilePermission;
-import java.nio.file.attribute.PosixFilePermissions;
-import java.nio.file.attribute.UserPrincipal;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Only support Linux
@@ -130,85 +127,6 @@ public class LinuxFileUtils {
         updatePermissions(filename, permissions, false);
     }
 
-    /**
-     * Update file Permissions
-     *
-     * @param dir         file path
-     * @param permissions {@code rwxr--r--}
-     * @param recursive   recursive
-     */
-    public static void updatePermissions(String dir, String permissions, 
boolean recursive) {
-        if (StringUtils.isBlank(dir)) {
-            log.error("dir must not be null");
-            return;
-        }
-        permissions = StringUtils.isBlank(permissions) ? 
Constants.PERMISSION_644 : permissions;
-
-        Path path = Paths.get(dir);
-        Set<PosixFilePermission> perms = 
PosixFilePermissions.fromString(permissions);
-        try {
-            log.info("Changing permissions to [{}] for [{}]", permissions, 
dir);
-            Files.setPosixFilePermissions(path, perms);
-        } catch (IOException e) {
-            log.error("Error when change permissions", e);
-        }
-
-        // When is a directory, recursive update
-        if (recursive && Files.isDirectory(path)) {
-            try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
-                for (Path subPath : ds) {
-                    updatePermissions(dir + File.separator + 
subPath.getFileName(), permissions, true);
-                }
-            } catch (IOException e) {
-                log.error("Error when change permissions", e);
-            }
-        }
-    }
-
-    /**
-     * Update file owner
-     *
-     * @param dir       file path
-     * @param owner     owner
-     * @param group     group
-     * @param recursive recursive
-     */
-    public static void updateOwner(String dir, String owner, String group, 
boolean recursive) {
-        if (StringUtils.isBlank(dir)) {
-            log.error("dir must not be null");
-            return;
-        }
-        owner = StringUtils.isBlank(owner) ? "root" : owner;
-        group = StringUtils.isBlank(group) ? "root" : group;
-
-        Path path = Paths.get(dir);
-        try {
-            log.info("Changing owner to [{}:{}] for [{}]", owner, group, dir);
-            UserPrincipal userPrincipal =
-                    
path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(owner);
-
-            GroupPrincipal groupPrincipal =
-                    
path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByGroupName(group);
-
-            PosixFileAttributeView fileAttributeView = 
Files.getFileAttributeView(path, PosixFileAttributeView.class);
-            fileAttributeView.setOwner(userPrincipal);
-            fileAttributeView.setGroup(groupPrincipal);
-        } catch (IOException e) {
-            log.error("Error when change owner", e);
-        }
-
-        // When it is a directory, recursively set the file owner
-        if (recursive && Files.isDirectory(path)) {
-            try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
-                for (Path subPath : ds) {
-                    updateOwner(dir + File.separator + subPath.getFileName(), 
owner, group, true);
-                }
-            } catch (IOException e) {
-                log.error("Error when change owner", e);
-            }
-        }
-    }
-
     /**
      * create directories
      *
@@ -231,25 +149,45 @@ public class LinuxFileUtils {
             return;
         }
 
+        List<String> builderParameters = new ArrayList<>();
+        builderParameters.add("mkdir");
+        builderParameters.add("-p");
+        builderParameters.add(dirPath);
+
         try {
-            log.info("Creating directory: [{}]", path);
-            if (Files.exists(path)) {
-                if (Files.isDirectory(path)) {
-                    log.info("Directory already exists: [{}], skip creating", 
path);
-                } else {
-                    throw new IOException("Path exists and is not a directory: 
" + path);
-                }
-            } else {
-                Files.createDirectories(path);
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
             }
         } catch (IOException e) {
-            log.error("Error when create directory", e);
+            throw new StackException(e);
         }
 
         updateOwner(dirPath, owner, group, recursive);
         updatePermissions(dirPath, permissions, recursive);
     }
 
+    public static void removeDirectories(String dirPath) {
+        if (StringUtils.isBlank(dirPath)) {
+            log.error("dirPath must not be null");
+            return;
+        }
+
+        List<String> builderParameters = new ArrayList<>();
+        builderParameters.add("rm");
+        builderParameters.add("-rf");
+        builderParameters.add(dirPath);
+
+        try {
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
+        } catch (IOException e) {
+            throw new StackException(e);
+        }
+    }
+
     /**
      * create symbolic link
      *
@@ -285,4 +223,85 @@ public class LinuxFileUtils {
             throw new StackException(e);
         }
     }
+
+    /**
+     * Update file Permissions
+     *
+     * @param dir         file path
+     * @param permissions {@code rwxr--r--}
+     * @param recursive   recursive
+     */
+    public static void updatePermissions(String dir, String permissions, 
boolean recursive) {
+        if (StringUtils.isBlank(dir)) {
+            log.error("dir must not be null");
+            return;
+        }
+
+        permissions = StringUtils.isBlank(permissions) ? 
Constants.PERMISSION_644 : permissions;
+
+        List<String> builderParameters = new ArrayList<>();
+        builderParameters.add("chmod");
+        if (recursive && Files.isDirectory(Paths.get(dir))) {
+            builderParameters.add("-R");
+        }
+        builderParameters.add(permissions);
+        builderParameters.add(dir);
+
+        try {
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
+        } catch (IOException e) {
+            throw new StackException(e);
+        }
+    }
+
+    /**
+     * Update file owner
+     *
+     * @param dir       file path
+     * @param owner     owner
+     * @param group     group
+     * @param recursive recursive
+     */
+    public static void updateOwner(String dir, String owner, String group, 
boolean recursive) {
+        if (StringUtils.isBlank(dir)) {
+            log.error("dir must not be null");
+            return;
+        }
+
+        owner = StringUtils.isBlank(owner) ? "root" : owner;
+        group = StringUtils.isBlank(group) ? "root" : group;
+
+        List<String> builderParameters = new ArrayList<>();
+
+        builderParameters.add("chown");
+        if (recursive && Files.isDirectory(Paths.get(dir))) {
+            builderParameters.add("-R");
+        }
+        builderParameters.add(owner + ":" + group);
+        builderParameters.add(dir);
+
+        try {
+            ShellResult shellResult = sudoExecCmd(builderParameters);
+            if (shellResult.getExitCode() != MessageConstants.SUCCESS_CODE) {
+                throw new StackException(shellResult.getErrMsg());
+            }
+        } catch (IOException e) {
+            throw new StackException(e);
+        }
+    }
+
+    private static ShellResult sudoExecCmd(List<String> params) throws 
IOException {
+        if ("root".equals(System.getProperty("user.name"))) {
+            return ShellExecutor.execCommand(params);
+        } else {
+            List<String> sudoParams = new ArrayList<>();
+            sudoParams.add("sudo");
+            sudoParams.addAll(params);
+
+            return ShellExecutor.execCommand(sudoParams);
+        }
+    }
 }
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusServerScript.java
 
b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusServerScript.java
index 78efa805..69a32846 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusServerScript.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusServerScript.java
@@ -54,7 +54,10 @@ public class PrometheusServerScript extends 
AbstractServerScript {
                 "nohup {0}/prometheus --config.file={0}/prometheus.yml 
--storage.tsdb.path={0}/data > {0}/nohup.out 2>&1 &",
                 prometheusParams.serviceHome());
         try {
-            LinuxOSUtils.sudoExecCmd(cmd, prometheusParams.user());
+            ShellResult shellResult = LinuxOSUtils.sudoExecCmd(cmd, 
prometheusParams.user());
+            if (shellResult.getExitCode() != 0) {
+                throw new StackException("Failed to start Prometheus: {0}", 
shellResult.getErrMsg());
+            }
             long startTime = System.currentTimeMillis();
             long maxWaitTime = 5000;
             long pollInterval = 500;
diff --git 
a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusSetup.java
 
b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusSetup.java
index 0a1eb357..b4063b91 100644
--- 
a/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusSetup.java
+++ 
b/bigtop-manager-stack/bigtop-manager-stack-infra/src/main/java/org/apache/bigtop/manager/stack/infra/v1_0_0/prometheus/PrometheusSetup.java
@@ -37,7 +37,6 @@ public class PrometheusSetup {
         String user = prometheusParams.user();
         String group = prometheusParams.group();
 
-        LinuxFileUtils.createDirectories(prometheusParams.serviceHome(), user, 
group, PERMISSION_755, true);
         LinuxFileUtils.createDirectories(prometheusParams.dataDir(), user, 
group, PERMISSION_755, true);
 
         return ShellResult.success("Prometheus Configure success!");

Reply via email to