Skip to content

feat(cli): add display of open ports in coder show #16464

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

Merged
merged 2 commits into from
Feb 10, 2025
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
85 changes: 61 additions & 24 deletions cli/cliui/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"io"
"sort"
"strconv"
"strings"

"github.com/google/uuid"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/mod/semver"

Expand All @@ -14,12 +16,18 @@ import (
"github.com/coder/pretty"
)

var (
pipeMid = "├"
pipeEnd = "└"
)

type WorkspaceResourcesOptions struct {
WorkspaceName string
HideAgentState bool
HideAccess bool
Title string
ServerVersion string
ListeningPorts map[uuid.UUID]codersdk.WorkspaceAgentListeningPortsResponse
}

// WorkspaceResources displays the connection status and tree-view of provided resources.
Expand Down Expand Up @@ -86,39 +94,61 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource
})
// Display all agents associated with the resource.
for index, agent := range resource.Agents {
pipe := "├"
if index == len(resource.Agents)-1 {
pipe = "└"
}
row := table.Row{
// These tree from a resource!
fmt.Sprintf("%s─ %s (%s, %s)", pipe, agent.Name, agent.OperatingSystem, agent.Architecture),
}
if !options.HideAgentState {
var agentStatus, agentHealth, agentVersion string
if !options.HideAgentState {
agentStatus = renderAgentStatus(agent)
agentHealth = renderAgentHealth(agent)
agentVersion = renderAgentVersion(agent.Version, options.ServerVersion)
}
row = append(row, agentStatus, agentHealth, agentVersion)
}
if !options.HideAccess {
sshCommand := "coder ssh " + options.WorkspaceName
if totalAgents > 1 {
sshCommand += "." + agent.Name
tableWriter.AppendRow(renderAgentRow(agent, index, totalAgents, options))
if options.ListeningPorts != nil {
if lp, ok := options.ListeningPorts[agent.ID]; ok && len(lp.Ports) > 0 {
tableWriter.AppendRow(table.Row{
fmt.Sprintf(" %s─ %s", renderPipe(index, totalAgents), "Open Ports"),
})
for _, port := range lp.Ports {
tableWriter.AppendRow(renderPortRow(port, index, totalAgents))
}
}
sshCommand = pretty.Sprint(DefaultStyles.Code, sshCommand)
row = append(row, sshCommand)
}
tableWriter.AppendRow(row)
}
tableWriter.AppendSeparator()
}
_, err := fmt.Fprintln(writer, tableWriter.Render())
return err
}

func renderAgentRow(agent codersdk.WorkspaceAgent, index, totalAgents int, options WorkspaceResourcesOptions) table.Row {
row := table.Row{
// These tree from a resource!
fmt.Sprintf("%s─ %s (%s, %s)", renderPipe(index, totalAgents), agent.Name, agent.OperatingSystem, agent.Architecture),
}
if !options.HideAgentState {
var agentStatus, agentHealth, agentVersion string
if !options.HideAgentState {
agentStatus = renderAgentStatus(agent)
agentHealth = renderAgentHealth(agent)
agentVersion = renderAgentVersion(agent.Version, options.ServerVersion)
}
row = append(row, agentStatus, agentHealth, agentVersion)
}
if !options.HideAccess {
sshCommand := "coder ssh " + options.WorkspaceName
if totalAgents > 1 {
sshCommand += "." + agent.Name
}
sshCommand = pretty.Sprint(DefaultStyles.Code, sshCommand)
row = append(row, sshCommand)
}
return row
}

func renderPortRow(port codersdk.WorkspaceAgentListeningPort, index, totalPorts int) table.Row {
var sb strings.Builder
_, _ = sb.WriteString(" ")
_, _ = sb.WriteString(renderPipe(index, totalPorts))
_, _ = sb.WriteString("─ ")
_, _ = sb.WriteString(pretty.Sprintf(DefaultStyles.Code, "%5d/%s", port.Port, port.Network))
if port.ProcessName != "" {
_, _ = sb.WriteString(pretty.Sprintf(DefaultStyles.Keyword, " [%s]", port.ProcessName))
}
return table.Row{sb.String()}
}

func renderAgentStatus(agent codersdk.WorkspaceAgent) string {
switch agent.Status {
case codersdk.WorkspaceAgentConnecting:
Expand Down Expand Up @@ -163,3 +193,10 @@ func renderAgentVersion(agentVersion, serverVersion string) string {
}
return pretty.Sprint(DefaultStyles.Keyword, agentVersion)
}

func renderPipe(idx, total int) string {
if idx == total-1 {
return pipeEnd
}
return pipeMid
}
41 changes: 39 additions & 2 deletions cli/show.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cli

import (
"sort"
"sync"

"golang.org/x/xerrors"

"github.com/google/uuid"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
Expand All @@ -26,10 +31,42 @@ func (r *RootCmd) show() *serpent.Command {
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
return cliui.WorkspaceResources(inv.Stdout, workspace.LatestBuild.Resources, cliui.WorkspaceResourcesOptions{

options := cliui.WorkspaceResourcesOptions{
WorkspaceName: workspace.Name,
ServerVersion: buildInfo.Version,
})
}
if workspace.LatestBuild.Status == codersdk.WorkspaceStatusRunning {
// Get listening ports for each agent.
options.ListeningPorts = fetchListeningPorts(inv, client, workspace.LatestBuild.Resources...)
}
return cliui.WorkspaceResources(inv.Stdout, workspace.LatestBuild.Resources, options)
},
}
}

func fetchListeningPorts(inv *serpent.Invocation, client *codersdk.Client, resources ...codersdk.WorkspaceResource) map[uuid.UUID]codersdk.WorkspaceAgentListeningPortsResponse {
ports := make(map[uuid.UUID]codersdk.WorkspaceAgentListeningPortsResponse)
var wg sync.WaitGroup
var mu sync.Mutex
for _, res := range resources {
for _, agent := range res.Agents {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not used to seeing async for loops without temp variables, scary 😆

wg.Add(1)
go func() {
defer wg.Done()
lp, err := client.WorkspaceAgentListeningPorts(inv.Context(), agent.ID)
if err != nil {
cliui.Warnf(inv.Stderr, "Failed to get listening ports for agent %s: %v", agent.Name, err)
}
sort.Slice(lp.Ports, func(i, j int) bool {
return lp.Ports[i].Port < lp.Ports[j].Port
})
mu.Lock()
ports[agent.ID] = lp
mu.Unlock()
}()
}
}
wg.Wait()
return ports
}
Loading