Skip to content

Commit

Permalink
XXX BMC WIP WORK
Browse files Browse the repository at this point in the history
This is an initial rough POC of how BMC power integration would work.
  • Loading branch information
purpleidea committed Oct 29, 2024
1 parent 1cd4af5 commit 78b0b95
Show file tree
Hide file tree
Showing 8 changed files with 532 additions and 5 deletions.
309 changes: 309 additions & 0 deletions engine/resources/bmc_power.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <[email protected]> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.

package resources

import (
"context"
"fmt"
"net"
"strconv"

"github.com/purpleidea/mgmt/engine"
"github.com/purpleidea/mgmt/engine/traits"

bmclib "github.com/bmc-toolbox/bmclib/v2"
"github.com/bmc-toolbox/bmclib/v2/providers/rpc"
)

func init() {
engine.RegisterResource("bmc:power", func() engine.Res { return &BmcPowerRes{} })
}

const (
// DefaultBmcPowerPort is the default port we try to connect on.
DefaultBmcPowerPort = 443
)

// BmcPowerRes is a no-op resource that does nothing.
type BmcPowerRes struct {
traits.Base // add the base methods without re-implementation

init *engine.Init

// Hostname to connect to.
Hostname string `lang:"hostname" yaml:"hostname"`

// Port to connect to.
Port int `lang:"port" yaml:"port"`

// Username to use to connect.
Username string `lang:"username" yaml:"username"`

// Password to use to connect.
// XXX: Use mgmt magic credentials in the future.
Password string `lang:"password" yaml:"password"`

// Driver to use, such as: "gofish". If you set this, then
// PreferredDriver is ignored.
Driver string `lang:"driver" yaml:"driver"`

// PreferredDriver to use, such as: "gofish".
// TODO: Should we Validate that Driver is not also specified?
PreferredDriver string `lang:"preferred_driver" yaml:"preferred_driver"`

// State of machine power. Can be "on" or "off".
State string `lang:"state" yaml:"state"`
}

// getHostname returns the hostname that we want to connect to. If the Hostname
// field is set, we use that, otherwise we parse from the Name.
func (obj *BmcPowerRes) getHostname() string {
if obj.Hostname != "" {
return obj.Hostname
}

// SplitHostPort splits a network address of the form "host:port",
// "host%zone:port", "[host]:port" or "[host%zone]:port" into host or
// host%zone and port.
host, port, err := net.SplitHostPort(obj.Name())
if err != nil {
return obj.Name() // must be a naked hostname or ip w/o port
}
_ = port

return host
}

// getPort returns the port that we want to connect to. If the Port field is
// set, we use that, otherwise we parse from the Name.
func (obj *BmcPowerRes) getPort() string {
if obj.Port != 0 {
return strconv.Itoa(obj.Port)
}

// SplitHostPort splits a network address of the form "host:port",
// "host%zone:port", "[host]:port" or "[host%zone]:port" into host or
// host%zone and port.
host, port, err := net.SplitHostPort(obj.Name())
if err != nil {
return strconv.Itoa(DefaultBmcPowerPort) // default port
}
_ = host

return port
}

// getUsername returns the username that we want to connect with.
// TODO: If the Username field is not set, should we parse from the Name?
func (obj *BmcPowerRes) getUsername() string {
return obj.Username
}

// Default returns some sensible defaults for this resource.
func (obj *BmcPowerRes) Default() engine.Res {
return &BmcPowerRes{}
}

// Validate if the params passed in are valid data.
func (obj *BmcPowerRes) Validate() error {
// XXX: Force polling until we have real events...
if obj.MetaParams().Poll == 0 {
return fmt.Errorf("events are not yet supported, use polling")
}

if obj.getHostname() == "" {
return fmt.Errorf("need a Hostname")
}
//if obj.getUsername() == "" {
// return fmt.Errorf("need a Username")
//}

return nil
}

// Init runs some startup code for this resource.
func (obj *BmcPowerRes) Init(init *engine.Init) error {
obj.init = init // save for later

return nil
}

// Cleanup is run by the engine to clean up after the resource is done.
func (obj *BmcPowerRes) Cleanup() error {
return nil
}

// Watch is the primary listener for this resource and it outputs events.
func (obj *BmcPowerRes) Watch(ctx context.Context) error {
obj.init.Running() // when started, notify engine that we're running

select {
case <-ctx.Done(): // closed by the engine to signal shutdown
}

//obj.init.Event() // notify engine of an event (this can block)

return nil
}

//func (obj *BmcPowerRes) connect(ctx context.Context error {
// TODO: refactor all that mess into here
//}

// CheckApply method for BmcPower resource. Does nothing, returns happy!
func (obj *BmcPowerRes) CheckApply(ctx context.Context, apply bool) (bool, error) {

u := fmt.Sprintf("http://%s", net.JoinHostPort(obj.getHostname(), obj.getPort()))

opts := []bmclib.Option{
//openbmc.WithPort("42"), // XXX: not an int?
//bmclib.WithLogger(log),
//bmclib.WithPerProviderTimeout(5 * time.Second),
bmclib.WithRPCOpt(rpc.Provider{
//ConsumerURL: "http://127.0.0.1:8800",
ConsumerURL: u,
// Opts are not required.
//Opts: rpc.Opts{
// HMAC: rpc.HMACOpts{
// Secrets: rpc.Secrets{rpc.SHA256: {"superSecret1"}},
// },
// Signature: rpc.SignatureOpts{
// HeaderName: "X-Bespoke-Signature",
// IncludedPayloadHeaders: []string{"X-Bespoke-Timestamp"},
// },
// Request: rpc.RequestOpts{
// TimestampHeader: "X-Bespoke-Timestamp",
// },
//},
}),
}

// XXX: this u doesn't seem to get used anywhere... WAT?
client := bmclib.NewClient(u, obj.getUsername(), obj.Password, opts...)

// client.PreferProvider("gofish")

//if obj.Driver != "" {
// // Only use this one driver.
// client.Registry.Drivers = client.Registry.Using(obj.Driver)

//} else if obj.PreferredDriver != "" {
// // This will modify the order for all subsequent BMC calls.
// client.Registry.Drivers = client.Registry.PreferDriver(obj.PreferredDriver)
//}

if err := client.Open(ctx); err != nil {
return false, err
}
defer client.Close(ctx) // (err error)

if obj.init.Debug || true { // XXX !!!
obj.init.Logf("connected ok")
}

state, err := client.GetPowerState(ctx)
if err != nil {
return false, err
}
obj.init.Logf("get state: %s", state)

if !apply {
return false, nil
}

if obj.State == state {
return true, nil
}

ok, err := client.SetPowerState(ctx, obj.State)
if err != nil {
return false, err
}
if !ok {
// TODO: When is this ever false?
}
obj.init.Logf("set state: %s", obj.State)

return false, nil
}

// Cmp compares two resources and returns an error if they are not equivalent.
func (obj *BmcPowerRes) Cmp(r engine.Res) error {
// we can only compare BmcPowerRes to others of the same resource kind
res, ok := r.(*BmcPowerRes)
if !ok {
return fmt.Errorf("not a %s", obj.Kind())
}

if obj.Hostname != res.Hostname {
return fmt.Errorf("the Hostname differs")
}
if obj.Port != res.Port {
return fmt.Errorf("the Port differs")
}
if obj.Username != res.Username {
return fmt.Errorf("the Username differs")
}
if obj.Password != res.Password {
return fmt.Errorf("the Password differs")
}

if obj.Driver != res.Driver {
return fmt.Errorf("the Driver differs")
}
if obj.PreferredDriver != res.PreferredDriver {
return fmt.Errorf("the PreferredDriver differs")
}
if obj.State != res.State {
return fmt.Errorf("the State differs")
}

return nil
}

// UnmarshalYAML is the custom unmarshal handler for this struct. It is
// primarily useful for setting the defaults.
func (obj *BmcPowerRes) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawRes BmcPowerRes // indirection to avoid infinite recursion

def := obj.Default() // get the default
res, ok := def.(*BmcPowerRes) // put in the right format
if !ok {
return fmt.Errorf("could not convert to BmcPowerRes")
}
raw := rawRes(*res) // convert; the defaults go here

if err := unmarshal(&raw); err != nil {
return err
}

*obj = BmcPowerRes(raw) // restore from indirection with type conversion!
return nil
}
Loading

0 comments on commit 78b0b95

Please sign in to comment.