This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
/
dockerregistry.go
228 lines (207 loc) · 6.02 KB
/
dockerregistry.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
225
226
227
228
package dockerregistry
import (
"fmt"
"github.com/enaml-ops/enaml"
"github.com/enaml-ops/omg-product-bundle/products/cloudfoundry/enaml-gen/debian_nfs_server"
"github.com/enaml-ops/omg-product-bundle/products/cloudfoundry/enaml-gen/nfs_mounter"
"github.com/enaml-ops/omg-product-bundle/products/dockerregistry/enaml-gen/proxy"
"github.com/enaml-ops/omg-product-bundle/products/dockerregistry/enaml-gen/registry"
"github.com/xchapter7x/lo"
"gopkg.in/yaml.v2"
)
const (
sharePath = "/var/vcap/nfs"
rootPath = "/var/vcap/nfs/shared"
registryReleaseName = "docker-registry"
)
type DockerRegistry struct {
DeploymentName string
DockerRegistryReleaseVer string
DockerRegistryReleaseURL string
DockerRegistryReleaseSHA string
StemcellVersion string
StemcellAlias string
StemcellOS string
AZs []string
NetworkName string
RegistryVMType string
RegistryIPs []string
ProxyVMType string
ProxyIPs []string
PublicIP []string
ProxyCert string
ProxyCertKey string
NFSServerVMType string
NFSDiskType string
NFSIP string
Secret string
}
func (d *DockerRegistry) CreateDeploymentManifest(cloudConfig []byte) (*enaml.DeploymentManifest, error) {
lo.G.Debug("Cloud Config:", string(cloudConfig))
c := &enaml.CloudConfigManifest{}
if err := yaml.Unmarshal(cloudConfig, &c); err != nil {
return nil, err
}
if err := d.doCloudConfigValidation(c); err != nil {
return nil, err
}
manifest := &enaml.DeploymentManifest{}
manifest.SetName(d.DeploymentName)
manifest.AddRelease(enaml.Release{
Name: registryReleaseName,
URL: d.DockerRegistryReleaseURL,
SHA1: d.DockerRegistryReleaseSHA,
Version: d.DockerRegistryReleaseVer,
})
manifest.AddStemcell(enaml.Stemcell{
Alias: d.StemcellAlias,
OS: d.StemcellOS,
Version: d.StemcellVersion,
})
manifest.SetUpdate(d.CreateUpdate())
manifest.AddInstanceGroup(d.CreateNFSServerInstanceGroup())
manifest.AddInstanceGroup(d.CreateRegistryInstanceGroup())
manifest.AddInstanceGroup(d.CreateProxyInstanceGroup())
return manifest, nil
}
func (d *DockerRegistry) doCloudConfigValidation(cloudConfigManifest *enaml.CloudConfigManifest) (err error) {
for _, azName := range d.AZs {
if !cloudConfigManifest.ContainsAZName(azName) {
err = fmt.Errorf("AZ [%s] is not defined as a AZ in cloud config", azName)
return
}
}
if !cloudConfigManifest.ContainsVMType(d.RegistryVMType) {
err = fmt.Errorf("RegistryVMType[%s] is not defined as a VMType in cloud config", d.RegistryVMType)
return
}
if !cloudConfigManifest.ContainsVMType(d.ProxyVMType) {
err = fmt.Errorf("ProxyVMType[%s] is not defined as a VMType in cloud config", d.ProxyVMType)
return
}
if !cloudConfigManifest.ContainsVMType(d.NFSServerVMType) {
err = fmt.Errorf("NFSServerType[%s] is not defined as a VMType in cloud config", d.NFSServerVMType)
return
}
if !cloudConfigManifest.ContainsDiskType(d.NFSDiskType) {
err = fmt.Errorf("NFSDiskType[%s] is not defined as a DiskType in cloud config", d.NFSDiskType)
return
}
return
}
func (d *DockerRegistry) CreateRegistryInstanceGroup() *enaml.InstanceGroup {
server := &enaml.InstanceGroup{
Name: "registry",
Instances: len(d.RegistryIPs),
Stemcell: d.StemcellAlias,
VMType: d.RegistryVMType,
AZs: d.AZs,
}
server.AddNetwork(enaml.Network{
Name: d.NetworkName,
StaticIPs: d.RegistryIPs,
})
server.AddJob(d.createRegistryJob())
server.AddJob(d.createNFSMounterJob())
return server
}
func (d *DockerRegistry) createRegistryJob() *enaml.InstanceJob {
job := &enaml.InstanceJob{
Name: "registry",
Release: registryReleaseName,
Properties: ®istry.RegistryJob{
Docker: ®istry.Docker{
Registry: ®istry.Registry{
Root: rootPath,
Cookie: d.Secret,
},
},
},
}
return job
}
func (d *DockerRegistry) createNFSMounterJob() *enaml.InstanceJob {
job := &enaml.InstanceJob{
Name: "nfs_mounter",
Release: registryReleaseName,
Properties: &nfs_mounter.NfsMounterJob{
NfsServer: &nfs_mounter.NfsServer{
Address: d.NFSIP,
SharePath: sharePath,
},
},
}
return job
}
func (d *DockerRegistry) CreateProxyInstanceGroup() *enaml.InstanceGroup {
server := &enaml.InstanceGroup{
Name: "proxy",
Instances: len(d.ProxyIPs),
Stemcell: d.StemcellAlias,
VMType: d.ProxyVMType,
AZs: d.AZs,
}
server.AddNetwork(enaml.Network{
Name: d.NetworkName,
StaticIPs: d.ProxyIPs,
})
server.AddJob(d.createProxyJob())
return server
}
func (d *DockerRegistry) createProxyJob() *enaml.InstanceJob {
job := &enaml.InstanceJob{
Name: "proxy",
Release: registryReleaseName,
Properties: &proxy.ProxyJob{
Docker: &proxy.Docker{
Proxy: &proxy.Proxy{
Backend: &proxy.Backend{
Hosts: d.RegistryIPs,
},
Ssl: &proxy.Ssl{
Cert: d.ProxyCert,
Key: d.ProxyCertKey,
},
},
},
},
}
return job
}
func (d *DockerRegistry) CreateNFSServerInstanceGroup() *enaml.InstanceGroup {
server := &enaml.InstanceGroup{
Name: "nfs-server",
Instances: 1,
Stemcell: d.StemcellAlias,
VMType: d.NFSServerVMType,
PersistentDiskType: d.NFSDiskType,
AZs: d.AZs,
}
server.AddNetwork(enaml.Network{
Name: d.NetworkName,
StaticIPs: []string{d.NFSIP},
})
server.AddJob(d.createNFSServerJob())
return server
}
func (d *DockerRegistry) createNFSServerJob() *enaml.InstanceJob {
job := &enaml.InstanceJob{
Name: "debian_nfs_server",
Release: registryReleaseName,
Properties: &debian_nfs_server.DebianNfsServerJob{
NfsServer: &debian_nfs_server.NfsServer{
AllowFromEntries: d.RegistryIPs,
},
},
}
return job
}
func (d *DockerRegistry) CreateUpdate() (update enaml.Update) {
update = enaml.Update{
Canaries: 1,
MaxInFlight: 3,
CanaryWatchTime: "1000-60000",
UpdateWatchTime: "1000-60000",
}
return
}