-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathplugin.go
executable file
·189 lines (162 loc) · 5.25 KB
/
plugin.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
package main
import (
"errors"
"fmt"
"strings"
"time"
log "github.com/Sirupsen/logrus"
client "github.com/rancher/go-rancher/v2"
)
type Plugin struct {
URL string
Key string
Secret string
Service string
SidekickDockerImage []string
DockerImage string
StartFirst bool
Confirm bool
Timeout int
IntervalMillis int64
BatchSize int64
YamlVerified bool
Environment string
}
func (p *Plugin) Exec() error {
log.Info("Drone Rancher Plugin built")
if p.URL == "" || p.Key == "" || p.Secret == "" || p.Service == "" {
return errors.New("Eek: Must have url, key, secret, and service definied")
}
var wantedService, wantedStack string
if strings.Contains(p.Service, "/") {
parts := strings.SplitN(p.Service, "/", 2)
wantedStack = parts[0]
wantedService = parts[1]
} else {
wantedService = p.Service
}
rancher, err := client.NewRancherClient(&client.ClientOpts{
Url: p.URL,
AccessKey: p.Key,
SecretKey: p.Secret,
})
if err != nil {
return fmt.Errorf("Failed to create rancher client: %s", err)
}
// Prepare service filters for service listing
serviceFilters := map[string]interface{}{"name": wantedService}
if len(p.Environment) >= 1 {
environments, err := rancher.Account.List(&client.ListOpts{Filters: map[string]interface{}{"name": p.Environment}})
if err != nil {
return fmt.Errorf("Failed to find given rancher environment: %s", err)
}
if len(environments.Data) <= 0 {
return fmt.Errorf("Unable to find environment %s", p.Environment)
}
// If found add environmentID to serviceFilters
serviceFilters["accountId"] = environments.Data[0].Id
}
// Query stacks with filter name=wantedStack
if wantedStack != "" {
stacks, err := rancher.Stack.List(&client.ListOpts{Filters: map[string]interface{}{"name": wantedStack}})
// If environment is defined re-query the API with the accountId
if len(p.Environment) >= 1 {
stacks, err = rancher.Stack.List(&client.ListOpts{Filters: map[string]interface{}{"accountId": serviceFilters["accountId"], "name": wantedStack}})
}
if err != nil {
return fmt.Errorf("Failed to list rancher environments: %s", err)
}
if len(stacks.Data) <= 0 {
return fmt.Errorf("Unable to find stack %s", wantedStack)
}
// If found add stackID to serviceFilters
serviceFilters["stackId"] = stacks.Data[0].Id
}
// Query services with prepared filters
services, err := rancher.Service.List(&client.ListOpts{Filters: serviceFilters})
if err != nil {
return fmt.Errorf("Failed to list rancher services: %s", err)
}
if len(services.Data) <= 0 {
return fmt.Errorf("Unable to find service %s", p.Service)
}
service := services.Data[0]
// Service is found, proceed with upgrade
// We want to exit if there is no docker image updates
if p.DockerImage == "" && len(p.SidekickDockerImage) <= 0 {
return fmt.Errorf("Nothing to upgrade")
}
// Only change value if it's not null.
if p.DockerImage != "" {
// Add prefix when missing to meet Rancher API requirement
service.LaunchConfig.ImageUuid = prepareDockerPrefix(p.DockerImage)
}
// Iterate over provided sidekick flags
for _, sidekick := range p.SidekickDockerImage {
// Split flag in two from "--sidekick nginx nginx:latest"
parts := strings.SplitN(sidekick, " ", 2)
wantedSidekick := parts[0]
wantedImage := parts[1]
for i, s := range service.SecondaryLaunchConfigs {
if wantedSidekick == s.Name {
service.SecondaryLaunchConfigs[i].ImageUuid = prepareDockerPrefix(wantedImage)
}
}
}
upgrade := &client.ServiceUpgrade{}
upgrade.InServiceStrategy = &client.InServiceUpgradeStrategy{
LaunchConfig: service.LaunchConfig,
SecondaryLaunchConfigs: service.SecondaryLaunchConfigs,
StartFirst: p.StartFirst,
IntervalMillis: p.IntervalMillis,
BatchSize: p.BatchSize,
}
upgrade.ToServiceStrategy = &client.ToServiceUpgradeStrategy{}
_, err = rancher.Service.ActionUpgrade(&service, upgrade)
if err != nil {
return fmt.Errorf("Unable to upgrade service %s: %s", p.Service, err)
}
if p.Confirm {
srv, err := retry(func() (interface{}, error) {
s, e := rancher.Service.ById(service.Id)
if e != nil {
return nil, e
}
if s.State != "upgraded" {
return nil, fmt.Errorf("Service not upgraded: %s", s.State)
}
return s, nil
}, time.Duration(p.Timeout)*time.Second, 3*time.Second)
if err != nil {
return fmt.Errorf("Error waiting for service upgrade to complete: %s", err)
}
_, err = rancher.Service.ActionFinishupgrade(srv.(*client.Service))
if err != nil {
return fmt.Errorf("Unable to finish upgrade %s: %s", p.Service, err)
}
log.Infof("Finished upgrade %s", p.Service)
}
log.Infof("Upgraded %s to %s", p.Service, p.DockerImage)
return nil
}
type retryFunc func() (interface{}, error)
func prepareDockerPrefix(image string) string {
if !strings.HasPrefix(image, "docker:") {
image = fmt.Sprintf("docker:%s", image)
}
return image
}
func retry(f retryFunc, timeout time.Duration, interval time.Duration) (interface{}, error) {
finish := time.After(timeout)
for {
result, err := f()
if err == nil {
return result, nil
}
select {
case <-finish:
return nil, err
case <-time.After(interval):
}
}
}