-
Notifications
You must be signed in to change notification settings - Fork 64
/
logs.go
176 lines (148 loc) · 3.72 KB
/
logs.go
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package logs
import (
"context"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
mplatform "kraftkit.sh/machine/platform"
)
type LogOptions struct {
platform string
Follow bool `long:"follow" short:"f" usage:"Follow log output"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&LogOptions{}, cobra.Command{
Short: "Fetch the logs of a unikernel.",
Use: "logs [FLAGS] MACHINE",
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "run",
},
})
if err != nil {
panic(err)
}
cmd.Flags().VarP(
cmdfactory.NewEnumFlag[mplatform.Platform](
mplatform.Platforms(),
mplatform.Platform("auto"),
),
"plat",
"p",
"Set the platform virtual machine monitor driver. Set to 'auto' to detect the guest's platform and 'host' to use the host platform.",
)
return cmd
}
func (opts *LogOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.platform = cmd.Flag("plat").Value.String()
return nil
}
func (opts *LogOptions) Run(ctx context.Context, args []string) error {
var err error
platform := mplatform.PlatformUnknown
var controller machineapi.MachineService
if opts.platform == "auto" {
controller, err = mplatform.NewMachineV1alpha1ServiceIterator(ctx)
} else {
if opts.platform == "host" {
platform, _, err = mplatform.Detect(ctx)
if err != nil {
return err
}
} else {
var ok bool
platform, ok = mplatform.PlatformsByName()[opts.platform]
if !ok {
return fmt.Errorf("unknown platform driver: %s", opts.platform)
}
}
strategy, ok := mplatform.Strategies()[platform]
if !ok {
return fmt.Errorf("unsupported platform driver: %s (contributions welcome!)", platform.String())
}
controller, err = strategy.NewMachineV1alpha1(ctx)
}
if err != nil {
return err
}
machines, err := controller.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
var machine *machineapi.Machine
for _, candidate := range machines.Items {
if args[0] == candidate.Name {
machine = &candidate
break
} else if string(candidate.UID) == args[0] {
machine = &candidate
break
}
}
if machine == nil {
return fmt.Errorf("could not find instance %s", args[0])
}
if opts.Follow && machine.Status.State == machineapi.MachineStateRunning {
ctx, cancel := context.WithCancel(ctx)
go func() {
events, errs, err := controller.Watch(ctx, machine)
if err != nil {
cancel()
log.G(ctx).Errorf("could not listen for machine updates: %v", err)
return
}
loop:
for {
// Wait on either channel
select {
case status := <-events:
switch status.Status.State {
case machineapi.MachineStateExited, machineapi.MachineStateFailed:
break loop
}
case err := <-errs:
log.G(ctx).Errorf("received event error: %v", err)
break loop
case <-ctx.Done():
break loop
}
}
}()
logs, errs, err := controller.Logs(ctx, machine)
if err != nil {
cancel()
return err
}
loop:
for {
// Wait on either channel
select {
case line := <-logs:
fmt.Fprint(iostreams.G(ctx).Out, line)
case err := <-errs:
log.G(ctx).Errorf("received event error: %v", err)
break loop
case <-ctx.Done():
break loop
}
}
} else {
fd, err := os.Open(machine.Status.LogFile)
if err != nil {
return err
}
if _, err := io.Copy(iostreams.G(ctx).Out, fd); err != nil {
return err
}
}
return nil
}