From 36323757c09857ef61f69b29dd49a58fb66fcc46 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Thu, 21 Nov 2024 10:38:57 +0100 Subject: [PATCH] LED works for redfish and dell --- cli/led.go | 12 ++++++------ cli/main.go | 2 +- hal.go | 3 +++ internal/redfish/redfish.go | 23 ++++++++++++++--------- internal/vendors/dell/dell.go | 4 ++++ internal/vendors/lenovo/lenovo.go | 7 +++++++ internal/vendors/supermicro/supermicro.go | 8 ++++++++ internal/vendors/vagrant/vagrant.go | 8 +++++++- 8 files changed, 50 insertions(+), 17 deletions(-) diff --git a/cli/led.go b/cli/led.go index 6e89642..32e2893 100644 --- a/cli/led.go +++ b/cli/led.go @@ -14,11 +14,11 @@ var ledCmd = &cli.Command{ if err != nil { return err } - board, err := c.Board() + ledstate, err := c.GetIdentifyLED() if err != nil { return err } - log.Infow("lead", "state", board.IndicatorLED) + log.Infow("led", "state", ledstate.String()) return nil }, Subcommands: []*cli.Command{ @@ -36,11 +36,11 @@ var ledCmd = &cli.Command{ return err } log.Infow("led state set to on") - board, err := c.Board() + ledstate, err := c.GetIdentifyLED() if err != nil { return err } - log.Infow("lead", "state", board.IndicatorLED) + log.Infow("led", "state", ledstate.String()) return nil }, }, @@ -58,11 +58,11 @@ var ledCmd = &cli.Command{ return err } log.Infow("led state set to off") - board, err := c.Board() + ledstate, err := c.GetIdentifyLED() if err != nil { return err } - log.Infow("lead", "state", board.IndicatorLED) + log.Infow("led", "state", ledstate.String()) return nil }, }, diff --git a/cli/main.go b/cli/main.go index bff5eda..ba26e65 100644 --- a/cli/main.go +++ b/cli/main.go @@ -79,7 +79,7 @@ func main() { log.Infow("go hal cli", "host", host, "port", port, "password", password, "bandtype", bandtype) if err := app.Run(os.Args); err != nil { - panic(err) + log.Errorw("execution failed", "error", err) } if outBandBMCConnection != nil { diff --git a/hal.go b/hal.go index 1cc27d9..fbdb782 100644 --- a/hal.go +++ b/hal.go @@ -113,6 +113,7 @@ type Hal interface { // PowerCycle cycle the power state of the server PowerCycle() error + GetIdentifyLED() (IdentifyLEDState, error) // IdentifyLEDState get the identify LED state IdentifyLEDState(IdentifyLEDState) error // IdentifyLEDOn set the identify LED to on @@ -144,6 +145,7 @@ type InBand interface { // PowerCycle cycle the power state of the server PowerCycle() error + GetIdentifyLED() (IdentifyLEDState, error) // IdentifyLEDState get the identify LED state IdentifyLEDState(IdentifyLEDState) error // IdentifyLEDOn set the identify LED to on @@ -194,6 +196,7 @@ type OutBand interface { // PowerCycle cycle the power state of the server PowerCycle() error + GetIdentifyLED() (IdentifyLEDState, error) // IdentifyLEDState get the identify LED state IdentifyLEDState(IdentifyLEDState) error // IdentifyLEDOn set the identify LED to on diff --git a/internal/redfish/redfish.go b/internal/redfish/redfish.go index ea5e0d9..227c695 100644 --- a/internal/redfish/redfish.go +++ b/internal/redfish/redfish.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" "net/http" "sort" "strings" @@ -412,11 +413,10 @@ func (c *APIClient) IdentifyLEDState(state hal.IdentifyLEDState) error { case hal.IdentifyLEDStateUnknown: return fmt.Errorf("unknown LED state:%s", state) } - resp, err := c.Gofish.Patch("/redfish/v1/Chassis/System.Embedded.1", payload) + _, err := c.Gofish.Patch("/redfish/v1/Chassis/System.Embedded.1", payload) if err != nil { c.log.Errorw("unable to set led", "error", err) } - c.log.Infow("set led", "response", resp.Body) return nil } @@ -428,23 +428,28 @@ func (c *APIClient) GetIdentifyLED() (hal.IdentifyLEDState, error) { c.log.Errorw("unable to get led", "error", err) return hal.IdentifyLEDStateUnknown, err } - c.log.Infow("set led", "response", resp.Body) - var ledstate map[string]string - err = json.NewDecoder(resp.Body).Decode(ledstate) + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return hal.IdentifyLEDStateUnknown, err + } + + var parsedBody map[string]any + err = json.Unmarshal(body, &parsedBody) if err != nil { c.log.Errorw("unable to parse led state", "error", err) return hal.IdentifyLEDStateUnknown, err } - state, ok := ledstate["LocationIndicatorActive"] + state, ok := parsedBody["LocationIndicatorActive"] if !ok { return hal.IdentifyLEDStateUnknown, fmt.Errorf("ledstate does not contain a LocationIndicatorActive key") } - switch state { - case "true": + switch state.(bool) { + case true: return hal.IdentifyLEDStateOn, nil - case "false": + case false: return hal.IdentifyLEDStateOff, nil } return hal.IdentifyLEDStateUnknown, fmt.Errorf("unknown state:%s", state) diff --git a/internal/vendors/dell/dell.go b/internal/vendors/dell/dell.go index 6c446f6..877d9f4 100644 --- a/internal/vendors/dell/dell.go +++ b/internal/vendors/dell/dell.go @@ -86,6 +86,10 @@ func (o *outBand) IPMIConnection() (ip string, port int, user string, password s panic("unimplemented") } +func (o *outBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return o.Redfish.GetIdentifyLED() +} + // IdentifyLEDOff implements hal.OutBand. func (o *outBand) IdentifyLEDOff() error { return o.Redfish.IdentifyLEDState(hal.IdentifyLEDStateOff) diff --git a/internal/vendors/lenovo/lenovo.go b/internal/vendors/lenovo/lenovo.go index a6be1c1..50cdc9f 100644 --- a/internal/vendors/lenovo/lenovo.go +++ b/internal/vendors/lenovo/lenovo.go @@ -80,6 +80,9 @@ func (ib *inBand) PowerCycle() error { func (ib *inBand) PowerReset() error { return ib.IpmiTool.SetChassisControl(ipmi.ChassisControlHardReset) } +func (o *inBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return hal.IdentifyLEDStateUnknown, nil +} func (ib *inBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return ib.IpmiTool.SetChassisIdentifyLEDState(state) @@ -207,6 +210,10 @@ func (ob *outBand) PowerCycle() error { return ob.Redfish.PowerReset() // PowerCycle is not supported } +func (o *outBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return o.Redfish.GetIdentifyLED() +} + func (ob *outBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return errorNotImplemented //TODO https://github.com/metal-stack/go-hal/issues/11 } diff --git a/internal/vendors/supermicro/supermicro.go b/internal/vendors/supermicro/supermicro.go index 178c7e5..f72440a 100644 --- a/internal/vendors/supermicro/supermicro.go +++ b/internal/vendors/supermicro/supermicro.go @@ -98,6 +98,10 @@ func (ib *inBand) PowerReset() error { return ib.IpmiTool.SetChassisControl(ipmi.ChassisControlHardReset) } +func (o *inBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return hal.IdentifyLEDStateUnknown, nil +} + func (ib *inBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return ib.IpmiTool.SetChassisIdentifyLEDState(state) } @@ -250,6 +254,10 @@ func (ob *outBand) PowerCycle() error { }) } +func (o *outBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return o.Redfish.GetIdentifyLED() +} + func (ob *outBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return ob.Goipmi(func(client *ipmi.Client) error { return client.SetChassisIdentifyLEDState(state) diff --git a/internal/vendors/vagrant/vagrant.go b/internal/vendors/vagrant/vagrant.go index edf93fd..a1a03a2 100644 --- a/internal/vendors/vagrant/vagrant.go +++ b/internal/vendors/vagrant/vagrant.go @@ -79,7 +79,9 @@ func (ib *inBand) PowerCycle() error { func (ib *inBand) PowerReset() error { return ib.IpmiTool.SetChassisControl(ipmi.ChassisControlHardReset) } - +func (o *inBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return hal.IdentifyLEDStateUnknown, nil +} func (ib *inBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return nil } @@ -187,6 +189,10 @@ func (ob *outBand) PowerCycle() error { }) } +func (o *outBand) GetIdentifyLED() (hal.IdentifyLEDState, error) { + return hal.IdentifyLEDStateUnknown, nil +} + func (ob *outBand) IdentifyLEDState(state hal.IdentifyLEDState) error { return nil }