Skip to content

Commit 310a077

Browse files
authored
BIGTOP-4458: Auto find java bin when start server/agent (#239)
1 parent d010282 commit 310a077

7 files changed

Lines changed: 107 additions & 11 deletions

File tree

bigtop-manager-agent/src/main/resources/bin/env.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,49 @@
1818
# under the License.
1919
#
2020

21+
set -e
22+
23+
JAVA_SEARCH_DIRS=(
24+
"/usr/local/java*"
25+
"/usr/lib/jvm/java-*"
26+
"/usr/java/jdk*"
27+
"/opt/java*"
28+
"/opt/jdk*"
29+
"/usr/lib/jvm/*"
30+
)
31+
32+
# Find a suitable Java (version >= 17)
33+
find_java() {
34+
if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/java" ]; then
35+
JAVA_CANDIDATES=("$JAVA_HOME/bin/java")
36+
else
37+
JAVA_CANDIDATES=()
38+
JAVA_PATH=$(command -v java 2>/dev/null || true)
39+
[ -n "$JAVA_PATH" ] && JAVA_CANDIDATES+=("$JAVA_PATH")
40+
41+
for pattern in "${JAVA_SEARCH_DIRS[@]}"; do
42+
for dir in $(compgen -G "$pattern" 2>/dev/null); do
43+
# Check privileges, make sure the script won't exit
44+
[ -x "$dir/bin/java" ] 2>/dev/null || true
45+
[ -x "$dir/bin/java" ] && JAVA_CANDIDATES+=("$dir/bin/java")
46+
done
47+
done
48+
fi
49+
50+
for JAVA_CMD in "${JAVA_CANDIDATES[@]}"; do
51+
VERSION_STR=$("$JAVA_CMD" -version 2>&1 | awk -F '"' '/version/ {print $2}')
52+
MAJOR_VERSION=$(echo "$VERSION_STR" | awk -F. '{if ($1 == "1") print $2; else print $1}')
53+
if [ "$MAJOR_VERSION" -ge 17 ]; then
54+
export JAVA_CMD
55+
return 0
56+
fi
57+
done
58+
59+
echo "Error: Java 17 or higher is required. No suitable java found." >&2
60+
exit 1
61+
}
62+
63+
find_java
64+
2165
export JAVA_OPTS=""
66+
export JAVA_CMD

bigtop-manager-agent/src/main/resources/bin/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ fi
6262

6363
cd $BIGTOP_MANAGER_HOME
6464

65-
$JAVA_HOME/bin/java $JAVA_OPTS \
65+
$JAVA_CMD $JAVA_OPTS \
6666
-cp "${BIGTOP_MANAGER_HOME}/conf":"${BIGTOP_MANAGER_HOME}/libs/*" \
6767
org.apache.bigtop.manager.agent.AgentApplication

bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/aop/AuditAspect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.bigtop.manager.server.aop;
2020

21-
import org.apache.bigtop.manager.common.utils.JsonUtils;
2221
import org.apache.bigtop.manager.dao.po.AuditLogPO;
2322
import org.apache.bigtop.manager.dao.repository.AuditLogDao;
2423
import org.apache.bigtop.manager.server.holder.SessionUserHolder;
@@ -82,7 +81,9 @@ public void before(JoinPoint joinPoint) {
8281
auditLogPO.setTagDesc(apiDesc);
8382
auditLogPO.setOperationSummary(operationSummary);
8483
auditLogPO.setOperationDesc(operationDesc);
85-
auditLogPO.setArgs(JsonUtils.writeAsString(joinPoint.getArgs()));
84+
85+
// Temporary disable since params of command api sometimes are too long
86+
// auditLogPO.setArgs(JsonUtils.writeAsString(joinPoint.getArgs()));
8687

8788
log.debug("auditLog: {}", auditLogPO);
8889
log.debug("request method:{}.{}", joinPoint.getSignature().getDeclaringTypeName(), methodName);

bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/HostServiceImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,22 @@ public void installDependencies(HostDTO hostDTO, String hostname, InstalledStatu
274274
try {
275275
String script = ProjectPathUtils.getServerScriptPath() + File.separator + "setup-agent.sh";
276276
String content = Files.readString(Path.of(script));
277-
command = "cat << 'EOF' > ./setup-agent.sh\n"
278-
+ content
279-
+ "\nEOF\n"
280-
+ "chmod +x ./setup-agent.sh && ./setup-agent.sh " + path + " " + repoUrl + " " + grpcPort;
277+
command = "cat << 'EOF' > ./setup-agent.sh\n" + content + "\nEOF\n" + "chmod +x ./setup-agent.sh";
278+
ShellResult result = execCommandOnRemoteHost(hostDTO, hostname, command);
279+
if (result.getExitCode() != MessageConstants.SUCCESS_CODE) {
280+
log.error("Unable to write agent script, hostname: {}, msg: {}", hostname, result);
281+
installedStatusVO.setStatus(InstalledStatusEnum.FAILED);
282+
installedStatusVO.setMessage(result.getErrMsg());
283+
return;
284+
}
281285
} catch (IOException e) {
282286
log.error("Unable to write agent script, hostname: {}, msg: {}", hostname, e.getMessage());
283287
installedStatusVO.setStatus(InstalledStatusEnum.FAILED);
284288
installedStatusVO.setMessage(e.getMessage());
285289
return;
286290
}
287291

292+
command = "./setup-agent.sh " + path + " " + repoUrl + " " + grpcPort;
288293
ShellResult result = execCommandOnRemoteHost(hostDTO, hostname, command);
289294
if (result.getExitCode() != MessageConstants.SUCCESS_CODE) {
290295
log.error("Unable to setup agent, hostname: {}, msg: {}", hostname, result);

bigtop-manager-server/src/main/resources/bin/env.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
#
23
# Licensed to the Apache Software Foundation (ASF) under one
34
# or more contributor license agreements. See the NOTICE file
@@ -17,5 +18,49 @@
1718
# under the License.
1819
#
1920

21+
set -e
2022

21-
export JAVA_OPTS=""
23+
JAVA_SEARCH_DIRS=(
24+
"/usr/local/java*"
25+
"/usr/lib/jvm/java-*"
26+
"/usr/java/jdk*"
27+
"/opt/java*"
28+
"/opt/jdk*"
29+
"/usr/lib/jvm/*"
30+
)
31+
32+
# Find a suitable Java (version >= 17)
33+
find_java() {
34+
if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/java" ]; then
35+
JAVA_CANDIDATES=("$JAVA_HOME/bin/java")
36+
else
37+
JAVA_CANDIDATES=()
38+
JAVA_PATH=$(command -v java 2>/dev/null || true)
39+
[ -n "$JAVA_PATH" ] && JAVA_CANDIDATES+=("$JAVA_PATH")
40+
41+
for pattern in "${JAVA_SEARCH_DIRS[@]}"; do
42+
for dir in $(compgen -G "$pattern" 2>/dev/null); do
43+
# Check privileges, make sure the script won't exit
44+
[ -x "$dir/bin/java" ] 2>/dev/null || true
45+
[ -x "$dir/bin/java" ] && JAVA_CANDIDATES+=("$dir/bin/java")
46+
done
47+
done
48+
fi
49+
50+
for JAVA_CMD in "${JAVA_CANDIDATES[@]}"; do
51+
VERSION_STR=$("$JAVA_CMD" -version 2>&1 | awk -F '"' '/version/ {print $2}')
52+
MAJOR_VERSION=$(echo "$VERSION_STR" | awk -F. '{if ($1 == "1") print $2; else print $1}')
53+
if [ "$MAJOR_VERSION" -ge 17 ]; then
54+
export JAVA_CMD
55+
return 0
56+
fi
57+
done
58+
59+
echo "Error: Java 17 or higher is required. No suitable java found." >&2
60+
exit 1
61+
}
62+
63+
find_java
64+
65+
export JAVA_OPTS=""
66+
export JAVA_CMD

bigtop-manager-server/src/main/resources/bin/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ fi
6060

6161
cd $BIGTOP_MANAGER_HOME
6262

63-
$JAVA_HOME/bin/java $JAVA_OPTS \
63+
$JAVA_CMD $JAVA_OPTS \
6464
-cp "${BIGTOP_MANAGER_HOME}/conf":"${BIGTOP_MANAGER_HOME}/libs/*" \
6565
org.apache.bigtop.manager.server.ServerApplication

dev-support/docker/containers/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ while [ $# -gt 0 ]; do
197197
echo "Requires a container number" 1>&2
198198
usage
199199
fi
200-
if [ $2 -gt 10 ] || [ $2 -lt 3 ]; then
201-
echo "NUM-INSTANCES should be between [3-10]" 1>&2
200+
if [ $2 -gt 10 ] || [ $2 -lt 1 ]; then
201+
echo "NUM-INSTANCES should be between [1-10]" 1>&2
202202
usage
203203
fi
204204
NUM_INSTANCES=$2

0 commit comments

Comments
 (0)