This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
gcp_client.go
393 lines (334 loc) · 11.4 KB
/
gcp_client.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package gcp
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/pivotal-cf/cliaas/iaas"
errwrap "github.com/pkg/errors"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
)
type GoogleComputeClient interface {
List(project string, zone string) (*compute.InstanceList, error)
DiskList(project string, zone string) (*compute.DiskList, error)
Delete(project string, zone string, instanceName string) (*compute.Operation, error)
Insert(project string, zone string, instance *compute.Instance) (*compute.Operation, error)
ImageInsert(project string, image *compute.Image, timeout time.Duration) (*compute.Operation, error)
Stop(project string, zone string, instanceName string) (*compute.Operation, error)
}
type ClientAPI interface {
CreateVM(instance compute.Instance) error
DeleteVM(instanceName string) error
GetVMInfo(filter Filter) (*compute.Instance, error)
Disk(filter Filter) (*compute.Disk, error)
StopVM(instanceName string) error
CreateImage(tarball string, diskSizeGB int64) (string, error)
WaitForStatus(vmName string, desiredStatus string) error
}
type Client struct {
projectName string
zoneName string
googleClient GoogleComputeClient
timeout time.Duration
}
//NewDefaultGoogleComputeClient -- builds a gcp client which connects to your gcp using `GOOGLE_APPLICATION_CREDENTIALS`
func NewDefaultGoogleComputeClient(credpath string) (GoogleComputeClient, error) {
err := os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", credpath)
if err != nil {
return nil, errwrap.Wrap(err, "couldnt set credentials ENV Var")
}
ctx := context.Background()
hc, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
if err != nil {
return nil, errwrap.Wrap(err, "we have a DefaultClient error")
}
c, err := compute.New(hc)
if err != nil {
return nil, errwrap.Wrap(err, "we have a compute.New error")
}
return &googleComputeClientWrapper{
instanceService: c.Instances,
disksService: c.Disks,
imageService: c.Images,
ctx: ctx,
}, nil
}
func NewClient(configs ...func(*Client) error) (*Client, error) {
gcpClient := new(Client)
gcpClient.timeout = 5 * time.Minute
for _, cfg := range configs {
err := cfg(gcpClient)
if err != nil {
return nil, errwrap.Wrap(err, "new GCP Client config loading error")
}
}
if gcpClient.googleClient == nil {
return nil, fmt.Errorf("You have an incomplete GCPClientAPI.googleClient")
}
if gcpClient.zoneName == "" {
return nil, fmt.Errorf("You have an incomplete GCPClientAPI.zoneName")
}
if gcpClient.projectName == "" {
return nil, fmt.Errorf("You have an incomplete GCPClientAPI.projectName")
}
return gcpClient, nil
}
/* Cliaas Client Interface */
func (c *Client) Delete(identifier string) error {
return c.DeleteVM(identifier)
}
func (c *Client) Replace(identifier string, sourceImageTarballURL string, diskSizeGB int64) error {
vmInstance, err := c.GetVMInfo(Filter{
NameRegexString: identifier + "*",
})
if err != nil {
return errwrap.Wrap(err, "getvminfo failed")
}
err = c.StopVM(vmInstance.Name)
if err != nil {
return errwrap.Wrap(err, "stopvm failed")
}
err = c.WaitForStatus(vmInstance.Name, InstanceTerminated)
if err != nil {
return errwrap.Wrap(err, "waitforstatus after stopvm failed")
}
sourceImage, err := c.CreateImage(sourceImageTarballURL, diskSizeGB)
if err != nil {
return errwrap.Wrap(err, "could not create new disk image")
}
newInstance := createGCPInstanceFromExisting(vmInstance, sourceImage, diskSizeGB, fmt.Sprintf("%s-%s", identifier, time.Now().Format("2006-01-02-15-04-05")))
err = c.CreateVM(*newInstance)
if err != nil {
return errwrap.Wrap(err, "CreateVM call failed")
}
return c.WaitForStatus(newInstance.Name, InstanceRunning)
}
func ConfigTimeout(value time.Duration) func(*Client) error {
return func(gcpClient *Client) error {
gcpClient.timeout = value * time.Second
return nil
}
}
func (s *Client) GetDisk(identifier string) (iaas.Disk, error) {
disk, err := s.Disk(Filter{
NameRegexString: identifier + "*",
})
if err != nil {
return iaas.Disk{}, err
}
return iaas.Disk{
SizeGB: int64(disk.SizeGb),
}, nil
}
/* End Cliaas Client Interface */
func (s *Client) Disk(filter Filter) (*compute.Disk, error) {
list, err := s.googleClient.DiskList(s.projectName, s.zoneName)
if err != nil {
return nil, errwrap.Wrap(err, "call DiskList on google client failed")
}
for _, item := range list.Items {
var validName = regexp.MustCompile(filter.NameRegexString)
nameMatch := validName.MatchString(item.Name)
if nameMatch {
return item, nil
}
}
return nil, fmt.Errorf("No disk matches found")
}
func ConfigGoogleClient(value GoogleComputeClient) func(*Client) error {
return func(gcpClient *Client) error {
gcpClient.googleClient = value
return nil
}
}
func ConfigZoneName(value string) func(*Client) error {
return func(gcpClient *Client) error {
gcpClient.zoneName = value
return nil
}
}
func ConfigProjectName(value string) func(*Client) error {
return func(gcpClient *Client) error {
gcpClient.projectName = value
return nil
}
}
func (s *Client) CreateImage(tarball string, diskSizeGB int64) (string, error) {
imageName := fmt.Sprintf("opsman-disk-%v", time.Now().Format("2006-01-02-15-04-05"))
_, err := s.googleClient.ImageInsert(s.projectName, &compute.Image{
Name: imageName,
DiskSizeGb: diskSizeGB,
RawDisk: &compute.ImageRawDisk{
Source: fmt.Sprintf("http://storage.googleapis.com/%v", tarball),
},
}, s.timeout)
if err != nil {
return "", err
}
sourceImage := fmt.Sprintf("projects/%s/global/images/%s", s.projectName, imageName)
return sourceImage, nil
}
func (s *Client) CreateVM(instance compute.Instance) error {
operation, err := s.googleClient.Insert(s.projectName, s.zoneName, &instance)
if err != nil {
return errwrap.Wrap(err, "call to googleclient.Insert yielded error")
}
if operation.Error != nil {
return errors.New("unexpected errors from operation response from google client")
}
return nil
}
func (s *Client) DeleteVM(instanceName string) error {
operation, err := s.googleClient.Delete(s.projectName, s.zoneName, instanceName)
if err != nil {
return errwrap.Wrap(err, "call to googleclient.Delete yielded error")
}
if operation.Error != nil {
return errors.New("unexpected errors from operation response from google client")
}
return nil
}
//StopVM - will try to stop the VM with the given name
func (s *Client) StopVM(instanceName string) error {
operation, err := s.googleClient.Stop(s.projectName, s.zoneName, instanceName)
if err != nil {
return errwrap.Wrap(err, "call to googleclient.Stop yielded error")
}
if operation.Error != nil {
return errors.New("unexpected errors from operation response from google client")
}
return nil
}
//GetVMInfo - gets the information on the first VM to match the given filter argument
// currently filter will only do a regex on teh tag||name regex fields against
// the List's result set
func (s *Client) GetVMInfo(filter Filter) (*compute.Instance, error) {
return s.getVMInfo(filter, InstanceRunning)
}
func (s *Client) getVMInfo(filter Filter, status string) (*compute.Instance, error) {
list, err := s.googleClient.List(s.projectName, s.zoneName)
if err != nil {
return nil, errwrap.Wrap(err, "call List on google client failed")
}
for _, item := range list.Items {
var validID = regexp.MustCompile(filter.TagRegexString)
var validName = regexp.MustCompile(filter.NameRegexString)
taglist := strings.Join(item.Tags.Items, " ")
tagMatch := validID.MatchString(taglist)
nameMatch := validName.MatchString(item.Name)
if tagMatch &&
nameMatch &&
(status == InstanceAll || item.Status == InstanceRunning) {
return item, nil
}
}
return nil, fmt.Errorf("No instance matches found")
}
func (s *Client) WaitForStatus(vmName string, desiredStatus string) error {
errChannel := make(chan error)
go func() {
for {
vmInfo, err := s.getVMInfo(Filter{NameRegexString: vmName}, InstanceAll)
if err != nil {
errChannel <- errwrap.Wrap(err, "GetVMInfo call failed")
return
}
if vmInfo.Status == desiredStatus {
errChannel <- nil
return
}
}
}()
select {
case res := <-errChannel:
return res
case <-time.After(s.timeout):
return errors.New("polling for status timed out")
}
}
type googleComputeClientWrapper struct {
imageService *compute.ImagesService
instanceService *compute.InstancesService
disksService *compute.DisksService
ctx context.Context
}
func (s *googleComputeClientWrapper) List(project string, zone string) (*compute.InstanceList, error) {
return s.instanceService.List(project, zone).Context(s.ctx).Do()
}
func (s *googleComputeClientWrapper) Delete(project string, zone string, instance string) (*compute.Operation, error) {
return s.instanceService.Delete(project, zone, instance).Context(s.ctx).Do()
}
func (s *googleComputeClientWrapper) Stop(project string, zone string, instance string) (*compute.Operation, error) {
vmInstance, err := s.instanceService.Get(project, zone, instance).Context(s.ctx).Do()
if err != nil {
return nil, errwrap.Wrap(err, "failed getting vm instance")
}
if len(vmInstance.NetworkInterfaces) > 0 && len(vmInstance.NetworkInterfaces[0].AccessConfigs) > 0 {
accessConfigName := vmInstance.NetworkInterfaces[0].AccessConfigs[0].Name
nicName := vmInstance.NetworkInterfaces[0].Name
operation, err := s.instanceService.DeleteAccessConfig(project, zone, instance, accessConfigName, nicName).Context(s.ctx).Do()
if err != nil {
return operation, errwrap.Wrap(err, "could not delete access config")
}
}
return s.instanceService.Stop(project, zone, instance).Context(s.ctx).Do()
}
func (s *googleComputeClientWrapper) Insert(project string, zone string, instance *compute.Instance) (*compute.Operation, error) {
return s.instanceService.Insert(project, zone, instance).Context(s.ctx).Do()
}
func (s *googleComputeClientWrapper) DiskList(project string, zone string) (*compute.DiskList, error) {
return s.disksService.List(project, zone).Context(s.ctx).Do()
}
func (s *googleComputeClientWrapper) ImageInsert(project string, image *compute.Image, timeout time.Duration) (*compute.Operation, error) {
operation, err := s.imageService.Insert(project, image).Context(s.ctx).Do()
if err != nil {
return operation, errwrap.Wrap(err, "disk image insert failed")
}
errChannel := make(chan error)
go func() {
for {
image, err := s.imageService.Get(project, image.Name).Context(s.ctx).Do()
if err != nil {
errChannel <- errwrap.Wrap(err, "image get failed")
}
if image != nil && image.Status == ImageReady {
errChannel <- nil
}
if image != nil && image.Status == ImageFailed {
errChannel <- errors.New("image creation failed")
}
}
}()
select {
case res := <-errChannel:
return operation, res
case <-time.After(timeout):
return nil, errors.New("polling for status timed out")
}
return nil, nil
}
func createGCPInstanceFromExisting(vmInstance *compute.Instance, sourceImage string, diskSizeGB int64, name string) *compute.Instance {
newInstance := &compute.Instance{
NetworkInterfaces: vmInstance.NetworkInterfaces,
MachineType: vmInstance.MachineType,
Name: name,
Tags: &compute.Tags{
Items: vmInstance.Tags.Items,
},
Disks: []*compute.AttachedDisk{
&compute.AttachedDisk{
Boot: true,
InitializeParams: &compute.AttachedDiskInitializeParams{
SourceImage: sourceImage,
DiskSizeGb: diskSizeGB,
},
},
},
}
newInstance.NetworkInterfaces[0].NetworkIP = ""
return newInstance
}