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) +}