Merge pull request #27 from lowescott/consul-erb

Fix Consul cluster setup for Docker Swarm cluster
This commit is contained in:
Scott Lowe 2016-01-24 14:44:19 -07:00
commit 1fa7826848
3 changed files with 88 additions and 45 deletions

View file

@ -2,22 +2,41 @@
# vi: set ft=ruby :
# Specify Vagrant version, Vagrant API version, and Vagrant clone location
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant'
# Require 'yaml' and 'fileutils' modules
# Require 'yaml', 'fileutils', and 'erb' modules
require 'yaml'
require 'fileutils'
require 'erb'
# Look for user-data file to configure/customize CoreOS boxes
# No changes should need to be made to this file
USER_DATA = File.join(File.dirname(__FILE__), "user-data")
USER_DATA = File.join(File.dirname(__FILE__), 'user-data')
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Be sure to edit servers.yml to provide correct IP addresses
servers = YAML.load_file(File.join(File.dirname(__FILE__), 'servers.yml'))
# Build array of IP addresses for Consul cluster
consul_cluster_list = []
servers.each do |server|
if server['box'] == 'slowe/ubuntu-trusty-x64'
consul_cluster_list << "\"#{server['priv_ip']}\""
end # if server['box']
end # servers.each
# Build a Consul configuration file from ERB template
template = File.join(File.dirname(__FILE__), 'server.json.erb')
content = ERB.new File.new(template).read
servers.each do |server|
if server['box'] == 'slowe/ubuntu-trusty-x64'
target = File.join(File.dirname(__FILE__), "#{server['name']}.json")
File.open(target, 'w') { |f| f.write(content.result(binding)) }
end # if server['box']
end # servers.each
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
@ -25,38 +44,41 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
servers.each do |servers|
config.vm.define servers["name"] do |srv|
servers.each do |server|
config.vm.define server['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
srv.vm.hostname = servers["name"]
srv.vm.box = servers["box"]
srv.vm.hostname = server['name']
srv.vm.box = server['box']
# Assign an additional static private network
srv.vm.network "private_network", ip: servers["priv_ip"]
srv.vm.network 'private_network', ip: server['priv_ip']
# Configure VMs based on CoreOS box
if srv.vm.box == "coreos-stable"
if srv.vm.box == 'coreos-stable'
# Disable default synced folder for CoreOS VMs
srv.vm.synced_folder ".", "/vagrant", disabled: true
srv.vm.synced_folder '.', '/vagrant', disabled: true
# Copy user_data file into CoreOS VM
srv.vm.provision "file", source: "#{USER_DATA}", destination: "/tmp/vagrantfile-user-data"
srv.vm.provision 'file', source: "#{USER_DATA}", destination: '/tmp/vagrantfile-user-data'
# Move user_data to correct location to be processed by cloud-init
srv.vm.provision "shell", inline: "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", privileged: true
end
srv.vm.provision 'shell', inline: 'mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/', privileged: true
end # srv.vm.box == 'coreos-stable'
# Configure VMs based on Ubuntu box
if srv.vm.box == "slowe/ubuntu-trusty-x64"
if srv.vm.box == 'slowe/ubuntu-trusty-x64'
# Enable default synced folder
srv.vm.synced_folder ".", "/vagrant"
srv.vm.synced_folder '.', '/vagrant'
# Copy files into the VM
srv.vm.provision "file", source: "server.json", destination: "/home/vagrant/config.json"
srv.vm.provision "file", source: "consul.conf", destination: "/home/vagrant/consul.conf"
srv.vm.provision 'file', source: "#{server['name']}.json", destination: '/home/vagrant/config.json'
srv.vm.provision 'file', source: 'consul.conf', destination: '/home/vagrant/consul.conf'
# Run final provisioning script
srv.vm.provision "shell", path: "consul.sh"
end
srv.vm.provision 'shell', path: 'consul.sh'
end # srv.vm.box == 'ubuntu-trusty-x64'
# Configure VMs with RAM and CPUs per settings in servers.yml
srv.vm.provider :vmware_fusion do |vmw|
vmw.vmx["memsize"] = servers["ram"]
vmw.vmx["numvcpus"] = servers["vcpu"]
end
end
end
end
vmw.vmx['memsize'] = server['ram']
vmw.vmx['numvcpus'] = server['vcpu']
end # srv.vm.provider
end # config.vm.define
end # servers.each
end # Vagrant.configure

View file

@ -1,30 +1,50 @@
#!/bin/bash
# Update list of packages
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
# Install unzip package needed to decompress Consul download
export DEBIAN_FRONTEND=noninteractive
sudo apt-get -y install unzip
# Install packages as needed
if [[ ! -e /usr/bin/curl ]]; then
apt-get -yqq install curl
fi
# Get Consul download
wget https://dl.bintray.com/mitchellh/consul/0.5.0_linux_amd64.zip
if [[ ! -e /usr/bin/unzip ]]; then
apt-get -yqq install unzip
fi
# Decompress and remove Consul download
unzip 0.5.0_linux_amd64.zip
rm 0.5.0_linux_amd64.zip
# Download and install Consul binary, if needed
if [[ ! -e /usr/local/bin/consul ]];then
# Move Consul binary to location in path
sudo mv consul /usr/local/bin/
# Download Consul
curl -kLO https://releases.hashicorp.com/consul/0.6.3/consul_0.6.3_linux_amd64.zip
# Create consul user
useradd -M -d /var/consul -r -s /usr/bin/nologin consul
# Decompress and remove Consul download
unzip consul_0.6.3_linux_amd64.zip
rm consul_0.6.3_linux_amd64.zip
# Create directories needed by Consul
sudo mkdir /var/consul
sudo chown -R consul:consul /var/consul
# Move Consul binary to location in path
sudo mv consul /usr/local/bin/
fi
# Create consul user, if it doesn't already exist
if [ -z "$(getent passwd consul)" ]; then
useradd -M -d /var/consul -r -s /usr/bin/nologin consul
else
echo "Consul user already created."
fi
# Create and configure Consul working directory
sudo mkdir -p /var/consul
if [ -d /var/consul ]; then
sudo chown -R consul:consul /var/consul
fi
# Create and configure Consul configuration directories
sudo mkdir -p /etc/consul.d/server
sudo chown -R root:consul /etc/consul.d
if [ -d /etc/consul.d ]; then
sudo chown -R root:consul /etc/consul.d
fi
# Move files into the correct locations
sudo mv /home/vagrant/consul.conf /etc/init/consul.conf

View file

@ -5,6 +5,7 @@
"data_dir": "/var/consul",
"log_level": "INFO",
"enable_syslog": false,
"retry_join": ["192.168.1.101", "192.168.1.102", "192.168.1.103"],
"client_addr": "0.0.0.0"
}
"retry_join": [<%= consul_cluster_list.join(',') %>],
"client_addr": "0.0.0.0",
"advertise_addr": "<%= server['priv_ip'] %>"
}