Skip to content
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
37 changes: 18 additions & 19 deletions cmd/thv/app/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,29 @@ func logsCmdFunc(cmd *cobra.Command, args []string) error {
follow := viper.GetBool("follow")
proxy := viper.GetBool("proxy")

if proxy {
return getProxyLogs(workloadName, follow)
}

manager, err := workloads.NewManager(ctx)
if err != nil {
return fmt.Errorf("failed to create workload manager: %v", err)
}

if proxy {
if follow {
return getProxyLogs(workloadName)
}
// Use the shared manager method for non-follow proxy logs
logs, err := manager.GetProxyLogs(ctx, workloadName)
if err != nil {
logger.Infof("Proxy logs not found for workload %s", workloadName)
return nil
}
fmt.Print(logs)
return nil
}

logs, err := manager.GetLogs(ctx, workloadName, follow)
if err != nil {
if errors.Is(err, rt.ErrWorkloadNotFound) {
logger.Infof("workload %s not found", workloadName)
logger.Infof("Workload %s not found", workloadName)
return nil
}
return fmt.Errorf("failed to get logs for workload %s: %v", workloadName, err)
Expand Down Expand Up @@ -217,8 +227,8 @@ func reportPruneResults(prunedFiles, errs []string) {
}
}

// getProxyLogs reads and displays the proxy logs for a given workload
func getProxyLogs(workloadName string, follow bool) error {
// getProxyLogs reads and displays the proxy logs for a given workload in follow mode
func getProxyLogs(workloadName string) error {
// Get the proxy log file path
logFilePath, err := xdg.DataFile(fmt.Sprintf("toolhive/logs/%s.log", workloadName))
if err != nil {
Expand All @@ -234,18 +244,7 @@ func getProxyLogs(workloadName string, follow bool) error {
return nil
}

if follow {
return followProxyLogFile(cleanLogFilePath)
}

// Read and display the entire log file
content, err := os.ReadFile(cleanLogFilePath)
if err != nil {
return fmt.Errorf("failed to read proxy log %s: %v", cleanLogFilePath, err)
}

fmt.Print(string(content))
return nil
return followProxyLogFile(cleanLogFilePath)
}

// followProxyLogFile implements tail -f functionality for proxy logs
Expand Down
2 changes: 1 addition & 1 deletion docs/server/docs.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/server/swagger.json

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions docs/server/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions pkg/api/v1/workloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func WorkloadRouter(
r.Post("/{name}/restart", routes.restartWorkload)
r.Get("/{name}/status", routes.getWorkloadStatus)
r.Get("/{name}/logs", routes.getLogsForWorkload)
r.Get("/{name}/proxy-logs", routes.getProxyLogsForWorkload)
r.Get("/{name}/export", routes.exportWorkload)
r.Delete("/{name}", routes.deleteWorkload)

Expand Down Expand Up @@ -520,12 +521,19 @@ func (s *WorkloadRoutes) deleteWorkloadsBulk(w http.ResponseWriter, r *http.Requ
// @Produce text/plain
// @Param name path string true "Workload name"
// @Success 200 {string} string "Logs for the specified workload"
// @Failure 400 {string} string "Invalid workload name"
// @Failure 404 {string} string "Not Found"
// @Router /api/v1beta/workloads/{name}/logs [get]
func (s *WorkloadRoutes) getLogsForWorkload(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
name := chi.URLParam(r, "name")

// Validate workload name to prevent path traversal
if err := wt.ValidateWorkloadName(name); err != nil {
http.Error(w, "Invalid workload name: "+err.Error(), http.StatusBadRequest)
return
}

logs, err := s.workloadManager.GetLogs(ctx, name, false)
if err != nil {
if errors.Is(err, runtime.ErrWorkloadNotFound) {
Expand All @@ -545,6 +553,43 @@ func (s *WorkloadRoutes) getLogsForWorkload(w http.ResponseWriter, r *http.Reque
}
}

// getProxyLogsForWorkload
//
// @Summary Get proxy logs for a specific workload
// @Description Retrieve proxy logs for a specific workload by name from the file system.
// @Tags logs
// @Produce text/plain
// @Param name path string true "Workload name"
// @Success 200 {string} string "Proxy logs for the specified workload"
// @Failure 400 {string} string "Invalid workload name"
// @Failure 404 {string} string "Proxy logs not found for workload"
// @Router /api/v1beta/workloads/{name}/proxy-logs [get]
func (s *WorkloadRoutes) getProxyLogsForWorkload(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
name := chi.URLParam(r, "name")

// Validate workload name to prevent path traversal
if err := wt.ValidateWorkloadName(name); err != nil {
http.Error(w, "Invalid workload name: "+err.Error(), http.StatusBadRequest)
return
}

logs, err := s.workloadManager.GetProxyLogs(ctx, name)
if err != nil {
logger.Errorf("Failed to get proxy logs: %v", err)
http.Error(w, "Proxy logs not found for workload", http.StatusNotFound)
return
}

w.Header().Set("Content-Type", "text/plain")
_, err = w.Write([]byte(logs))
if err != nil {
logger.Errorf("Failed to write proxy logs response: %v", err)
http.Error(w, "Failed to write proxy logs response", http.StatusInternalServerError)
return
}
}

// getWorkloadStatus
//
// @Summary Get workload status
Expand Down
28 changes: 28 additions & 0 deletions pkg/workloads/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -60,6 +61,8 @@ type Manager interface {
UpdateWorkload(ctx context.Context, workloadName string, newConfig *runner.RunConfig) (*errgroup.Group, error)
// GetLogs retrieves the logs of a container.
GetLogs(ctx context.Context, containerName string, follow bool) (string, error)
// GetProxyLogs retrieves the proxy logs from the filesystem.
GetProxyLogs(ctx context.Context, workloadName string) (string, error)
// MoveToGroup moves the specified workloads from one group to another by updating their runconfig.
MoveToGroup(ctx context.Context, workloadNames []string, groupFrom string, groupTo string) error
// ListWorkloadsInGroup returns all workload names that belong to the specified group, including stopped workloads.
Expand Down Expand Up @@ -466,6 +469,31 @@ func (d *defaultManager) GetLogs(ctx context.Context, workloadName string, follo
return logs, nil
}

// GetProxyLogs retrieves proxy logs from the filesystem
func (*defaultManager) GetProxyLogs(_ context.Context, workloadName string) (string, error) {
// Get the proxy log file path
logFilePath, err := xdg.DataFile(fmt.Sprintf("toolhive/logs/%s.log", workloadName))
if err != nil {
return "", fmt.Errorf("failed to get proxy log file path for workload %s: %w", workloadName, err)
}

// Clean the file path to prevent path traversal
cleanLogFilePath := filepath.Clean(logFilePath)

// Check if the log file exists
if _, err := os.Stat(cleanLogFilePath); os.IsNotExist(err) {
return "", fmt.Errorf("proxy logs not found for workload %s", workloadName)
}

// Read and return the entire log file
content, err := os.ReadFile(cleanLogFilePath)
if err != nil {
return "", fmt.Errorf("failed to read proxy log for workload %s: %w", workloadName, err)
}

return string(content), nil
}

// deleteWorkload handles deletion of a single workload
func (d *defaultManager) deleteWorkload(name string) error {
// Create a child context with a longer timeout
Expand Down
15 changes: 15 additions & 0 deletions pkg/workloads/mocks/mock_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading