-
Notifications
You must be signed in to change notification settings - Fork 179
/
service.go
181 lines (146 loc) · 4.06 KB
/
service.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
package manifest
import (
"crypto/sha1"
"fmt"
"sort"
"strings"
)
type Service struct {
Name string `yaml:"-"`
Agent ServiceAgent `yaml:"agent,omitempty"`
Build ServiceBuild `yaml:"build,omitempty"`
Command ServiceCommand `yaml:"command,omitempty"`
Deployment ServiceDeployment `yaml:"deployment,omitempty"`
Domains ServiceDomains `yaml:"domain,omitempty"`
Drain int `yaml:"drain,omitempty"`
Environment Environment `yaml:"environment,omitempty"`
Health ServiceHealth `yaml:"health,omitempty"`
Image string `yaml:"image,omitempty"`
Init bool `yaml:"init,omitempty"`
Internal bool `yaml:"internal,omitempty"`
Links []string `yaml:"links,omitempty"`
Policies []string `yaml:"policies,omitempty"`
Port ServicePort `yaml:"port,omitempty"`
Privileged bool `yaml:"privileged,omitempty"`
Resources []string `yaml:"resources,omitempty"`
Scale ServiceScale `yaml:"scale,omitempty"`
Singleton bool `yaml:"singleton,omitempty"`
Sticky bool `yaml:"sticky,omitempty"`
Termination ServiceTermination `yaml:"termination,omitempty"`
Test string `yaml:"test,omitempty"`
Volumes []string `yaml:"volumes,omitempty"`
}
type Services []Service
type ServiceAgent struct {
Enabled bool `yaml:"enabled,omitempty"`
Ports []ServiceAgentPort `yaml:"ports,omitempty"`
}
type ServiceAgentPort struct {
Port int `yaml:"port,omitempty"`
Protocol string `yaml:"protocol,omitempty"`
}
type ServiceBuild struct {
Args []string `yaml:"args,omitempty"`
Manifest string `yaml:"manifest,omitempty"`
Path string `yaml:"path,omitempty"`
}
type ServiceCommand []string
type ServiceDeployment struct {
Maximum int `yaml:"maximum,omitempty"`
Minimum int `yaml:"minimum,omitempty"`
}
type ServiceDomains []string
type ServiceHealth struct {
Grace int
Interval int
Path string
Timeout int
}
type ServicePort struct {
Port int `yaml:"port,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
}
type ServiceScale struct {
Cooldown ServiceScaleCooldown
Count ServiceScaleCount
Cpu int
Memory int
Targets ServiceScaleTargets `yaml:"targets,omitempty"`
}
type ServiceScaleCooldown struct {
Down int
Up int
}
type ServiceScaleCount struct {
Min int
Max int
}
type ServiceScaleMetric struct {
Aggregate string
Dimensions map[string]string
Namespace string
Name string
Value float64
}
type ServiceScaleMetrics []ServiceScaleMetric
type ServiceScaleTargets struct {
Cpu int
Custom ServiceScaleMetrics
Memory int
Requests int
}
type ServiceTermination struct {
Grace int `yaml:"grace,omitempty"`
}
func (s Service) BuildHash(key string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("key=%q build[path=%q, manifest=%q, args=%v] image=%q", key, s.Build.Path, s.Build.Manifest, s.Build.Args, s.Image))))
}
func (s Service) Domain() string {
if len(s.Domains) < 1 {
return ""
}
return s.Domains[0]
}
func (s Service) EnvironmentDefaults() map[string]string {
defaults := map[string]string{}
for _, e := range s.Environment {
switch parts := strings.Split(e, "="); len(parts) {
case 2:
defaults[parts[0]] = parts[1]
}
}
return defaults
}
func (s Service) EnvironmentKeys() string {
kh := map[string]bool{}
for _, e := range s.Environment {
kh[strings.Split(e, "=")[0]] = true
}
keys := []string{}
for k := range kh {
keys = append(keys, k)
}
sort.Strings(keys)
return strings.Join(keys, ",")
}
func (s Service) GetName() string {
return s.Name
}
func (s Service) Autoscale() bool {
if s.Agent.Enabled {
return false
}
switch {
case s.Scale.Count.Min == s.Scale.Count.Max:
return false
case s.Scale.Targets.Cpu > 0:
return true
case len(s.Scale.Targets.Custom) > 0:
return true
case s.Scale.Targets.Memory > 0:
return true
case s.Scale.Targets.Requests > 0:
return true
}
return false
}