Skip to content
Closed
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
1 change: 1 addition & 0 deletions cmd/uncloud/context/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewListCommand() *cobra.Command {
Use: "ls",
Aliases: []string{"list"},
Short: "List available cluster contexts.",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return list(uncli)
Expand Down
77 changes: 77 additions & 0 deletions cmd/uncloud/machine/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package machine

import (
"context"
"encoding/json"
"fmt"

"github.com/psviderski/uncloud/internal/cli"
"github.com/psviderski/uncloud/pkg/api"
"github.com/spf13/cobra"
)

func NewInspectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "inspect [MACHINE]",
Short: "Display detailed information of a machine. Without an argument it shows all machines.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
if len(args) == 0 {
return inspectAll(cmd.Context(), uncli)
}
return inspect(cmd.Context(), uncli, args[0])
},
}

return cmd
}

func inspect(ctx context.Context, uncli *cli.CLI, name string) error {
client, err := uncli.ConnectCluster(ctx)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()

machines, err := client.ListMachines(ctx, &api.MachineFilter{NamesOrIDs: []string{name}})
if err != nil {
return fmt.Errorf("list machines: %w", err)
}

if len(machines) == 0 {
return fmt.Errorf("machine '%s' not found", name)
}
data, err := json.MarshalIndent(machines[0], "", " ")
if err != nil {
return fmt.Errorf("marshal machines %w", err)
}
fmt.Println(string(data))

return nil
}

func inspectAll(ctx context.Context, uncli *cli.CLI) error {
client, err := uncli.ConnectCluster(ctx)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()

machines, err := client.ListMachines(ctx, &api.MachineFilter{})
if err != nil {
return fmt.Errorf("list machines: %w", err)
}

// Wrap in Machines type to create array of Machines.
type Machines struct {
Machines api.MachineMembersList `json:"machines"`
}
data, err := json.MarshalIndent(Machines{machines}, "", " ")
if err != nil {
return fmt.Errorf("marshal machines: %w", err)
}
fmt.Println(string(data))

return nil
}
13 changes: 4 additions & 9 deletions cmd/uncloud/machine/ls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package machine

import (
"cmp"
"context"
"fmt"
"net/netip"
Expand All @@ -17,6 +18,7 @@ func NewListCommand() *cobra.Command {
Use: "ls",
Aliases: []string{"list"},
Short: "List machines in a cluster.",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return list(cmd.Context(), uncli)
Expand Down Expand Up @@ -46,23 +48,16 @@ func list(ctx context.Context, uncli *cli.CLI) error {
subnet, _ := m.Network.Subnet.ToPrefix()
subnet = netip.PrefixFrom(network.MachineIP(subnet), subnet.Bits())

publicIP := "-"
if m.PublicIp != nil {
ip, _ := m.PublicIp.ToAddr()
publicIP = ip.String()
}

endpoints := make([]string, len(m.Network.Endpoints))
for i, ep := range m.Network.Endpoints {
addrPort, _ := ep.ToAddrPort()
endpoints[i] = addrPort.String()
endpoints[i] = ep.ToString()
}

t.Row(
m.Name,
capitalise(member.State.String()),
subnet.String(),
publicIP,
cmp.Or(m.PublicIp.ToString(), "-"),
strings.Join(endpoints, tui.Faint.Render(", ")),
member.Machine.Id,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/uncloud/machine/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewRootCommand() *cobra.Command {
cmd.AddCommand(
NewAddCommand(),
NewInitCommand(),
NewInspectCommand(),
NewListCommand(),
NewLogsCommand(),
NewRenameCommand(),
Expand Down
6 changes: 3 additions & 3 deletions cmd/uncloud/service/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type inspectOptions struct {
func NewInspectCommand(groupID string) *cobra.Command {
opts := inspectOptions{}
cmd := &cobra.Command{
Use: "inspect SERVICE",
Short: "Display detailed information on a service.",
Args: cobra.ExactArgs(1),
Use: "inspect [SERVICE]",
Short: "Display detailed information on a service. Without arguments it shows all services.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
opts.service = args[0]
Expand Down
1 change: 1 addition & 0 deletions cmd/uncloud/service/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewListCommand(groupID string) *cobra.Command {
Use: "ls",
Aliases: []string{"list"},
Short: "List services.",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return list(cmd.Context(), uncli)
Expand Down
32 changes: 30 additions & 2 deletions cmd/uncloud/volume/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ func NewInspectCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "inspect VOLUME_NAME",
Short: "Display detailed information on a volume.",
Args: cobra.ExactArgs(1),
Short: "Display detailed information on a volume. Without an argument it shows all volumes.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
if len(args) == 0 {
return inspectAll(cmd.Context(), uncli)
}
return inspect(cmd.Context(), uncli, args[0], opts)
},
}
Expand Down Expand Up @@ -76,3 +79,28 @@ func inspect(ctx context.Context, uncli *cli.CLI, name string, opts inspectOptio

return nil
}

func inspectAll(ctx context.Context, uncli *cli.CLI) error {
client, err := uncli.ConnectCluster(ctx)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()

volumes, err := client.ListVolumes(ctx, &api.VolumeFilter{})
if err != nil {
return fmt.Errorf("list volumes: %w", err)
}

// Wrap in Volumes type to create array of Volumes.
type Volumes struct {
Volumes []api.MachineVolume `json:"volumes"`
}
data, err := json.MarshalIndent(Volumes{volumes}, "", " ")
if err != nil {
return fmt.Errorf("marshal volumes: %w", err)
}
fmt.Println(string(data))

return nil
}
1 change: 1 addition & 0 deletions cmd/uncloud/volume/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewListCommand() *cobra.Command {
Use: "ls",
Aliases: []string{"list"},
Short: "List volumes across all machines in the cluster.",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return list(cmd.Context(), uncli, opts)
Expand Down
26 changes: 26 additions & 0 deletions internal/machine/api/pb/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ func (ip *IP) ToAddr() (netip.Addr, error) {
return addr, nil
}

func (ip *IP) ToString() string {
// Has to be ToString, because String is generated (and doesn't do the right thing).
if ip == nil {
return ""
}
addr, _ := ip.ToAddr()
return addr.String()
}

func (ip *IP) Equal(other *IP) bool {
return bytes.Equal(ip.Ip, other.Ip)
}

func (ip *IP) MarshalJSON() ([]byte, error) {
return []byte("\"" + ip.ToString() + "\""), nil
}

func NewIPPort(ap netip.AddrPort) *IPPort {
return &IPPort{Ip: NewIP(ap.Addr()), Port: uint32(ap.Port())}
}
Expand All @@ -39,6 +52,19 @@ func (ipp *IPPort) ToAddrPort() (netip.AddrPort, error) {
return netip.AddrPortFrom(addr, uint16(ipp.Port)), nil
}

func (ipp *IPPort) ToString() string {
// Has to be ToString, because String is generated (and doesn't do the right thing).
if ipp == nil {
return ""
}
addrPort, _ := ipp.ToAddrPort()
return addrPort.String()
}

func (ipp *IPPort) MarshalJSON() ([]byte, error) {
return []byte("\"" + ipp.ToString() + "\""), nil
}

func NewIPPrefix(p netip.Prefix) *IPPrefix {
return &IPPrefix{Ip: NewIP(p.Addr()), Bits: uint32(p.Bits())}
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/9-cli-reference/uc.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A CLI tool for managing Uncloud resources such as machines, services, and volume
* [uc exec](uc_exec.md) - Execute a command in a running service container.
* [uc image](uc_image.md) - Manage images on machines in the cluster.
* [uc images](uc_images.md) - List images on machines in the cluster.
* [uc inspect](uc_inspect.md) - Display detailed information on a service.
* [uc inspect](uc_inspect.md) - Display detailed information on a service. Without arguments it shows all services.
* [uc logs](uc_logs.md) - View service logs.
* [uc ls](uc_ls.md) - List services.
* [uc machine](uc_machine.md) - Manage machines in the cluster.
Expand Down
4 changes: 2 additions & 2 deletions website/docs/9-cli-reference/uc_inspect.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# uc inspect

Display detailed information on a service.
Display detailed information on a service. Without arguments it shows all services.

```
uc inspect SERVICE [flags]
uc inspect [SERVICE] [flags]
```

## Options
Expand Down
1 change: 1 addition & 0 deletions website/docs/9-cli-reference/uc_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Manage machines in the cluster.
* [uc](uc.md) - A CLI tool for managing Uncloud resources such as machines, services, and volumes.
* [uc machine add](uc_machine_add.md) - Add a remote machine to a cluster.
* [uc machine init](uc_machine_init.md) - Initialise a new cluster with a remote machine as the first member.
* [uc machine inspect](uc_machine_inspect.md) - Display detailed information of a machine. Without an argument it shows all machines.
* [uc machine logs](uc_machine_logs.md) - View systemd service logs.
* [uc machine ls](uc_machine_ls.md) - List machines in a cluster.
* [uc machine rename](uc_machine_rename.md) - Rename a machine in the cluster.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/9-cli-reference/uc_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Manage services in the cluster.

* [uc](uc.md) - A CLI tool for managing Uncloud resources such as machines, services, and volumes.
* [uc service exec](uc_service_exec.md) - Execute a command in a running service container.
* [uc service inspect](uc_service_inspect.md) - Display detailed information on a service.
* [uc service inspect](uc_service_inspect.md) - Display detailed information on a service. Without arguments it shows all services.
* [uc service logs](uc_service_logs.md) - View service logs.
* [uc service ls](uc_service_ls.md) - List services.
* [uc service rm](uc_service_rm.md) - Remove one or more services.
Expand Down
4 changes: 2 additions & 2 deletions website/docs/9-cli-reference/uc_service_inspect.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# uc service inspect

Display detailed information on a service.
Display detailed information on a service. Without arguments it shows all services.

```
uc service inspect SERVICE [flags]
uc service inspect [SERVICE] [flags]
```

## Options
Expand Down
2 changes: 1 addition & 1 deletion website/docs/9-cli-reference/uc_volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Manage volumes in the cluster.

* [uc](uc.md) - A CLI tool for managing Uncloud resources such as machines, services, and volumes.
* [uc volume create](uc_volume_create.md) - Create a volume on a specific machine.
* [uc volume inspect](uc_volume_inspect.md) - Display detailed information on a volume.
* [uc volume inspect](uc_volume_inspect.md) - Display detailed information on a volume. Without an argument it shows all volumes.
* [uc volume ls](uc_volume_ls.md) - List volumes across all machines in the cluster.
* [uc volume rm](uc_volume_rm.md) - Remove one or more volumes.

2 changes: 1 addition & 1 deletion website/docs/9-cli-reference/uc_volume_inspect.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# uc volume inspect

Display detailed information on a volume.
Display detailed information on a volume. Without an argument it shows all volumes.

```
uc volume inspect VOLUME_NAME [flags]
Expand Down
Loading