This commit is contained in:
pratik
2025-10-21 10:46:31 -05:00
parent 1f12f1a28b
commit c4e18d585b

View File

@@ -6,12 +6,14 @@ import lombok.RequiredArgsConstructor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -204,32 +206,70 @@ public class CfCliService {
log.info("Detected operating system: {}", os); log.info("Detected operating system: {}", os);
log.info("CF CLI executable name: {}", executable); 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" : ""); File tempFile = File.createTempFile("cf-cli-", os.equals("windows") ? ".exe" : "");
tempFile.deleteOnExit(); 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)) { try (InputStream inputStream = resource.getInputStream()) {
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);
}
long bytesCopied = Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); long bytesCopied = Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
log.info("CF CLI extracted to temporary file: {} ({} bytes)", tempFile.getAbsolutePath(), bytesCopied); 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")) { if (!os.equals("windows")) {
Set<PosixFilePermission> perms = new HashSet<>(); try {
perms.add(PosixFilePermission.OWNER_READ); Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(tempFile.toPath(), perms); perms.add(PosixFilePermission.OWNER_EXECUTE);
log.info("Set executable permissions on CF CLI binary"); 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("Successfully prepared CF CLI executable: {}", tempFile.getAbsolutePath());
log.info("File exists: {}, Can read: {}, Can execute: {}",
tempFile.exists(), tempFile.canRead(), tempFile.canExecute());
return tempFile.getAbsolutePath(); return tempFile.getAbsolutePath();
} }