From d728e025d443d0acdc12863a30ab91771e8ba20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Schiavo?= Date: Tue, 18 Jun 2024 14:39:55 +0200 Subject: [PATCH] Display previous machines versions events We want to provide a breadcrumb view of a machine current state. For that we are now listing the previous machines versions events. In this way we users can track what happened to a given machine from initial creation to current state. --- gql/schema.graphql | 14 +++++++++ internal/command/machine/status.go | 50 ++++++++++++++++++++++++++++++ internal/flyutil/client.go | 1 + 3 files changed, 65 insertions(+) diff --git a/gql/schema.graphql b/gql/schema.graphql index cc4fe7b672..d26a1b486e 100644 --- a/gql/schema.graphql +++ b/gql/schema.graphql @@ -6356,6 +6356,8 @@ A machine state change event interface MachineEvent { id: ID! kind: String! + machineVersion: MachineVersion! + source: String timestamp: ISO8601DateTime! } @@ -6382,6 +6384,8 @@ type MachineEventConnection { type MachineEventDestroy implements MachineEvent { id: ID! kind: String! + machineVersion: MachineVersion! + source: String timestamp: ISO8601DateTime! } @@ -6404,21 +6408,27 @@ type MachineEventExit implements MachineEvent { exitCode: Int! id: ID! kind: String! + machineVersion: MachineVersion! metadata: JSON! oomKilled: Boolean! requestedStop: Boolean! + source: String timestamp: ISO8601DateTime! } type MachineEventGeneric implements MachineEvent { id: ID! kind: String! + machineVersion: MachineVersion! + source: String timestamp: ISO8601DateTime! } type MachineEventStart implements MachineEvent { id: ID! kind: String! + machineVersion: MachineVersion! + source: String timestamp: ISO8601DateTime! } @@ -6470,6 +6480,10 @@ type MachineIPEdge { node: MachineIP } +type MachineVersion implements Node { + id: ID! +} + """ Autogenerated input type of MigrateVolume """ diff --git a/internal/command/machine/status.go b/internal/command/machine/status.go index ab6573cf22..9591ff2d76 100644 --- a/internal/command/machine/status.go +++ b/internal/command/machine/status.go @@ -8,9 +8,12 @@ import ( "github.com/alecthomas/chroma/quick" "github.com/spf13/cobra" + fly "github.com/superfly/fly-go" "github.com/superfly/flyctl/internal/command" "github.com/superfly/flyctl/internal/flag" + "github.com/superfly/flyctl/internal/flyutil" "github.com/superfly/flyctl/internal/format" + "github.com/superfly/flyctl/internal/logger" "github.com/superfly/flyctl/internal/render" "github.com/superfly/flyctl/iostreams" ) @@ -160,10 +163,57 @@ func runMachineStatus(ctx context.Context) (err error) { exitEvent.ExitCode, exitEvent.OOMKilled, exitEvent.RequestedStop)) } + // This is terrible but will inform the users good enough while I build something + // elegant like the ExitEvent above + if event.Type == "launch" && event.Status == "created" && event.Source == "flyd" { + fields = append(fields, "migrated=true") + } + eventLogs = append(eventLogs, fields) } _ = render.Table(io.Out, "Event Logs", eventLogs, "State", "Event", "Source", "Timestamp", "Info") + // Get a historical list of events for this machine + apiClient := flyutil.ClientFromContext(ctx) + var gqlMachine *fly.GqlMachine + gqlMachine, err = apiClient.GetMachineWithEvents(ctx, machine.ID) + + if err == nil { + allVersionsEventLogs := [][]string{} + for _, event := range gqlMachine.Events.Nodes { + + // Only print older versions events + if machine.InstanceID == event.MachineVersion["id"] { + continue + } + + fields := []string{ + event.Status, + event.Kind, + event.Source, + event.Timestamp.String(), + } + + if event.Body != nil && event.Body["exit_event"] != nil { + exitEvent := event.Body["exit_event"].(map[string]interface{}) + fields = append(fields, fmt.Sprintf("exit_code=%d,oom_killed=%t,requested_stop=%t", + int(exitEvent["exit_code"].(float64)), exitEvent["oom_killed"], exitEvent["requested_stop"])) + } + + // This is STILL terrible but will inform the users good enough while I build something + // elegant like the ExitEvent above + if event.Kind == "launch" && event.Status == "created" && event.Source == "flyd" { + fields = append(fields, "migrated=true") + } + + allVersionsEventLogs = append(allVersionsEventLogs, fields) + } + _ = render.Table(io.Out, "Past Event Logs", allVersionsEventLogs, "State", "Event", "Source", "Timestamp", "Info") + } else { + logger := logger.FromContext(ctx) + logger.Debugf("failed to get historical events for machine %s", machine.ID) + } + if flag.GetBool(ctx, "display-config") { var prettyConfig []byte prettyConfig, err = json.MarshalIndent(machine.Config, "", " ") diff --git a/internal/flyutil/client.go b/internal/flyutil/client.go index 2b5381783c..c9b1b01a58 100644 --- a/internal/flyutil/client.go +++ b/internal/flyutil/client.go @@ -70,6 +70,7 @@ type Client interface { GetLatestImageTag(ctx context.Context, repository string, snapshotId *string) (string, error) GetLoggedCertificates(ctx context.Context, slug string) ([]fly.LoggedCertificate, error) GetMachine(ctx context.Context, machineId string) (*fly.GqlMachine, error) + GetMachineWithEvents(ctx context.Context, machineId string) (*fly.GqlMachine, error) GetNearestRegion(ctx context.Context) (*fly.Region, error) GetOrganizationBySlug(ctx context.Context, slug string) (*fly.Organization, error) GetOrganizationByApp(ctx context.Context, appName string) (*fly.Organization, error)