Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pure go ipmi provider #338

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

bmclib v2 is a library to abstract interacting with baseboard management controllers.

## Supported BMC interfaces.
## Supported BMC interfaces

- [Redfish](https://github.com/bmc-toolbox/bmclib/tree/main/providers/redfish)
- [IPMItool](https://github.com/bmc-toolbox/bmclib/tree/main/providers/ipmitool)
- [Intel AMT](https://github.com/bmc-toolbox/bmclib/tree/main/providers/intelamt)
- [Asrockrack](https://github.com/bmc-toolbox/bmclib/tree/main/providers/asrockrack)
See each provider for details on the supported features.

- [Redfish](https://github.com/bmc-toolbox/bmclib/tree/main/providers/redfish)
- [IPMItool](https://github.com/bmc-toolbox/bmclib/tree/main/providers/ipmitool)
- [Intel AMT](https://github.com/bmc-toolbox/bmclib/tree/main/providers/intelamt)
- [Asrockrack](https://github.com/bmc-toolbox/bmclib/tree/main/providers/asrockrack)
- [Pure Go IPMI](https://github.com/bmc-toolbox/bmclib/tree/main/providers/goipmi)
- [Supermicro](https://github.com/bmc-toolbox/bmclib/tree/main/providers/supermicro)

## Installation

Expand Down
16 changes: 16 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bmc-toolbox/bmclib/v2/internal/httpclient"
"github.com/bmc-toolbox/bmclib/v2/providers/asrockrack"
"github.com/bmc-toolbox/bmclib/v2/providers/dell"
"github.com/bmc-toolbox/bmclib/v2/providers/goipmi"
"github.com/bmc-toolbox/bmclib/v2/providers/intelamt"
"github.com/bmc-toolbox/bmclib/v2/providers/ipmitool"
"github.com/bmc-toolbox/bmclib/v2/providers/redfish"
Expand Down Expand Up @@ -58,6 +59,7 @@ type providerConfig struct {
intelamt intelamt.Config
dell dell.Config
supermicro supermicro.Config
goipmi goipmi.Config
}

// NewClient returns a new Client struct
Expand Down Expand Up @@ -90,6 +92,10 @@ func NewClient(host, user, pass string, opts ...Option) *Client {
supermicro: supermicro.Config{
Port: "443",
},
goipmi: goipmi.Config{
CipherSuite: 3,
Port: 623,
},
},
}

Expand Down Expand Up @@ -187,6 +193,16 @@ func (c *Client) registerProviders() {
smcHttpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
driverSupermicro := supermicro.NewClient(c.Auth.Host, c.Auth.User, c.Auth.Pass, c.Logger, supermicro.WithHttpClient(&smcHttpClient), supermicro.WithPort(c.providerConfig.supermicro.Port))
c.Registry.Register(supermicro.ProviderName, supermicro.ProviderProtocol, supermicro.Features, nil, driverSupermicro)

// register goipmi provider
goipmiOpts := []goipmi.Option{goipmi.WithCipherSuite(c.providerConfig.goipmi.CipherSuite)}
driverGoipmi, err := goipmi.New(c.Auth.Host, c.providerConfig.goipmi.Port, c.Auth.User, c.Auth.Pass, goipmiOpts...)
if err != nil {
c.Logger.Info("goipmi provider not available", "error", err.Error())
} else {
driverGoipmi.Log = c.Logger
c.Registry.Register(goipmi.ProviderName, goipmi.ProviderProtocol, goipmi.Features, nil, driverGoipmi)
}
}

// GetMetadata returns the metadata that is populated after each BMC function/method call
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d
github.com/bombsimon/logrusr/v2 v2.0.1
github.com/bougou/go-ipmi v0.4.1-0.20230702095700-b7dd37559417
github.com/go-logr/logr v1.2.4
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-multierror v1.1.1
Expand All @@ -25,8 +26,14 @@ require (
github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230 // indirect
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
golang.org/x/sys v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 17 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d h1:cQ30Wa8mhLzK
github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c=
github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM=
github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio=
github.com/bougou/go-ipmi v0.4.0 h1:lt25FldaHHmvjJFp62dEH+cc3u0VpzLAOcALrLl6HYc=
github.com/bougou/go-ipmi v0.4.0/go.mod h1:+MKvz/6aFcJNmoQm27SLj41BJ5vdYxiQiQxdLZXQ78o=
github.com/bougou/go-ipmi v0.4.1-0.20230702095700-b7dd37559417 h1:sOpeHNFf3ObqFzqFyl1XZnqhYrPG8AoL1OPB0bJp14E=
github.com/bougou/go-ipmi v0.4.1-0.20230702095700-b7dd37559417/go.mod h1:+MKvz/6aFcJNmoQm27SLj41BJ5vdYxiQiQxdLZXQ78o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -15,6 +19,8 @@ github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand All @@ -25,16 +31,24 @@ github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef/go.mod h1:Fgmi
github.com/jacobweinstock/registrar v0.4.7 h1:s4dOExccgD+Pc7rJC+f3Mc3D+NXHcXUaOibtcEsPxOc=
github.com/jacobweinstock/registrar v0.4.7/go.mod h1:PWmkdGFG5/ZdCqgMo7pvB3pXABOLHc5l8oQ0sgmBNDU=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
Expand Down Expand Up @@ -63,8 +77,10 @@ golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
6 changes: 6 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ func WithDellRedfishUseBasicAuth(useBasicAuth bool) Option {
args.providerConfig.dell.UseBasicAuth = useBasicAuth
}
}

func WithGoipmiCipherSuite(cipherSuite int) Option {
return func(args *Client) {
args.providerConfig.goipmi.CipherSuite = cipherSuite
}
}
230 changes: 230 additions & 0 deletions providers/goipmi/goipmi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package goipmi

import (
"context"
"fmt"
"strings"
"time"

"github.com/bmc-toolbox/bmclib/v2/providers"
"github.com/bougou/go-ipmi"
"github.com/go-logr/logr"
"github.com/jacobweinstock/registrar"
)

const (
// ProviderName for the provider pure go ipmi (go-ipmi) implementation.
ProviderName = "goipmi"
// ProviderProtocol for the provider pure go ipmi (go-ipmi) implementation.
ProviderProtocol = "ipmi"
)

var (
// Features implemented by goipmi.
Features = registrar.Features{
providers.FeaturePowerSet,
providers.FeaturePowerState,
providers.FeatureBootDeviceSet,
providers.FeatureUserRead,
}
)

// Config for goipmi provider.
type Config struct {
CipherSuite int
Log logr.Logger
Port int

client *ipmi.Client
}

// Option for setting optional Client values.
type Option func(*Config)

func New(host string, port int, user, pass string, opts ...Option) (*Config, error) {
cl, err := ipmi.NewClient(host, port, user, pass)
if err != nil {
return nil, err
}
c := &Config{
CipherSuite: 3,
Log: logr.Discard(),
client: cl,
}
for _, opt := range opts {
opt(c)
}
c.client.WithInterface(ipmi.InterfaceLanplus)
c.client.WithCipherSuiteID(toCipherSuiteID(c.CipherSuite))

return c, nil
}

func WithCipherSuite(cipherSuite int) Option {
return func(c *Config) {
c.CipherSuite = cipherSuite
}
}

func (c *Config) Name() string {
return ProviderName
}

func (c *Config) Open(ctx context.Context) error {
c.client.WithTimeout(getTimeout(ctx))
return c.client.Connect()
}

func getTimeout(ctx context.Context) time.Duration {
deadline, ok := ctx.Deadline()
if !ok {
return 30 * time.Second
}

return time.Until(deadline)
}

func (c *Config) Close(_ context.Context) error {
return c.client.Close()
}

func (c *Config) PowerStateGet(_ context.Context) (string, error) {
r, err := c.client.GetChassisStatus()
if err != nil {
return "", err
}
state := "off"
if r.PowerIsOn {
state = "on"
}

return state, nil
}

func (c *Config) PowerSet(_ context.Context, state string) (bool, error) {
var action ipmi.ChassisControl
switch strings.ToLower(state) {
case "on":
action = ipmi.ChassisControlPowerUp
case "off":
action = ipmi.ChassisControlPowerDown
case "soft":
action = ipmi.ChassisControlSoftShutdown
case "reset":
action = ipmi.ChassisControlHardwareRest
case "cycle":
action = ipmi.ChassisControlPowerCycle
default:
return false, fmt.Errorf("unknown or unimplemented state request: %v", state)
}

// ipmi.ChassisControlResponse is an empty struct.
// No methods return any actual response. So we ignore it.
_, err := c.client.ChassisControl(action)
if err != nil {
return false, err
}

return true, nil
}

func (c *Config) BootDeviceSet(_ context.Context, bootDevice string, setPersistent, efiBoot bool) (ok bool, err error) {
d := ipmi.BootDeviceSelectorNoOverride
switch strings.ToLower(bootDevice) {
case "pxe":
d = ipmi.BootDeviceSelectorForcePXE
case "disk":
d = ipmi.BootDeviceSelectorForceHardDrive
case "safe":
d = ipmi.BootDeviceSelectorForceHardDriveSafe
case "diag":
d = ipmi.BootDeviceSelectorForceDiagnosticPartition
case "cdrom":
d = ipmi.BootDeviceSelectorForceCDROM
case "bios":
d = ipmi.BootDeviceSelectorForceBIOSSetup
case "floppy":
d = ipmi.BootDeviceSelectorForceFloppy
}
bt := ipmi.BIOSBootTypeLegacy
if efiBoot {
bt = ipmi.BIOSBootTypeEFI
}

if err := c.client.SetBootDevice(d, bt, setPersistent); err != nil {
return false, err
}

return true, nil
}

func (c *Config) UserRead(_ context.Context) (users []map[string]string, err error) {
u, err := c.client.ListUser(0)
if err != nil {
return nil, err
}

for _, v := range u {
if v.Name == "" {
continue
}
users = append(users, map[string]string{
"id": fmt.Sprintf("%v", v.ID),
"name": v.Name,
"callin": fmt.Sprintf("%v", v.Callin),
"linkAuth": fmt.Sprintf("%v", v.LinkAuthEnabled),
"ipmiMsg": fmt.Sprintf("%v", v.IPMIMessagingEnabled),
"channelPrivLimit": fmt.Sprintf("%v", v.MaxPrivLevel),
})
}

return users, nil
}

func toCipherSuiteID(c int) ipmi.CipherSuiteID {
switch c {
case 0:
return ipmi.CipherSuiteID0
case 1:
return ipmi.CipherSuiteID1
case 2:
return ipmi.CipherSuiteID2
case 3:
return ipmi.CipherSuiteID3
case 4:
return ipmi.CipherSuiteID4
case 5:
return ipmi.CipherSuiteID5
case 6:
return ipmi.CipherSuiteID6
case 7:
return ipmi.CipherSuiteID7
case 8:
return ipmi.CipherSuiteID8
case 9:
return ipmi.CipherSuiteID9
case 10:
return ipmi.CipherSuiteID10
case 11:
return ipmi.CipherSuiteID11
case 12:
return ipmi.CipherSuiteID12
case 13:
return ipmi.CipherSuiteID13
case 14:
return ipmi.CipherSuiteID14
case 15:
return ipmi.CipherSuiteID15
case 16:
return ipmi.CipherSuiteID16
case 17:
return ipmi.CipherSuiteID17
case 18:
return ipmi.CipherSuiteID18
case 19:
return ipmi.CipherSuiteID19
default:
return ipmi.CipherSuiteID3
}

}