From c4e18d585bec570a7652adfa53fd7be0ceb9c1b4 Mon Sep 17 00:00:00 2001 From: pratik Date: Tue, 21 Oct 2025 10:46:31 -0500 Subject: [PATCH] Fix path --- .../com/cfdeployer/service/CfCliService.java | 66 +++++++++++++++---- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/cfdeployer/service/CfCliService.java b/src/main/java/com/cfdeployer/service/CfCliService.java index 47e1ef2..070b2b3 100644 --- a/src/main/java/com/cfdeployer/service/CfCliService.java +++ b/src/main/java/com/cfdeployer/service/CfCliService.java @@ -6,12 +6,14 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; @@ -204,32 +206,70 @@ public class CfCliService { log.info("Detected operating system: {}", os); log.info("CF CLI executable name: {}", executable); + log.info("Java temp dir: {}", System.getProperty("java.io.tmpdir")); + log.info("User home: {}", System.getProperty("user.home")); + log.info("Current working directory: {}", System.getProperty("user.dir")); + + // Use Spring's ClassPathResource for proper Spring Boot JAR support + String resourcePath = String.format("cf-cli/%s/%s", os, executable); + log.info("Looking for CF CLI binary at classpath resource: {}", resourcePath); + + ClassPathResource resource = new ClassPathResource(resourcePath); + + if (!resource.exists()) { + log.error("CF CLI binary not found in classpath for OS: {}. Resource path: {}", os, resourcePath); + log.error("Resource exists: {}, isReadable: {}", resource.exists(), resource.isReadable()); + throw new IOException("CF CLI binary not found for OS: " + os + " at classpath: " + resourcePath); + } + + log.info("CF CLI resource found - exists: {}, isReadable: {}, contentLength: {} bytes", + resource.exists(), resource.isReadable(), resource.contentLength()); - String resourcePath = String.format("/cf-cli/%s/%s", os, executable); File tempFile = File.createTempFile("cf-cli-", os.equals("windows") ? ".exe" : ""); tempFile.deleteOnExit(); + log.info("Created temp file: {}", tempFile.getAbsolutePath()); - log.info("Extracting CF CLI from resource path: {}", resourcePath); + log.info("Extracting CF CLI from classpath resource: {}", resourcePath); - try (var inputStream = getClass().getResourceAsStream(resourcePath)) { - if (inputStream == null) { - log.error("CF CLI binary not found in resources for OS: {}. Expected path: {}", os, resourcePath); - throw new IOException("CF CLI binary not found for OS: " + os + " at path: " + resourcePath); - } + try (InputStream inputStream = resource.getInputStream()) { long bytesCopied = Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); log.info("CF CLI extracted to temporary file: {} ({} bytes)", tempFile.getAbsolutePath(), bytesCopied); + + if (bytesCopied == 0) { + throw new IOException("CF CLI extraction failed - 0 bytes copied from " + resourcePath); + } } if (!os.equals("windows")) { - Set perms = new HashSet<>(); - perms.add(PosixFilePermission.OWNER_READ); - perms.add(PosixFilePermission.OWNER_WRITE); - perms.add(PosixFilePermission.OWNER_EXECUTE); - Files.setPosixFilePermissions(tempFile.toPath(), perms); - log.info("Set executable permissions on CF CLI binary"); + try { + Set perms = new HashSet<>(); + perms.add(PosixFilePermission.OWNER_READ); + perms.add(PosixFilePermission.OWNER_WRITE); + perms.add(PosixFilePermission.OWNER_EXECUTE); + Files.setPosixFilePermissions(tempFile.toPath(), perms); + log.info("Set executable permissions on CF CLI binary using Java NIO"); + } catch (Exception e) { + log.warn("Failed to set permissions using Java NIO, trying chmod command: {}", e.getMessage()); + try { + ProcessBuilder pb = new ProcessBuilder("chmod", "+x", tempFile.getAbsolutePath()); + Process p = pb.start(); + int exitCode = p.waitFor(); + if (exitCode == 0) { + log.info("Successfully set executable permissions using chmod command"); + } else { + log.error("chmod command failed with exit code: {}", exitCode); + } + } catch (Exception ex) { + log.error("Failed to set executable permissions: {}", ex.getMessage(), ex); + throw new IOException("Unable to set executable permissions on CF CLI binary", ex); + } + } } log.info("Successfully prepared CF CLI executable: {}", tempFile.getAbsolutePath()); + log.info("File exists: {}, Can read: {}, Can execute: {}", + tempFile.exists(), tempFile.canRead(), tempFile.canExecute()); + return tempFile.getAbsolutePath(); }