diff --git a/.kitchen.yml b/.kitchen.yml index c09bb7bd..4fd3c8d5 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -20,6 +20,9 @@ platforms: install_dir: '/usr' suites: + - name: default + run_list: + - recipe[consul::default] - name: binary run_list: - recipe[consul::binary_install] diff --git a/README.md b/README.md index 0b75778d..33276506 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Ubuntu 12.04, 14.04 ['consul']['version'] String Version to install - 0.1.0 + 0.2.0 ['consul']['base_url'] @@ -28,19 +28,43 @@ Ubuntu 12.04, 14.04 Base URL for binary downloads https://dl.bintray.com/mitchellh/consul/ - + + ['consul']['install_method'] + String + Method to install consul with when using default recipe: binary or source + binary + + ['consul']['install_dir'] String Directory to install binary to. /usr/local/bin + + ['consul']['service_mode'] + String + Mode to run consul as: bootstrap, server, or client + bootstrap + + + ['consul']['data_dir'] + String + Location to store consul's data in + /var/lib/consul + + + ['consul']['servers'] + Array Strings + Consul servers to join + [] + ## Usage ### consul::default -This uses the binary installation recipe by default. +This uses the binary installation recipe by default. It also starts consul at boot time. ### consul::binary_install diff --git a/attributes/default.rb b/attributes/default.rb index 3a6755f3..e63cf107 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -17,6 +17,7 @@ default[:consul][:base_url] = 'https://dl.bintray.com/mitchellh/consul/' default[:consul][:version] = '0.2.0' +default[:consul][:install_method] = 'binary' default[:consul][:install_dir] = '/usr/local/bin' default[:consul][:checksums] = { '0.2.0_darwin_amd64' => '0a03a42fa3ea945d19152bc2429b4098a195a68f7a8f10a1b63e805f7f251fe9', @@ -24,3 +25,8 @@ '0.2.0_linux_amd64' => '2802ce8e173ee37e1a1e992ba230963e09d4b41ce4ac60c1222714c036787b4f', '0.2.0_windows_386' => '353da0b0321293d81a1e2351b7bc6902d462c6573e44a4495d1a61df6b0a0179' } + +# Service attributes +default[:consul][:service_mode] = 'bootstrap' +default[:consul][:data_dir] = '/var/lib/consul' +default[:consul][:servers] = [] diff --git a/recipes/default.rb b/recipes/default.rb index b0a5a770..ad994187 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -14,3 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +include_recipe "consul::#{ node[:consul][:install_method] }_install" +include_recipe 'consul::service' diff --git a/recipes/service.rb b/recipes/service.rb new file mode 100644 index 00000000..257d9c1e --- /dev/null +++ b/recipes/service.rb @@ -0,0 +1,29 @@ +# Determine servers to connect to +server_conn = node[:consul][:servers].map{|s| "-join=#{s}"}.join(' ') + +# Determine service params +case node[:consul][:service_mode] +when 'bootstrap' + service_params = '-server -bootstrap' +when 'server' + service_params = "-server #{server_conn}" +when 'client' + service_params = server_conn +else + raise 'node[:consul][:service_mode] must be "bootstrap", "server", or "client"' +end + +template '/etc/init.d/consul' do + source 'consul-init.erb' + mode 0755 + variables( + consul_binary: "#{node[:consul][:install_dir]}/consul", + data_dir: node[:consul][:data_dir], + service_params: service_params + ) +end + +service 'consul' do + supports status: true, restart: true + action [:enable, :start] +end diff --git a/templates/default/consul-init.erb b/templates/default/consul-init.erb new file mode 100644 index 00000000..07fa6911 --- /dev/null +++ b/templates/default/consul-init.erb @@ -0,0 +1,95 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: consul +# Required-Start: $network $remote_fs $syslog +# Required-Stop: $network $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Consul Service Discovery Platform +# Description: Consul is a tool for discovering and configuring services +# in your infrastructure. It provides several key features: +# * Service Discovery +# * Health Checking +# * Key/Valuye Store +# * Multi Datacenter +### END INIT INFO + +CMD="<%= @consul_binary %> agent <%= @service_params %> -data-dir <%= @data_dir %>" +NAME="consul" + +PIDFILE="/var/run/$NAME.pid" +LOGFILE="/var/log/$NAME.log" + +get_pid() { + cat "$PIDFILE" +} + +is_running() { + [ -f "$PIDFILE" ] && ps `get_pid` > /dev/null 2>&1 +} + +case "$1" in + start) + if is_running; then + echo "$NAME already running" + else + echo "Starting $NAME" + $CMD >> "$LOGFILE" & + echo $! > "$PIDFILE" + if ! is_running; then + echo "Unable to start $NAME, see $LOGFILE" + exit 1 + fi + fi + ;; + stop) + if is_running; then + echo -n "Stopping $NAME..." + kill `get_pid` + for i in {1..10} + do + if ! is_running; then + break + fi + + echo -n "." + sleep 1 + done + echo + + if is_running; then + echo "$NAME not stopped; may still be shutting down or shutdown may have failed" + exit 1 + else + echo "$NAME stopped" + if [ -f "$PIDFILE" ]; then + rm "$PIDFILE" + fi + fi + else + echo "$NAME not running" + fi + ;; + restart) + $0 stop + if is_running; then + echo "Unable to stop $NAME, will not attempt to start" + exit 1 + fi + $0 start + ;; + status) + if is_running; then + echo "$NAME is running" + else + echo "$NAME is stopped" + exit 1 + fi + ;; + *) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 + ;; +esac + +exit 0 diff --git a/test/integration/default/bats/consul.bats b/test/integration/default/bats/consul.bats index c789d2af..8749c56c 100755 --- a/test/integration/default/bats/consul.bats +++ b/test/integration/default/bats/consul.bats @@ -1,5 +1,6 @@ #!/usr/bin/env bash +export PATH=$PATH:/usr/local/bin @test "consul is installed and in the PATH" { which consul } diff --git a/test/integration/default/serverspec/consul_spec.rb b/test/integration/default/serverspec/consul_spec.rb new file mode 100644 index 00000000..f208d23f --- /dev/null +++ b/test/integration/default/serverspec/consul_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe service('consul') do + it { should be_enabled } + it { should be_running } +end + +[8300, 8400, 8500, 8600].each do |p| + describe port(p) do + it { should be_listening } + end +end diff --git a/test/integration/default/serverspec/spec_helper.rb b/test/integration/default/serverspec/spec_helper.rb new file mode 100644 index 00000000..f9b3d8ac --- /dev/null +++ b/test/integration/default/serverspec/spec_helper.rb @@ -0,0 +1,8 @@ +require 'serverspec' + +include SpecInfra::Helper::Exec +include SpecInfra::Helper::DetectOS + +RSpec.configure do |c| + c.path = '/usr/local/bin:/sbin:/bin:/usr/bin' +end