Skip to content
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
126 changes: 44 additions & 82 deletions cmd/admin/v2/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,9 @@ func newMachineCmd(c *config.Config) *cobra.Command {
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("id", "", "id of machine which should be listed")
cmd.Flags().String("name", "", "name from machines which should be listed")
cmd.Flags().String("hostname", "", "hostname from machines which should be listed")
cmd.Flags().String("size", "", "size from machines which should be listed")
cmd.Flags().String("image", "", "image")
cmd.Flags().StringP("project", "p", "", "project from where machines should be listed")
cmd.Flags().StringP("partition", "", "", "partition from where machines should be listed")
helpers.AddMachineQueryFlags(cmd, c.Completion)

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("size", c.Completion.Size))
genericcli.Must(cmd.RegisterFlagCompletionFunc("image", c.Completion.Image))
genericcli.Must(cmd.RegisterFlagCompletionFunc("partition", c.Completion.Partition))
genericcli.Must(cmd.RegisterFlagCompletionFunc("id", c.Completion.AdminMachine))

cmd.Long = cmd.Short + "\n" + helpers.EmojiHelpText()
cmd.Long = cmd.Short + "\n" + helpers.MachineListEmojiHelpText()
},
DescribeCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project of the machine")
Expand Down Expand Up @@ -144,8 +132,14 @@ If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picke
ValidArgsFn: c.Completion.AdminMachine,
}

bmcCmd := &cobra.Command{
Use: "bmc",
Aliases: []string{"ipmi"},
Short: "get and list machine bmc/ipmi information",
}

bmcCommandCmd := &cobra.Command{
Use: "bmc-command",
Use: "command",
Short: "send a command to the bmc of a machine",
RunE: func(cmd *cobra.Command, args []string) error {
return w.bmcCommand(args)
Expand All @@ -156,15 +150,10 @@ If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picke
genericcli.Must(bmcCommandCmd.RegisterFlagCompletionFunc("command", c.Completion.BMCCommands))
genericcli.Must(bmcCommandCmd.MarkFlagRequired("command"))

bmcCmd := &cobra.Command{
Use: "bmc",
Aliases: []string{"ipmi"},
Short: "get and list machine bmc/ipmi information",
}

bmcGetCmd := &cobra.Command{
Use: "get",
Short: "get the bmc of a machine",
bmcDescribeCmd := &cobra.Command{
Use: "describe",
Aliases: []string{"get"},
Short: "get the bmc of a machine",
RunE: func(cmd *cobra.Command, args []string) error {
return w.bmcGet(cmd.Context(), args)
},
Expand All @@ -178,19 +167,11 @@ If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picke
return w.bmcList(cmd.Context())
},
}
bmcListCmd.Flags().String("id", "", "id of machine which should be listed")
bmcListCmd.Flags().String("size", "", "size from machines which should be listed")
bmcListCmd.Flags().String("image", "", "image")
bmcListCmd.Flags().StringP("project", "p", "", "project from where machines should be listed")
bmcListCmd.Flags().StringP("partition", "", "", "partition from where machines should be listed")

genericcli.Must(bmcListCmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(bmcListCmd.RegisterFlagCompletionFunc("size", c.Completion.Size))
genericcli.Must(bmcListCmd.RegisterFlagCompletionFunc("image", c.Completion.Image))
genericcli.Must(bmcListCmd.RegisterFlagCompletionFunc("partition", c.Completion.Partition))
genericcli.Must(bmcListCmd.RegisterFlagCompletionFunc("id", c.Completion.AdminMachine))
helpers.AddMachineQueryFlags(bmcListCmd, c.Completion)
genericcli.AddSortFlag(bmcListCmd, sorters.MachineBmcSorter())

bmcCmd.AddCommand(bmcGetCmd, bmcListCmd)
bmcCmd.AddCommand(bmcDescribeCmd, bmcListCmd, bmcCommandCmd)

lockCmd := &cobra.Command{
Use: "lock",
Expand Down Expand Up @@ -247,7 +228,7 @@ If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picke
firewallSSHCmd.Flags().StringP("identity", "i", "~/.ssh/id_rsa", "specify identity file to SSH to the firewall like: -i path/to/id_rsa")
firewallSSHCmd.Flags().String("reason", "", "the reason why to connect to the firewall through SSH")

return genericcli.NewCmds(cmdsConfig, bmcCommandCmd, bmcCmd, lockCmd, taintCmd, consoleCmd, consolePasswordCmd, firewallSSHCmd)
return genericcli.NewCmds(cmdsConfig, bmcCmd, lockCmd, taintCmd, consoleCmd, consolePasswordCmd, firewallSSHCmd)
}

func (c *machine) Create(rq *apiv2.MachineServiceCreateRequest) (*apiv2.Machine, error) {
Expand Down Expand Up @@ -298,49 +279,13 @@ func (c *machine) List() ([]*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

var allocation *apiv2.MachineAllocationQuery

if viper.IsSet("hostname") || viper.IsSet("name") || viper.IsSet("project") || viper.IsSet("image") {
allocation = &apiv2.MachineAllocationQuery{
Hostname: pointer.PointerOrNil(viper.GetString("hostname")),
Name: pointer.PointerOrNil(viper.GetString("name")),
Project: pointer.PointerOrNil(viper.GetString("project")),
Image: pointer.PointerOrNil(viper.GetString("image")),
}
query, err := helpers.MachineQuery(true)
if err != nil {
return nil, err
}

resp, err := c.c.Client.Adminv2().Machine().List(ctx, &adminv2.MachineServiceListRequest{
Query: &apiv2.MachineQuery{
Uuid: pointer.PointerOrNil(viper.GetString("id")),
Partition: pointer.PointerOrNil(viper.GetString("partition")),
Size: pointer.PointerOrNil(viper.GetString("size")),
Allocation: allocation,
// Rack: pointer.PointerOrNil(viper.GetString("rack")),
// Labels: &apiv2.Labels{
// Labels: tag.NewTagMap(viper.GetStringSlice("labels")),
// },
// Bmc: &apiv2.MachineBMCQuery{
// Address: pointer.PointerOrNil(viper.GetString("bmc-address")),
// Mac: pointer.PointerOrNil(viper.GetString("bmc-mac")),
// User: pointer.PointerOrNil(viper.GetString("bmc-user")),
// Interface: pointer.PointerOrNil(viper.GetString("bmc-interface")),
// },
// Fru: &apiv2.MachineFRUQuery{
// ChassisPartNumber: pointer.PointerOrNil(viper.GetString("chassis-part-number")),
// ChassisPartSerial: pointer.PointerOrNil(viper.GetString("chassis-part-serial")),
// BoardMfg: pointer.PointerOrNil(viper.GetString("board-mfg")),
// BoardSerial: pointer.PointerOrNil(viper.GetString("board-serial")),
// BoardPartNumber: pointer.PointerOrNil(viper.GetString("board-part-number")),
// ProductManufacturer: pointer.PointerOrNil(viper.GetString("product-manufacturer")),
// ProductPartNumber: pointer.PointerOrNil(viper.GetString("product-part-number")),
// ProductSerial: pointer.PointerOrNil(viper.GetString("product-serial")),
// },
// Hardware: &apiv2.MachineHardwareQuery{
// Memory: pointer.PointerOrNil(viper.GetUint64("memory")),
// CpuCores: pointer.PointerOrNil(viper.GetUint32("cpu-cores")),
// },
// State: &0,
},
Query: query,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -445,19 +390,32 @@ func (c *machine) bmcGet(ctx context.Context, args []string) error {
return err
}

return c.c.DescribePrinter.Print(resp)
return c.c.DescribePrinter.Print(resp.BmcDetails)
}

func (c *machine) bmcList(ctx context.Context) error {
// FIXME api contains the wrong query here, must be a MachineQuery instead of a MachineBMCQuery
req := &adminv2.MachineServiceListBMCRequest{}
sortKeys, err := genericcli.ParseSortFlags()
if err != nil {
return err
}

resp, err := c.c.Client.Adminv2().Machine().ListBMC(ctx, req)
query, err := helpers.MachineQuery(true)
if err != nil {
return err
}

return c.c.ListPrinter.Print(resp.BmcReports)
resp, err := c.c.Client.Adminv2().Machine().ListBMC(ctx, &adminv2.MachineServiceListBMCRequest{
Query: query,
})
if err != nil {
return err
}

if err := sorters.MachineBmcSorter().SortBy(resp.BmcDetails, sortKeys...); err != nil {
return err
}

return c.c.ListPrinter.Print(resp.BmcDetails)
}

func (c *machine) consolePassword(ctx context.Context, args []string) error {
Expand Down Expand Up @@ -515,7 +473,11 @@ func (c *machine) impitool(ctx context.Context, id string) error {
return err
}

bmc := resp.Bmc.Bmc
if resp.BmcDetails == nil || resp.BmcDetails.BmcReport == nil || resp.BmcDetails.BmcReport.Bmc == nil {
return fmt.Errorf("no bmc details present")
}

bmc := resp.BmcDetails.BmcReport.Bmc
intf := "lanplus"

// -I lanplus -H 192.168.2.19 -U ADMIN -P ADMIN sol activate
Expand Down
34 changes: 6 additions & 28 deletions cmd/api/v2/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -40,20 +39,9 @@ func newMachineCmd(c *config.Config) *cobra.Command {
cmd.Aliases = []string{"allocate"}
},
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("uuid", "", "allocation uuid of machine which should be listed")
cmd.Flags().String("name", "", "name from machines which should be listed")
cmd.Flags().String("hostname", "", "hostname from machines which should be listed")
cmd.Flags().String("size", "", "size from machines which should be listed")
cmd.Flags().String("image", "", "image")
cmd.Flags().StringP("project", "p", "", "project from where machines should be listed")
cmd.Flags().StringP("partition", "", "", "partition from where machines should be listed")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("size", c.Completion.Size))
genericcli.Must(cmd.RegisterFlagCompletionFunc("image", c.Completion.Image))
genericcli.Must(cmd.RegisterFlagCompletionFunc("partition", c.Completion.Partition))
helpers.AddMachineQueryFlags(cmd, c.Completion)

cmd.Long = cmd.Short + "\n" + helpers.EmojiHelpText()
cmd.Long = cmd.Short + "\n" + helpers.MachineListEmojiHelpText()
},
UpdateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project from where machines should be listed")
Expand Down Expand Up @@ -147,24 +135,14 @@ func (c *machine) List() ([]*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

var allocation *apiv2.MachineAllocationQuery

if viper.IsSet("hostname") || viper.IsSet("image") {
allocation = &apiv2.MachineAllocationQuery{
Hostname: pointer.PointerOrNil(viper.GetString("hostname")),
Image: pointer.PointerOrNil(viper.GetString("image")),
}
query, err := helpers.MachineQuery(false)
if err != nil {
return nil, err
}

resp, err := c.c.Client.Apiv2().Machine().List(ctx, &apiv2.MachineServiceListRequest{
Project: c.c.GetProject(),
Query: &apiv2.MachineQuery{
Uuid: pointer.PointerOrNil(viper.GetString("id")),
Name: pointer.PointerOrNil(viper.GetString("name")),
Partition: pointer.PointerOrNil(viper.GetString("partition")),
Size: pointer.PointerOrNil(viper.GetString("size")),
Allocation: allocation,
},
Query: query,
})
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions cmd/completion/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func (c *Completion) Firewall(cmd *cobra.Command, args []string, toComplete stri
func (c *Completion) BMCCommands(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.genericEnums(apiv2.MachineBMCCommand_name)
}

func (c *Completion) MachineState(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.genericEnums(apiv2.MachineState_name)
}

func (c *Completion) MachineAllocationType(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.genericEnums(apiv2.MachineAllocationType_name)
}
14 changes: 14 additions & 0 deletions cmd/completion/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func (c *Completion) SwitchRack(cmd *cobra.Command, args []string, toComplete st
return racks, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) SwitchRoom(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var rooms []string
for _, s := range resp.Switches {
rooms = append(rooms, pointer.SafeDeref(s.Room))
}

return rooms, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) SwitchOSVendor(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.Client.Adminv2().Switch().List(cmd.Context(), &adminv2.SwitchServiceListRequest{})
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions cmd/sorters/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ func MachineSorter() *multisort.Sorter[*apiv2.Machine] {
},
}, multisort.Keys{{ID: "partition"}, {ID: "size"}, {ID: "project"}, {ID: "uuid"}})
}

func MachineBmcSorter() *multisort.Sorter[*apiv2.MachineBMCDetails] {
return multisort.New(multisort.FieldMap[*apiv2.MachineBMCDetails]{
"uuid": func(a, b *apiv2.MachineBMCDetails, descending bool) multisort.CompareResult {
return multisort.Compare(a.Uuid, b.Uuid, descending)
},
"partition": func(a, b *apiv2.MachineBMCDetails, descending bool) multisort.CompareResult {
return multisort.Compare(a.Partition, b.Partition, descending)
},
"size": func(a, b *apiv2.MachineBMCDetails, descending bool) multisort.CompareResult {
return multisort.Compare(a.Size, b.Size, descending)
},
"rack": func(a, b *apiv2.MachineBMCDetails, descending bool) multisort.CompareResult {
return multisort.Compare(a.Rack, b.Rack, descending)
},
"room": func(a, b *apiv2.MachineBMCDetails, descending bool) multisort.CompareResult {
return multisort.Compare(a.Room, b.Room, descending)
},
}, multisort.Keys{{ID: "partition"}, {ID: "size"}, {ID: "uuid"}})
}
5 changes: 4 additions & 1 deletion cmd/tableprinters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
threequarterpie = "◕"
poweron = "⏻"
powersleep = "⏾"
ledon = "🟒"
)

type TablePrinter struct {
Expand Down Expand Up @@ -65,7 +66,9 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
case []*apiv2.Machine:
return t.MachineTable(d, wide)

case map[string]*apiv2.MachineBMCReport:
case *apiv2.MachineBMCDetails:
return t.MachineBMCTable(pointer.WrapInSlice(d), wide)
case []*apiv2.MachineBMCDetails:
return t.MachineBMCTable(d, wide)

case *apiv2.IP:
Expand Down
Loading
Loading