This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipmi.go
115 lines (108 loc) · 3.46 KB
/
ipmi.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"os/exec"
"strconv"
"github.com/digitalrebar/logger"
"github.com/digitalrebar/provision/v4/models"
)
type ipmi struct {
username, password, address string
port int
}
func (i *ipmi) Name() string {
return "ipmi"
}
func (i *ipmi) run(cmd ...string) ([]byte, error) {
args := []string{"-H", i.address, "-U", i.username, "-P", i.password, "-I", "lanplus"}
if i.port != 0 {
args = append(args, "-p", strconv.Itoa(i.port))
}
args = append(args, cmd...)
return exec.Command("ipmitool", args...).CombinedOutput()
}
func (i *ipmi) Probe(l logger.Logger, address string, port int, username, password string) bool {
i.address = address
i.port = port
i.username = username
i.password = password
res, err := exec.Command("ipmitool", "-V").CombinedOutput()
if len(res) > 0 && err == nil {
return true
}
l.Warnf("Missing ipmitool")
return false
}
func (i *ipmi) Action(l logger.Logger, ma *models.Action) (supported bool, res interface{}, err *models.Error) {
bootOptionsStick := []string{"chassis", "bootparam", "set", "bootflag", "none", "options=no-power,no-reset,no-watchdog,no-timeout"}
cmds := [][]string{}
switch ma.Command {
case "powerstatus":
cmds = append(cmds, []string{"chassis", "power", "status"})
case "poweron":
cmds = append(cmds, []string{"chassis", "power", "on"})
case "poweroff":
cmds = append(cmds, []string{"chassis", "power", "off"})
case "powercycle":
cmds = append(cmds, []string{"chassis", "power", "cycle"})
case "nextbootpxe":
bootCmd := []string{"chassis", "bootdev", "pxe"}
if ma.Params["detected-bios-mode"].(string) == "uefi" {
bootCmd = append(bootCmd, "options=efiboot")
}
cmds = append(cmds, bootOptionsStick, bootCmd)
case "nextbootdisk":
bootCmd := []string{"chassis", "bootdev", "disk"}
if ma.Params["detected-bios-mode"].(string) == "uefi" {
bootCmd = append(bootCmd, "options=efiboot")
}
cmds = append(cmds, bootOptionsStick, bootCmd)
case "forcebootpxe":
bootCmd := []string{"chassis", "bootdev", "pxe"}
if ma.Params["detected-bios-mode"].(string) == "uefi" {
// older versions of ipmitool have issues setting efoboot and persistent at the same time.
// use raw commands to do it instead.
bootCmd = []string{"raw", "0x00", "0x08", "0x05", "0xe0", "0x04", "0x00", "0x00", "0x00"}
} else {
bootCmd = append(bootCmd, "options=persistent")
}
cmds = append(cmds, bootOptionsStick, bootCmd)
case "forcebootdisk":
bootCmd := []string{"chassis", "bootdev", "disk"}
if ma.Params["detected-bios-mode"].(string) == "uefi" {
// older versions of ipmitool have issues setting efoboot and persistent at the same time.
// use raw commands to do it instead.
bootCmd = []string{"raw", "0x00", "0x08", "0x05", "0xe0", "0x08", "0x00", "0x00", "0x00"}
} else {
bootCmd = append(bootCmd, "options=persistent")
}
cmds = append(cmds, bootOptionsStick, bootCmd)
case "identify":
blink := 15
if val, ok := ma.Params["ipmi/identify-duration"]; ok {
blink = val.(int)
}
if blink > 0 {
cmds = append(cmds, []string{"chassis", "identify", strconv.Itoa(blink)})
} else {
cmds = append(cmds, []string{"chassis", "identify"})
}
default:
return
}
supported = true
for _, cmd := range cmds {
out, cmdErr := i.run(cmd...)
if cmdErr != nil {
err = &models.Error{
Code: 404,
Model: "plugin",
Key: "ipmi",
}
err.Errorf("ipmi error: %v", cmdErr)
err.Errorf("ipmi out: %s", string(out))
return
}
res = string(out)
}
return
}