Skip to content

Commit 68732c6

Browse files
authored
2.0.0 Improved local vs remote docker, added terraform testing env (#9)
* Started adding remote and local docker testing in DoodContainerTest.groovy * Minor bugfixes Added terraform script for Docker env * Started adding README.md * Terraform Output tweak * Consolidated container constructors in to one Container now longer has file containerNetworkName and does not connect to that network during startup Started updating BitbucketH2Deployment to support the single container constructors * HarborDeploymentTest.groovy * Tweaks to allow better testing of local and remote docker engine DevStackSpec.groovy * Fixed bugs in cleanup script * main.tf * Updated to setup a larger (16GB) root drive on EC2 ubuntu_user_data.sh * Updated to configure sshd not to timeout as quickly. * HarborManagerContainer.groovy * Minor tweaks to script Minor tweaks to other stuff * Tweaks to JenkinsAndHarborDeployment.groovy * JenkinsAndHarborDeployment.groovy * Fixes to make script wait for jenkins startup JenkinsDeployment.groovy * Now waits for WEB ui to startup * Fixed bug related to port
1 parent 56fea74 commit 68732c6

35 files changed

Lines changed: 1162 additions & 446 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
/target/
22
/.idea/vcs.xml
3+
/Environments/Terraform/.terraform/
4+
/Environments/Terraform/.terraform.lock.hcl
5+
/Environments/Terraform/.terraform.tfstate.lock.info
6+
/Environments/Terraform/terraform.tfstate
7+
/Environments/Terraform/terraform.tfstate.backup

Environments/Terraform/iam.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
resource "aws_iam_role" "default_instance_role" {
3+
name = "${var.tags.useCase}-${var.tags.owner}-instance-role"
4+
5+
# Terraform's "jsonencode" function converts a
6+
# Terraform expression result to valid JSON syntax.
7+
assume_role_policy = jsonencode({
8+
Version = "2012-10-17"
9+
Statement = [
10+
{
11+
Action = "sts:AssumeRole"
12+
Effect = "Allow"
13+
Sid = ""
14+
Principal = {
15+
Service = "ec2.amazonaws.com"
16+
}
17+
}
18+
]
19+
})
20+
21+
}
22+
23+
24+
resource "aws_iam_instance_profile" "default_profile" {
25+
26+
name = "${var.tags.useCase}-${var.tags.owner}-instance-profile"
27+
role = aws_iam_role.default_instance_role.name
28+
29+
}
30+
31+

Environments/Terraform/main.tf

Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "4.34.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
12+
region = "eu-central-1"
13+
access_key = var.aws_credentials.access_key
14+
secret_key = var.aws_credentials.secret_key
15+
allowed_account_ids = [var.aws_credentials.account]
16+
17+
skip_get_ec2_platforms = true
18+
skip_metadata_api_check = true
19+
skip_region_validation = true
20+
skip_credentials_validation = true
21+
22+
default_tags {
23+
tags = var.tags
24+
}
25+
26+
}
27+
28+
29+
resource "aws_key_pair" "ec2-ssh-key" {
30+
31+
key_name = "${var.tags.useCase}-${var.tags.owner}-key"
32+
public_key = file(var.ssh-public-key-local-path)
33+
}
34+
35+
/*
36+
Get the NIC of the LB in the public net
37+
*/
38+
data "aws_network_interface" "lb_nic" {
39+
40+
41+
filter {
42+
name = "description"
43+
values = ["ELB ${aws_lb.load-balancer.arn_suffix}"]
44+
}
45+
46+
47+
48+
filter {
49+
name = "subnet-id"
50+
values = [aws_subnet.base-stack-public-subnet.id]
51+
}
52+
53+
}
54+
55+
56+
data "aws_region" "current" {}
57+
58+
data "aws_availability_zones" "available" {
59+
state = "available"
60+
}
61+
62+
63+
//Get the latest AmazoneLinux2 AMI
64+
data "aws_ami" "latest_amazon_linux_2" {
65+
most_recent = true
66+
filter {
67+
name = "name"
68+
values = ["*amzn2-ami-hvm*"]
69+
}
70+
filter {
71+
name = "virtualization-type"
72+
values = ["hvm"]
73+
}
74+
filter {
75+
name = "architecture"
76+
values = ["x86_64"]
77+
}
78+
owners = ["amazon"]
79+
}
80+
81+
//Get the latest Ubuntu 22.04 AMI
82+
data "aws_ami" "ubuntuAMI" {
83+
most_recent = true
84+
85+
filter {
86+
name = "name"
87+
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
88+
}
89+
90+
filter {
91+
name = "virtualization-type"
92+
values = ["hvm"]
93+
}
94+
95+
owners = ["099720109477"] # Canonical
96+
}
97+
98+
resource "aws_vpc" "base-stack" {
99+
100+
cidr_block = "10.0.0.0/16"
101+
enable_dns_hostnames = true
102+
enable_dns_support = true
103+
104+
tags = {
105+
Name = "${var.tags.useCase}-${var.tags.owner}-vpc"
106+
}
107+
108+
109+
}
110+
111+
resource "aws_internet_gateway" "base-stack" {
112+
113+
vpc_id = aws_vpc.base-stack.id
114+
115+
116+
tags = {
117+
Name = "${var.tags.useCase}-${var.tags.owner}-gw"
118+
}
119+
120+
}
121+
resource "aws_eip" "nat_eip" {
122+
123+
vpc = true
124+
depends_on = [aws_internet_gateway.base-stack]
125+
tags = {
126+
Name = "${var.tags.useCase}-${var.tags.owner}-eip"
127+
}
128+
129+
}
130+
131+
132+
resource "aws_subnet" "base-stack-private-subnet" {
133+
134+
135+
tags = {
136+
Name = "${var.tags.useCase}-${var.tags.owner}-private-subnet"
137+
}
138+
139+
availability_zone = data.aws_availability_zones.available.names[0]
140+
cidr_block = "10.0.66.0/24"
141+
vpc_id = aws_vpc.base-stack.id
142+
map_public_ip_on_launch = false
143+
144+
145+
}
146+
147+
resource "aws_subnet" "base-stack-public-subnet" {
148+
149+
150+
tags = {
151+
Name = "${var.tags.useCase}-${var.tags.owner}-public-subnet"
152+
}
153+
154+
availability_zone = data.aws_availability_zones.available.names[0]
155+
cidr_block = "10.0.99.0/24"
156+
vpc_id = aws_vpc.base-stack.id
157+
158+
159+
}
160+
161+
resource "aws_nat_gateway" "outbound-nat" {
162+
163+
tags = {
164+
Name = "${var.tags.useCase}-${var.tags.owner}-outbount-nat"
165+
}
166+
allocation_id = aws_eip.nat_eip.id
167+
subnet_id = aws_subnet.base-stack-public-subnet.id
168+
169+
170+
}
171+
resource "aws_route_table" "private-route-tb" {
172+
173+
vpc_id = aws_vpc.base-stack.id
174+
175+
tags = {
176+
Name = "${var.tags.useCase}-${var.tags.owner}-private-routetable"
177+
}
178+
179+
}
180+
181+
resource "aws_route_table" "public-route-tb" {
182+
183+
vpc_id = aws_vpc.base-stack.id
184+
185+
tags = {
186+
Name = "${var.tags.useCase}-${var.tags.owner}-private-routetable"
187+
}
188+
189+
}
190+
191+
resource "aws_route" "route-to-world" {
192+
193+
route_table_id = aws_route_table.public-route-tb.id
194+
destination_cidr_block = "0.0.0.0/0"
195+
gateway_id = aws_internet_gateway.base-stack.id
196+
197+
}
198+
199+
resource "aws_route" "route-to-nat" {
200+
201+
route_table_id = aws_route_table.private-route-tb.id
202+
destination_cidr_block = "0.0.0.0/0"
203+
gateway_id = aws_nat_gateway.outbound-nat.id
204+
205+
}
206+
207+
resource "aws_route_table_association" "route-public" {
208+
209+
subnet_id = aws_subnet.base-stack-public-subnet.id
210+
route_table_id = aws_route_table.public-route-tb.id
211+
212+
213+
}
214+
215+
resource "aws_route_table_association" "route-private" {
216+
217+
subnet_id = aws_subnet.base-stack-private-subnet.id
218+
route_table_id = aws_route_table.private-route-tb.id
219+
220+
221+
}
222+
223+
224+
225+
resource "aws_security_group_rule" "ingress_rules" {
226+
count = length(var.ingress_rules_from_trusted)
227+
228+
type = "ingress"
229+
from_port = var.ingress_rules_from_trusted[count.index].port
230+
to_port = var.ingress_rules_from_trusted[count.index].port
231+
protocol = upper(var.ingress_rules_from_trusted[count.index].protocol)
232+
cidr_blocks = setunion(var.trusted-external-ips, ["${data.aws_network_interface.lb_nic.private_ip}/32"])
233+
description = var.ingress_rules_from_trusted[count.index].description
234+
security_group_id = aws_security_group.private-subnet-sg.id
235+
}
236+
237+
238+
resource "aws_security_group" "private-subnet-sg" {
239+
240+
name = "${var.tags.useCase}-${var.tags.owner}-sg"
241+
vpc_id = aws_vpc.base-stack.id
242+
243+
244+
egress {
245+
from_port = 0
246+
to_port = 0
247+
protocol = "-1"
248+
cidr_blocks = ["0.0.0.0/0"]
249+
}
250+
251+
}
252+
253+
254+
255+
resource "aws_instance" "ec2-node" {
256+
257+
tags = {
258+
Name = "${var.tags.useCase}-${var.tags.owner}-ec2"
259+
}
260+
261+
ami = data.aws_ami.ubuntuAMI.id
262+
instance_type = "t3.xlarge"
263+
subnet_id = aws_subnet.base-stack-private-subnet.id
264+
key_name = aws_key_pair.ec2-ssh-key.key_name
265+
vpc_security_group_ids = [aws_security_group.private-subnet-sg.id]
266+
availability_zone = data.aws_availability_zones.available.names[0]
267+
iam_instance_profile = aws_iam_instance_profile.default_profile.name
268+
269+
root_block_device {
270+
volume_size = 24
271+
tags = var.tags
272+
}
273+
274+
user_data = templatefile("ubuntu_user_data.sh", {
275+
awsRegion : data.aws_region.current.name
276+
tlscacert : file(var.dockerServerCert.tlscacert)
277+
tlscert : file(var.dockerServerCert.tlscert)
278+
tlskey : file(var.dockerServerCert.tlskey)
279+
}
280+
)
281+
282+
283+
}
284+
285+
286+
287+
resource "aws_lb" "load-balancer" {
288+
289+
name = "${var.tags.useCase}-${var.tags.owner}-lb"
290+
internal = false
291+
load_balancer_type = "network"
292+
subnets = [aws_subnet.base-stack-public-subnet.id]
293+
294+
295+
}
296+
297+
resource "aws_lb_listener" "lb-listener" {
298+
count = length(var.ingress_rules_from_trusted)
299+
load_balancer_arn = aws_lb.load-balancer.arn
300+
port = var.ingress_rules_from_trusted[count.index].port
301+
protocol = upper(var.ingress_rules_from_trusted[count.index].protocol)
302+
303+
304+
default_action {
305+
type = "forward"
306+
target_group_arn = aws_lb_target_group.target-group[count.index].arn
307+
}
308+
309+
}
310+
311+
resource "aws_lb_target_group" "target-group" {
312+
count = length(var.ingress_rules_from_trusted)
313+
314+
name = "${var.tags.useCase}-${var.tags.owner}-port-${var.ingress_rules_from_trusted[count.index].port}"
315+
port = var.ingress_rules_from_trusted[count.index].port
316+
protocol = upper(var.ingress_rules_from_trusted[count.index].protocol)
317+
target_type = "instance"
318+
vpc_id = aws_vpc.base-stack.id
319+
320+
321+
}
322+
323+
324+
resource "aws_lb_target_group_attachment" "ssh-target-hosts" {
325+
326+
count = length(var.ingress_rules_from_trusted)
327+
328+
target_group_arn = aws_lb_target_group.target-group[count.index].arn
329+
target_id = aws_instance.ec2-node.id
330+
port = var.ingress_rules_from_trusted[count.index].port
331+
332+
}
333+
334+
335+
output "SSH-TO-Node" {
336+
value = "ssh -v ${var.ec2-username}@${aws_lb.load-balancer.dns_name} -p 22 -o StrictHostKeyChecking=no"
337+
}
338+
339+
output "Hosts-record" {
340+
value = "${data.aws_network_interface.lb_nic.association[0].public_ip} jira.test.com docker.domain.se bitbucket.domain.se jira.domain.se jira2.domain.se bitbucket2.domain.se jira.auga.se bitbucket.auga.se jenkins.domain.se harbor.domain.se jenkins-agent.domain.se"
341+
342+
}
343+

0 commit comments

Comments
 (0)