|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "golang.org/x/xerrors" |
| 9 | + |
| 10 | + "cdr.dev/slog" |
| 11 | + "github.com/coder/coder/v2/cli/cliui" |
| 12 | + "github.com/coder/coder/v2/codersdk" |
| 13 | + "github.com/coder/serpent" |
| 14 | +) |
| 15 | + |
| 16 | +// immortalStreamClient provides methods to interact with immortal streams API |
| 17 | +// This uses the main codersdk.Client to make server-proxied requests to agents |
| 18 | +type immortalStreamClient struct { |
| 19 | + client *codersdk.Client |
| 20 | + agentID uuid.UUID |
| 21 | + logger slog.Logger |
| 22 | +} |
| 23 | + |
| 24 | +// newImmortalStreamClient creates a new client for immortal streams |
| 25 | +func newImmortalStreamClient(client *codersdk.Client, agentID uuid.UUID, logger slog.Logger) *immortalStreamClient { |
| 26 | + return &immortalStreamClient{ |
| 27 | + client: client, |
| 28 | + agentID: agentID, |
| 29 | + logger: logger, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// createStream creates a new immortal stream |
| 34 | +func (c *immortalStreamClient) createStream(ctx context.Context, port int) (*codersdk.ImmortalStream, error) { |
| 35 | + stream, err := c.client.WorkspaceAgentCreateImmortalStream(ctx, c.agentID, codersdk.CreateImmortalStreamRequest{ |
| 36 | + TCPPort: port, |
| 37 | + }) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + return &stream, nil |
| 42 | +} |
| 43 | + |
| 44 | +// listStreams lists all immortal streams |
| 45 | +func (c *immortalStreamClient) listStreams(ctx context.Context) ([]codersdk.ImmortalStream, error) { |
| 46 | + return c.client.WorkspaceAgentImmortalStreams(ctx, c.agentID) |
| 47 | +} |
| 48 | + |
| 49 | +// deleteStream deletes an immortal stream |
| 50 | +func (c *immortalStreamClient) deleteStream(ctx context.Context, streamID uuid.UUID) error { |
| 51 | + return c.client.WorkspaceAgentDeleteImmortalStream(ctx, c.agentID, streamID) |
| 52 | +} |
| 53 | + |
| 54 | +// CLI Commands |
| 55 | + |
| 56 | +func (r *RootCmd) immortalStreamCmd() *serpent.Command { |
| 57 | + client := new(codersdk.Client) |
| 58 | + cmd := &serpent.Command{ |
| 59 | + Use: "immortal-stream", |
| 60 | + Short: "Manage immortal streams in workspaces", |
| 61 | + Long: "Immortal streams provide persistent TCP connections to workspace services that automatically reconnect when interrupted.", |
| 62 | + Middleware: serpent.Chain( |
| 63 | + r.InitClient(client), |
| 64 | + ), |
| 65 | + Handler: func(inv *serpent.Invocation) error { |
| 66 | + return inv.Command.HelpHandler(inv) |
| 67 | + }, |
| 68 | + Children: []*serpent.Command{ |
| 69 | + r.immortalStreamListCmd(), |
| 70 | + r.immortalStreamDeleteCmd(), |
| 71 | + }, |
| 72 | + } |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func (r *RootCmd) immortalStreamListCmd() *serpent.Command { |
| 77 | + client := new(codersdk.Client) |
| 78 | + cmd := &serpent.Command{ |
| 79 | + Use: "list <workspace-name>", |
| 80 | + Short: "List active immortal streams in a workspace", |
| 81 | + Middleware: serpent.Chain( |
| 82 | + serpent.RequireNArgs(1), |
| 83 | + r.InitClient(client), |
| 84 | + ), |
| 85 | + Handler: func(inv *serpent.Invocation) error { |
| 86 | + ctx := inv.Context() |
| 87 | + workspaceName := inv.Args[0] |
| 88 | + |
| 89 | + workspace, workspaceAgent, _, err := getWorkspaceAndAgent(ctx, inv, client, false, workspaceName) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + if workspace.LatestBuild.Transition != codersdk.WorkspaceTransitionStart { |
| 95 | + return xerrors.New("workspace must be running to list immortal streams") |
| 96 | + } |
| 97 | + |
| 98 | + // Create immortal stream client |
| 99 | + // Note: We don't need to dial the agent for management operations |
| 100 | + // as these go through the server's proxy endpoints |
| 101 | + streamClient := newImmortalStreamClient(client, workspaceAgent.ID, inv.Logger) |
| 102 | + streams, err := streamClient.listStreams(ctx) |
| 103 | + if err != nil { |
| 104 | + return xerrors.Errorf("list immortal streams: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + if len(streams) == 0 { |
| 108 | + cliui.Info(inv.Stderr, "No active immortal streams found.") |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + // Display the streams in a table |
| 113 | + displayImmortalStreams(inv, streams) |
| 114 | + return nil |
| 115 | + }, |
| 116 | + } |
| 117 | + return cmd |
| 118 | +} |
| 119 | + |
| 120 | +func (r *RootCmd) immortalStreamDeleteCmd() *serpent.Command { |
| 121 | + client := new(codersdk.Client) |
| 122 | + cmd := &serpent.Command{ |
| 123 | + Use: "delete <workspace-name> <immortal-stream-name>", |
| 124 | + Short: "Delete an active immortal stream", |
| 125 | + Middleware: serpent.Chain( |
| 126 | + serpent.RequireNArgs(2), |
| 127 | + r.InitClient(client), |
| 128 | + ), |
| 129 | + Handler: func(inv *serpent.Invocation) error { |
| 130 | + ctx := inv.Context() |
| 131 | + workspaceName := inv.Args[0] |
| 132 | + streamName := inv.Args[1] |
| 133 | + |
| 134 | + workspace, workspaceAgent, _, err := getWorkspaceAndAgent(ctx, inv, client, false, workspaceName) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + if workspace.LatestBuild.Transition != codersdk.WorkspaceTransitionStart { |
| 140 | + return xerrors.New("workspace must be running to delete immortal streams") |
| 141 | + } |
| 142 | + |
| 143 | + // Create immortal stream client |
| 144 | + streamClient := newImmortalStreamClient(client, workspaceAgent.ID, inv.Logger) |
| 145 | + streams, err := streamClient.listStreams(ctx) |
| 146 | + if err != nil { |
| 147 | + return xerrors.Errorf("list immortal streams: %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + var targetStream *codersdk.ImmortalStream |
| 151 | + for _, stream := range streams { |
| 152 | + if stream.Name == streamName { |
| 153 | + targetStream = &stream |
| 154 | + break |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if targetStream == nil { |
| 159 | + return xerrors.Errorf("immortal stream %q not found", streamName) |
| 160 | + } |
| 161 | + |
| 162 | + // Delete the stream |
| 163 | + err = streamClient.deleteStream(ctx, targetStream.ID) |
| 164 | + if err != nil { |
| 165 | + return xerrors.Errorf("delete immortal stream: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + cliui.Info(inv.Stderr, fmt.Sprintf("Deleted immortal stream %q (ID: %s)", streamName, targetStream.ID)) |
| 169 | + return nil |
| 170 | + }, |
| 171 | + } |
| 172 | + return cmd |
| 173 | +} |
| 174 | + |
| 175 | +func displayImmortalStreams(inv *serpent.Invocation, streams []codersdk.ImmortalStream) { |
| 176 | + _, _ = fmt.Fprintf(inv.Stderr, "Active Immortal Streams:\n\n") |
| 177 | + _, _ = fmt.Fprintf(inv.Stderr, "%-20s %-6s %-20s %-20s\n", "NAME", "PORT", "CREATED", "LAST CONNECTED") |
| 178 | + _, _ = fmt.Fprintf(inv.Stderr, "%-20s %-6s %-20s %-20s\n", "----", "----", "-------", "--------------") |
| 179 | + |
| 180 | + for _, stream := range streams { |
| 181 | + createdTime := stream.CreatedAt.Format("2006-01-02 15:04:05") |
| 182 | + lastConnTime := stream.LastConnectionAt.Format("2006-01-02 15:04:05") |
| 183 | + |
| 184 | + _, _ = fmt.Fprintf(inv.Stderr, "%-20s %-6d %-20s %-20s\n", |
| 185 | + stream.Name, stream.TCPPort, createdTime, lastConnTime) |
| 186 | + } |
| 187 | + _, _ = fmt.Fprintf(inv.Stderr, "\n") |
| 188 | +} |
0 commit comments