forked from bmc-toolbox/bmclib
/
setupConnections.go
144 lines (116 loc) · 2.99 KB
/
setupConnections.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package idrac8
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/bmc-toolbox/bmclib/errors"
"github.com/bmc-toolbox/bmclib/internal/httpclient"
"github.com/bmc-toolbox/bmclib/internal/sshclient"
"github.com/bmc-toolbox/bmclib/providers/dell"
multierror "github.com/hashicorp/go-multierror"
// this make possible to setup logging and properties at any stage
_ "github.com/bmc-toolbox/bmclib/logging"
log "github.com/sirupsen/logrus"
)
func (i *IDrac8) httpLogin() (err error) {
if i.httpClient != nil {
return
}
httpClient, err := httpclient.Build()
if err != nil {
return err
}
log.WithFields(log.Fields{"step": "bmc connection", "vendor": dell.VendorID, "ip": i.ip}).Debug("connecting to bmc")
data := fmt.Sprintf("user=%s&password=%s", i.username, i.password)
url := fmt.Sprintf("https://%s/data/login", i.ip)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(data))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode == 404 {
return errors.ErrPageNotFound
}
payload, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
iDracAuth := &dell.IDracAuth{}
err = xml.Unmarshal(payload, iDracAuth)
if err != nil {
return err
}
stTemp := strings.Split(iDracAuth.ForwardURL, ",")
if len(stTemp) != 2 {
return errors.ErrLoginFailed
}
i.httpClient = httpClient
i.st1 = strings.TrimLeft(stTemp[0], "index.html?ST1=")
i.st2 = strings.TrimLeft(stTemp[1], "ST2=")
return err
}
// loadHwData load the full hardware information from the iDrac
func (i *IDrac8) loadHwData() (err error) {
err = i.httpLogin()
if err != nil {
return err
}
if i.iDracInventory != nil {
return err
}
url := "sysmgmt/2012/server/inventory/hardware"
payload, err := i.get(url, nil)
if err != nil {
return err
}
iDracInventory := &dell.IDracInventory{}
err = xml.Unmarshal(payload, iDracInventory)
if err != nil {
return err
}
if iDracInventory == nil || iDracInventory.Component == nil {
return errors.ErrUnableToReadData
}
i.iDracInventory = iDracInventory
return err
}
// sshLogin initiates the connection to a bmc device
func (i *IDrac8) sshLogin() (err error) {
if i.sshClient != nil {
return
}
log.WithFields(log.Fields{"step": "bmc connection", "vendor": dell.VendorID, "ip": i.ip}).Debug("connecting to bmc")
i.sshClient, err = sshclient.New(i.ip, i.username, i.password)
if err != nil {
return err
}
return err
}
// Close closes the connection properly
func (i *IDrac8) Close() (err error) {
if i.httpClient != nil {
resp, e := i.httpClient.Get(fmt.Sprintf("https://%s/data/logout", i.ip))
if e != nil {
err = multierror.Append(e, err)
} else {
defer resp.Body.Close()
defer io.Copy(ioutil.Discard, resp.Body)
}
}
if i.sshClient != nil {
e := i.sshClient.Close()
if e != nil {
err = multierror.Append(e, err)
}
}
return err
}