From 698984f2cb12bbf95df772a487a73c882caea0e5 Mon Sep 17 00:00:00 2001 From: Sam Fredrickson Date: Sun, 25 Aug 2024 19:12:23 -0700 Subject: [PATCH] Add missing file. --- pkg/economize/lib.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pkg/economize/lib.go diff --git a/pkg/economize/lib.go b/pkg/economize/lib.go new file mode 100644 index 0000000..9595ab5 --- /dev/null +++ b/pkg/economize/lib.go @@ -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) +}