Merge pull request #992 from sathiyas/terraform-aws-rhelcentos-enhancement

Terraform aws rhel6 enhancement
This commit is contained in:
Paul Hinze 2015-06-02 15:47:04 -05:00
commit 2fd6ff82e0
13 changed files with 156 additions and 13 deletions

17
terraform/aws/README.md Normal file
View File

@ -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'

View File

@ -1,12 +1,12 @@
resource "aws_instance" "server" {
ami = "${lookup(var.ami, var.region)}"
ami = "${lookup(var.ami, concat(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}"
}
@ -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",
]
}
}

View File

@ -0,0 +1,34 @@
#!/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

View File

@ -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

View File

@ -0,0 +1,26 @@
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
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

View File

@ -0,0 +1,5 @@
#!/bin/bash
set -e
echo "Starting Consul..."
sudo start consul

View File

@ -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

View File

@ -1,7 +1,22 @@
variable "ami" {
variable "platform" {
default = "ubuntu"
description = "The region of AWS, for AMI lookups."
}
variable "user" {
default = {
us-east-1 = "ami-3acc7a52"
us-west-2 = "ami-37501207"
ubuntu = "ubuntu"
rhel6 = "ec2-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-83c525e8"
us-west-2-ubuntu = "ami-57e8d767"
us-east-1-rhel6 = "ami-b0fed2d8"
us-west-2-rhel6 = "ami-2faa861f"
}
}
@ -24,10 +39,16 @@ variable "servers" {
}
variable "instance_type" {
default = "m1.small"
description = "The instance type to launch."
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" {
default = "consul"
description = "Name tag for the servers"
}
}
variable "platform" {
default = "ubuntu"
description = "The OS Platform"
}