-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathlogs.go
More file actions
205 lines (193 loc) · 5.9 KB
/
Copy pathlogs.go
File metadata and controls
205 lines (193 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package commands
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/docker/model-runner/cmd/cli/commands/completion"
"github.com/docker/model-runner/cmd/cli/desktop"
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
"github.com/docker/model-runner/cmd/cli/pkg/types"
dmrlogs "github.com/docker/model-runner/pkg/logs"
"github.com/moby/moby/api/pkg/stdcopy"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
)
func newLogsCmd() *cobra.Command {
var follow, noEngines bool
c := &cobra.Command{
Use: "logs [OPTIONS]",
Short: "Fetch the Docker Model Runner logs",
RunE: func(cmd *cobra.Command, args []string) error {
// Standalone mode: fetch container logs via Docker API.
engineKind := modelRunner.EngineKind()
useStandaloneLogs := engineKind == types.ModelRunnerEngineKindMoby ||
engineKind == types.ModelRunnerEngineKindCloud
if useStandaloneLogs {
dockerClient, err := desktop.DockerClientForContext(
dockerCLI, dockerCLI.CurrentContext(),
)
if err != nil {
return fmt.Errorf("failed to create Docker client: %w", err)
}
ctrID, _, _, err := standalone.FindControllerContainer(
cmd.Context(), dockerClient,
)
if err != nil {
return fmt.Errorf(
"unable to identify Model Runner container: %w", err,
)
} else if ctrID == "" {
return errors.New("unable to identify Model Runner container")
}
log, err := dockerClient.ContainerLogs(
cmd.Context(), ctrID, client.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: follow,
},
)
if err != nil {
return fmt.Errorf(
"unable to query Model Runner container logs: %w", err,
)
}
defer log.Close()
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, log)
return err
}
// Desktop mode: try local log files first.
serviceLogPath, runtimeLogPath, localErr := resolveDesktopLogPaths(
cmd.Context(),
)
if localErr == nil {
// Verify we can actually open the service log.
f, openErr := os.Open(serviceLogPath)
if openErr != nil {
localErr = openErr
} else {
f.Close()
}
}
if localErr != nil {
// Local files unavailable (e.g. running inside a container).
// Fall back to the DMR /logs API.
apiErr := desktopClient.Logs(
cmd.Context(), follow, noEngines, cmd.OutOrStdout(),
)
if apiErr != nil {
return fmt.Errorf(
"local logs unavailable (%w); API fallback failed: %w",
localErr, apiErr,
)
}
return nil
}
// Local files are accessible: use shared merge/follow logic.
enginePath := ""
if !noEngines {
enginePath = runtimeLogPath
}
// Poll mode is needed when tailing files over a mounted
// filesystem (Windows or WSL2 accessing the Windows host).
pollMode := runtime.GOOS == "windows" ||
(runtime.GOOS == "linux" && isWSL())
result, err := dmrlogs.MergeLogs(
cmd.OutOrStdout(), serviceLogPath, enginePath,
)
if err != nil {
return err
}
if !follow {
return nil
}
return dmrlogs.Follow(
cmd.Context(), cmd.OutOrStdout(),
serviceLogPath, enginePath,
result, pollMode,
)
},
ValidArgsFunction: completion.NoComplete,
}
c.Flags().BoolVarP(&follow, "follow", "f", false, "View logs with real-time streaming")
c.Flags().BoolVar(&noEngines, "no-engines", false, "Exclude inference engine logs from the output")
return c
}
// resolveDesktopLogPaths returns the service and engine log file paths
// for Docker Desktop mode. It returns an error when the OS is not
// supported or path discovery fails (e.g. native Linux, WSL failure).
func resolveDesktopLogPaths(ctx context.Context) (string, string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", "", fmt.Errorf("home directory: %w", err)
}
switch runtime.GOOS {
case "darwin":
base := filepath.Join(
homeDir,
"Library/Containers/com.docker.docker/Data/log/host",
)
return filepath.Join(base, dmrlogs.ServiceLogName),
filepath.Join(base, dmrlogs.EngineLogName),
nil
case "windows":
base := filepath.Join(homeDir, "AppData/Local/Docker/log/host")
return filepath.Join(base, dmrlogs.ServiceLogName),
filepath.Join(base, dmrlogs.EngineLogName),
nil
case "linux":
if !isWSL() {
return "", "", fmt.Errorf(
"log viewing on native Linux is only supported in standalone mode",
)
}
// In WSL2 with Docker Desktop, log files are on the Windows
// host filesystem mounted under /mnt/.
winHomeDir, wslErr := windowsHomeDirFromWSL(ctx)
if wslErr != nil {
return "", "", fmt.Errorf(
"unable to determine Windows home directory from WSL2: %w",
wslErr,
)
}
base := filepath.Join(winHomeDir, "AppData/Local/Docker/log/host")
return filepath.Join(base, dmrlogs.ServiceLogName),
filepath.Join(base, dmrlogs.EngineLogName),
nil
default:
return "", "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
}
// isWSL reports whether the current process is running inside a WSL2
// environment.
func isWSL() bool {
_, ok := os.LookupEnv("WSL_DISTRO_NAME")
return ok
}
// windowsHomeDirFromWSL resolves the Windows user's home directory
// from within a WSL2 environment by running "wslpath" on the
// USERPROFILE path obtained via "wslvar". Returns a Linux path such
// as /mnt/c/Users/Name.
func windowsHomeDirFromWSL(ctx context.Context) (string, error) {
out, err := exec.CommandContext(ctx, "wslvar", "USERPROFILE").Output()
if err != nil {
return "", fmt.Errorf("wslvar USERPROFILE: %w", err)
}
winPath := strings.TrimSpace(string(out))
if winPath == "" {
return "", fmt.Errorf("USERPROFILE is empty")
}
out, err = exec.CommandContext(ctx, "wslpath", "-u", winPath).Output()
if err != nil {
return "", fmt.Errorf("wslpath -u %q: %w", winPath, err)
}
linuxPath := strings.TrimSpace(string(out))
if linuxPath == "" {
return "", fmt.Errorf("wslpath returned empty path")
}
return linuxPath, nil
}