terraform: fix formatting of consul.tf (#4580)

pull/4542/head^2
Evan Farrell 2018-10-24 11:02:38 -04:00 committed by Hans Hasselberg
parent 344d78dc54
commit 4ea03e7db7
8 changed files with 233 additions and 231 deletions

View File

@ -1,77 +1,76 @@
resource "aws_instance" "server" { resource "aws_instance" "server" {
ami = "${lookup(var.ami, "${var.region}-${var.platform}")}" ami = "${lookup(var.ami, "${var.region}-${var.platform}")}"
instance_type = "${var.instance_type}" instance_type = "${var.instance_type}"
key_name = "${var.key_name}" key_name = "${var.key_name}"
count = "${var.servers}" count = "${var.servers}"
security_groups = ["${aws_security_group.consul.id}"] security_groups = ["${aws_security_group.consul.id}"]
subnet_id = "${lookup(var.subnets, count.index % var.servers)}" subnet_id = "${lookup(var.subnets, count.index % var.servers)}"
connection { connection {
user = "${lookup(var.user, var.platform)}" user = "${lookup(var.user, var.platform)}"
private_key = "${file("${var.key_path}")}" private_key = "${file("${var.key_path}")}"
} }
#Instance tags #Instance tags
tags { tags {
Name = "${var.tagName}-${count.index}" Name = "${var.tagName}-${count.index}"
ConsulRole = "Server" ConsulRole = "Server"
} }
provisioner "file" { provisioner "file" {
source = "${path.module}/../shared/scripts/${lookup(var.service_conf, var.platform)}" source = "${path.module}/../shared/scripts/${lookup(var.service_conf, var.platform)}"
destination = "/tmp/${lookup(var.service_conf_dest, var.platform)}" destination = "/tmp/${lookup(var.service_conf_dest, var.platform)}"
} }
provisioner "remote-exec" {
inline = [
"echo ${var.servers} > /tmp/consul-server-count",
"echo ${aws_instance.server.0.private_ip} > /tmp/consul-server-addr",
]
}
provisioner "remote-exec" { provisioner "remote-exec" {
inline = [ scripts = [
"echo ${var.servers} > /tmp/consul-server-count", "${path.module}/../shared/scripts/install.sh",
"echo ${aws_instance.server.0.private_ip} > /tmp/consul-server-addr", "${path.module}/../shared/scripts/service.sh",
] "${path.module}/../shared/scripts/ip_tables.sh",
} ]
}
provisioner "remote-exec" {
scripts = [
"${path.module}/../shared/scripts/install.sh",
"${path.module}/../shared/scripts/service.sh",
"${path.module}/../shared/scripts/ip_tables.sh",
]
}
} }
resource "aws_security_group" "consul" { resource "aws_security_group" "consul" {
name = "consul_${var.platform}" name = "consul_${var.platform}"
description = "Consul internal traffic + maintenance." description = "Consul internal traffic + maintenance."
vpc_id = "${var.vpc_id}" vpc_id = "${var.vpc_id}"
// These are for internal traffic // These are for internal traffic
ingress { ingress {
from_port = 0 from_port = 0
to_port = 65535 to_port = 65535
protocol = "tcp" protocol = "tcp"
self = true self = true
} }
ingress { ingress {
from_port = 0 from_port = 0
to_port = 65535 to_port = 65535
protocol = "udp" protocol = "udp"
self = true self = true
} }
// These are for maintenance // These are for maintenance
ingress { ingress {
from_port = 22 from_port = 22
to_port = 22 to_port = 22
protocol = "tcp" protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
// This is for outbound internet access // This is for outbound internet access
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
} }

View File

@ -1,3 +1,3 @@
output "server_address" { output "server_address" {
value = "${aws_instance.server.0.public_dns}" value = "${aws_instance.server.0.public_dns}"
} }

View File

@ -17,20 +17,20 @@ variable "ami" {
description = "AWS AMI Id, if you change, make sure it is compatible with instance type, not all AMIs allow all instance types " description = "AWS AMI Id, if you change, make sure it is compatible with instance type, not all AMIs allow all instance types "
default = { default = {
ap-south-1-ubuntu = "ami-08a5e367" ap-south-1-ubuntu = "ami-08a5e367"
us-east-1-ubuntu = "ami-d651b8ac" us-east-1-ubuntu = "ami-d651b8ac"
ap-northeast-1-ubuntu = "ami-8422ebe2" ap-northeast-1-ubuntu = "ami-8422ebe2"
eu-west-1-ubuntu = "ami-17d11e6e" eu-west-1-ubuntu = "ami-17d11e6e"
ap-southeast-1-ubuntu = "ami-e6d3a585" ap-southeast-1-ubuntu = "ami-e6d3a585"
ca-central-1-ubuntu = "ami-e59c2581" ca-central-1-ubuntu = "ami-e59c2581"
us-west-1-ubuntu = "ami-2d5c6d4d" us-west-1-ubuntu = "ami-2d5c6d4d"
eu-central-1-ubuntu = "ami-5a922335" eu-central-1-ubuntu = "ami-5a922335"
sa-east-1-ubuntu = "ami-a3e39ecf" sa-east-1-ubuntu = "ami-a3e39ecf"
ap-southeast-2-ubuntu = "ami-391ff95b" ap-southeast-2-ubuntu = "ami-391ff95b"
eu-west-2-ubuntu = "ami-e1f2e185" eu-west-2-ubuntu = "ami-e1f2e185"
ap-northeast-2-ubuntu = "ami-0f6fb461" ap-northeast-2-ubuntu = "ami-0f6fb461"
us-west-2-ubuntu = "ami-ecc63a94" us-west-2-ubuntu = "ami-ecc63a94"
us-east-2-ubuntu = "ami-9686a4f3" us-east-2-ubuntu = "ami-9686a4f3"
us-east-1-rhel6 = "ami-0d28fe66" us-east-1-rhel6 = "ami-0d28fe66"
us-east-2-rhel6 = "ami-aff2a9ca" us-east-2-rhel6 = "ami-aff2a9ca"
us-west-2-rhel6 = "ami-3d3c0a0d" us-west-2-rhel6 = "ami-3d3c0a0d"
@ -95,11 +95,11 @@ variable "tagName" {
} }
variable "subnets" { variable "subnets" {
type = "map" type = "map"
description = "map of subnets to deploy your infrastructure in, must have as many keys as your server count (default 3), -var 'subnets={\"0\"=\"subnet-12345\",\"1\"=\"subnets-23456\"}' " description = "map of subnets to deploy your infrastructure in, must have as many keys as your server count (default 3), -var 'subnets={\"0\"=\"subnet-12345\",\"1\"=\"subnets-23456\"}' "
} }
variable "vpc_id" { variable "vpc_id" {
type = "string" type = "string"
description = "ID of the VPC to use - in case your account doesn't have default VPC" description = "ID of the VPC to use - in case your account doesn't have default VPC"
} }

View File

@ -1,68 +1,69 @@
resource "google_compute_instance" "consul" { resource "google_compute_instance" "consul" {
count = "${var.servers}" count = "${var.servers}"
name = "consul-${count.index}" name = "consul-${count.index}"
zone = "${var.region_zone}" zone = "${var.region_zone}"
tags = ["${var.tag_name}"] tags = ["${var.tag_name}"]
machine_type = "${var.machine_type}" machine_type = "${var.machine_type}"
disk { disk {
image = "${lookup(var.machine_image, var.platform)}" image = "${lookup(var.machine_image, var.platform)}"
}
network_interface {
network = "default"
access_config {
# Ephemeral
} }
}
network_interface { service_account {
network = "default" scopes = ["https://www.googleapis.com/auth/compute.readonly"]
}
access_config { connection {
# Ephemeral user = "${lookup(var.user, var.platform)}"
} private_key = "${file("${var.key_path}")}"
} }
service_account { provisioner "file" {
scopes = ["https://www.googleapis.com/auth/compute.readonly"] source = "${path.module}/../shared/scripts/${lookup(var.service_conf, var.platform)}"
} destination = "/tmp/${lookup(var.service_conf_dest, var.platform)}"
}
connection { provisioner "remote-exec" {
user = "${lookup(var.user, var.platform)}" inline = [
private_key = "${file("${var.key_path}")}" "echo ${var.servers} > /tmp/consul-server-count",
} "echo ${google_compute_instance.consul.0.network_interface.0.address} > /tmp/consul-server-addr",
]
}
provisioner "file" { provisioner "remote-exec" {
source = "${path.module}/../shared/scripts/${lookup(var.service_conf, var.platform)}" scripts = [
destination = "/tmp/${lookup(var.service_conf_dest, var.platform)}" "${path.module}/../shared/scripts/install.sh",
} "${path.module}/../shared/scripts/service.sh",
"${path.module}/../shared/scripts/ip_tables.sh",
provisioner "remote-exec" { ]
inline = [ }
"echo ${var.servers} > /tmp/consul-server-count",
"echo ${google_compute_instance.consul.0.network_interface.0.address} > /tmp/consul-server-addr",
]
}
provisioner "remote-exec" {
scripts = [
"${path.module}/../shared/scripts/install.sh",
"${path.module}/../shared/scripts/service.sh",
"${path.module}/../shared/scripts/ip_tables.sh",
]
}
} }
resource "google_compute_firewall" "consul_ingress" { resource "google_compute_firewall" "consul_ingress" {
name = "consul-internal-access" name = "consul-internal-access"
network = "default" network = "default"
allow { allow {
protocol = "tcp" protocol = "tcp"
ports = [
"8300", # Server RPC
"8301", # Serf LAN
"8302", # Serf WAN
"8400", # RPC
]
}
source_tags = ["${var.tag_name}"] ports = [
target_tags = ["${var.tag_name}"] "8300", # Server RPC
"8301", # Serf LAN
"8302", # Serf WAN
"8400", # RPC
]
}
source_tags = ["${var.tag_name}"]
target_tags = ["${var.tag_name}"]
} }

View File

@ -1,4 +1,3 @@
output "server_address" { output "server_address" {
value = "${google_compute_instance.consul.0.network_interface.0.address}" value = "${google_compute_instance.consul.0.network_interface.0.address}"
} }

View File

@ -1,72 +1,73 @@
variable "platform" { variable "platform" {
default = "ubuntu" default = "ubuntu"
description = "The OS Platform" description = "The OS Platform"
} }
variable "user" { variable "user" {
default = { default = {
ubuntu = "ubuntu" ubuntu = "ubuntu"
rhel6 = "root" rhel6 = "root"
rhel7 = "root" rhel7 = "root"
centos6 = "root" centos6 = "root"
centos7 = "root" centos7 = "root"
} }
} }
variable "machine_image" { variable "machine_image" {
default = { default = {
ubuntu = "ubuntu-os-cloud/ubuntu-1404-trusty-v20160314" ubuntu = "ubuntu-os-cloud/ubuntu-1404-trusty-v20160314"
rhel6 = "rhel-cloud/rhel-6-v20160303" rhel6 = "rhel-cloud/rhel-6-v20160303"
rhel7 = "rhel-cloud/rhel-7-v20160303" rhel7 = "rhel-cloud/rhel-7-v20160303"
centos6 = "centos-cloud/centos-6-v20160301" centos6 = "centos-cloud/centos-6-v20160301"
centos7 = "centos-cloud/centos-7-v20160301" centos7 = "centos-cloud/centos-7-v20160301"
} }
} }
variable "service_conf" { variable "service_conf" {
default = { default = {
ubuntu = "debian_upstart.conf" ubuntu = "debian_upstart.conf"
rhel6 = "rhel_upstart.conf" rhel6 = "rhel_upstart.conf"
rhel7 = "rhel_consul.service" rhel7 = "rhel_consul.service"
centos6 = "rhel_upstart.conf" centos6 = "rhel_upstart.conf"
centos7 = "rhel_consul.service" centos7 = "rhel_consul.service"
} }
} }
variable "service_conf_dest" { variable "service_conf_dest" {
default = { default = {
ubuntu = "upstart.conf" ubuntu = "upstart.conf"
rhel6 = "upstart.conf" rhel6 = "upstart.conf"
rhel7 = "consul.service" rhel7 = "consul.service"
centos6 = "upstart.conf" centos6 = "upstart.conf"
centos7 = "consul.service" centos7 = "consul.service"
} }
} }
variable "key_path" { variable "key_path" {
description = "Path to the private key used to access the cloud servers" description = "Path to the private key used to access the cloud servers"
} }
variable "region" { variable "region" {
default = "us-central1" default = "us-central1"
description = "The region of Google Cloud where to launch the cluster" description = "The region of Google Cloud where to launch the cluster"
} }
variable "region_zone" { variable "region_zone" {
default = "us-central1-f" default = "us-central1-f"
description = "The zone of Google Cloud in which to launch the cluster" description = "The zone of Google Cloud in which to launch the cluster"
} }
variable "servers" { variable "servers" {
default = "3" default = "3"
description = "The number of Consul servers to launch" description = "The number of Consul servers to launch"
} }
variable "machine_type" { variable "machine_type" {
default = "f1-micro" default = "f1-micro"
description = "Google Cloud Compute machine type" description = "Google Cloud Compute machine type"
} }
variable "tag_name" { variable "tag_name" {
default = "consul" default = "consul"
description = "Name tag for the servers" description = "Name tag for the servers"
} }

View File

@ -1,60 +1,60 @@
provider "openstack" { provider "openstack" {
user_name = "${var.username}" user_name = "${var.username}"
tenant_name = "${var.tenant_name}" tenant_name = "${var.tenant_name}"
password = "${var.password}" password = "${var.password}"
auth_url = "${var.auth_url}" auth_url = "${var.auth_url}"
} }
resource "openstack_compute_keypair_v2" "consul_keypair" { resource "openstack_compute_keypair_v2" "consul_keypair" {
name = "consul-keypair" name = "consul-keypair"
region = "${var.region}" region = "${var.region}"
public_key = "${var.public_key}" public_key = "${var.public_key}"
} }
resource "openstack_compute_floatingip_v2" "consul_ip" { resource "openstack_compute_floatingip_v2" "consul_ip" {
region = "${var.region}" region = "${var.region}"
pool = "${lookup(var.pub_net_id, var.region)}" pool = "${lookup(var.pub_net_id, var.region)}"
count = "${var.servers}" count = "${var.servers}"
} }
resource "openstack_compute_instance_v2" "consul_node" { resource "openstack_compute_instance_v2" "consul_node" {
name = "consul-node-${count.index}" name = "consul-node-${count.index}"
region = "${var.region}" region = "${var.region}"
image_id = "${lookup(var.image, var.region)}" image_id = "${lookup(var.image, var.region)}"
flavor_id = "${lookup(var.flavor, var.region)}" flavor_id = "${lookup(var.flavor, var.region)}"
floating_ip = "${element(openstack_compute_floatingip_v2.consul_ip.*.address,count.index)}" floating_ip = "${element(openstack_compute_floatingip_v2.consul_ip.*.address,count.index)}"
key_pair = "consul-keypair" key_pair = "consul-keypair"
count = "${var.servers}" count = "${var.servers}"
connection { connection {
user = "${var.user_login}" user = "${var.user_login}"
key_file = "${var.key_file_path}" key_file = "${var.key_file_path}"
timeout = "1m" timeout = "1m"
} }
provisioner "file" { provisioner "file" {
source = "${path.module}/scripts/upstart.conf" source = "${path.module}/scripts/upstart.conf"
destination = "/tmp/upstart.conf" destination = "/tmp/upstart.conf"
} }
provisioner "file" { provisioner "file" {
source = "${path.module}/scripts/upstart-join.conf" source = "${path.module}/scripts/upstart-join.conf"
destination = "/tmp/upstart-join.conf" destination = "/tmp/upstart-join.conf"
} }
provisioner "remote-exec" { provisioner "remote-exec" {
inline = [ inline = [
"echo ${var.servers} > /tmp/consul-server-count", "echo ${var.servers} > /tmp/consul-server-count",
"echo ${count.index} > /tmp/consul-server-index", "echo ${count.index} > /tmp/consul-server-index",
"echo ${openstack_compute_instance_v2.consul_node.0.network.0.fixed_ip_v4} > /tmp/consul-server-addr", "echo ${openstack_compute_instance_v2.consul_node.0.network.0.fixed_ip_v4} > /tmp/consul-server-addr",
] ]
} }
provisioner "remote-exec" { provisioner "remote-exec" {
scripts = [ scripts = [
"${path.module}/scripts/install.sh", "${path.module}/scripts/install.sh",
"${path.module}/scripts/server.sh", "${path.module}/scripts/server.sh",
"${path.module}/scripts/service.sh", "${path.module}/scripts/service.sh",
] ]
} }
} }

View File

@ -3,42 +3,44 @@ variable "password" {}
variable "tenant_name" {} variable "tenant_name" {}
variable "auth_url" {} variable "auth_url" {}
variable "public_key" {} variable "public_key" {}
variable "user_login" { variable "user_login" {
default = "stack" default = "stack"
} }
variable "key_file_path" {} variable "key_file_path" {}
variable "nb_of_nodes" { variable "nb_of_nodes" {
default = "4" default = "4"
} }
variable "pub_net_id" { variable "pub_net_id" {
default = { default = {
tr2="PublicNetwork-01" tr2 = "PublicNetwork-01"
tr2-1 = "" tr2-1 = ""
} }
} }
variable "region" { variable "region" {
default = "tr2" default = "tr2"
description = "The region of openstack, for image/flavor/network lookups." description = "The region of openstack, for image/flavor/network lookups."
} }
variable "image" { variable "image" {
default = { default = {
tr2 = "eee08821-c95a-448f-9292-73908c794661" tr2 = "eee08821-c95a-448f-9292-73908c794661"
tr2-1 = "" tr2-1 = ""
} }
} }
variable "flavor" { variable "flavor" {
default = { default = {
tr2 = "100" tr2 = "100"
tr2-1 = "" tr2-1 = ""
} }
} }
variable "servers" { variable "servers" {
default = "3" default = "3"
description = "The number of Consul servers to launch." description = "The number of Consul servers to launch."
} }