generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutdown.go
107 lines (98 loc) · 2.76 KB
/
shutdown.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
package instancedeleter
import (
"context"
"net/http"
"time"
"cloud.google.com/go/pubsub"
"github.com/cybozu-go/log"
"github.com/cybozu-go/neco-gcp/pkg/gcp"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
func ShutdownEntryPoint(ctx context.Context, m *pubsub.Message, necoTestProject, necoTestZone string) error {
client, err := google.DefaultClient(context.Background(), "https://www.googleapis.com/auth/compute")
if err != nil {
log.ErrorExit(err)
}
cfg := gcp.NecoTestConfig(necoTestProject, necoTestZone)
return shutdown(ctx, m, client, cfg)
}
func shutdown(ctx context.Context, m *pubsub.Message, client *http.Client, cfg *gcp.Config) error {
project := cfg.Common.Project
commonZone := cfg.Common.Zone
addZones := cfg.App.Shutdown.AdditionalZones
exclude := cfg.App.Shutdown.Exclude
stop := cfg.App.Shutdown.Stop
status := ShutdownStatus{}
now := time.Now().UTC()
expiration := cfg.App.Shutdown.Expiration
service, err := compute.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return err
}
targetZones := append([]string{commonZone}, addZones...)
var errList []error
for _, zone := range targetZones {
instanceList, err := service.Instances.List(project, zone).Do()
if err != nil {
errList = append(errList, err)
continue
}
for _, instance := range instanceList.Items {
if contain(instance.Name, exclude) {
continue
}
shutdownAt, err := getShutdownAt(instance)
switch err {
case errShutdownMetadataNotFound:
case nil:
if now.Sub(shutdownAt) >= 0 {
_, err := service.Instances.Delete(project, zone, instance.Name).Do()
if err != nil {
errList = append(errList, err)
continue
}
status.Deleted = append(status.Deleted, instance.Name)
}
continue
default:
errList = append(errList, err)
continue
}
creationTime, err := time.Parse(time.RFC3339, instance.CreationTimestamp)
if err != nil {
errList = append(errList, err)
continue
}
elapsed := now.Sub(creationTime)
if elapsed.Seconds() < expiration.Seconds() {
continue
}
if contain(instance.Name, stop) {
_, err := service.Instances.Stop(project, zone, instance.Name).Do()
if err != nil {
continue
}
status.Stopped = append(status.Stopped, instance.Name)
} else {
_, err := service.Instances.Delete(project, zone, instance.Name).Do()
if err != nil {
continue
}
status.Deleted = append(status.Deleted, instance.Name)
}
}
}
log.Info("shutdown instances", map[string]interface{}{
"deleted": status.Deleted,
"stopped": status.Stopped,
})
if len(errList) != 0 {
log.Error("shutdown failed", map[string]interface{}{
"errors": errList,
})
return errList[0]
}
return nil
}