From 42ba37a417a4482676b15c7ca3b5060fa2ece10f Mon Sep 17 00:00:00 2001 From: baixinsui Date: Tue, 15 Oct 2024 15:37:45 +0800 Subject: [PATCH] Fix error when tofu with version used --- .../service/OpenTofuDirectoryService.java | 2 +- .../opentofu/tool/OpenTofuVersionsHelper.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java index 8849aa6..0f719cf 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java @@ -57,7 +57,7 @@ @Service public class OpenTofuDirectoryService { - private static final String STATE_FILE_NAME = "openTofu.tfstate"; + private static final String STATE_FILE_NAME = "terraform.tfstate"; private static final String TEST_FILE_NAME = "hello-world.tf"; private static final String HEALTH_CHECK_DIR = UUID.randomUUID().toString(); private static final List EXCLUDED_FILE_SUFFIX_LIST = diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsHelper.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsHelper.java index fba913c..56d964e 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsHelper.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsHelper.java @@ -180,7 +180,7 @@ public boolean checkIfExecutorIsMatchedRequiredVersion(File executorFile, * @return If true, the executor can be executed, otherwise return false. */ public boolean checkIfExecutorCanBeExecuted(File executorFile) { - String versionOutput = getVersionCommandOutput(executorFile); + String versionOutput = getVersionCommandOutput(executorFile.getAbsolutePath()); return StringUtils.isNotBlank(versionOutput); } @@ -191,7 +191,7 @@ public boolean checkIfExecutorCanBeExecuted(File executorFile) { * @return exact version of executor. */ public String getExactVersionOfExecutor(String executorPath) { - String versionOutput = getVersionCommandOutput(new File(executorPath)); + String versionOutput = getVersionCommandOutput(executorPath); Matcher matcher = OPENTOFU_VERSION_OUTPUT_PATTERN.matcher(versionOutput); if (matcher.find()) { // return only the version number. @@ -201,12 +201,10 @@ public String getExactVersionOfExecutor(String executorPath) { } - private String getVersionCommandOutput(File executorFile) { + private String getVersionCommandOutput(String executorPath) { try { - if (!executorFile.exists() && !executorFile.isFile()) { - return null; - } - if (!executorFile.canExecute()) { + File executorFile = new File(executorPath); + if (executorFile.exists() && !executorFile.canExecute()) { SystemCmdResult chmodResult = systemCmd.execute( String.format("chmod +x %s", executorFile.getAbsolutePath()), 5, System.getProperty("java.io.tmpdir"), false, new HashMap<>()); @@ -215,18 +213,20 @@ private String getVersionCommandOutput(File executorFile) { } } SystemCmdResult versionCheckResult = - systemCmd.execute(executorFile.getAbsolutePath() + " version", + systemCmd.execute(executorPath + " version", 5, System.getProperty("java.io.tmpdir"), false, new HashMap<>()); if (versionCheckResult.isCommandSuccessful()) { + log.info("Get version of executor {} output: {}", executorPath, + versionCheckResult.getCommandStdOutput()); return versionCheckResult.getCommandStdOutput(); } else { - log.error(versionCheckResult.getCommandStdError()); - return null; + log.error("Get version of executor {} output error: {}", executorPath, + versionCheckResult.getCommandStdError()); } } catch (Exception e) { - log.error("Failed to get version of executor {}.", executorFile.getAbsolutePath(), e); - return null; + log.error("Failed to get version of executor {}.", executorPath, e); } + return null; }