mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
* Add server side of journal logs This add the server side and grpc methods to get a journal logs from a machine. It repurposes ServiceLogEntry for these logs to keep the changes somewhat to a minimum. And it lets us re-use the merging of the various logs. In the protobufs ContainerLog has been renamed to just Log and LogEntry, as these are now also used for journal logs. It does api.LogOptions in more places to reduce the various logOpts that were used. It does not yet plumb it through to the uc client, that needs a follow up pr. Following logs is also not yet implemented. Signed-off-by: Miek Gieben <miek@miek.nl> * Fix test too Signed-off-by: Miek Gieben <miek@miek.nl> * remove entire comment Signed-off-by: Miek Gieben <miek@miek.nl> * Implement the follow option, untested mind you Signed-off-by: Miek Gieben <miek@miek.nl> * update debug line Signed-off-by: Miek Gieben <miek@miek.nl> * internal/jounal: First batch of PR comments Signed-off-by: Miek Gieben <miek@miek.nl> * internal/journal: code review comments Signed-off-by: Miek Gieben <miek@miek.nl> * Manually apply suggestion Signed-off-by: Miek Gieben <miek@miek.nl> * apply comment manually Signed-off-by: Miek Gieben <miek@miek.nl> * internal/journal: add unit test Signed-off-by: Miek Gieben <miek@miek.nl> * Use testify Signed-off-by: Miek Gieben <miek@miek.nl> * -amFix scanner.Err checking Signed-off-by: Miek Gieben <miek@miek.nl> * Implement code review comments Signed-off-by: Miek Gieben <miek@miek.nl> --------- Signed-off-by: Miek Gieben <miek@miek.nl>
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package journal
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
|
|
"github.com/psviderski/uncloud/pkg/api"
|
|
)
|
|
|
|
const journalctl = "journalctl"
|
|
|
|
var commandContext = exec.CommandContext // overidable for the test
|
|
|
|
func logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (io.ReadCloser, error) {
|
|
args := []string{"-u", unit, "--no-hostname"}
|
|
args = append(args, "-n")
|
|
if opts.Tail > -1 {
|
|
args = append(args, fmt.Sprintf("%d", opts.Tail))
|
|
} else {
|
|
args = append(args, "all")
|
|
}
|
|
if opts.Follow {
|
|
args = append(args, "-f")
|
|
}
|
|
|
|
args = append(args, "-o")
|
|
args = append(args, "short-iso-precise")
|
|
|
|
if opts.Since != "" {
|
|
args = append(args, "-S")
|
|
args = append(args, opts.Since)
|
|
}
|
|
if opts.Until != "" {
|
|
args = append(args, "-U")
|
|
args = append(args, opts.Until)
|
|
}
|
|
|
|
cmd := commandContext(ctx, journalctl, args...)
|
|
p, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
// follow synchronously follows the io.Reader, writing each new journal entry to channel.
|
|
func follow(ctx context.Context, reader io.Reader, outCh chan api.LogEntry) error {
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
select {
|
|
case outCh <- entry(scanner.Bytes()):
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)}
|
|
}
|
|
return nil
|
|
}
|