-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cli: fix docker run on linux #604
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,22 +20,28 @@ | |
import ai.langstream.admin.client.http.GenericRetryExecution; | ||
import ai.langstream.admin.client.http.HttpClientProperties; | ||
import ai.langstream.admin.client.http.NoRetryPolicy; | ||
import ai.langstream.cli.LangStreamCLI; | ||
import ai.langstream.cli.NamedProfile; | ||
import ai.langstream.cli.api.model.Gateways; | ||
import ai.langstream.cli.commands.VersionProvider; | ||
import ai.langstream.cli.commands.applications.MermaidAppDiagramGenerator; | ||
import ai.langstream.cli.commands.applications.UIAppCmd; | ||
import ai.langstream.cli.util.LocalFileReferenceResolver; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executors; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.input.Tailer; | ||
import org.apache.commons.io.input.TailerListener; | ||
import picocli.CommandLine; | ||
|
@@ -45,6 +51,8 @@ public class LocalRunApplicationCmd extends BaseDockerCmd { | |
|
||
protected static final String LOCAL_DOCKER_RUN_PROFILE = "local-docker-run"; | ||
|
||
private static final Set<Path> temporaryFiles = new HashSet<>(); | ||
|
||
@CommandLine.Parameters(description = "ID of the application") | ||
private String applicationId; | ||
|
||
|
@@ -168,8 +176,8 @@ public void run() { | |
} | ||
final File secretsFile = checkFileExistsOrDownload(secretFilePath); | ||
|
||
log("Tenant " + tenant); | ||
log("Application " + applicationId); | ||
log("Tenant: " + tenant); | ||
log("Application: " + applicationId); | ||
log("Application directory: " + appDirectory.getAbsolutePath()); | ||
if (singleAgentId != null && !singleAgentId.isEmpty()) { | ||
log("Filter agent: " + singleAgentId); | ||
|
@@ -241,6 +249,17 @@ public void run() { | |
|
||
updateLocalDockerRunProfile(tenant); | ||
|
||
Runtime.getRuntime() | ||
.addShutdownHook( | ||
new Thread( | ||
() -> { | ||
log("Cleaning environment"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we log this only if there are "temporary files" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
for (Path temporaryFile : temporaryFiles) { | ||
debug("Deleting temporary file: " + temporaryFile); | ||
FileUtils.deleteQuietly(temporaryFile.toFile()); | ||
} | ||
})); | ||
|
||
executeOnDocker( | ||
tenant, | ||
applicationId, | ||
|
@@ -281,12 +300,13 @@ private void executeOnDocker( | |
boolean startDatabase, | ||
boolean dryRun) | ||
throws Exception { | ||
File tmpInstanceFile = Files.createTempFile("instance", ".yaml").toFile(); | ||
Files.write(tmpInstanceFile.toPath(), instanceContents.getBytes(StandardCharsets.UTF_8)); | ||
final File appTmp = generateTempFile("app"); | ||
FileUtils.copyDirectory(appDirectory, appTmp); | ||
makeDirOrFileReadable(appTmp); | ||
File tmpInstanceFile = createReadableTempFile("instance", instanceContents); | ||
File tmpSecretsFile = null; | ||
if (secretsContents != null) { | ||
tmpSecretsFile = Files.createTempFile("secrets", ".yaml").toFile(); | ||
Files.write(tmpSecretsFile.toPath(), secretsContents.getBytes(StandardCharsets.UTF_8)); | ||
tmpSecretsFile = createReadableTempFile("secrets", secretsContents); | ||
nicoloboschi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
String imageName = dockerImageName + ":" + dockerImageVersion; | ||
List<String> commandLine = new ArrayList<>(); | ||
|
@@ -315,7 +335,7 @@ private void executeOnDocker( | |
} | ||
|
||
commandLine.add("-v"); | ||
commandLine.add(appDirectory.getAbsolutePath() + ":/code/application"); | ||
commandLine.add(appTmp.getAbsolutePath() + ":/code/application"); | ||
commandLine.add("-v"); | ||
commandLine.add(tmpInstanceFile.getAbsolutePath() + ":/code/instance.yaml"); | ||
if (tmpSecretsFile != null) { | ||
|
@@ -381,6 +401,38 @@ private void executeOnDocker( | |
} | ||
} | ||
|
||
private static File createReadableTempFile(String prefix, String instanceContents) | ||
throws IOException { | ||
File tempFile = generateTempFile(prefix); | ||
Files.write(tempFile.toPath(), instanceContents.getBytes(StandardCharsets.UTF_8)); | ||
makeDirOrFileReadable(tempFile); | ||
return tempFile; | ||
} | ||
|
||
private static void makeDirOrFileReadable(File file) throws IOException { | ||
if (file.isDirectory()) { | ||
for (File child : file.listFiles()) { | ||
makeDirOrFileReadable(child); | ||
} | ||
} | ||
Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rw-r--r--")); | ||
} | ||
|
||
@SneakyThrows | ||
private static File generateTempFile(String prefix) { | ||
Path home = LangStreamCLI.getLangstreamCLIHomeDirectory(); | ||
final String generatedName = ".langstream_" + prefix + "_" + System.nanoTime(); | ||
if (home == null) { | ||
home = new File(".").toPath(); | ||
} else { | ||
home = home.resolve("tmp"); | ||
Files.createDirectories(home); | ||
} | ||
File tempFile = home.resolve(generatedName).toFile(); | ||
temporaryFiles.add(tempFile.toPath()); | ||
return tempFile; | ||
} | ||
|
||
private void startUI(String tenant, String applicationId, Path outputLog, Process process) { | ||
String body; | ||
try (final AdminClient localAdminClient = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a Concurrent set, this structure is accessed from multiple thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done