Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit 51a64e1

Browse files
author
Denis Brækhus
committed
Fix proper hostsfile creation
This version of the hostsfile creation is non racy and works.
1 parent 4d51eb6 commit 51a64e1

File tree

4 files changed

+221
-62
lines changed

4 files changed

+221
-62
lines changed

deployment/terraform/aws-ec2/main.tf

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,107 @@ provider "aws" {
55
region = "${var.aws_ec2_region}"
66
}
77

8-
resource "aws_key_pair" "apiperftestkey" {
9-
key_name = "apiperftestkey"
8+
resource "aws_key_pair" "apiperf-sshkey" {
9+
key_name = "apiperf-sshkey"
1010
public_key = "${var.sshkey}"
1111
}
1212

13-
resource "aws_instance" "apiperf" {
14-
count = "${var.count}"
13+
resource "aws_instance" "apiperf-gateway" {
1514
ami = "${var.aws_ec2_ami}"
16-
instance_type = "${lookup(var.instance_types, count.index)}"
17-
key_name = "apiperftestkey"
15+
instance_type = "${lookup(var.instance_types, "gateway")}"
16+
key_name = "apiperf-sshkey"
1817
user_data = "${file("aws-ec2/userdata.sh")}"
1918
tags {
20-
name = "${lookup(var.instance_names, count.index)}"
19+
name = "${lookup(var.instance_names, "gateway")}"
2120
}
2221
provisioner "local-exec" {
23-
command = "echo ${self.private_ip} ${lookup(var.instance_names, count.index)} > hosts/${lookup(var.instance_names, count.index)}"
24-
}
25-
# provisioner "file" {
26-
# connection {
27-
# user = "centos"
28-
# }
29-
# source = "hosts"
30-
# destination = "/tmp"
31-
# }
32-
# provisioner "remote-exec" {
33-
# connection {
34-
# user = "centos"
35-
# }
36-
# inline = [
37-
# "sudo cp /etc/hosts /tmp/newhostsfile",
38-
# "sudo chown centos /tmp/newhostsfile",
39-
# "cat /tmp/hosts/* >> /tmp/newhostsfile",
40-
# "sudo cp /tmp/newhostsfile /etc/hosts",
41-
# "sudo chown root /etc/hosts"
42-
# ]
43-
# }
22+
command = "echo ${self.private_ip} ${lookup(var.instance_names, "gateway")} > hosts/${lookup(var.instance_names, "gateway")}.host"
23+
}
4424
}
25+
26+
resource "aws_instance" "apiperf-webserver" {
27+
ami = "${var.aws_ec2_ami}"
28+
instance_type = "${lookup(var.instance_types, "webserver")}"
29+
key_name = "apiperf-sshkey"
30+
user_data = "${file("aws-ec2/userdata.sh")}"
31+
tags {
32+
name = "${lookup(var.instance_names, "webserver")}"
33+
}
34+
provisioner "local-exec" {
35+
command = "echo ${self.private_ip} ${lookup(var.instance_names, "webserver")} > hosts/${lookup(var.instance_names, "webserver")}.host"
36+
}
37+
}
38+
39+
resource "aws_instance" "apiperf-consumer" {
40+
ami = "${var.aws_ec2_ami}"
41+
instance_type = "${lookup(var.instance_types, "consumer")}"
42+
key_name = "apiperf-sshkey"
43+
user_data = "${file("aws-ec2/userdata.sh")}"
44+
tags {
45+
name = "${lookup(var.instance_names, "consumer")}"
46+
}
47+
provisioner "local-exec" {
48+
command = "echo ${self.private_ip} ${lookup(var.instance_names, "consumer")} > hosts/${lookup(var.instance_names, "consumer")}.host"
49+
}
50+
}
51+
52+
resource "null_resource" "hostsfile" {
53+
triggers {
54+
aws_instance_consumer = "${aws_instance.apiperf-consumer.private_ip}"
55+
aws_instance_webserver = "${aws_instance.apiperf-webserver.private_ip}"
56+
aws_instance_gateway = "${aws_instance.apiperf-gateway.private_ip}"
57+
}
58+
connection {
59+
user = "centos"
60+
}
61+
provisioner "file" {
62+
connection {
63+
host = "${aws_instance.apiperf-consumer.public_ip}"
64+
}
65+
source = "hosts"
66+
destination = "/tmp"
67+
}
68+
provisioner "remote-exec" {
69+
connection {
70+
host = "${aws_instance.apiperf-consumer.public_ip}"
71+
}
72+
inline = [
73+
"sudo sh -c 'cat /tmp/hosts/*.host >> /etc/hosts'",
74+
]
75+
}
76+
connection {
77+
user = "centos"
78+
host = "${aws_instance.apiperf-gateway.public_ip}"
79+
}
80+
provisioner "file" {
81+
connection {
82+
host = "${aws_instance.apiperf-gateway.public_ip}"
83+
}
84+
source = "hosts"
85+
destination = "/tmp"
86+
}
87+
provisioner "remote-exec" {
88+
connection {
89+
host = "${aws_instance.apiperf-gateway.public_ip}"
90+
}
91+
inline = [
92+
"sudo sh -c 'cat /tmp/hosts/*.host >> /etc/hosts'",
93+
]
94+
}
95+
provisioner "file" {
96+
connection {
97+
host = "${aws_instance.apiperf-webserver.public_ip}"
98+
}
99+
source = "hosts"
100+
destination = "/tmp"
101+
}
102+
provisioner "remote-exec" {
103+
connection {
104+
host = "${aws_instance.apiperf-webserver.public_ip}"
105+
}
106+
inline = [
107+
"sudo sh -c 'cat /tmp/hosts/*.host >> /etc/hosts'",
108+
]
109+
}
110+
}
111+
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
variable "aws_access_key" {
22
description = "Valid Amazon AWS access key"
33
}
4+
45
variable "aws_secret_key" {
56
description = "Valid Amazon AWS secret key"
67
}
8+
79
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html
810
variable "aws_ec2_region" {
911
description = "Amazon EC2 region to use"
1012
default = "us-east-1"
1113
}
14+
1215
# https://aws.amazon.com/marketplace/pp/B00O7WM7QW
1316
variable "aws_ec2_ami" {
1417
description = "The EC2 AMI to use"
1518
default = "ami-61bbf104"
1619
}
20+
1721
variable "sshkey" {
1822
description = "SSH key to use for provisioning"
1923
}
20-
variable "count" {
21-
description = "Number of instances to create"
22-
default = "1"
23-
}
2424

2525
variable "instance_names" {
2626
description = "Base names to use for instances"
2727
default = {
28-
"0" = "gateway"
29-
"1" = "webserver"
30-
"2" = "consumer"
28+
"gateway" = "gateway"
29+
"webserver" = "webserver"
30+
"consumer" = "consumer"
3131
}
3232
}
3333

3434
# https://aws.amazon.com/ec2/instance-types/
3535
variable "instance_types" {
3636
description = "Instance types to use"
3737
default = {
38-
"0" = "m3.medium"
39-
"1" = "m3.medium"
40-
"2" = "m3.medium"
38+
"gateway" = "m3.medium"
39+
"webserver" = "m3.medium"
40+
"consumer" = "m3.medium"
4141
}
4242
}

deployment/terraform/digitalocean/main.tf

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,111 @@ provider "digitalocean" {
33
token = "${var.do_apikey}"
44
}
55

6-
resource "digitalocean_ssh_key" "apigwperftest" {
6+
resource "digitalocean_ssh_key" "apiperf-sshkey" {
77
name = "apigwperftest"
88
public_key = "${var.sshkey}"
99
}
1010

11-
resource "digitalocean_droplet" "apiperf" {
12-
count = "${var.count}"
13-
image = "centos-7-0-x64"
14-
private_networking = "True"
15-
name = "apiperf-${lookup(var.droplet_names, count.index)}"
16-
size = "${lookup(var.droplet_sizes, count.index)}"
17-
region = "ams3"
18-
ssh_keys = [ "${digitalocean_ssh_key.apigwperftest.id}" ]
11+
resource "digitalocean_droplet" "apiperf-webserver" {
12+
image = "${var.droplet_image}"
13+
private_networking = "${var.droplet_use_privatenet}"
14+
name = "apiperf-${lookup(var.droplet_names, "webserver")}"
15+
size = "${lookup(var.droplet_sizes, "webserver")}"
16+
region = "${var.droplet_region}"
17+
ssh_keys = [ "${digitalocean_ssh_key.apiperf-sshkey.id}" ]
18+
provisioner "local-exec" {
19+
command = "echo ${self.ipv4_address} ${lookup(var.droplet_names, "webserver")} > hosts/${lookup(var.droplet_names, "webserver")}"
20+
}
21+
provisioner "remote-exec" {
22+
inline = [
23+
"service firewalld stop"
24+
]
25+
}
26+
}
27+
28+
resource "digitalocean_droplet" "apiperf-gateway" {
29+
image = "${var.droplet_image}"
30+
private_networking = "${var.droplet_use_privatenet}"
31+
name = "apiperf-${lookup(var.droplet_names, "gateway")}"
32+
size = "${lookup(var.droplet_sizes, "gateway")}"
33+
region = "${var.droplet_region}"
34+
ssh_keys = [ "${digitalocean_ssh_key.apiperf-sshkey.id}" ]
35+
provisioner "local-exec" {
36+
command = "echo ${self.ipv4_address} ${lookup(var.droplet_names, "gateway")} > hosts/${lookup(var.droplet_names, "gateway")}"
37+
}
38+
provisioner "remote-exec" {
39+
inline = [
40+
"service firewalld stop"
41+
]
42+
}
43+
}
1944

45+
resource "digitalocean_droplet" "apiperf-consumer" {
46+
image = "${var.droplet_image}"
47+
private_networking = "${var.droplet_use_privatenet}"
48+
name = "apiperf-${lookup(var.droplet_names, "consumer")}"
49+
size = "${lookup(var.droplet_sizes, "consumer")}"
50+
region = "${var.droplet_region}"
51+
ssh_keys = [ "${digitalocean_ssh_key.apiperf-sshkey.id}" ]
2052
provisioner "local-exec" {
21-
command = "echo ${self.ipv4_address} ${lookup(var.droplet_names, count.index)} > hosts/${lookup(var.droplet_names, count.index)}"
53+
command = "echo ${self.ipv4_address} ${lookup(var.droplet_names, "consumer")} > hosts/${lookup(var.droplet_names, "consumer")}"
2254
}
23-
# provisioner "file" {
24-
# source = "hosts"
25-
# destination = "/tmp"
26-
# }
2755
provisioner "remote-exec" {
2856
inline = [
29-
# "cat /tmp/hosts/* >> /etc/hosts",
3057
"service firewalld stop"
3158
]
3259
}
3360
}
61+
62+
resource "null_resource" "hostsfile" {
63+
triggers {
64+
droplet_consumer = "${digitalocean_droplet.apiperf-consumer.ipv4_address}"
65+
droplet_webserver = "${digitalocean_droplet.apiperf-webserver.ipv4_address}"
66+
droplet_gateway = "${digitalocean_droplet.apiperf-gateway.ipv4_address}"
67+
}
68+
provisioner "file" {
69+
connection {
70+
host = "${digitalocean_droplet.apiperf-consumer.ipv4_address}"
71+
}
72+
source = "hosts"
73+
destination = "/tmp"
74+
}
75+
provisioner "remote-exec" {
76+
connection {
77+
host = "${digitalocean_droplet.apiperf-consumer.ipv4_address}"
78+
}
79+
inline = [
80+
"cat /tmp/hosts/*.host >> /etc/hosts",
81+
]
82+
}
83+
provisioner "file" {
84+
connection {
85+
host = "${digitalocean_droplet.apiperf-gateway.ipv4_address}"
86+
}
87+
source = "hosts"
88+
destination = "/tmp"
89+
}
90+
provisioner "remote-exec" {
91+
connection {
92+
host = "${digitalocean_droplet.apiperf-gateway.ipv4_address}"
93+
}
94+
inline = [
95+
"cat /tmp/hosts/*.host >> /etc/hosts",
96+
]
97+
}
98+
provisioner "file" {
99+
connection {
100+
host = "${digitalocean_droplet.apiperf-webserver.ipv4_address}"
101+
}
102+
source = "hosts"
103+
destination = "/tmp"
104+
}
105+
provisioner "remote-exec" {
106+
connection {
107+
host = "${digitalocean_droplet.apiperf-webserver.ipv4_address}"
108+
}
109+
inline = [
110+
"cat /tmp/hosts/*.host >> /etc/hosts",
111+
]
112+
}
113+
}
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
variable "do_apikey" {
22
description = "Valid Digital Ocean API Key"
33
}
4+
45
variable "sshkey" {
56
description = "SSH key to use for provisioning"
67
}
7-
variable "count" {
8-
description = "Number of droplets to create"
9-
default = "3"
8+
9+
variable "droplet_region" {
10+
description = "The region to use for droplets"
11+
default = "ams3"
12+
}
13+
14+
variable "droplet_image" {
15+
description = "The droplet image to use"
16+
default = "centos-7-0-x64"
17+
}
18+
19+
variable "droplet_use_privatenet" {
20+
description = "Disable or enable private networking"
21+
default = "False"
1022
}
1123

1224
variable "droplet_names" {
1325
description = "Base names to use for droplets"
1426
default = {
15-
"0" = "gateway"
16-
"1" = "webserver"
17-
"2" = "consumer"
27+
"gateway" = "gateway"
28+
"webserver" = "webserver"
29+
"consumer" = "consumer"
1830
}
1931
}
2032

2133
variable "droplet_sizes" {
2234
description = "Droplet sizes"
2335
default = {
24-
"0" = "1gb"
25-
"1" = "512mb"
26-
"2" = "512mb"
36+
"gateway" = "1gb"
37+
"webserver" = "512mb"
38+
"consumer" = "512mb"
2739
}
2840
}

0 commit comments

Comments
 (0)