forked from Netflix/chaosmonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
224 lines (182 loc) · 5.62 KB
/
deployment.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2016 Netflix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package deploy contains information about all of the deployed instances, and how
// they are organized across accounts, apps, regions, clusters, and autoscaling
// groups.
package deploy
import (
"fmt"
"github.com/SmartThingsOSS/frigga-go"
)
// Deployment contains information about how apps are deployed
type Deployment interface {
// Apps sends App objects over a channel
Apps(c chan<- *App, appNames []string)
// GetApp retrieves a single App
GetApp(name string) (*App, error)
// AppNames returns the names of all apps
AppNames() ([]string, error)
}
// Account represents the set of clusters associated with an App that reside
// in one AWS account (e.g., "prod", "test").
type Account struct {
name string // e.g., "prod", "test"
clusters []*Cluster
app *App
cloudProvider string // e.g., "aws"
}
// Name returns the name of the account associated with this account
func (a *Account) Name() string {
return a.name
}
// Clusters returns a slice of clusters
func (a *Account) Clusters() []*Cluster {
return a.clusters
}
// AppName returns the name of the app associated with this Account
func (a *Account) AppName() string {
return a.app.name
}
// RegionNames returns the name of the regions that clusters in this account are
// running in
func (a *Account) RegionNames() []string {
m := make(map[string]bool)
// Get the region names of the clusters
for _, cluster := range a.Clusters() {
for _, name := range cluster.RegionNames() {
m[name] = true
}
}
result := make([]string, 0, len(m))
for name := range m {
result = append(result, name)
}
return result
}
// CloudProvider returns the cloud provider (e.g., "aws")
func (a *Account) CloudProvider() string {
return a.cloudProvider
}
type stringSet map[string]bool
func (s *stringSet) add(val string) {
(*s)[val] = true
}
// slice converts a stringSet to a string slice
func (s stringSet) slice() []string {
result := []string{}
for val := range s {
result = append(result, val)
}
return result
}
// StackNames returns the names of the stacks associated with this account
func (a *Account) StackNames() []string {
stacks := make(stringSet)
for _, cluster := range a.Clusters() {
stacks.add(cluster.StackName())
}
return stacks.slice()
}
// Cluster represents what Spinnaker refers to as a "cluster", which
// contains app-stack-detail.
// Every ASG is associated with exactly one cluster.
// Note that clusters can span regions
type Cluster struct {
name string
asgs []*ASG
account *Account
}
// Name returns the name of the cluster, convention: app-stack-detail
func (c *Cluster) Name() string {
return c.name
}
// AppName returns the name of the app associated with this cluster
func (c *Cluster) AppName() string {
return c.account.AppName()
}
// StackName returns the name of the stack, following the app-stack-detail convention
func (c *Cluster) StackName() string {
names, err := frigga.Parse(c.Name())
if err != nil {
panic(err)
}
return names.Stack
}
// AccountName returns the name of the account associated with this cluster
func (c *Cluster) AccountName() string {
return c.account.Name()
}
// ASGs returns a slice of ASGs
func (c *Cluster) ASGs() []*ASG {
return c.asgs
}
// RegionNames returns the name of the region that this cluster runs in
func (c *Cluster) RegionNames() []string {
m := make(map[string]bool)
for _, asg := range c.ASGs() {
m[asg.RegionName()] = true
}
result := []string{}
for name := range m {
result = append(result, name)
}
return result
}
// CloudProvider returns the cloud provider (e.g., "aws")
func (c *Cluster) CloudProvider() string {
return c.account.CloudProvider()
}
// Instance implements instance.Instance
type Instance struct {
// instance id (e.g., "i-74e93ddb")
id string
// ASG that this instance is part of
asg *ASG
}
func (i *Instance) String() string {
return fmt.Sprintf("app=%s account=%s region=%s stack=%s cluster=%s asg=%s instance-id=%s",
i.AppName(), i.AccountName(), i.RegionName(), i.StackName(), i.ClusterName(), i.ASGName(), i.ID())
}
// AppName returns the name of the app associated with this instance
func (i *Instance) AppName() string {
return i.asg.AppName()
}
// AccountName returns the name of the AWS account associated with the instance
func (i *Instance) AccountName() string {
return i.asg.AccountName()
}
// ClusterName returns the name of the cluster associated with the instance
func (i *Instance) ClusterName() string {
return i.asg.ClusterName()
}
// RegionName returns the name of the region associated with the instance
func (i *Instance) RegionName() string {
return i.asg.RegionName()
}
// ASGName returns the name of the ASG associated with the instance
func (i *Instance) ASGName() string {
return i.asg.Name()
}
// StackName returns the name of the stack associated with the instance
func (i *Instance) StackName() string {
return i.asg.StackName()
}
// CloudProvider returns the cloud provider (e.g., "aws")
func (i *Instance) CloudProvider() string {
return i.asg.CloudProvider()
}
// ID returns the instance id
func (i *Instance) ID() string {
return i.id
}