-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
80 lines (67 loc) · 1.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
// chassis-control sends a chassis control command to a system, e.g. to power it
// on, or do a hard reset.
import (
"context"
"fmt"
"log"
"time"
"github.com/gebn/bmc"
"github.com/gebn/bmc/pkg/ipmi"
"github.com/alecthomas/kingpin"
)
var (
argBMCAddr = kingpin.Arg("addr", "IP[:port] of the BMC to control.").
Required().
String()
argCommand = kingpin.Arg("command", "The command to send (on/off/cycle/reset/interrupt/softoff).").
Required().
String()
flgUsername = kingpin.Flag("username", "The username to connect as.").
Required().
String()
flgPassword = kingpin.Flag("password", "The password of the user to connect as.").
Required().
String()
cmdControls = map[string]ipmi.ChassisControl{
"off": ipmi.ChassisControlPowerOff,
"on": ipmi.ChassisControlPowerOn,
"cycle": ipmi.ChassisControlPowerCycle,
"reset": ipmi.ChassisControlHardReset,
"interrupt": ipmi.ChassisControlDiagnosticInterrupt,
"softoff": ipmi.ChassisControlSoftPowerOff,
}
)
func lookupCommand(cmd string) (ipmi.ChassisControl, error) {
if ctrl, ok := cmdControls[cmd]; ok {
return ctrl, nil
}
return ipmi.ChassisControlPowerOff, fmt.Errorf("invalid command: %v", cmd)
}
func main() {
kingpin.Parse()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
machine, err := bmc.Dial(ctx, *argBMCAddr)
if err != nil {
log.Fatal(err)
}
defer machine.Close()
log.Printf("connected to %v over IPMI v%v", machine.Address(), machine.Version())
sess, err := machine.NewSession(ctx, &bmc.SessionOpts{
Username: *flgUsername,
Password: []byte(*flgPassword),
MaxPrivilegeLevel: ipmi.PrivilegeLevelOperator,
})
if err != nil {
log.Fatal(err)
}
defer sess.Close(ctx)
cmd, err := lookupCommand(*argCommand)
if err != nil {
log.Fatal(err)
}
if err := sess.ChassisControl(ctx, cmd); err != nil {
log.Fatal(err)
}
}