-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmistifyagent.go
More file actions
309 lines (268 loc) · 7.99 KB
/
mistifyagent.go
File metadata and controls
309 lines (268 loc) · 7.99 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package lochness
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path"
"time"
magent "github.com/mistifyio/mistify-agent"
"github.com/mistifyio/mistify-agent/client"
"github.com/mistifyio/mistify-agent/rpc"
logx "github.com/mistifyio/mistify-logrus-ext"
)
// AgentPort is the default port on which to attempt contacting an agent
const AgentPort int = 8080
type (
// MistifyAgent is an Agent that communicates with a hypervisor agent to perform
// actions relating to guests
MistifyAgent struct {
context *Context
port int
}
// ErrorHTTPCode should be used for errors resulting from an http response
// code not matching the expected code
ErrorHTTPCode struct {
Expected int
Code int
}
)
// Error returns a string error message
func (e ErrorHTTPCode) Error() string {
return fmt.Sprintf("unexpected HTTP Response Code: Expected %d, Received %d", e.Expected, e.Code)
}
// NewMistifyAgent creates a new MistifyAgent instance within the context
func (context *Context) NewMistifyAgent(port int) *MistifyAgent {
if port <= 0 {
port = AgentPort
}
return &MistifyAgent{
context: context,
port: port,
}
}
// generateClientGuest creates a client.Guest object based on the stored guest
// properties. Used during guest creation
func (agent *MistifyAgent) generateClientGuest(g *Guest) (*client.Guest, error) {
if err := g.Validate(); err != nil {
return nil, err
}
flavor, err := agent.context.Flavor(g.FlavorID)
if err != nil {
return nil, err
}
subnet, err := agent.context.Subnet(g.SubnetID)
if err != nil {
return nil, err
}
var vlans []int
if g.VLANGroupID != "" {
vlanGroup, err := agent.context.VLANGroup(g.VLANGroupID)
if err != nil {
return nil, err
}
vlans = vlanGroup.VLANs()
}
// Default to vlan tag 1
if len(vlans) == 0 {
vlans = []int{1}
}
nic := client.Nic{
Name: "eth0",
Network: g.Bridge,
Model: "virtio", // TODO: Check whether this is alwalys the case
Mac: g.MAC.String(),
Address: g.IP.String(),
Netmask: subnet.CIDR.Mask.String(),
Gateway: subnet.Gateway.String(),
VLANs: vlans,
}
disk := client.Disk{
Size: flavor.Disk,
Image: flavor.Image,
Source: flavor.Image,
}
return &client.Guest{
ID: g.ID,
Type: g.Type,
Image: flavor.Image,
Nics: []client.Nic{nic},
Disks: []client.Disk{disk},
Memory: uint(flavor.Memory),
CPU: uint(flavor.CPU),
Metadata: g.Metadata,
}, nil
}
// guestActionURL crafts the guest action url
func (agent *MistifyAgent) guestActionURL(host, guestID, action string) string {
// Create and Get don't have the action name in the URL, so blank it out
// Create doesn't specify a guest id in the URL
if action == "create" || action == "get" {
if action == "create" {
guestID = ""
}
action = ""
}
// Join with appropriate seperators whether action is blank or not
urlPath := path.Join("guests", guestID, action)
return fmt.Sprintf("http://%s:%d/%s", host, agent.port, urlPath)
}
// jobURL crafts the job status url
func (agent *MistifyAgent) jobURL(host, jobID string) string {
return fmt.Sprintf("http://%s:%d/jobs/%s", host, agent.port, jobID)
}
// request is the generic way to hit an agent endpoint with minimal response
// checking. It returns the body string for later parsing and an optional jobID.
// Generally don't use directly; other, more convenient methods will wrap this
func (agent *MistifyAgent) request(url, httpMethod string, expectedCode int, dataObj interface{}) ([]byte, string, error) {
httpClient := &http.Client{
Timeout: 15 * time.Second,
}
// Make the request. POST sends JSON data, GET doesn't
var resp *http.Response
var reqErr error
if httpMethod == "POST" {
dataJSON, err := json.Marshal(dataObj)
if err != nil {
return nil, "", err
}
resp, reqErr = httpClient.Post(url, "application/json", bytes.NewReader(dataJSON))
} else {
resp, reqErr = httpClient.Get(url)
}
if reqErr != nil {
return nil, "", reqErr
}
defer logx.LogReturnedErr(resp.Body.Close, nil, "failed to close response body")
if resp.StatusCode != expectedCode {
return nil, "", ErrorHTTPCode{expectedCode, resp.StatusCode}
}
body, err := ioutil.ReadAll(resp.Body)
return body, resp.Header.Get("X-Guest-Job-ID"), err
}
// getHypervisor loads the hypervisor based on guest id
func (agent *MistifyAgent) getHypervisor(guestID string) (*Hypervisor, error) {
guest, err := agent.context.Guest(guestID)
if err != nil {
return nil, err
}
hypervisor, err := agent.context.Hypervisor(guest.HypervisorID)
if err != nil {
return nil, err
}
return hypervisor, nil
}
// requestGuestAction makes requests for a guest to a hypervisor agent.
func (agent *MistifyAgent) requestGuestAction(guestID, actionName string) (string, error) {
hypervisor, err := agent.getHypervisor(guestID)
if err != nil {
return "", err
}
url := agent.guestActionURL(hypervisor.IP.String(), guestID, actionName)
// Make the request
_, jobID, err := agent.request(url, "POST", http.StatusAccepted, nil)
if err != nil {
return "", err
}
return jobID, nil
}
// CheckJobStatus looks up whether a guest job has been completed or not.
func (agent *MistifyAgent) CheckJobStatus(guestID, jobID string) (bool, error) {
if jobID == "" {
return false, errors.New("missing job id")
}
hypervisor, err := agent.getHypervisor(guestID)
if err != nil {
return false, err
}
url := agent.jobURL(hypervisor.IP.String(), jobID)
body, _, err := agent.request(url, "GET", http.StatusOK, nil)
if err != nil {
return false, err
}
var job magent.Job
if err := json.Unmarshal(body, &job); err != nil {
return false, err
}
switch job.Status {
case magent.Complete:
return true, nil
case magent.Errored:
return true, errors.New(job.Message)
default:
return false, nil
}
}
// GetGuest retrieves information on a guest from an agent
func (agent *MistifyAgent) GetGuest(guestID string) (*client.Guest, error) {
hypervisor, err := agent.getHypervisor(guestID)
if err != nil {
return nil, err
}
url := agent.guestActionURL(hypervisor.IP.String(), guestID, "get")
body, _, err := agent.request(url, "GET", http.StatusOK, nil)
if err != nil {
return nil, err
}
// Parse the response
var g client.Guest
if err := json.Unmarshal(body, &g); err != nil {
return nil, err
}
return &g, err
}
// CreateGuest tries to create a new guest on a hypervisor selected from a list
// of viable candidates
func (agent *MistifyAgent) CreateGuest(guestID string) (string, error) {
guest, err := agent.context.Guest(guestID)
if err != nil {
return "", err
}
hypervisor, err := agent.context.Hypervisor(guest.HypervisorID)
if err != nil {
return "", err
}
g, err := agent.generateClientGuest(guest)
if err != nil {
return "", err
}
url := agent.guestActionURL(hypervisor.IP.String(), guestID, "create")
_, jobID, err := agent.request(url, "POST", http.StatusAccepted, g)
return jobID, err
}
// DeleteGuest deletes a guest from a hypervisor
func (agent *MistifyAgent) DeleteGuest(guestID string) (string, error) {
jobID, err := agent.requestGuestAction(guestID, "delete")
return jobID, err
}
// GuestAction is used to run various actions on a guest under a hypervisor
// Actions: "shutdown", "reboot", "restart", "poweroff", "start", "suspend"
func (agent *MistifyAgent) GuestAction(guestID, actionName string) (string, error) {
jobID, err := agent.requestGuestAction(guestID, actionName)
return jobID, err
}
// FetchImage fetches a disk image that can be used for guest creation
func (agent *MistifyAgent) FetchImage(guestID string) (string, error) {
guest, err := agent.context.Guest(guestID)
if err != nil {
return "", err
}
flavor, err := agent.context.Flavor(guest.FlavorID)
if err != nil {
return "", err
}
hypervisor, err := agent.context.Hypervisor(guest.HypervisorID)
if err != nil {
return "", err
}
host := hypervisor.IP.String()
req := &rpc.ImageRequest{
ID: flavor.Image,
Type: guest.Type,
}
url := fmt.Sprintf("http://%s:%d/images", host, agent.port)
_, jobID, err := agent.request(url, "POST", http.StatusAccepted, req)
return jobID, err
}