This commit is contained in:
parent
5de2a77625
commit
698984f2cb
49
pkg/economize/lib.go
Normal file
49
pkg/economize/lib.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user