diff --git a/consul/Vagrantfile b/consul/Vagrantfile index 28cccdd..e9fe790 100644 --- a/consul/Vagrantfile +++ b/consul/Vagrantfile @@ -2,8 +2,8 @@ # 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' ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion' #ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_appcatalyst' @@ -22,40 +22,40 @@ 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 # Specify hostname and Vagrant box - 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'] # Specify default synced folder; requires VMware Tools - srv.vm.synced_folder ".", "/vagrant" - - # Provision consul to the VMs - srv.vm.provision "shell", path: "consul.sh" + srv.vm.synced_folder '.', '/vagrant', disabled: true # Copy in the Consul configuration files - srv.vm.provision "file", source: "bootstrap.json", destination: "/etc/consul.d/bootstrap/config.json" - srv.vm.provision "file", source: "server.json", destination: "/etc/consul.d/server/config.json" + srv.vm.provision 'file', source: 'server.json', destination: '/home/vagrant/config.json' + srv.vm.provision 'file', source: 'consul.conf', destination: '/home/vagrant/consul.conf' + + # Provision consul to the VMs + srv.vm.provision 'shell', path: 'consul.sh' # 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"] + vmw.vmx['memsize'] = server['ram'] + vmw.vmx['numvcpus'] = server['vcpu'] end # srv.vm.provider vmware_fusion # Configure VMs with RAM and CPUs per settings in servers.yml srv.vm.provider :vmware_appcatalyst do |vmw| - vmw.vmx["memsize"] = servers["ram"] - vmw.vmx["numvcpus"] = servers["vcpu"] - end # srv.vm.provider vmware_fusion + vmw.vmx['memsize'] = server['ram'] + vmw.vmx['numvcpus'] = server['vcpu'] + end # srv.vm.provider vmware_appcatalyst end # config.vm.define end # servers.each end # Vagrant.configure diff --git a/consul/bootstrap.json b/consul/bootstrap.json deleted file mode 100644 index 1a40c73..0000000 --- a/consul/bootstrap.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "bootstrap": true, - "server": true, - "datacenter": "dc1", - "data_dir": "/tmp", - "log_level": "INFO", - "enable_syslog": true -} \ No newline at end of file diff --git a/consul/consul.conf b/consul/consul.conf new file mode 100644 index 0000000..d045021 --- /dev/null +++ b/consul/consul.conf @@ -0,0 +1,19 @@ +description "Consul server process" + +start on runlevel [2345] +stop on runlevel [!2345] + +respawn + +script + if [ -f "/etc/service/consul" ]; then + . /etc/service/consul + fi + +export GOMAXPROCS=`nproc` + +exec /usr/local/bin/consul agent \ + -config-dir="/etc/consul.d/server" \ + ${CONSUL_FLAGS} \ + >>/var/log/consul.log 2>&1 +end script \ No newline at end of file diff --git a/consul/consul.sh b/consul/consul.sh index bbd0ae3..7df0cad 100644 --- a/consul/consul.sh +++ b/consul/consul.sh @@ -1,22 +1,53 @@ #!/bin/bash # Update list of packages +export DEBIAN_FRONTEND=noninteractive sudo apt-get update -# Install unzip package, needed to decompress Consul download -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.4.1_linux_amd64.zip +if [[ ! -e /usr/bin/unzip ]]; then + apt-get -yqq install unzip +fi -# Decompress and remove Consul download -unzip 0.4.1_linux_amd64.zip -rm 0.4.1_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://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip -# Create directories needed by Consul -sudo mkdir -p /etc/consul.d/{bootstrap,server} -sudo chmod -R a+rw /etc/consul.d/ + # Decompress and remove Consul download + unzip 0.5.2_linux_amd64.zip + rm 0.5.2_linux_amd64.zip + # 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 +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 +sudo chown root:root /etc/init/consul.conf +sudo mv /home/vagrant/config.json /etc/consul.d/server/config.json +sudo chown root:consul /etc/consul.d/server/config.json diff --git a/consul/server.json b/consul/server.json index 3b301ad..2417bec 100644 --- a/consul/server.json +++ b/consul/server.json @@ -5,5 +5,5 @@ "data_dir": "/tmp", "log_level": "INFO", "enable_syslog": true, - "start_join": ["192.168.1.101", "192.168.1.102", "192.168.1.103"] + "start_join": ["192.168.100.101", "192.168.100.102", "192.168.100.103"] } \ No newline at end of file diff --git a/consul/servers.yml b/consul/servers.yml index ebe79e9..d15854f 100644 --- a/consul/servers.yml +++ b/consul/servers.yml @@ -3,14 +3,14 @@ box: "slowe/ubuntu-trusty-x64" ram: "512" vcpu: "1" - priv_ip: 192.168.1.101 -- name: consul-02 - box: slowe/ubuntu-trusty-x64 + priv_ip: 192.168.100.101 +- name: "consul-02" + box: "slowe/ubuntu-trusty-x64" ram: "512" vcpu: "1" - priv_ip: 192.168.1.102 -- name: consul-03 - box: slowe/ubuntu-trusty-x64 + priv_ip: 192.168.100.102 +- name: "consul-03" + box: "slowe/ubuntu-trusty-x64" ram: "512" vcpu: "1" - priv_ip: 192.168.1.103 + priv_ip: 192.168.100.103