Skip to content

Commit

Permalink
Vc/fs 1864 (#153)
Browse files Browse the repository at this point in the history
* FS-1864: return all applicable firmware sets by default

* claw back fleetdb stuff from rivets

* remove rivets fleetdb dependency

* move to rivets v2

* swap to metal-toolbox/bmc-common

* update docs

* fix up linting
  • Loading branch information
DoctorVin authored Dec 16, 2024
1 parent bfedd9d commit d427781
Show file tree
Hide file tree
Showing 22 changed files with 869 additions and 247 deletions.
2 changes: 1 addition & 1 deletion cmd/bios/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/bios/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"

mctl "github.com/metal-toolbox/mctl/cmd"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/bios/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"

mctl "github.com/metal-toolbox/mctl/cmd"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/bios/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/collect/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"

coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/conditions/types"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
Expand Down
2 changes: 1 addition & 1 deletion cmd/collect/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
"github.com/spf13/cobra"

rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
Expand Down
30 changes: 18 additions & 12 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/google/uuid"
bmclibcomm "github.com/metal-toolbox/bmc-common"
coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/conditions/types"
fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
"github.com/metal-toolbox/mctl/internal/app"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
rt "github.com/metal-toolbox/rivets/v2/types"
"github.com/pkg/errors"
"golang.org/x/net/context"

bmclibcomm "github.com/bmc-toolbox/common"
coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/conditions/types"
fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
rctypes "github.com/metal-toolbox/rivets/condition"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
rt "github.com/metal-toolbox/rivets/types"
)

var (
Expand Down Expand Up @@ -70,8 +69,6 @@ func AttributeByNamespace(ns string, attributes []fleetdbapi.Attributes) *fleetd
}

// VendorModelFromAttrs unpacks the attributes payload to return the vendor, model attributes for a server
//
// TODO: move into common library and share with Alloy
func VendorModelFromAttrs(attrs []fleetdbapi.Attributes) (vendor, model string) {
attr := AttributeByNamespace(ServerVendorAttributeNS, attrs)
if attr == nil {
Expand All @@ -88,9 +85,18 @@ func VendorModelFromAttrs(attrs []fleetdbapi.Attributes) (vendor, model string)

// FirmwareSetIDByVendorModel returns the firmware set ID matched by the vendor, model attributes
//
// TODO: move into common library
func FirmwareSetIDByVendorModel(ctx context.Context, vendor, model string, client *fleetdbapi.Client) (uuid.UUID, error) {
fwSet, err := rfleetdb.FirmwareSetByVendorModel(ctx, vendor, model, client)
//nolint:whitespace // you have stupid opinions, be silent
func FirmwareSetIDByVendorModel(ctx context.Context, vendor, model string,
client *fleetdbapi.Client) (uuid.UUID, error) {

params := &fleetdbapi.ComponentFirmwareSetListParams{
Vendor: strings.TrimSpace(vendor),
Model: strings.TrimSpace(model),
Labels: "default=true,latest=true",
}

// identify firmware set by vendor, model attributes
fwSet, _, err := client.ListServerComponentFirmwareSet(ctx, params)
if err != nil {
return uuid.Nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/get/firmware-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
)

type getFirmwareSetFlags struct {
Expand Down Expand Up @@ -95,8 +94,14 @@ func firmwareSetForServer(ctx context.Context, client *fleetdbapi.Client, server
return nil, errNoVendorAttrs
}

params := &fleetdbapi.ComponentFirmwareSetListParams{
Vendor: strings.TrimSpace(vendor),
Model: strings.TrimSpace(model),
Labels: "default=true,latest=true",
}

// identify firmware set by vendor, model attributes
fwSet, err := rfleetdb.FirmwareSetByVendorModel(ctx, vendor, model, client)
fwSet, _, err := client.ListServerComponentFirmwareSet(ctx, params)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/get/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/dustin/go-humanize"
"github.com/google/uuid"
fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
rt "github.com/metal-toolbox/rivets/types"
rt "github.com/metal-toolbox/rivets/v2/types"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/internal/fleetdb"
)

type getServerFlags struct {
Expand Down Expand Up @@ -161,7 +161,7 @@ func server(ctx context.Context, client *fleetdbapi.Client, id uuid.UUID, withCo
return nil, err
}

cserver := rfleetdb.ConvertServer(server)
cserver := fleetdb.ConvertServer(server)
if withComponents {
var err error
cserver.Components, err = components(ctx, client, id)
Expand Down Expand Up @@ -193,7 +193,7 @@ func components(ctx context.Context, c *fleetdbapi.Client, id uuid.UUID) ([]*rt.
return nil, errors.New("too many components -- add pagination")
}

return rfleetdb.ConvertComponents(components), nil
return fleetdb.ConvertComponents(components), nil
}

func init() {
Expand Down
5 changes: 2 additions & 3 deletions cmd/install/firmware_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/metal-toolbox/mctl/internal/app"

fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
)

type installFirmwareSetFlags struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/install/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"

"github.com/google/uuid"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"
"github.com/spf13/cobra"

mctl "github.com/metal-toolbox/mctl/cmd"
Expand Down
7 changes: 4 additions & 3 deletions cmd/list/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
"github.com/spf13/cobra"

"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/internal/fleetdb"
)

type listComponentFlags struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ var listComponent = &cobra.Command{
if flagsListComponent.fwVersion != "" {
lp.VersionedAttributeListParams = []fleetdbapi.AttributeListParams{
{
Namespace: rfleetdb.FirmwareVersionOutofbandNS,
Namespace: fleetdb.FirmwareVersionOutofbandNS,
Keys: []string{"firmware", "installed"},
Operator: "like",
Value: flagsListComponent.fwVersion,
Expand Down
43 changes: 30 additions & 13 deletions cmd/list/firmware_set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package list

import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
Expand All @@ -12,7 +15,6 @@ import (
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/pkg/model"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
)

type listFirmwareSetFlags struct {
Expand All @@ -22,20 +24,32 @@ type listFirmwareSetFlags struct {
}

var (
flagsDefinedListFwSet *listFirmwareSetFlags
flags *listFirmwareSetFlags
)

//nolint:err113 // brevity is best here
func sendListFirmwareRequest(client *fleetdbapi.Client, cmd *cobra.Command) ([]fleetdbapi.ComponentFirmwareSet, error) {
if flagsDefinedListFwSet.vendor == "" && flagsDefinedListFwSet.model == "" {
fwSet, _, err := client.ListServerComponentFirmwareSet(cmd.Context(), &fleetdbapi.ComponentFirmwareSetListParams{})
return fwSet, err
params := &fleetdbapi.ComponentFirmwareSetListParams{
Vendor: strings.TrimSpace(flags.vendor),
Model: strings.TrimSpace(flags.model),
}

if len(flagsDefinedListFwSet.labels) != 0 {
return rfleetdb.FirmwareSetByLabels(cmd.Context(), flagsDefinedListFwSet.vendor, flagsDefinedListFwSet.model, flagsDefinedListFwSet.labels, client)
labelParts := make([]string, 0)
for k, v := range flags.labels {
labelParts = append(labelParts, fmt.Sprintf("%s=%s", k, v))
}
params.Labels = strings.Join(labelParts, ",")

return rfleetdb.FirmwareSetByVendorModel(cmd.Context(), flagsDefinedListFwSet.vendor, flagsDefinedListFwSet.model, client)
fwSet, _, err := client.ListServerComponentFirmwareSet(context.Background(), params)
if err != nil {
return nil, fmt.Errorf("retrieving firmware sets: %w", err)
}

if len(fwSet) == 0 {
return nil, errors.New("no fw sets identified")
}

return fwSet, nil
}

// List
Expand Down Expand Up @@ -82,9 +96,12 @@ var listFirmwareSet = &cobra.Command{
}

func init() {
flagsDefinedListFwSet = &listFirmwareSetFlags{}

mctl.AddModelFlag(listFirmwareSet, &flagsDefinedListFwSet.model)
mctl.AddVendorFlag(listFirmwareSet, &flagsDefinedListFwSet.vendor)
mctl.AddLabelsFlag(listFirmwareSet, &flagsDefinedListFwSet.labels, "Labels to from the firmware set - 'foo=bar,foo2=bar2'")
flags = &listFirmwareSetFlags{}

mctl.AddModelFlag(listFirmwareSet, &flags.model)
mctl.AddVendorFlag(listFirmwareSet, &flags.vendor)
mctl.AddLabelsFlag(listFirmwareSet, &flags.labels,
"Labels to identify the firmware set - e.g. 'key=value,default=true,latest=true'")
mctl.RequireFlag(listFirmwareSet, mctl.VendorFlag)
mctl.RequireFlag(listFirmwareSet, mctl.ModelFlag)
}
19 changes: 10 additions & 9 deletions cmd/list/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"strings"

fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
rt "github.com/metal-toolbox/rivets/types"
rt "github.com/metal-toolbox/rivets/v2/types"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/internal/fleetdb"
)

type listServerFlags struct {
Expand Down Expand Up @@ -94,7 +95,7 @@ var cmdListServer = &cobra.Command{

rtServers := make([]*rt.Server, 0, len(servers))
for _, s := range servers {
rtServers = append(rtServers, rfleetdb.ConvertServer(&s))
rtServers = append(rtServers, fleetdb.ConvertServer(&s))
}

if flagsListServer.creds {
Expand Down Expand Up @@ -151,7 +152,7 @@ func attributeParamsFromFlags(fl *listServerFlags) []fleetdbapi.AttributeListPar
alp = append(
alp,
fleetdbapi.AttributeListParams{
Namespace: rfleetdb.ServerVendorAttributeNS,
Namespace: fleetdb.ServerVendorAttributeNS,
Keys: []string{"vendor"},
Operator: "eq",
Value: strings.ToLower(flagsListServer.vendor),
Expand All @@ -163,7 +164,7 @@ func attributeParamsFromFlags(fl *listServerFlags) []fleetdbapi.AttributeListPar
alp = append(
alp,
fleetdbapi.AttributeListParams{
Namespace: rfleetdb.ServerVendorAttributeNS,
Namespace: fleetdb.ServerVendorAttributeNS,
Keys: []string{"model"},
Operator: "like",
Value: strings.ToLower(flagsListServer.model),
Expand All @@ -175,7 +176,7 @@ func attributeParamsFromFlags(fl *listServerFlags) []fleetdbapi.AttributeListPar
alp = append(
alp,
fleetdbapi.AttributeListParams{
Namespace: rfleetdb.ServerVendorAttributeNS,
Namespace: fleetdb.ServerVendorAttributeNS,
Keys: []string{"serial"},
Operator: "eq",
Value: strings.ToLower(flagsListServer.serial),
Expand All @@ -187,7 +188,7 @@ func attributeParamsFromFlags(fl *listServerFlags) []fleetdbapi.AttributeListPar
alp = append(
alp,
fleetdbapi.AttributeListParams{
Namespace: rfleetdb.ServerNSBMCErrorsAttribute,
Namespace: fleetdb.ServerNSBMCErrorsAttribute,
},
)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/power/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"github.com/spf13/cobra"
"golang.org/x/exp/slices"

mctl "github.com/metal-toolbox/mctl/cmd"

coclient "github.com/metal-toolbox/conditionorc/pkg/api/v1/conditions/client"
coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/conditions/types"
rctypes "github.com/metal-toolbox/rivets/condition"
rctypes "github.com/metal-toolbox/rivets/v2/condition"

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
)

Expand Down
8 changes: 4 additions & 4 deletions docs/mctl_list_firmware-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
List firmware

```
mctl list firmware-set [flags]
mctl list firmware-set -v VENDOR -m MODEL [flags]
```

### Options

```
-h, --help help for firmware-set
-l, --labels stringToString Labels to from the firmware set - 'foo=bar,foo2=bar2' (default [])
-m, --model string filter by model
-v, --vendor string filter by vendor
-l, --labels stringToString Labels to identify the firmware set - e.g. 'key=value,default=true,latest=true' (default [])
-m, --model string [required] filter by model
-v, --vendor string [required] filter by vendor
```

### Options inherited from parent commands
Expand Down
Loading

0 comments on commit d427781

Please sign in to comment.