-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipmi.go
More file actions
70 lines (63 loc) · 1.67 KB
/
ipmi.go
File metadata and controls
70 lines (63 loc) · 1.67 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
61
62
63
64
65
66
67
68
69
70
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"time"
"github.com/pkg/errors"
)
func resetServer(s *Settings) error {
if err := loginIpmi(s); err != nil {
err = errors.Wrap(err, "logging in to IPMI")
return err
}
log.Println("[INFO] resetting server via IPMI")
payload := bytes.NewBuffer([]byte(s.IpmiResetPayload))
req, err := http.NewRequest("POST", s.IpmiResetUrl, payload)
if err != nil {
err = errors.Wrap(err, "creating request to reset server over IPMI")
return err
}
req.AddCookie(&http.Cookie{
Name: "SID",
Value: s.SidCookie,
})
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = errors.Wrap(err, "making http request to reset server over IPMI")
return err
}
defer resp.Body.Close()
header := resp.Header.Get("content-type")
if header != "application/xml" {
return errors.Errorf("reset request returned incorrect content-type: %s", header)
}
time.Sleep(time.Minute * 10)
return nil
}
func loginIpmi(s *Settings) error {
log.Println("[INFO] logging in to IPMI")
payload := bytes.NewBuffer([]byte(fmt.Sprintf(s.IpmiLoginPayload, s.IpmiUser, s.IpmiPassword)))
req, err := http.NewRequest("POST", s.IpmiLoginUrl, payload)
if err != nil {
err = errors.Wrap(err, "creating request to login to IPMI")
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = errors.Wrap(err, "making http request to reset server over IPMI")
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return errors.Errorf("login returned error %s", resp.Status)
}
for _, cookie := range resp.Cookies() {
if cookie.Name != "SID" || cookie.Value == "" {
continue
}
s.SidCookie = cookie.Value
}
return nil
}