From fc82a5ef6d6d96ad173e9dadd1147b5f176f9202 Mon Sep 17 00:00:00 2001 From: "Michael H. Oshita" Date: Sat, 3 May 2014 19:12:45 +0900 Subject: [PATCH 1/6] Update README.md default version is 0.2.0, according to the attributes. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b75778d..928d2efd 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'] From 5d2b0cc4434685c8f6a5673c416e994b8d087bf2 Mon Sep 17 00:00:00 2001 From: Kevin Reedy Date: Mon, 5 May 2014 19:02:06 -0500 Subject: [PATCH 2/6] add default recipe and kitchen suite --- .kitchen.yml | 3 +++ attributes/default.rb | 1 + recipes/default.rb | 2 ++ 3 files changed, 6 insertions(+) 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/attributes/default.rb b/attributes/default.rb index 3a6755f3..1eba200e 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', diff --git a/recipes/default.rb b/recipes/default.rb index b0a5a770..12706222 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -14,3 +14,5 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +include_recipe "consul::#{ node[:consul][:install_method] }_install" From 1f0bc8d734b4257556513109cd874faa6a608993 Mon Sep 17 00:00:00 2001 From: Kevin Reedy Date: Mon, 5 May 2014 19:43:36 -0500 Subject: [PATCH 3/6] start consul service in default recipe --- attributes/default.rb | 4 + recipes/default.rb | 1 + recipes/service.rb | 26 +++++ templates/default/consul-init.erb | 95 +++++++++++++++++++ .../default/serverspec/consul_spec.rb | 12 +++ .../default/serverspec/spec_helper.rb | 4 + 6 files changed, 142 insertions(+) create mode 100644 recipes/service.rb create mode 100644 templates/default/consul-init.erb create mode 100644 test/integration/default/serverspec/consul_spec.rb create mode 100644 test/integration/default/serverspec/spec_helper.rb diff --git a/attributes/default.rb b/attributes/default.rb index 1eba200e..d97eaeac 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -25,3 +25,7 @@ '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' diff --git a/recipes/default.rb b/recipes/default.rb index 12706222..ad994187 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -16,3 +16,4 @@ # 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..80f1b2de --- /dev/null +++ b/recipes/service.rb @@ -0,0 +1,26 @@ +# Determine service params +case node[:consul][:service_mode] +when 'bootstrap' + service_params = '-server -bootstrap' +when 'server' + service_params = '-server' +when 'client' + service_params = '' +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/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..47a2e21d --- /dev/null +++ b/test/integration/default/serverspec/spec_helper.rb @@ -0,0 +1,4 @@ +require 'serverspec' + +include SpecInfra::Helper::Exec +include SpecInfra::Helper::DetectOS From a56dfed10070156e3d610291d37455a7ab3cb14e Mon Sep 17 00:00:00 2001 From: Kevin Reedy Date: Mon, 5 May 2014 20:52:20 -0500 Subject: [PATCH 4/6] fix PATH issues in tests --- test/integration/default/bats/consul.bats | 1 + test/integration/default/serverspec/spec_helper.rb | 4 ++++ 2 files changed, 5 insertions(+) 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/spec_helper.rb b/test/integration/default/serverspec/spec_helper.rb index 47a2e21d..f9b3d8ac 100644 --- a/test/integration/default/serverspec/spec_helper.rb +++ b/test/integration/default/serverspec/spec_helper.rb @@ -2,3 +2,7 @@ include SpecInfra::Helper::Exec include SpecInfra::Helper::DetectOS + +RSpec.configure do |c| + c.path = '/usr/local/bin:/sbin:/bin:/usr/bin' +end From e418bfc645335d7bff4a71f77d6d9488e890cb7f Mon Sep 17 00:00:00 2001 From: Kevin Reedy Date: Mon, 5 May 2014 21:34:56 -0500 Subject: [PATCH 5/6] allow connecting to existing servers --- attributes/default.rb | 1 + recipes/service.rb | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index d97eaeac..e63cf107 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -29,3 +29,4 @@ # Service attributes default[:consul][:service_mode] = 'bootstrap' default[:consul][:data_dir] = '/var/lib/consul' +default[:consul][:servers] = [] diff --git a/recipes/service.rb b/recipes/service.rb index 80f1b2de..257d9c1e 100644 --- a/recipes/service.rb +++ b/recipes/service.rb @@ -1,11 +1,14 @@ +# 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' + service_params = "-server #{server_conn}" when 'client' - service_params = '' + service_params = server_conn else raise 'node[:consul][:service_mode] must be "bootstrap", "server", or "client"' end From adc1fcad481f6a2127d33785ca1b8a55c0708487 Mon Sep 17 00:00:00 2001 From: Kevin Reedy Date: Mon, 5 May 2014 21:47:51 -0500 Subject: [PATCH 6/6] Update README --- README.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 928d2efd..33276506 100644 --- a/README.md +++ b/README.md @@ -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