Add missing file.
All checks were successful
Build & Test / Main (push) Successful in 28s

This commit is contained in:
Sam Fredrickson 2024-08-25 19:12:23 -07:00
parent 5de2a77625
commit 698984f2cb

49
pkg/economize/lib.go Normal file
View File

@ -0,0 +1,49 @@
package economize
import (
"context"
"github.com/gebn/bmc"
"github.com/gebn/bmc/pkg/ipmi"
)
type Config struct {
Host string
Credentials struct {
Username string
Password string
}
}
func (cfg Config) OpenSession(ctx context.Context) (bmc.Session, error) {
transport, err := bmc.DialV2(cfg.Host)
if err != nil {
return nil, err
}
session, err := transport.NewSession(ctx, &bmc.SessionOpts{
Username: cfg.Credentials.Username,
Password: []byte(cfg.Credentials.Password),
MaxPrivilegeLevel: ipmi.PrivilegeLevelOperator,
})
if err != nil {
return nil, err
}
return session, nil
}
func (cfg Config) ChassisControl(ctx context.Context, control ipmi.ChassisControl) error {
session, err := cfg.OpenSession(ctx)
if err != nil {
return err
}
err = session.ChassisControl(ctx, control)
return err
}
func Up(ctx context.Context, cfg Config) error {
return cfg.ChassisControl(ctx, ipmi.ChassisControlPowerOn)
}
func Down(ctx context.Context, cfg Config) error {
return cfg.ChassisControl(ctx, ipmi.ChassisControlSoftPowerOff)
}