-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (147 loc) · 4.08 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"context"
"time"
"github.com/pkg/errors"
rancher "github.com/rancher/go-rancher/v2"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
var (
env string
service string
rancherURL string
dockerImage string
upgradeTimeout time.Duration
)
func init() {
flag.StringVar(&env, "env", "", "Rancher environment")
flag.StringVar(&service, "service", "", "Service name")
flag.StringVar(&rancherURL, "rancher-url", "", "Rancher url")
flag.StringVar(&dockerImage, "docker-image", "", "Docker image")
flag.DurationVar(&upgradeTimeout, "upgrade-timeout", time.Second*60, "Upgrade timeout (in seconds)")
}
func main() {
flag.Parse()
fmt.Printf("Run rancher-deploy %v %v %v\n", version, commit, date)
if service == "" || rancherURL == "" || dockerImage == "" {
log.Fatal("service, rancher-url, docker-image are required")
}
client, err := rancher.NewRancherClient(&rancher.ClientOpts{
Url: rancherURL,
AccessKey: os.Getenv("RANCHER_ACCESS_KEY"),
SecretKey: os.Getenv("RANCHER_SECRET_KEY"),
})
if err != nil {
log.Fatal(err)
}
deploy := &Deploy{
Client: client,
DockerImage: dockerImage,
Env: env,
Service: service,
}
if err := deploy.Run(); err != nil {
log.Fatal(err)
}
}
type Deploy struct {
// Service could be some-service or some-stack/some-service format. Required!
Service string
// DockerImage eg. flyin/screenshot:latest. Required!
DockerImage string
// Env is environment name eg. default
Env string
Client *rancher.RancherClient
stack string
}
func (d *Deploy) Run() error {
if strings.Contains(d.Service, "/") {
parts := strings.SplitN(d.Service, "/", 2)
d.stack = parts[0]
d.Service = parts[1]
}
serviceFilters, err := d.getServiceFilters()
if err != nil {
return err
}
services, err := d.Client.Service.List(&rancher.ListOpts{Filters: serviceFilters})
if err != nil {
return errors.Wrap(err, "couldn't retrieve services list")
}
if len(services.Data) <= 0 {
return fmt.Errorf("service %s not found", d.Service)
}
service := services.Data[0]
upgrade := &rancher.ServiceUpgrade{
InServiceStrategy: &rancher.InServiceUpgradeStrategy{
LaunchConfig: service.LaunchConfig,
SecondaryLaunchConfigs: service.SecondaryLaunchConfigs,
StartFirst: false,
IntervalMillis: 1000,
BatchSize: 1,
},
ToServiceStrategy: &rancher.ToServiceUpgradeStrategy{},
}
_, err = d.Client.Service.ActionUpgrade(&service, upgrade)
if err != nil {
return errors.Wrap(err, "couldn't upgrade")
}
upgradedService, err := d.WaitUpgrade(service.Id)
if err != nil {
return err
}
_, err = d.Client.Service.ActionFinishupgrade(upgradedService)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("couldn't upgrader service %s", service.Name))
}
return nil
}
func (d *Deploy) WaitUpgrade(serviceId string) (*rancher.Service, error) {
log.Printf("%s(%s) - Upgrading...\n", d.Service, serviceId)
ticker := time.NewTicker(time.Second * 3)
defer ticker.Stop()
ctx, _ := context.WithTimeout(context.Background(), upgradeTimeout)
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("timeout for service %s exceed", d.Service)
case <-ticker.C:
s, err := d.Client.Service.ById(serviceId)
if err != nil {
return nil, errors.Wrap(err, "couldn't retrieve service by Id")
}
if s.State == "upgraded" {
log.Printf("%s(%s) - Done...\n", d.Service, serviceId)
return s, nil
}
}
}
}
func (d *Deploy) getServiceFilters() (map[string]interface{}, error) {
filters := map[string]interface{}{"name": d.Service}
if d.stack == "" {
return filters, nil
}
stackFilters := map[string]interface{}{"name": d.stack}
if d.Env != "" {
stackFilters["env"] = d.Env
}
stacks, err := d.Client.Stack.List(&rancher.ListOpts{Filters: stackFilters})
if err != nil {
return nil, errors.Wrap(err, "couldn't retrieve stack list")
}
if len(stacks.Data) <= 0 {
return nil, fmt.Errorf("stack %s not found", d.stack)
}
filters["stackId"] = stacks.Data[0].Id
return filters, nil
}