-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachines.go
264 lines (225 loc) · 5.38 KB
/
machines.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
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
package goapi
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/machinebox/graphql"
"github.com/sisatech/goapi/pkg/objects"
)
// MachinesManager provides access to 'Machine' APIs
type MachinesManager struct {
c *Client
environment *Repository
}
// WithEnvironment ..
func (m *MachinesManager) WithEnvironment(r *Repository) *MachinesManager {
out := new(MachinesManager)
*out = *m
out.environment = r
return out
}
// Provision a virtual machine.
func (m *MachinesManager) Provision(args *ProvisionArguments) (*ProvisionOperation, error) {
if args.Injections == nil {
args.Injections = make([]string, 0)
}
for _, a := range args.Injections {
a = fmt.Sprintf(`"%s"`, a)
}
argsStr := make([]string, 0)
argsStr = append(argsStr, fmt.Sprintf(`germ: "%s"`, args.Germ))
if args.InstanceName != "" {
argsStr = append(argsStr, fmt.Sprintf(`name: "%s"`, args.InstanceName))
}
if args.KernelType != "" {
argsStr = append(argsStr, fmt.Sprintf(`kernelType: "%s"`, args.KernelType))
}
if args.Platform != "" {
argsStr = append(argsStr, fmt.Sprintf(`platform: "%s"`, args.Platform))
}
argsStr = append(argsStr, fmt.Sprintf(`start: %v`, args.PoweredOn))
if len(args.Injections) != 0 {
argsStr = append(argsStr, fmt.Sprintf("injections: [%s]", strings.Join(args.Injections, ", ")))
}
req := m.environment.newRequest(fmt.Sprintf(`
mutation {
provision (%s) {
uri
job {
id
}
}
}
`, strings.Join(argsStr, ", ")))
type responseContainer struct {
Provision objects.CompoundProvisionResponse `json:"provision"`
}
resp := new(responseContainer)
err := m.c.graphql.Run(m.c.ctx, req, &resp)
if err != nil {
return nil, err
}
return &ProvisionOperation{
c: m.c,
host: m.environment.host,
jobID: resp.Provision.Job.ID,
uri: resp.Provision.URI,
}, nil
}
// Inject ..
func (p *ProvisionOperation) Inject(key string, itype InjectionType, value io.Reader, headers http.Header) error {
url := fmt.Sprintf("%s/api/provision/%s", p.host, p.uri)
req, err := http.NewRequest(http.MethodPost, url, value)
if err != nil {
return err
}
defer req.Body.Close()
req.Header.Add("Injection-ID", key)
req.Header.Add("Injection-Type", string(itype))
// Allow users to pass as many custom headers as desired, if any at all.
for k, h := range headers {
if len(h) == 1 {
req.Header.Set(k, h[0])
} else {
for _, x := range h {
req.Header.Add(k, x)
}
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server returned non-200 status code: %v - %s", resp.StatusCode, resp.Status)
}
return nil
}
// List all virtual machines.
func (m *MachinesManager) ListMachines(cursor *Cursor) (*VirtualMachineList, error) {
var vd, v string
if cursor != nil {
vd, v = cursor.Strings()
}
req := m.environment.newRequest(fmt.Sprintf(`
query%s {
listMachines%s {
edges {
node {
id
name
}
cursor
}
pageInfo {
endCursor
startCursor
hasNextPage
hasPreviousPage
}
}
}
`, vd, v))
if cursor != nil {
cursor.AddToRequest(req)
}
type responseContainer struct {
ListMachines objects.VMsConnection `json:"listMachines"`
}
resp := new(responseContainer)
err := m.c.graphql.Run(m.c.ctx, req, &resp)
if err != nil {
return nil, err
}
out := &VirtualMachineList{
PageInfo: resp.ListMachines.PageInfo,
Items: make([]VirtualMachineListItem, 0),
}
for _, v := range resp.ListMachines.Edges {
out.Items = append(out.Items, VirtualMachineListItem{
Cursor: v.Cursor,
VirtualMachine: VirtualMachine{
id: v.Node.ID,
name: v.Node.Name,
},
})
}
return out, nil
}
// Get a virtual machine.
func (m *MachinesManager) Get(id string) (*VirtualMachine, error) {
req := m.environment.newRequest(fmt.Sprintf(`
query {
vm(id: "%s") {
id
name
}
}
`, id))
type responseContainer struct {
VM objects.VM `json:"vm"`
}
resp := new(responseContainer)
err := m.c.graphql.Run(m.c.ctx, req, &resp)
if err != nil {
return nil, err
}
return &VirtualMachine{
mgr: m,
id: resp.VM.ID,
name: resp.VM.Name,
}, nil
}
// WaitUntilFinished ..
func (p *ProvisionOperation) WaitUntilFinished() error {
req := graphql.NewRequest(fmt.Sprintf(`
query {
job(id: "%s") {
logFilePath
progress {
status
finished
error
}
}
}
`, p.jobID))
type responseContainer struct {
Job objects.Job `json:"job"`
}
resp := new(responseContainer)
for {
err := p.c.graphql.Run(context.TODO(), req, &resp)
if err != nil {
return err
}
if resp.Job.Progress.Finished != 0 {
if resp.Job.Progress.Error != "" {
return fmt.Errorf("job error: %s", resp.Job.Progress.Error)
}
return nil
}
time.Sleep(time.Second * 1)
}
return nil
}
// ProvisionOperation ..
type ProvisionOperation struct {
c *Client
jobID string
uri string
host string
}
// ProvisionArguments ..
type ProvisionArguments struct {
Germ string
InstanceName string
PoweredOn bool
Platform string
KernelType KernelType
Injections []string
}