Skip to content

Commit

Permalink
Remove execution breaks
Browse files Browse the repository at this point in the history
Allow failures to occur when running host queries, logging the error in
the host's query structs.

Adds some more place-holder/skeleton files for the first set of
subcommands.
  • Loading branch information
rustydb committed Jul 18, 2023
1 parent ee4b409 commit cd3a94c
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 64 deletions.
1 change: 1 addition & 0 deletions gru.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
# Used for all hosts except those defined under `hosts:`
insecure: true
username: admin
password: admin
hosts:
Expand Down
15 changes: 6 additions & 9 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ package auth

import (
"fmt"
"github.com/Cray-HPE/gru/pkg/cmd"
"github.com/spf13/viper"
"github.com/stmcginnis/gofish"
"os"
)

type configuration struct {
Expand All @@ -46,7 +46,7 @@ func LoadConfig(path string) {
viper.SetDefault("username", "")
viper.SetDefault("password", "")
if viper.BindEnv("password", "IPMI_PASSWORD") != nil {
panic(fmt.Errorf("failed to bind ipmi_password environment variable"))
cmd.CheckError(fmt.Errorf("failed to bind ipmi_password environment variable"))
}
viper.SetConfigFile(path)

Expand All @@ -56,6 +56,7 @@ func LoadConfig(path string) {
fmt.Printf("Loading config file %s", path)
} else {
// Config file was found but another error was produced
// TODO: Handle all errors except if the file is missing.
}
}

Expand All @@ -67,10 +68,9 @@ func LoadConfig(path string) {
}

// Connection establishes a connection to an endpoint.
func Connection(host string) *gofish.APIClient {
func Connection(host string) (*gofish.APIClient, error) {
if (viper.GetString("username") == "") || (viper.GetString("password") == "") {
fmt.Println(fmt.Errorf("no credentials provided, please provide a config file or environment varialbes"))
os.Exit(1)
cmd.CheckError(fmt.Errorf("no credentials provided, please provide a config file or environment varialbes"))
}
hosts := viper.GetStringMap("hosts")
username := viper.GetString("username")
Expand All @@ -87,8 +87,5 @@ func Connection(host string) *gofish.APIClient {
Insecure: viper.GetBool("insecure"),
}
c, err := gofish.Connect(config)
if err != nil {
panic(err)
}
return c
return c, err
}
11 changes: 6 additions & 5 deletions pkg/cmd/cli/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import "container/list"

// Boot represents boot configuration on the BMC.
type Boot struct {
next string
order list.List
bootOrderPolicy string
networkRetry string
networkRetryCount int
Next string `json:"next"`
Order list.List `json:"order"`
BootOrderPolicy string `json:"bootOrderPolicy"`
NetworkRetry string `json:"networkRetry"`
NetworkRetryCount int `json:"networkRetryCount"`
Error error `json:"error"`
}
4 changes: 1 addition & 3 deletions pkg/cmd/cli/boot/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func NewShowCommand() *cobra.Command {
c := &cobra.Command{
Use: "boot [flags] host [...host]",
Short: "Boot information",
Long: `
Show the current BootOrder; BootNext, networkRetry, and more.
`,
Long: `Show the current BootOrder; BootNext, networkRetry, and more.`,
Run: func(c *cobra.Command, args []string) {
// TODO.
},
Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/cli/chassis/chassis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
MIT License
(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package chassis

import (
"github.com/spf13/cobra"
)

// NewCommand creates the `chassis` subcommand.
func NewCommand() *cobra.Command {
c := &cobra.Command{
Use: "chassis",
DisableFlagsInUseLine: true,
Short: "Chassis control.",
Long: `Interact with a host's chassis.`,
Run: func(c *cobra.Command, args []string) {
},
Hidden: true,
}
return c
}
4 changes: 1 addition & 3 deletions pkg/cmd/cli/disks/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func NewShowCommand() *cobra.Command {
c := &cobra.Command{
Use: "disks [flags] host [...host]",
Short: "Disk device information.",
Long: `
Show available disk devices.
`,
Long: `Show available disk devices.`,
Run: func(c *cobra.Command, args []string) {
// TODO.
},
Expand Down
4 changes: 1 addition & 3 deletions pkg/cmd/cli/nics/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func NewShowCommand() *cobra.Command {
c := &cobra.Command{
Use: "nics [flags] host [...host]",
Short: "Network interface information",
Long: `
Show available network interface hardware.
`,
Long: `Show available network interface hardware.`,
Run: func(c *cobra.Command, args []string) {
// TODO.
},
Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/cli/power/chassis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
MIT License
(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package power

import (
"github.com/spf13/cobra"
)

// NewChassisCommand creates the `power` subcommand for `chassis`.
func NewChassisCommand() *cobra.Command {
c := &cobra.Command{
Use: "power [flags] host [...host]",
Short: "Power Control",
Long: `Check power status, or power on, off, cycle, or reset a host.`,
Run: func(c *cobra.Command, args []string) {
// TODO.
},
Hidden: true,
}
return c
}
19 changes: 0 additions & 19 deletions pkg/cmd/cli/power/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,3 @@
*/

package power

import (
"github.com/spf13/cobra"
)

// NewCommand creates the `power` subcommand.
func NewCommand() *cobra.Command {
c := &cobra.Command{
Use: "power",
DisableFlagsInUseLine: true,
Short: "Power control.",
Long: `
Power control; off, on, cycle, reset.`,
Run: func(c *cobra.Command, args []string) {
},
Hidden: true,
}
return c
}
3 changes: 1 addition & 2 deletions pkg/cmd/cli/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func NewCommand() *cobra.Command {
Use: "show",
DisableFlagsInUseLine: true,
Short: "Curated server information.",
Long: `
Print pre-defined classes of information from one or more BMCs.`,
Long: `Print pre-defined classes of information from one or more BMCs.`,
Run: func(c *cobra.Command, args []string) {
},
}
Expand Down
35 changes: 19 additions & 16 deletions pkg/cmd/cli/system/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ package system
import (
"fmt"
"github.com/Cray-HPE/gru/pkg/auth"
"github.com/Cray-HPE/gru/pkg/cmd"
"github.com/Cray-HPE/gru/pkg/cmd/cli"
"github.com/spf13/cobra"
"sync"
Expand All @@ -40,9 +39,7 @@ func NewShowCommand() *cobra.Command {
c := &cobra.Command{
Use: "system [flags] host [...host]",
Short: "System information.",
Long: `
Show the Server Manufacturer, Server Model, System Version, and Firmware Version for the given server(s).
`,
Long: `Show the Server Manufacturer, Server Model, System Version, and Firmware Version for the given server(s).`,
Run: func(c *cobra.Command, args []string) {
hosts := cli.ParseHosts(args)
content := query(hosts)
Expand Down Expand Up @@ -76,23 +73,29 @@ func query(hosts []string) map[string]interface{} {

// getSystemInformation gets the Manufacturer, Model, BIOSVersion, and FirmwareVersion of the host.
func getSystemInformation(host string) System {
c := auth.Connection(host)
system := System{}
c, err := auth.Connection(host)
if err != nil {
system.Error = err
return system
}
defer c.Logout()

// Retrieve the service root
service := c.Service

// FIXME: Return on failure, do not halt execution.
// Query the systems data using the session token
managers, err := service.Managers()
cmd.CheckError(err)
if err != nil {
system.Error = err
} else {
system.FirmwareVersion = managers[0].FirmwareVersion
}

systems, err := service.Systems()
cmd.CheckError(err)
bios := System{
Manufacturer: systems[0].Manufacturer,
Model: systems[0].Model,
BIOSVersion: systems[0].BIOSVersion,
FirmwareVersion: managers[0].FirmwareVersion,
if err != nil {
system.Error = err
}
return bios
system.Manufacturer = systems[0].Manufacturer
system.Model = systems[0].Model
system.BIOSVersion = systems[0].BIOSVersion
return system
}
1 change: 1 addition & 0 deletions pkg/cmd/cli/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type System struct {
Model string `json:"model"`
BIOSVersion string `json:"biosVersion"`
FirmwareVersion string `json:"firmwareVersion"`
Error error `json:"error"`
}
6 changes: 2 additions & 4 deletions pkg/cmd/gru/gru.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ package gru
import (
"fmt"
"github.com/Cray-HPE/gru/pkg/auth"
"github.com/Cray-HPE/gru/pkg/cmd"
"github.com/Cray-HPE/gru/pkg/cmd/cli/get"
"github.com/Cray-HPE/gru/pkg/cmd/cli/show"
"github.com/Cray-HPE/gru/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
)

var newArgs []string
Expand Down Expand Up @@ -63,9 +63,7 @@ the YAML file may provide these per host.
PersistentPreRun: func(c *cobra.Command, args []string) {
v := viper.GetViper()
bindErr := v.BindPFlags(c.Flags())
if bindErr != nil {
log.Fatalf("Error reading command line: %s", bindErr)
}
cmd.CheckError(bindErr)
cfg := v.GetString("config")
auth.LoadConfig(cfg)
},
Expand Down

0 comments on commit cd3a94c

Please sign in to comment.