-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/collect: Add command to invoke inventory and BIOS configuration c…
…ollection
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package collect | ||
|
||
import ( | ||
"github.com/metal-toolbox/mctl/cmd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var collect = &cobra.Command{ | ||
Use: "collect", | ||
Short: "Collect current server firmware status and bios configuration", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
//nolint:errcheck // returns nil | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
func init() { | ||
cmd.RootCmd.AddCommand(collect) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package collect | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/google/uuid" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/metal-toolbox/mctl/internal/app" | ||
|
||
coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/types" | ||
"github.com/metal-toolbox/conditionorc/pkg/types" | ||
mctl "github.com/metal-toolbox/mctl/cmd" | ||
rctypes "github.com/metal-toolbox/rivets/condition" | ||
) | ||
|
||
type collectInventoryFlags struct { | ||
serverID string | ||
skipFirmwareStatusCollect bool | ||
skipBiosConfigCollect bool | ||
} | ||
|
||
var ( | ||
flagsDefinedCollectInventory *collectInventoryFlags | ||
) | ||
|
||
var collectInventoryCmd = &cobra.Command{ | ||
Use: "inventory", | ||
Short: "Collect current server firmware status and bios configuration", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
collectInventory(cmd.Context()) | ||
|
||
}, | ||
} | ||
|
||
func collectInventory(ctx context.Context) { | ||
theApp := mctl.MustCreateApp(ctx) | ||
|
||
serverID, err := uuid.Parse(flagsDefinedCollectInventory.serverID) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
params, err := json.Marshal(rctypes.NewInventoryTaskParameters( | ||
serverID, | ||
rctypes.OutofbandInventory, | ||
!flagsDefinedCollectInventory.skipFirmwareStatusCollect, | ||
!flagsDefinedCollectInventory.skipBiosConfigCollect, | ||
)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
conditionCreate := coapiv1.ConditionCreate{ | ||
Exclusive: false, | ||
Parameters: params, | ||
} | ||
|
||
client, err := app.NewConditionsClient(ctx, theApp.Config.Conditions, theApp.Reauth) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
response, err := client.ServerConditionCreate(ctx, serverID, types.ConditionKind(rctypes.Inventory), conditionCreate) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
condition, err := mctl.ConditionFromResponse(response) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("status=%d msg=%s conditionID=%s", response.StatusCode, response.Message, condition.ID) | ||
|
||
} | ||
|
||
func init() { | ||
flagsDefinedCollectInventory = &collectInventoryFlags{} | ||
|
||
collect.AddCommand(collectInventoryCmd) | ||
collectInventoryCmd.PersistentFlags().StringVar(&flagsDefinedCollectInventory.serverID, "server", "", "server UUID") | ||
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipFirmwareStatusCollect, "skip-fw-status", false, "Skip firmware status data collection") | ||
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipBiosConfigCollect, "skip-bios-config", false, "Skip BIOS configuration data collection") | ||
|
||
if err := collectInventoryCmd.MarkPersistentFlagRequired("server"); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package collect | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/metal-toolbox/mctl/internal/app" | ||
"github.com/spf13/cobra" | ||
|
||
cotypes "github.com/metal-toolbox/conditionorc/pkg/types" | ||
mctl "github.com/metal-toolbox/mctl/cmd" | ||
rctypes "github.com/metal-toolbox/rivets/condition" | ||
) | ||
|
||
var serverIDStr string | ||
|
||
var inventoryStatus = &cobra.Command{ | ||
Use: "status --server | -s <server uuid>", | ||
Short: "check the progress of a inventory collection for a server", | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
statusCheck(cmd.Context()) | ||
}, | ||
} | ||
|
||
func statusCheck(ctx context.Context) { | ||
theApp := mctl.MustCreateApp(ctx) | ||
|
||
client, err := app.NewConditionsClient(ctx, theApp.Config.Conditions, theApp.Reauth) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
serverID, err := uuid.Parse(serverIDStr) | ||
if err != nil { | ||
log.Fatalf("parsing server id: %s", err.Error()) | ||
} | ||
|
||
// cotypes to be moved into rivets library | ||
resp, err := client.ServerConditionGet(ctx, serverID, cotypes.ConditionKind(rctypes.Inventory)) | ||
if err != nil { | ||
log.Fatalf("querying server conditions: %s", err.Error()) | ||
} | ||
|
||
s, err := mctl.FormatConditionResponse(resp) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
|
||
fmt.Println(s) | ||
} | ||
|
||
func init() { | ||
flags := inventoryStatus.Flags() | ||
flags.StringVarP(&serverIDStr, "server", "s", "", "server id (typically a UUID)") | ||
|
||
if err := inventoryStatus.MarkFlagRequired("server"); err != nil { | ||
log.Fatalf("marking server flag as required: %s", err.Error()) | ||
} | ||
|
||
collect.AddCommand(inventoryStatus) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters