-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexecutor.go
More file actions
60 lines (51 loc) · 1.38 KB
/
executor.go
File metadata and controls
60 lines (51 loc) · 1.38 KB
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
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os/exec"
"strings"
"syscall"
)
type Executor interface {
Execute(pwdId int, dutyCycle int) error
}
type IPMIExecutor struct{}
func (i *IPMIExecutor) Execute(pwdId int, dutyCycle int) error {
hexPwdId := fmt.Sprintf("%#02x", pwdId)
hexDutyCycle := fmt.Sprintf("%#02x", dutyCycle)
rawCodes := []string{"raw", "0x2e", "0x44", "0xfd", "0x19", "0x00", hexPwdId, "0x01", hexDutyCycle}
log.Println("IPMI executor: ipmitool", strings.Join(rawCodes, " "))
_, err, exitCode := RunCommand("ipmitool", rawCodes...)
if exitCode != 0 {
return errors.New(err)
}
return nil
}
type FakeExecutor struct {
}
func (f *FakeExecutor) Execute(pwdId int, dutyCycle int) error {
log.Printf("Fake executor: CMD=ipmitool raw 0x2e 0x44 0xfd 0x19 0x00 %#02x 0x01 %#02x", pwdId, dutyCycle)
return nil
}
func RunCommand(name string, args ...string) (stdout string, stderr string, exitCode int) {
var outBuffer, errBuffer bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Stdout = &outBuffer
cmd.Stderr = &errBuffer
err := cmd.Run()
stdout = outBuffer.String()
stderr = errBuffer.String()
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
ws := exitError.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
} else {
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
return
}