forked from bluemixgaragelondon/cf-blue-green-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (156 loc) · 4.6 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
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/manifest"
"code.cloudfoundry.org/cli/plugin"
go_i18n "github.com/nicksnyder/go-i18n/i18n"
"strings"
)
var PluginVersion string
type CfPlugin struct {
Connection plugin.CliConnection
Deployer BlueGreenDeployer
}
func (p *CfPlugin) Run(cliConnection plugin.CliConnection, args []string) {
if len(args) > 0 && args[0] == "CLI-MESSAGE-UNINSTALL" {
return
}
p.Connection = cliConnection
defaultCfDomain, err := p.DefaultCfDomain()
if err != nil {
fmt.Println("Failed to get default shared domain")
os.Exit(1)
}
p.Deployer.Setup(cliConnection)
if len(args) < 2 {
fmt.Println("App name must be provided")
os.Exit(1)
}
if !p.Deploy(defaultCfDomain, manifest.DiskRepository{}, args) {
fmt.Println("Smoke tests failed")
os.Exit(1)
}
}
func (p *CfPlugin) Deploy(defaultCfDomain string, repo manifest.Repository, args []string) bool {
appName := args[1]
p.Deployer.DeleteAllAppsExceptLiveApp(appName)
liveAppName, liveAppRoutes := p.Deployer.LiveApp(appName)
newAppName := appName + "-new"
tempRoute := Route{Host: newAppName, Domain: Domain{Name: defaultCfDomain}}
p.Deployer.PushNewApp(newAppName, tempRoute)
promoteNewApp := true
smokeTestScript := ExtractIntegrationTestScript(args)
if smokeTestScript != "" {
promoteNewApp = p.Deployer.RunSmokeTests(smokeTestScript, tempRoute.FQDN())
}
newAppRoutes := p.GetNewAppRoutes(appName, defaultCfDomain, repo, liveAppRoutes)
p.Deployer.UnmapRoutesFromApp(newAppName, tempRoute)
if promoteNewApp {
if liveAppName != "" {
p.Deployer.MapRoutesToApp(newAppName, newAppRoutes...)
p.Deployer.RenameApp(liveAppName, appName+"-old")
p.Deployer.RenameApp(newAppName, appName)
p.Deployer.UnmapRoutesFromApp(appName+"-old", liveAppRoutes...)
} else {
p.Deployer.MapRoutesToApp(newAppName, newAppRoutes...)
p.Deployer.RenameApp(newAppName, appName)
}
return true
} else {
p.Deployer.RenameApp(newAppName, appName+"-failed")
return false
}
}
func (p *CfPlugin) GetNewAppRoutes(appName string, defaultCfDomain string, repo manifest.Repository, liveAppRoutes []Route) []Route {
newAppRoutes := []Route{}
f := ManifestAppFinder{AppName: appName, Repo: repo}
if manifestRoutes := f.RoutesFromManifest(defaultCfDomain); manifestRoutes != nil {
newAppRoutes = append(newAppRoutes, manifestRoutes...)
}
uniqueRoutes := p.UnionRouteLists(newAppRoutes, liveAppRoutes)
if len(uniqueRoutes) == 0 {
uniqueRoutes = append(uniqueRoutes, Route{Host: appName, Domain: Domain{Name: defaultCfDomain}})
}
return uniqueRoutes
}
func (p *CfPlugin) UnionRouteLists(listA []Route, listB []Route) []Route {
duplicateList := append(listA, listB...)
routesSet := make(map[Route]struct{})
for _, route := range duplicateList {
routesSet[route] = struct{}{}
}
uniqueRoutes := []Route{}
for route := range routesSet {
uniqueRoutes = append(uniqueRoutes, route)
}
return uniqueRoutes
}
func (p *CfPlugin) GetMetadata() plugin.PluginMetadata {
var major, minor, build int
fmt.Sscanf(PluginVersion, "%d.%d.%d", &major, &minor, &build)
return plugin.PluginMetadata{
Name: "blue-green-deploy",
Version: plugin.VersionType{
Major: major,
Minor: minor,
Build: build,
},
Commands: []plugin.Command{
{
Name: "blue-green-deploy",
Alias: "bgd",
HelpText: "Zero-downtime deploys with smoke tests",
UsageDetails: plugin.Usage{
Usage: "blue-green-deploy APP_NAME [--smoke-test TEST_SCRIPT]",
Options: map[string]string{
"smoke-test": "The test script to run.",
},
},
},
},
}
}
func (p *CfPlugin) DefaultCfDomain() (domain string, err error) {
var res []string
if res, err = p.Connection.CliCommandWithoutTerminalOutput("curl", "/v2/shared_domains"); err != nil {
return
}
response := struct {
Resources []struct {
Entity struct {
Name string
}
}
}{}
var json_string string
json_string = strings.Join(res, "\n")
if err = json.Unmarshal([]byte(json_string), &response); err != nil {
return
}
domain = response.Resources[0].Entity.Name
return
}
func ExtractIntegrationTestScript(args []string) string {
f := flag.NewFlagSet("blue-green-deploy", flag.ExitOnError)
script := f.String("smoke-test", "", "")
f.Parse(args[2:])
return *script
}
func main() {
// T needs to point to a translate func, otherwise cf internals blow up
i18n.T, _ = go_i18n.Tfunc("")
p := CfPlugin{
Deployer: &BlueGreenDeploy{
ErrorFunc: func(message string, err error) {
fmt.Printf("%v - %v\n", message, err)
os.Exit(1)
},
Out: os.Stdout,
},
}
plugin.Start(&p)
}