Skip to content
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

fix: logs for tasks #2499

Merged
merged 12 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (builtin *RunPythonCapabilities) Execute(ctx context.Context, _ *builtin_ar
if err != nil {
return "", stacktrace.Propagate(err, "error occurred while preparing the sh command to execute on the image")
}
fullCommandToRun := []string{shellWrapperCommand, "-c", commandToRun}
fullCommandToRun := getCommandToRunForStreamingLogs(commandToRun)

// run the command passed in by user in the container
runPythonExecutionResult, err := executeWithWait(ctx, builtin.serviceNetwork, builtin.name, builtin.wait, fullCommandToRun)
Expand Down Expand Up @@ -418,7 +418,7 @@ func getPythonCommandToRun(builtin *RunPythonCapabilities) (string, error) {
argumentsAsString := strings.Join(maybePythonArgumentsWithRuntimeValueReplaced, spaceDelimiter)
runEscaped := strings.ReplaceAll(builtin.run, `"`, `\"`)
if len(argumentsAsString) > 0 {
return fmt.Sprintf(`python -c "%s" %s`, runEscaped, argumentsAsString), nil
return fmt.Sprintf(`python -u -c "%s" %s`, runEscaped, argumentsAsString), nil
}
return fmt.Sprintf(`python -c "%s"`, runEscaped), nil
return fmt.Sprintf(`python -u -c "%s"`, runEscaped), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (builtin *RunShCapabilities) Execute(ctx context.Context, _ *builtin_argume
if err != nil {
return "", stacktrace.Propagate(err, "error occurred while preparing the sh command to execute on the image")
}
fullCommandToRun := []string{shellWrapperCommand, "-c", commandToRun}
fullCommandToRun := getCommandToRunForStreamingLogs(commandToRun)

// run the command passed in by user in the container
createDefaultDirectoryResult, err := executeWithWait(ctx, builtin.serviceNetwork, builtin.name, builtin.wait, fullCommandToRun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ const (
runFilesArtifactsKey = "files_artifacts"

shellWrapperCommand = "/bin/sh"
taskLogFilePath = "/tmp/kurtosis-task.log"
noNameSet = ""
uniqueNameGenErrStr = "error occurred while generating unique name for the file artifact"

// enables init mode on containers; cleaning up any zombie processes
tiniEnabled = true
)

var runTailCommandToPreventContainerToStopOnCreating = []string{"tail", "-f", "/dev/null"}
var runCommandToStreamTaskLogs = []string{shellWrapperCommand, "-c", fmt.Sprintf("touch %s && tail -F %s", taskLogFilePath, taskLogFilePath)}

// Wraps [commandToRun] to enable streaming logs from tasks.
// Uses curly braces to execute the command(s) in the current shell.
// Adds an extra echo to ensure each log ends with a newline.
// Uses tee to direct output to the task log file while maintaining output to stdout.
// Redirects stderr to stdout.
func getCommandToRunForStreamingLogs(commandToRun string) []string {
return []string{shellWrapperCommand, "-c", fmt.Sprintf("{ %v; } %v %v %v %v %v %v %v %v", commandToRun, "2>&1", "|", "tee", taskLogFilePath, "&&", "echo", ">>", taskLogFilePath)}
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
}

func parseStoreFilesArg(serviceNetwork service_network.ServiceNetwork, arguments *builtin_argument.ArgumentValuesSet) ([]*store_spec.StoreSpec, *startosis_errors.InterpretationError) {
var result []*store_spec.StoreSpec
Expand Down Expand Up @@ -276,7 +286,7 @@ func getServiceConfig(
filesArtifactExpansion *service_directory.FilesArtifactsExpansion,
envVars *map[string]string,
) (*service.ServiceConfig, error) {
serviceConfig, err := service.CreateServiceConfig(maybeImageName, maybeImageBuildSpec, maybeImageRegistrySpec, maybeNixBuildSpec, nil, nil, runTailCommandToPreventContainerToStopOnCreating, nil, *envVars, filesArtifactExpansion, nil, 0, 0, service_config.DefaultPrivateIPAddrPlaceholder, 0, 0, map[string]string{}, nil, nil, map[string]string{}, image_download_mode.ImageDownloadMode_Missing, tiniEnabled)
serviceConfig, err := service.CreateServiceConfig(maybeImageName, maybeImageBuildSpec, maybeImageRegistrySpec, maybeNixBuildSpec, nil, nil, runCommandToStreamTaskLogs, nil, *envVars, filesArtifactExpansion, nil, 0, 0, service_config.DefaultPrivateIPAddrPlaceholder, 0, 0, map[string]string{}, nil, nil, map[string]string{}, image_download_mode.ImageDownloadMode_Missing, tiniEnabled)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating service config")
}
Expand Down
Loading