From 7c76a5d816614db0d4e158926b1586269614ee7a Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Sun, 31 May 2015 14:00:57 -0400 Subject: [PATCH 1/7] moved scripts/* under scripts/ubuntu to add support for other platforms --- terraform/aws/consul.tf | 10 +++++----- terraform/aws/scripts/{ => ubuntu}/install.sh | 0 terraform/aws/scripts/{ => ubuntu}/server.sh | 0 terraform/aws/scripts/{ => ubuntu}/service.sh | 0 terraform/aws/scripts/{ => ubuntu}/upstart-join.conf | 0 terraform/aws/scripts/{ => ubuntu}/upstart.conf | 0 terraform/aws/variables.tf | 6 ++++++ 7 files changed, 11 insertions(+), 5 deletions(-) rename terraform/aws/scripts/{ => ubuntu}/install.sh (100%) rename terraform/aws/scripts/{ => ubuntu}/server.sh (100%) rename terraform/aws/scripts/{ => ubuntu}/service.sh (100%) rename terraform/aws/scripts/{ => ubuntu}/upstart-join.conf (100%) rename terraform/aws/scripts/{ => ubuntu}/upstart.conf (100%) diff --git a/terraform/aws/consul.tf b/terraform/aws/consul.tf index 9a2c5fcf7e..23a4b05de5 100644 --- a/terraform/aws/consul.tf +++ b/terraform/aws/consul.tf @@ -16,12 +16,12 @@ resource "aws_instance" "server" { } provisioner "file" { - source = "${path.module}/scripts/upstart.conf" + source = "${path.module}/scripts/${var.platform}/upstart.conf" destination = "/tmp/upstart.conf" } provisioner "file" { - source = "${path.module}/scripts/upstart-join.conf" + source = "${path.module}/scripts/${var.platform}/upstart-join.conf" destination = "/tmp/upstart-join.conf" } @@ -34,9 +34,9 @@ resource "aws_instance" "server" { provisioner "remote-exec" { scripts = [ - "${path.module}/scripts/install.sh", - "${path.module}/scripts/server.sh", - "${path.module}/scripts/service.sh", + "${path.module}/scripts/${var.platform}/install.sh", + "${path.module}/scripts/${var.platform}/server.sh", + "${path.module}/scripts/${var.platform}/service.sh", ] } } diff --git a/terraform/aws/scripts/install.sh b/terraform/aws/scripts/ubuntu/install.sh similarity index 100% rename from terraform/aws/scripts/install.sh rename to terraform/aws/scripts/ubuntu/install.sh diff --git a/terraform/aws/scripts/server.sh b/terraform/aws/scripts/ubuntu/server.sh similarity index 100% rename from terraform/aws/scripts/server.sh rename to terraform/aws/scripts/ubuntu/server.sh diff --git a/terraform/aws/scripts/service.sh b/terraform/aws/scripts/ubuntu/service.sh similarity index 100% rename from terraform/aws/scripts/service.sh rename to terraform/aws/scripts/ubuntu/service.sh diff --git a/terraform/aws/scripts/upstart-join.conf b/terraform/aws/scripts/ubuntu/upstart-join.conf similarity index 100% rename from terraform/aws/scripts/upstart-join.conf rename to terraform/aws/scripts/ubuntu/upstart-join.conf diff --git a/terraform/aws/scripts/upstart.conf b/terraform/aws/scripts/ubuntu/upstart.conf similarity index 100% rename from terraform/aws/scripts/upstart.conf rename to terraform/aws/scripts/ubuntu/upstart.conf diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index 2d92711f64..f6d5f17676 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -27,7 +27,13 @@ variable "instance_type" { default = "m1.small" description = "The instance type to launch." } + variable "tagName" { default = "consul" description = "Name tag for the servers" +} + +variable "platform" { + default = "ubuntu" + description = "The OS Platform" } \ No newline at end of file From edbd023ee548f67f752a00363a961354a5e00b90 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Mon, 1 Jun 2015 01:26:50 -0400 Subject: [PATCH 2/7] Adding rhel scripts and changes for rhel paramaters --- terraform/aws/consul.tf | 5 +-- terraform/aws/scripts/rhel/install.sh | 36 ++++++++++++++++++++ terraform/aws/scripts/rhel/server.sh | 15 ++++++++ terraform/aws/scripts/rhel/service.sh | 5 +++ terraform/aws/scripts/rhel/upstart-join.conf | 25 ++++++++++++++ terraform/aws/scripts/rhel/upstart.conf | 28 +++++++++++++++ terraform/aws/variables.tf | 25 ++++++++++++-- 7 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 terraform/aws/scripts/rhel/install.sh create mode 100755 terraform/aws/scripts/rhel/server.sh create mode 100755 terraform/aws/scripts/rhel/service.sh create mode 100644 terraform/aws/scripts/rhel/upstart-join.conf create mode 100644 terraform/aws/scripts/rhel/upstart.conf diff --git a/terraform/aws/consul.tf b/terraform/aws/consul.tf index 23a4b05de5..1176f833af 100644 --- a/terraform/aws/consul.tf +++ b/terraform/aws/consul.tf @@ -1,12 +1,13 @@ resource "aws_instance" "server" { - ami = "${lookup(var.ami, var.region)}" + ami = "${lookup(var.ami, concat(var.region, "-", var.platform))}" + #ami = "${lookup(var.ami, var.region, var.platform)}" instance_type = "${var.instance_type}" key_name = "${var.key_name}" count = "${var.servers}" security_groups = ["${aws_security_group.consul.name}"] connection { - user = "ubuntu" + user = "${lookup(var.user, var.platform)}" key_file = "${var.key_path}" } diff --git a/terraform/aws/scripts/rhel/install.sh b/terraform/aws/scripts/rhel/install.sh new file mode 100644 index 0000000000..9b4d1c5f18 --- /dev/null +++ b/terraform/aws/scripts/rhel/install.sh @@ -0,0 +1,36 @@ +#!/bin/bash +set -e + +# Read the address to join from the file we provisioned +JOIN_ADDRS=$(cat /tmp/consul-server-addr | tr -d '\n') + +echo "Installing dependencies..." +sudo yum update -y +sudo yum install -y unzip wget + +echo "Fetching Consul..." +cd /tmp +wget https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip -O consul.zip + +echo "Installing Consul..." +unzip consul.zip >/dev/null +sudo chmod +x consul +sudo mv consul /usr/local/bin/consul +sudo mkdir -p /etc/consul.d +sudo mkdir -p /mnt/consul +sudo mkdir -p /etc/service + +# Setup the join address +cat >/tmp/consul-join << EOF +export CONSUL_JOIN="${JOIN_ADDRS}" +EOF +sudo mv /tmp/consul-join /etc/service/consul-join +chmod 0644 /etc/service/consul-join + +echo "Installing Upstart service..." +sudo chown root:root /tmp/upstart.conf +sudo chown root:root /tmp/upstart-join.conf +sudo mv /tmp/upstart.conf /etc/init/consul.conf +sudo mv /tmp/upstart-join.conf /etc/init/consul-join.conf +echo "Installed upstart" +sudo initctl list diff --git a/terraform/aws/scripts/rhel/server.sh b/terraform/aws/scripts/rhel/server.sh new file mode 100755 index 0000000000..80057cbf81 --- /dev/null +++ b/terraform/aws/scripts/rhel/server.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +# Read from the file we created +SERVER_COUNT=$(cat /tmp/consul-server-count | tr -d '\n') + +# Write the flags to a temporary file +cat >/tmp/consul_flags << EOF +export CONSUL_FLAGS="-server -bootstrap-expect=${SERVER_COUNT} -data-dir=/mnt/consul" +EOF + +# Write it to the full service file +sudo mv /tmp/consul_flags /etc/service/consul +sudo chown root:root /etc/service/consul +sudo chmod 0644 /etc/service/consul diff --git a/terraform/aws/scripts/rhel/service.sh b/terraform/aws/scripts/rhel/service.sh new file mode 100755 index 0000000000..7c186e97ac --- /dev/null +++ b/terraform/aws/scripts/rhel/service.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e + +echo "Starting Consul..." +sudo start consul diff --git a/terraform/aws/scripts/rhel/upstart-join.conf b/terraform/aws/scripts/rhel/upstart-join.conf new file mode 100644 index 0000000000..16343d8759 --- /dev/null +++ b/terraform/aws/scripts/rhel/upstart-join.conf @@ -0,0 +1,25 @@ +description "Join the consul cluster" + +start on started consul +stop on stopped consul + +task + +script + if [ -f "/etc/service/consul-join" ]; then + . /etc/service/consul-join + fi + + # Keep trying to join until it succeeds + set +e + while :; do + logger -t "consul-join" "Attempting join: ${CONSUL_JOIN}" + /usr/local/bin/consul join \ + ${CONSUL_JOIN} \ + >>/var/log/consul-join.log 2>&1 + [ $? -eq 0 ] && break + sleep 5 + done + + logger -t "consul-join" "Join success!" +end script diff --git a/terraform/aws/scripts/rhel/upstart.conf b/terraform/aws/scripts/rhel/upstart.conf new file mode 100644 index 0000000000..bc335b18ac --- /dev/null +++ b/terraform/aws/scripts/rhel/upstart.conf @@ -0,0 +1,28 @@ +description "Consul agent" + +start on runlevel [2345] +stop on runlevel [!2345] + +respawn +# This is to avoid Upstart re-spawning the process upon `consul leave` +normal exit 0 INT +# stop consul will not mark node as failed but left +#kill signal INT + +script + if [ -f "/etc/service/consul" ]; then + . /etc/service/consul + fi + + # Make sure to use all our CPUs, because Consul can block a scheduler thread + export GOMAXPROCS=`nproc` + + # Get the public IP + BIND=`ifconfig eth0 | grep "inet addr" | awk '{ print substr($2,6) }'` + + exec /usr/local/bin/consul agent \ + -config-dir="/etc/consul.d" \ + -bind=$BIND \ + ${CONSUL_FLAGS} \ + >>/var/log/consul.log 2>&1 +end script diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index f6d5f17676..1d31311e8d 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -1,7 +1,26 @@ +variable "platform" { + default = "ubuntu" + description = "The region of AWS, for AMI lookups." +} + +variable "user" { + default = "ubuntu" + description = "The region of AWS, for AMI lookups." +} + +variable "user" { + default = { + ubuntu = "ubuntu" + rhel = "ec2-user" + } +} + variable "ami" { default = { - us-east-1 = "ami-3acc7a52" - us-west-2 = "ami-37501207" + us-east-1-ubuntu = "ami-3acc7a52" + us-west-2-ubuntu = "ami-37501207" + us-east-1-rhel = "ami-b0fed2d8" + us-west-2-rhel = "ami-4dbf9e7d" } } @@ -24,7 +43,7 @@ variable "servers" { } variable "instance_type" { - default = "m1.small" + default = "m3.medium" description = "The instance type to launch." } From 52a15bd69eb0ce6e51ac18dbdb5dc3481731b777 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Mon, 1 Jun 2015 01:43:52 -0400 Subject: [PATCH 3/7] Rename rhel to rhel6 --- terraform/aws/variables.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index 1d31311e8d..7807c2a9e4 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -11,7 +11,7 @@ variable "user" { variable "user" { default = { ubuntu = "ubuntu" - rhel = "ec2-user" + rhel6 = "ec2-user" } } @@ -19,8 +19,8 @@ variable "ami" { default = { us-east-1-ubuntu = "ami-3acc7a52" us-west-2-ubuntu = "ami-37501207" - us-east-1-rhel = "ami-b0fed2d8" - us-west-2-rhel = "ami-4dbf9e7d" + us-east-1-rhel6 = "ami-b0fed2d8" + us-west-2-rhel6 = "ami-2faa861f" } } From f72c21a032db221f9e6a0a24316af9367f4e2fb1 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Mon, 1 Jun 2015 01:57:18 -0400 Subject: [PATCH 4/7] variable changes for instance types --- terraform/aws/consul.tf | 3 +-- terraform/aws/scripts/{rhel => rhel6}/install.sh | 0 terraform/aws/scripts/{rhel => rhel6}/server.sh | 0 terraform/aws/scripts/{rhel => rhel6}/service.sh | 0 .../aws/scripts/{rhel => rhel6}/upstart-join.conf | 0 terraform/aws/scripts/{rhel => rhel6}/upstart.conf | 0 terraform/aws/variables.tf | 11 ++++------- 7 files changed, 5 insertions(+), 9 deletions(-) rename terraform/aws/scripts/{rhel => rhel6}/install.sh (100%) rename terraform/aws/scripts/{rhel => rhel6}/server.sh (100%) rename terraform/aws/scripts/{rhel => rhel6}/service.sh (100%) rename terraform/aws/scripts/{rhel => rhel6}/upstart-join.conf (100%) rename terraform/aws/scripts/{rhel => rhel6}/upstart.conf (100%) diff --git a/terraform/aws/consul.tf b/terraform/aws/consul.tf index 1176f833af..0db1e1dba5 100644 --- a/terraform/aws/consul.tf +++ b/terraform/aws/consul.tf @@ -1,7 +1,6 @@ resource "aws_instance" "server" { ami = "${lookup(var.ami, concat(var.region, "-", var.platform))}" - #ami = "${lookup(var.ami, var.region, var.platform)}" - instance_type = "${var.instance_type}" + instance_type = "${lookup(var.instance_type, var.platform)}" key_name = "${var.key_name}" count = "${var.servers}" security_groups = ["${aws_security_group.consul.name}"] diff --git a/terraform/aws/scripts/rhel/install.sh b/terraform/aws/scripts/rhel6/install.sh similarity index 100% rename from terraform/aws/scripts/rhel/install.sh rename to terraform/aws/scripts/rhel6/install.sh diff --git a/terraform/aws/scripts/rhel/server.sh b/terraform/aws/scripts/rhel6/server.sh similarity index 100% rename from terraform/aws/scripts/rhel/server.sh rename to terraform/aws/scripts/rhel6/server.sh diff --git a/terraform/aws/scripts/rhel/service.sh b/terraform/aws/scripts/rhel6/service.sh similarity index 100% rename from terraform/aws/scripts/rhel/service.sh rename to terraform/aws/scripts/rhel6/service.sh diff --git a/terraform/aws/scripts/rhel/upstart-join.conf b/terraform/aws/scripts/rhel6/upstart-join.conf similarity index 100% rename from terraform/aws/scripts/rhel/upstart-join.conf rename to terraform/aws/scripts/rhel6/upstart-join.conf diff --git a/terraform/aws/scripts/rhel/upstart.conf b/terraform/aws/scripts/rhel6/upstart.conf similarity index 100% rename from terraform/aws/scripts/rhel/upstart.conf rename to terraform/aws/scripts/rhel6/upstart.conf diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index 7807c2a9e4..c06cbec965 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -3,11 +3,6 @@ variable "platform" { description = "The region of AWS, for AMI lookups." } -variable "user" { - default = "ubuntu" - description = "The region of AWS, for AMI lookups." -} - variable "user" { default = { ubuntu = "ubuntu" @@ -43,8 +38,10 @@ variable "servers" { } variable "instance_type" { - default = "m3.medium" - description = "The instance type to launch." + default = { + ubuntu = "m1.small" + rhel6 = "m3.medium" + } } variable "tagName" { From af28f3068e92ee8ceb1a7507cf803698056d07a0 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Mon, 1 Jun 2015 13:43:50 -0400 Subject: [PATCH 5/7] adding README --- terraform/aws/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 terraform/aws/README.md diff --git a/terraform/aws/README.md b/terraform/aws/README.md new file mode 100644 index 0000000000..441afe34c0 --- /dev/null +++ b/terraform/aws/README.md @@ -0,0 +1,17 @@ +## Running the aws templates to set up a consul cluster + +The platform variable defines the target OS, default is ubuntu, rhel6 is an option + +For AWS provider, set up your AWS environment as outlined in https://www.terraform.io/docs/providers/aws/index.html + +To set up ubuntu based, run like below, replace key_name and key_path with actual values + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' + +or + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' -var 'platform=ubuntu' + +To run rhel6, run like below + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' -var 'platform=rhel6' \ No newline at end of file From ae974bc824b26fab820c33c092b85735356439fa Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Mon, 1 Jun 2015 13:49:24 -0400 Subject: [PATCH 6/7] removed kill signal INT from upstart --- terraform/aws/scripts/rhel6/install.sh | 2 -- terraform/aws/scripts/rhel6/upstart.conf | 2 -- terraform/aws/variables.tf | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/terraform/aws/scripts/rhel6/install.sh b/terraform/aws/scripts/rhel6/install.sh index 9b4d1c5f18..da40412785 100644 --- a/terraform/aws/scripts/rhel6/install.sh +++ b/terraform/aws/scripts/rhel6/install.sh @@ -32,5 +32,3 @@ sudo chown root:root /tmp/upstart.conf sudo chown root:root /tmp/upstart-join.conf sudo mv /tmp/upstart.conf /etc/init/consul.conf sudo mv /tmp/upstart-join.conf /etc/init/consul-join.conf -echo "Installed upstart" -sudo initctl list diff --git a/terraform/aws/scripts/rhel6/upstart.conf b/terraform/aws/scripts/rhel6/upstart.conf index bc335b18ac..7c050dc011 100644 --- a/terraform/aws/scripts/rhel6/upstart.conf +++ b/terraform/aws/scripts/rhel6/upstart.conf @@ -6,8 +6,6 @@ stop on runlevel [!2345] respawn # This is to avoid Upstart re-spawning the process upon `consul leave` normal exit 0 INT -# stop consul will not mark node as failed but left -#kill signal INT script if [ -f "/etc/service/consul" ]; then diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index c06cbec965..f75fde2eae 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -52,4 +52,4 @@ variable "tagName" { variable "platform" { default = "ubuntu" description = "The OS Platform" -} \ No newline at end of file +} From 0b681edb6c31e199a94c29c6bcd913ed95bfae45 Mon Sep 17 00:00:00 2001 From: Sathiya Shunmugasundaram Date: Tue, 2 Jun 2015 15:54:08 -0400 Subject: [PATCH 7/7] AMI changes to reflect latest HVM and instance type changes to t2.micro --- terraform/aws/consul.tf | 2 +- terraform/aws/variables.tf | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/terraform/aws/consul.tf b/terraform/aws/consul.tf index 0db1e1dba5..d1c57d9bb8 100644 --- a/terraform/aws/consul.tf +++ b/terraform/aws/consul.tf @@ -1,6 +1,6 @@ resource "aws_instance" "server" { ami = "${lookup(var.ami, concat(var.region, "-", var.platform))}" - instance_type = "${lookup(var.instance_type, var.platform)}" + instance_type = "${var.instance_type}" key_name = "${var.key_name}" count = "${var.servers}" security_groups = ["${aws_security_group.consul.name}"] diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index f75fde2eae..94c0e310f7 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -11,9 +11,10 @@ variable "user" { } variable "ami" { + description = "AWS AMI Id, if you change, make sure it is compatible with insatnce type, not all AMIs allow all insatnce types " default = { - us-east-1-ubuntu = "ami-3acc7a52" - us-west-2-ubuntu = "ami-37501207" + us-east-1-ubuntu = "ami-83c525e8" + us-west-2-ubuntu = "ami-57e8d767" us-east-1-rhel6 = "ami-b0fed2d8" us-west-2-rhel6 = "ami-2faa861f" } @@ -38,10 +39,8 @@ variable "servers" { } variable "instance_type" { - default = { - ubuntu = "m1.small" - rhel6 = "m3.medium" - } + default = "t2.micro" + description = "AWS Instance type, if you change, make sure it is compatible with AMI, not all AMIs allow all insatnce types " } variable "tagName" {