Skip to content

Commit 76a2d23

Browse files
authored
[sshshim] persist debug file output with size limit (#549)
## Summary This PR employs the `lumberjack` library to do log file rotation and size limiting. Previously, the log file would get truncated on each `sshshim` command execution. Now, it acts like a sane log file: 1. logs get added to it across multiple command executions 2. when it hits the 2MB size limit, it gets rotated: - rotation copies to a backup file and the log file is truncated. This will help us in debugging issues with premature mutagen session termination. We can inspect the logs to understand why sshshim thought that it should terminate the connections for the VM. ## How was it tested? 1. created a 2MB fake log file: `yes "this is test file" | head -c 2000000 > test.file` and copied it to `~/.config/devbox/ssh/shims/logs.txt` 2. triggered an ssh shim command (`devbox cloud shell`) a few times. 3. observed the file get rotated: ``` ❯ ls -al ~/.config/devbox/ssh/shims/ total 24 drwxr--r-- 6 savil staff 192 Jan 27 14:48 . drwx------ 8 savil staff 256 Jan 27 14:48 .. -rw-r--r-- 1 savil staff 4901 Jan 27 14:48 logs-2023-01-27T22-48-47.703.txt.gz -rw-r--r-- 1 savil staff 1085 Jan 27 14:48 logs.txt lrwxr-xr-x 1 savil staff 30 Jan 27 14:48 scp -> /Users/savil/golang/bin/devbox lrwxr-xr-x 1 savil staff 30 Jan 27 14:48 ssh -> /Users/savil/golang/bin/devbox ```
1 parent b574133 commit 76a2d23

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/stretchr/testify v1.8.0
3232
golang.org/x/exp v0.0.0-20221109205753-fc8884afc316
3333
golang.org/x/mod v0.7.0
34+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
3435
gopkg.in/yaml.v3 v3.0.1
3536
)
3637

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cuelang.org/go v0.4.3 h1:W3oBBjDTm7+IZfCKZAmC8uDG0eYfJL4Pp/xbbCMKaVo=
22
cuelang.org/go v0.4.3/go.mod h1:7805vR9H+VoBNdWFdI7jyDR3QLUPp4+naHfbcgp55HI=
33
github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw=
44
github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
5+
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
56
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
67
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
78
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
@@ -152,6 +153,9 @@ golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
152153
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
153154
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
154155
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
156+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
157+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
158+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
155159
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
156160
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
157161
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/cloud/openssh/sshshim/logger.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import (
1212
"github.com/pkg/errors"
1313
"go.jetpack.io/devbox/internal/cloud/mutagenbox"
1414
"go.jetpack.io/devbox/internal/debug"
15+
"gopkg.in/natefinch/lumberjack.v2"
1516
)
1617

1718
const (
1819
logFileName = "logs.txt"
1920
)
2021

2122
func EnableDebug() {
22-
if w, err := logFile(); err == nil {
23+
if w, err := logFileWriter(); err == nil {
2324
debug.SetOutput(w)
2425
} else {
2526
fmt.Fprintf(os.Stderr, "failed to init ssh log file: %s", err)
@@ -29,22 +30,17 @@ func EnableDebug() {
2930
}
3031

3132
// logFile captures output for logging and when there is a failure
32-
// NOTE: Ideally, we should limit the size of this log file, but it is always truncated
33-
// because only the last ssh invocation (which may have failed) has its output saved.
34-
// So size should hopefully not be crazy big.
35-
func logFile() (io.Writer, error) {
33+
func logFileWriter() (io.Writer, error) {
3634
dirPath, err := mutagenbox.ShimDir()
3735
if err != nil {
3836
return nil, errors.WithStack(err)
3937
}
4038

41-
file, err := os.OpenFile(
42-
filepath.Join(dirPath, logFileName),
43-
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
44-
0700,
45-
)
46-
if err != nil {
47-
return nil, errors.WithStack(err)
48-
}
49-
return file, nil
39+
return &lumberjack.Logger{
40+
Filename: filepath.Join(dirPath, logFileName),
41+
MaxSize: 2, // megabytes
42+
MaxBackups: 2,
43+
MaxAge: 28, // days
44+
Compress: true, // disabled by default
45+
}, nil
5046
}

0 commit comments

Comments
 (0)