forked from CircleCI-Archived/enterprise-setup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcircleci.tf
544 lines (449 loc) · 14.6 KB
/
circleci.tf
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# Configure the AWS Provider
# AWS Specific configuration
variable "aws_access_key" {
description = "Access key used to create instances"
}
variable "aws_secret_key" {
description = "Secret key used to create instances"
}
variable "aws_region" {
description = "Region where instances get created"
}
variable "aws_vpc_id" {
description = "The VPC ID where the instances should reside"
}
variable "aws_subnet_id" {
description = "The subnet-id to be used for the instance"
}
variable "aws_ssh_key_name" {
description = "The SSH key to be used for the instances"
}
variable "circle_secret_passphrase" {
description = "Decryption key for secrets used by CircleCI machines"
}
variable "services_ami" {
description = "Override AMI lookup with provided AMI ID"
default = ""
}
variable "services_instance_type" {
description = "instance type for the centralized services box. We recommend a c4 instance"
default = "c4.2xlarge"
}
variable "builder_instance_type" {
description = "instance type for the builder machines. We recommend a r3 instance"
default = "r3.2xlarge"
}
variable "max_builders_count" {
description = "max number of builders"
default = "2"
}
variable "prefix" {
description = "prefix for resource names"
default = "circleci"
}
variable "enable_ansible_provisioning" {
description = "Enables Ansible provisioning of the Services box for automatic / no-touch installation / configuration."
default = 0
}
variable "ansible_extra_vars" {
type = "map"
default = {}
}
variable "services_delete_on_termination" {
description = "Configures AWS to delete the ELB volume for the Services box upon instance termination."
default = "false"
}
variable "enable_route" {
description = "enable creating a Route53 route for the Services box"
default = 0
}
variable "route_name" {
description = "Route name to configure for Services box"
default = ""
}
variable "route_zone_id" {
description = "Zone to configure route in"
default = ""
}
variable "http_proxy" {
description = ""
default = ""
}
variable "https_proxy" {
description = ""
default = ""
}
variable "no_proxy" {
description = ""
default = ""
}
data "aws_subnet" "subnet" {
id = "${var.aws_subnet_id}"
}
data "template_file" "services_user_data" {
template = "${file("templates/services_user_data.tpl")}"
vars {
circle_secret_passphrase = "${var.circle_secret_passphrase}"
sqs_queue_url = "${aws_sqs_queue.shutdown_queue.id}"
s3_bucket = "${aws_s3_bucket.circleci_bucket.id}"
aws_region = "${var.aws_region}"
subnet_id = "${var.aws_subnet_id}"
vm_sg_id = "${aws_security_group.circleci_vm_sg.id}"
http_proxy = "${var.http_proxy}"
https_proxy = "${var.https_proxy}"
no_proxy = "${var.no_proxy}"
}
}
data "template_file" "builders_user_data" {
template = "${file("templates/builders_user_data.tpl")}"
vars {
services_private_ip = "${aws_instance.services.private_ip}"
circle_secret_passphrase = "${var.circle_secret_passphrase}"
http_proxy = "${var.http_proxy}"
https_proxy = "${var.https_proxy}"
no_proxy = "${var.no_proxy}"
}
}
data "template_file" "circleci_policy" {
template = "${file("templates/circleci_policy.tpl")}"
vars {
bucket_arn = "${aws_s3_bucket.circleci_bucket.arn}"
sqs_queue_arn = "${aws_sqs_queue.shutdown_queue.arn}"
role_name = "${aws_iam_role.circleci_role.name}"
aws_region = "${var.aws_region}"
}
}
data "template_file" "shutdown_queue_role_policy" {
template = "${file("templates/shutdown_queue_role_policy.tpl")}"
vars {
sqs_queue_arn = "${aws_sqs_queue.shutdown_queue.arn}"
}
}
data "template_file" "output" {
template = "${file("templates/output.tpl")}"
vars {
services_public_ip = "${aws_instance.services.public_ip}"
ssh_key = "${var.aws_ssh_key_name}"
ansible = "${var.enable_ansible_provisioning}"
nomad = "${var.enable_nomad}"
hostname = "${lookup(var.ansible_extra_vars, "services_hostname", "")}"
}
}
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.aws_region}"
}
# SQS queue for hook
resource "aws_sqs_queue" "shutdown_queue" {
name = "${var.prefix}_queue"
}
# IAM for shutdown queue
resource "aws_iam_role" "shutdown_queue_role" {
name = "${var.prefix}_shutdown_queue_role"
assume_role_policy = "${file("files/shutdown_queue_role.json")}"
}
resource "aws_iam_role_policy" "shutdown_queue_role_policy" {
name = "${var.prefix}_shutdown_queue_role"
role = "${aws_iam_role.shutdown_queue_role.id}"
policy = "${data.template_file.shutdown_queue_role_policy.rendered}"
}
# Single general-purpose bucket
resource "aws_s3_bucket" "circleci_bucket" {
# VPC ID is used here to make bucket name globally unique(ish) while
# uuid/ignore_changes have some lingering issues
bucket = "${replace(var.prefix, "_", "-")}-bucket-${replace(var.aws_vpc_id, "vpc-", "")}"
cors_rule {
allowed_methods = ["GET"]
allowed_origins = ["*"]
max_age_seconds = 3600
}
}
## IAM for instances
resource "aws_iam_role" "circleci_role" {
name = "${var.prefix}_role"
path = "/"
assume_role_policy = "${file("files/circleci_role.json")}"
}
resource "aws_iam_role_policy" "circleci_policy" {
name = "${var.prefix}_policy"
role = "${aws_iam_role.circleci_role.id}"
policy = "${data.template_file.circleci_policy.rendered}"
}
resource "aws_iam_instance_profile" "circleci_profile" {
name = "${var.prefix}_profile"
role = "${aws_iam_role.circleci_role.name}"
}
## Configure the services machine
resource "aws_security_group" "circleci_builders_sg" {
name = "${var.prefix}_builders_sg"
description = "SG for CircleCI Builder instances"
vpc_id = "${var.aws_vpc_id}"
ingress {
self = true
from_port = 0
to_port = 0
protocol = "-1"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "circleci_services_sg" {
name = "${var.prefix}_services_sg"
description = "SG for CircleCI services/database instances"
vpc_id = "${var.aws_vpc_id}"
ingress {
security_groups = ["${aws_security_group.circleci_builders_sg.id}"]
protocol = "-1"
from_port = 0
to_port = 0
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# If using github.com (not GitHub Enterprise) whitelist GitHub cidr block
# https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/
#
#ingress {
# security_groups = ["192.30.252.0/22"]
# protocol = "tcp"
# from_protocol = 443
# to_protocol = 443
#}
#ingress {
# security_groups = ["192.30.252.0/22"]
# protocol = "tcp"
# from_protocol = 80
# to_protocol = 80
#}
}
resource "aws_security_group" "circleci_builders_admin_sg" {
name = "${var.prefix}_builders_admin_sg"
description = "SG for services to masters communication - avoids circular dependency"
vpc_id = "${var.aws_vpc_id}"
ingress {
security_groups = ["${aws_security_group.circleci_services_sg.id}"]
protocol = "tcp"
from_port = 443
to_port = 443
}
}
#
# This should be configured by admins to restrict access to machines
# TODO: Make this more extensible
#
resource "aws_security_group" "circleci_users_sg" {
name = "${var.prefix}_users_sg"
description = "SG representing users of CircleCI Enterprise"
vpc_id = "${var.aws_vpc_id}"
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 22
to_port = 22
}
# For Web traffic to services
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 80
to_port = 80
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 443
to_port = 443
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 8800
to_port = 8800
}
# For Nomad server in 2.0 clustered installation
ingress {
cidr_blocks = ["${data.aws_subnet.subnet.cidr_block}"]
protocol = "tcp"
from_port = 4647
to_port = 4647
}
# For output-processor in 2.0 clustered installation
ingress {
cidr_blocks = ["${data.aws_subnet.subnet.cidr_block}"]
protocol = "tcp"
from_port = 8585
to_port = 8585
}
# For build-agent to talk to vm-service
ingress {
cidr_blocks = ["${data.aws_subnet.subnet.cidr_block}"]
protocol = "tcp"
from_port = 3001
to_port = 3001
}
# For SSH traffic to builder boxes
# TODO: Update once services box has ngrok
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 64535
to_port = 65535
}
}
resource "aws_security_group" "circleci_vm_sg" {
name = "${var.prefix}_vm_sg"
description = "SG form VMs allocated by CircleCI for Remote Docker and machine executor"
vpc_id = "${var.aws_vpc_id}"
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 22
to_port = 22
}
# For Web traffic to services
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 2376
to_port = 2376
}
# For SSHing into 2.0 build
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 54782
to_port = 54782
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
variable "ubuntu_ami" {
default = {
ap-northeast-1 = "ami-0a16e26c"
ap-northeast-2 = "ami-ed6fb783"
ap-southeast-1 = "ami-5929b23a"
ap-southeast-2 = "ami-40180023"
eu-central-1 = "ami-488e2727"
eu-west-1 = "ami-a142b2d8"
sa-east-1 = "ami-ec1b6a80"
us-east-1 = "ami-845367ff"
us-east-2 = "ami-1680a373"
us-west-1 = "ami-5185ae31"
us-west-2 = "ami-103fdc68"
}
}
resource "aws_instance" "services" {
# Instance type - any of the c4 should do for now
instance_type = "${var.services_instance_type}"
ami = "${var.services_ami != "" ? var.services_ami : lookup(var.ubuntu_ami, var.aws_region)}"
key_name = "${var.aws_ssh_key_name}"
subnet_id = "${var.aws_subnet_id}"
associate_public_ip_address = true
disable_api_termination = true
iam_instance_profile = "${aws_iam_instance_profile.circleci_profile.name}"
vpc_security_group_ids = [
"${aws_security_group.circleci_services_sg.id}",
"${aws_security_group.circleci_users_sg.id}",
]
tags {
Name = "${var.prefix}_services"
}
root_block_device {
volume_type = "gp2"
volume_size = "150"
delete_on_termination = "${var.services_delete_on_termination}"
}
user_data = "${ var.enable_ansible_provisioning ? "sudo apt-get update" : data.template_file.services_user_data.rendered }"
provisioner "local-exec" {
command = "${ var.enable_ansible_provisioning ? "make ansible-setup" : "echo skipped" }"
on_failure = "continue"
}
provisioner "local-exec" {
command = "${ var.enable_ansible_provisioning ? "echo '\n[services]\n${aws_instance.services.public_ip} ansible_user=ubuntu ansible_ssh_common_args=\"-o ConnectionAttempts=30 -o StrictHostKeyChecking=no\"' > .ansible/hosts" : "echo skipped" }"
}
provisioner "local-exec" {
command = "${ var.enable_ansible_provisioning ? "echo '${jsonencode(merge(var.ansible_extra_vars, map("services_ip",aws_instance.services.private_ip,"secret_passphrase",var.circle_secret_passphrase,"aws_region",var.aws_region,"s3_bucket",aws_s3_bucket.circleci_bucket.id,"sqs_queue_url",aws_sqs_queue.shutdown_queue.id,"circle_version_2",var.enable_nomad)))}' > .ansible/extra_vars.json" : "echo skipped" }"
}
provisioner "local-exec" {
command = "${ var.enable_ansible_provisioning ? "ansible-playbook playbook.yml -v -i ./.ansible/hosts -e \"@./.ansible/extra_vars.json\"" : "echo skipped" }"
}
lifecycle {
prevent_destroy = false
}
}
resource "aws_route53_record" "services_route" {
count = "${var.enable_route}"
zone_id = "${var.route_zone_id}"
name = "${var.route_name}"
type = "A"
ttl = "300"
records = ["${aws_instance.services.public_ip}"]
}
## Builders ASG
resource "aws_launch_configuration" "builder_lc" {
# 4x or 8x are best
instance_type = "${var.builder_instance_type}"
image_id = "${lookup(var.ubuntu_ami, var.aws_region)}"
key_name = "${var.aws_ssh_key_name}"
iam_instance_profile = "${aws_iam_instance_profile.circleci_profile.name}"
security_groups = ["${aws_security_group.circleci_builders_sg.id}",
"${aws_security_group.circleci_builders_admin_sg.id}",
"${aws_security_group.circleci_users_sg.id}",
]
root_block_device {
volume_type = "gp2"
volume_size = "150"
delete_on_termination = "${var.services_delete_on_termination}"
}
user_data = "${data.template_file.builders_user_data.rendered}"
# To enable using spots
# spot_price = "1.00"
# Can't delete an LC until the replacement is applied
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "builder_asg" {
name = "${var.prefix}_builders_asg"
vpc_zone_identifier = ["${var.aws_subnet_id}"]
launch_configuration = "${aws_launch_configuration.builder_lc.name}"
max_size = "${var.max_builders_count}"
min_size = 0
desired_capacity = 1
force_delete = true
tag {
key = "Name"
value = "${var.prefix}_builder"
propagate_at_launch = "true"
}
}
# Shutdown hooks
resource "aws_autoscaling_lifecycle_hook" "builder_shutdown_hook" {
name = "builder_shutdown_hook"
autoscaling_group_name = "${aws_autoscaling_group.builder_asg.name}"
heartbeat_timeout = 3600
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
notification_target_arn = "${aws_sqs_queue.shutdown_queue.arn}"
role_arn = "${aws_iam_role.shutdown_queue_role.arn}"
}
output "success_message" {
value = "${data.template_file.output.rendered}"
}
output "install_url" {
value = "http://${aws_instance.services.public_ip}/"
}
output "ssh-services" {
value = "ssh ubuntu@${aws_instance.services.public_ip}"
}