forked from thefactory/cloudformation-docker-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-registry.json
354 lines (331 loc) · 12.4 KB
/
docker-registry.json
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Launches a Docker Registry",
"Parameters" : {
"InstanceType" : {
"Description" : "EC2 instance type",
"Type" : "String",
"Default" : "m1.small",
"AllowedValues" : ["m1.small","m1.medium","m1.large","m1.xlarge","m3.medium","m3.large","m3.xlarge","m3.2xlarge","c1.medium","c1.xlarge","c3.large","c3.xlarge","c3.2xlarge","c3.4xlarge","c3.8xlarge","cc2.8xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","r3.large","r3.xlarge","r3.2xlarge","r3.4xlarge","r3.8xlarge","cr1.8xlarge","hi1.4xlarge","hs1.8xlarge","i2.xlarge","i2.2xlarge","i2.4xlarge","i2.8xlarge","t1.micro","cg1.4xlarge","g2.2xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "String"
},
"DnsPrefix" : {
"Description" : "Prefix for the registry's DNS record (<prefix>.<zone>)",
"Type": "String",
"Default": "docker"
},
"DnsZone" : {
"Description" : "Route53-hosted zone to use for the registry's DNS record (<prefix>.<zone>)",
"Type": "String"
},
"DockerImage" : {
"Description" : "Path of the Docker Registry image to use (format: '[<registry>[:<port>]/]<repository>[:<version>]')",
"Type" : "String",
"Default" : "registry:0.7.3"
},
"S3Bucket" : {
"Description" : "Bucket to use for Docker images (e.g., 'mycompany-docker')",
"Type" : "String"
},
"SslCertificate" : {
"Description" : "ARN of an existing SSL certificate on AWS (e.g., 'arn:aws:iam::123456789:server-certificate/docker.mycompany.com')",
"Type" : "String"
},
"Subnets" : {
"Description" : "List of VPC subnet IDs for the cluster. Note: must match the passed AvailabilityZones.",
"Type" : "CommaDelimitedList"
},
"VpcId" : {
"Description" : "VPC associated with the provided subnets",
"Type" : "String"
},
"RegistryAuth" : {
"Description" : "Comma-delimited list of htpasswd strings that grant access to the registry (e.g., 'user1:$apr1$BspgNsum$8ZdGQ/KXRFccnlRXF6StP.,user2:$apr1$BspgNsum$8ZdGQ/KXRFccnlRXF6StP.')",
"Type": "CommaDelimitedList"
},
"AdminSecurityGroup" : {
"Description" : "Existing security group that should be granted administrative access (e.g., 'sg-123456')",
"Type" : "String"
},
"AvailabilityZones": {
"Description" : "(Optional) If passed, only launch Docker Registry nodes in these AZs (e.g., 'us-east-1a,us-east-1b'). Note: these must match the passed Subnets.",
"Type" : "CommaDelimitedList",
"Default" : ""
},
"HealthCheckGracePeriod": {
"Description": "The length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health.",
"Type": "String",
"Default": "500"
}
},
"Conditions" : {
"UseAllAvailabilityZones" : {"Fn::Equals" : [{ "Fn::Join" : ["", {"Ref" : "AvailabilityZones"} ]}, ""]}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : {
"AMI" : "ami-f40bbe9c"
},
"us-west-1" : {
"AMI" : "ami-cfe2ea8a"
},
"us-west-2" : {
"AMI" : "ami-3fb1f20f"
},
"eu-west-1" : {
"AMI" : "ami-e0d27397"
},
"ap-southeast-1" : {
"AMI" : "ami-8a7057d8"
},
"ap-southeast-2" : {
"AMI" : "ami-d5c1a2ef"
},
"ap-northeast-1" : {
"AMI" : "ami-a7def7a6"
},
"sa-east-1" : {
"AMI" : "ami-070fa51a"
}
}
},
"Resources" : {
"IAMUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Policies" : [{
"PolicyName" : "S3Access",
"PolicyDocument" : {
"Statement": [{
"Action" : "s3:*",
"Effect" : "Allow",
"Resource" : { "Fn::Join" : ["", ["arn:aws:s3:::", {"Ref" : "S3Bucket"} ]]}
},
{
"Action": "s3:*",
"Effect": "Allow",
"Resource" : { "Fn::Join" : ["", ["arn:aws:s3:::", {"Ref" : "S3Bucket"} , "/*"]]}
}]
}
},
{
"PolicyName" : "IAMAccess",
"PolicyDocument" : {
"Statement" : [{
"Effect" : "Allow",
"NotAction" : "iam:*",
"Resource" : "*"
}]
}
}]
}
},
"HostKeys" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : { "Ref" : "IAMUser" }
}
},
"ServerGroup" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"HealthCheckGracePeriod": { "Ref": "HealthCheckGracePeriod" },
"HealthCheckType": "ELB",
"AvailabilityZones" : {
"Fn::If" : [
"UseAllAvailabilityZones",
{ "Fn::GetAZs": "" },
{"Ref" : "AvailabilityZones"}
]
},
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MinSize" : "1",
"MaxSize" : "1",
"DesiredCapacity" : "1",
"LoadBalancerNames" : [ { "Ref" : "ElasticLoadBalancer" } ],
"VPCZoneIdentifier" : { "Ref" : "Subnets" }
}
},
"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config": {
"files" : {
"/opt/docker-registry/config.yaml" : {
"content" : { "Fn::Join" : ["\n", [
"prod:",
" loglevel: warn",
" storage: s3",
" s3_access_key: {{access_key}}",
" s3_secret_key: {{secret_key}}",
" s3_bucket: {{s3_bucket}}",
" boto_bucket: {{s3_bucket}}",
" storage_path: /srv/docker",
" secret_key: {{magic_string}}",
" standalone: true",
" disable_token_auth: true",
" search_backend: sqlalchemy",
" sqlalchemy_index_database: sqlite:////tmp/docker-registry.db",
""
]]},
"context" : {
"access_key" : { "Ref" : "HostKeys" },
"secret_key" : { "Fn::GetAtt" : ["HostKeys", "SecretAccessKey"]},
"s3_bucket" : { "Ref" : "S3Bucket"},
"magic_string": "fL3YHeP1cmCHH5FWm1PKaO7cdo0VXkabAgiSEestXYZDgAiQcDCsTiWpOaukB1e9"
},
"mode" : "000700",
"owner" : "root",
"group" : "root"
},
"/etc/nginx/sites-available/docker-registry" : {
"content" : { "Fn::Join" : ["\n", [
"upstream docker-registry {",
" server localhost:5000;",
"}",
"",
"server {",
" listen 80;",
"",
" proxy_set_header Host $http_host; # required for docker client's sake",
" proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP",
" ",
" client_max_body_size 0; # disable any limits to avoid HTTP 413 for large image uploads",
"",
" chunked_transfer_encoding on;",
" ",
" location / {",
" auth_basic \"Restricted\";",
" auth_basic_user_file /opt/docker-registry/htpasswd;",
" proxy_pass http://docker-registry;",
" }",
"",
" location /health {",
" proxy_pass http://docker-registry/v1/_ping;",
" }",
"}"
]]},
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/opt/docker-registry/htpasswd" : {
"content" : { "Fn::Join" : ["\n", { "Ref" : "RegistryAuth" }]},
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
}
}
}
},
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI"] },
"SecurityGroups" : [ { "Ref" : "ServerSecurityGroup" }, { "Ref" : "AdminSecurityGroup" } ],
"AssociatePublicIpAddress": "true",
"InstanceType" : { "Ref" : "InstanceType" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -ex\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r LaunchConfig ",
" --access-key ", { "Ref" : "HostKeys" },
" --secret-key ", { "Fn::GetAtt": ["HostKeys", "SecretAccessKey"] },
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"# make sure the package repository is up to date\n",
"apt-get update\n",
"# Set up nginx proxy\n",
"apt-get -y install nginx\n",
"rm -f /etc/nginx/sites-enabled/default\n",
"ln -s /etc/nginx/sites-available/docker-registry /etc/nginx/sites-enabled/docker-registry\n",
"service nginx restart\n",
"# Set up docker registry\n",
"docker pull ", { "Ref": "DockerImage" }, "\n",
"runit-service create docker-registry docker run",
" -p 5000:5000",
" -v /opt/docker-registry:/registry-conf",
" -e DOCKER_REGISTRY_CONFIG=/registry-conf/config.yaml",
" -e SETTINGS_FLAVOR=prod",
" ", { "Ref": "DockerImage" }, "|| error_exit 'Failed to launch Docker container'\n",
"runit-service enable docker-registry\n",
"# All is well so signal success\n",
"cfn-signal -e 0 -r \"Stack setup complete\" '", { "Ref" : "WaitHandle" }, "'\n",
"#EOF"
]]}}
}
},
"LbSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable Registry access",
"VpcId" : { "Ref" : "VpcId" },
"SecurityGroupIngress" : [
{ "IpProtocol" : "tcp", "FromPort" : "443", "ToPort" : "443", "CidrIp" : "0.0.0.0/0" }
]
}
},
"ServerSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH and Registry access",
"VpcId" : { "Ref" : "VpcId" },
"SecurityGroupIngress" : [
{ "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "SourceSecurityGroupId" : { "Ref" : "LbSecurityGroup"} }
]
}
},
"ElasticLoadBalancer" : {
"Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties" : {
"SecurityGroups": [{ "Ref": "LbSecurityGroup" }, { "Ref" : "AdminSecurityGroup" }],
"Subnets": { "Ref": "Subnets" },
"Listeners" : [ {
"LoadBalancerPort" : "443",
"Protocol" : "HTTPS",
"InstancePort" : "80",
"InstanceProtocol" : "HTTP",
"SSLCertificateId" : { "Ref" : "SslCertificate" }
} ],
"HealthCheck" : {
"Target" : "HTTP:80/health",
"HealthyThreshold" : "3",
"UnhealthyThreshold" : "5",
"Interval" : "30",
"Timeout" : "5"
}
}
},
"DnsRecord" : {
"Type" : "AWS::Route53::RecordSet",
"Properties" : {
"HostedZoneName" : { "Fn::Join" : [ "", [{"Ref" : "DnsZone"}, "." ]]},
"Comment" : "Docker Registry",
"Name" : { "Fn::Join" : [ "", [{"Ref" : "DnsPrefix"}, ".", {"Ref" : "DnsZone"}, "."]]},
"Type" : "A",
"AliasTarget" : {
"HostedZoneId" : { "Fn::GetAtt" : [ "ElasticLoadBalancer", "CanonicalHostedZoneNameID" ]},
"DNSName" : { "Fn::GetAtt" : [ "ElasticLoadBalancer", "CanonicalHostedZoneName" ]}
}
}
},
"WaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
}
},
"Outputs" : {
"DnsAddress" : {
"Description" : "Docker Registry",
"Value" : { "Ref" : "DnsRecord" }
}
}
}