forked from josmo/drone-rancher
/
main.go
115 lines (96 loc) · 2.65 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
package main
import (
"fmt"
"os"
"strings"
"github.com/drone/drone-go/plugin"
"github.com/rancher/go-rancher/client"
)
type Rancher struct {
Url string `json:"url"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Service string `json:"service"`
Image string `json:"docker_image"`
StartFirst bool `json:"start_first"`
}
func main() {
vargs := Rancher{StartFirst: true}
plugin.Param("vargs", &vargs)
err := plugin.Parse()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(vargs.Url) == 0 || len(vargs.AccessKey) == 0 || len(vargs.SecretKey) == 0 || len(vargs.Service) == 0 {
return
}
if !strings.HasPrefix(vargs.Image, "docker:") {
vargs.Image = fmt.Sprintf("docker:%s", vargs.Image)
}
var wantedService, wantedStack string
if strings.Contains(vargs.Service, "/") {
parts := strings.SplitN(vargs.Service, "/", 2)
wantedStack = parts[0]
wantedService = parts[1]
} else {
wantedService = vargs.Service
}
rancher, err := client.NewRancherClient(&client.ClientOpts{
Url: vargs.Url,
AccessKey: vargs.AccessKey,
SecretKey: vargs.SecretKey,
})
if err != nil {
fmt.Printf("Failed to create rancher client: %s\n", err)
os.Exit(1)
}
var stackId string
if wantedStack != "" {
environments, err := rancher.Environment.List(&client.ListOpts{})
if err != nil {
fmt.Printf("Failed to list rancher environments: %s\n", err)
os.Exit(1)
}
for _, env := range environments.Data {
if env.Name == wantedStack {
stackId = env.Id
}
}
if stackId == "" {
fmt.Printf("Unable to find stack %s\n", wantedStack)
os.Exit(1)
}
}
services, err := rancher.Service.List(&client.ListOpts{})
if err != nil {
fmt.Printf("Failed to list rancher services: %s\n", err)
os.Exit(1)
}
found := false
var service client.Service
for _, svc := range services.Data {
if svc.Name == wantedService && ((wantedStack != "" && svc.EnvironmentId == stackId) || wantedStack == "") {
service = svc
found = true
}
}
if !found {
fmt.Printf("Unable to find service %s\n", vargs.Service)
os.Exit(1)
}
service.LaunchConfig.ImageUuid = vargs.Image
upgrade := &client.ServiceUpgrade{}
upgrade.InServiceStrategy = &client.InServiceUpgradeStrategy{
LaunchConfig: service.LaunchConfig,
SecondaryLaunchConfigs: service.SecondaryLaunchConfigs,
StartFirst: vargs.StartFirst,
}
upgrade.ToServiceStrategy = &client.ToServiceUpgradeStrategy{}
_, err = rancher.Service.ActionUpgrade(&service, upgrade)
if err != nil {
fmt.Printf("Unable to upgrade service %s\n", vargs.Service)
os.Exit(1)
}
fmt.Printf("Upgraded %s to %s\n", vargs.Service, vargs.Image)
}