Skip to content

Commit

Permalink
Merge pull request #4 from bellycard/default-recipe-with-service
Browse files Browse the repository at this point in the history
Add default recipe which installs and starts consul as a service
  • Loading branch information
John Bellone committed May 6, 2014
2 parents 45b0025 + adc1fca commit 295fcc9
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ platforms:
install_dir: '/usr'

suites:
- name: default
run_list:
- recipe[consul::default]
- name: binary
run_list:
- recipe[consul::binary_install]
Expand Down
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,51 @@ Ubuntu 12.04, 14.04
<td><tt>['consul']['version']</tt></td>
<td>String</td>
<td>Version to install</td>
<td><tt>0.1.0</tt></td>
<td><tt>0.2.0</tt></td>
</tr>
<tr>
<td><tt>['consul']['base_url']</tt></td>
<td>String</td>
<td>Base URL for binary downloads</td>
<td><tt>https://dl.bintray.com/mitchellh/consul/</tt></td>
</tr>
<tr>
<tr>
<td><tt>['consul']['install_method']</tt></td>
<td>String</td>
<td>Method to install consul with when using default recipe: binary or source</td>
<td><tt>binary</tt></td>
</tr>
<tr>
<td><tt>['consul']['install_dir']</tt></td>
<td>String</td>
<td>Directory to install binary to.</td>
<td><tt>/usr/local/bin</tt></td>
</tr>
<tr>
<td><tt>['consul']['service_mode']</tt></td>
<td>String</td>
<td>Mode to run consul as: bootstrap, server, or client</td>
<td><tt>bootstrap</tt></td>
</tr>
<tr>
<td><tt>['consul']['data_dir']</tt></td>
<td>String</td>
<td>Location to store consul's data in</td>
<td><tt>/var/lib/consul</tt></td>
</tr>
<tr>
<td><tt>['consul']['servers']</tt></td>
<td>Array Strings</td>
<td>Consul servers to join</td>
<td><tt>[]</tt></td>
</tr>
</table>

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

Expand Down
6 changes: 6 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@

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',
'0.2.0_linux_386' => '564d6d8837c0db3efc4848bd85fad7911a291fcd948e74deae4d9ac6ce2a4637',
'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] = []
3 changes: 3 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
29 changes: 29 additions & 0 deletions recipes/service.rb
Original file line number Diff line number Diff line change
@@ -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
95 changes: 95 additions & 0 deletions templates/default/consul-init.erb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/integration/default/bats/consul.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

export PATH=$PATH:/usr/local/bin
@test "consul is installed and in the PATH" {
which consul
}
12 changes: 12 additions & 0 deletions test/integration/default/serverspec/consul_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/integration/default/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 295fcc9

Please sign in to comment.