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

feat(windows-agent): Redirect agent logs to LocalAppData #379

Merged
merged 6 commits into from
Nov 22, 2023
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
7 changes: 2 additions & 5 deletions end-to-end/manual_token_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestManualTokenInput(t *testing.T) {
ctx := context.Background()

testSetup(t)
defer logWindowsAgentOnError(t)

// Either runs the ubuntupro app before...
if tc.whenToken == beforeDistroRegistration {
Expand All @@ -50,11 +51,7 @@ func TestManualTokenInput(t *testing.T) {
name := registerFromTestImage(t, ctx)
d := wsl.NewDistro(ctx, name)

defer func() {
if t.Failed() {
logWslProServiceJournal(t, ctx, d)
}
}()
defer logWslProServiceOnError(t, ctx, d)

out, err := d.Command(ctx, "exit 0").CombinedOutput()
require.NoErrorf(t, err, "Setup: could not wake distro up: %v. %s", err, out)
Expand Down
3 changes: 2 additions & 1 deletion end-to-end/organization_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestOrganizationProvidedToken(t *testing.T) {
ctx := context.Background()

testSetup(t)
defer logWindowsAgentOnError(t)

if tc.whenToken == beforeDistroRegistration {
activateOrgSubscription(t)
Expand All @@ -50,7 +51,7 @@ func TestOrganizationProvidedToken(t *testing.T) {
name := registerFromTestImage(t, ctx)
d := wsl.NewDistro(ctx, name)

defer logWslProServiceJournal(t, ctx, d)
defer logWslProServiceOnError(t, ctx, d)

out, err := d.Command(ctx, "exit 0").CombinedOutput()
require.NoErrorf(t, err, "Setup: could not wake distro up: %v. %s", err, out)
Expand Down
7 changes: 2 additions & 5 deletions end-to-end/purchase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestPurchase(t *testing.T) {
tc := tc
t.Run(name, func(t *testing.T) {
testSetup(t)
defer logWindowsAgentOnError(t)

settings := contractsmockserver.DefaultSettings()

Expand Down Expand Up @@ -108,11 +109,7 @@ func TestPurchase(t *testing.T) {
name := registerFromTestImage(t, ctx)
d := wsl.NewDistro(ctx, name)

defer func() {
if t.Failed() {
logWslProServiceJournal(t, ctx, d)
}
}()
defer logWslProServiceOnError(t, ctx, d)

out, err := d.Command(ctx, "exit 0").CombinedOutput()
require.NoErrorf(t, err, "Setup: could not wake distro up: %v. %s", err, out)
Expand Down
34 changes: 31 additions & 3 deletions end-to-end/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/canonical/ubuntu-pro-for-windows/common"
"github.com/canonical/ubuntu-pro-for-windows/common/wsltestutils"
"github.com/stretchr/testify/require"
"github.com/ubuntu/gowsl"
Expand Down Expand Up @@ -182,12 +183,39 @@ func distroIsProAttached(t *testing.T, ctx context.Context, d gowsl.Distro) (boo
}

//nolint:revive // testing.T must precede the context
func logWslProServiceJournal(t *testing.T, ctx context.Context, d gowsl.Distro) {
func logWslProServiceOnError(t *testing.T, ctx context.Context, d gowsl.Distro) {
t.Helper()

if !t.Failed() {
return
}

out, err := d.Command(ctx, "journalctl -b --no-pager -u wsl-pro.service").CombinedOutput()
if err != nil {
t.Logf("could not access logs: %v\n%s\n", err, out)
t.Logf("could not access WSL Pro Service logs: %v\n%s\n", err, out)
return
}
t.Logf("WSL Pro Service logs:\n%s\n", out)
}

func logWindowsAgentOnError(t *testing.T) {
t.Helper()

if !t.Failed() {
return
}
t.Logf("wsl-pro-service logs:\n%s\n", out)

localAppData := os.Getenv("LocalAppData")
if localAppData == "" {
t.Log("could not access Windows Agent's logs: $env:LocalAppData is not assigned")
return
}

out, err := os.ReadFile(filepath.Join(localAppData, common.LocalAppDataDir, "log"))
if err != nil {
t.Logf("could not read Windows Agent's logs: %v", err)
return
}

t.Logf("Windows Agent's logs:\n%s\n", out)
}
34 changes: 34 additions & 0 deletions windows-agent/cmd/ubuntu-pro-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"

"github.com/canonical/ubuntu-pro-for-windows/common"
"github.com/canonical/ubuntu-pro-for-windows/common/i18n"
Expand Down Expand Up @@ -40,6 +45,13 @@ func run(a app) int {
ForceColors: true,
})

cleanup, err := setLoggerOutput()
if err != nil {
log.Warningf("could not set logger output: %v", err)
} else {
defer cleanup()
}

if err := a.Run(); err != nil {
log.Error(context.Background(), err)

Expand All @@ -52,6 +64,28 @@ func run(a app) int {
return 0
}

func setLoggerOutput() (func(), error) {
lad := os.Getenv("LocalAppData")
if lad == "" {
return nil, errors.New("could not find LocalAppData")
}

p := filepath.Join(lad, common.LocalAppDataDir, "log")

f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("could not open log file: %v", err)
}

fmt.Fprintf(f, "\n======== Startup %s ========\n", time.Now().Format(time.RFC3339))

// Write both to file and to Stdout. The latter is useful for local development.
w := io.MultiWriter(f, os.Stdout)
log.SetOutput(w)
EduardGomezEscandell marked this conversation as resolved.
Show resolved Hide resolved

return func() { _ = f.Close() }, nil
}

func installSignalHandler(a app) func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
Expand Down
Loading