Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define consul::service and consul::check types #8

Merged
merged 3 commits into from
Jun 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions manifests/check.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
define consul::check(
$ttl = undef,
$script = undef,
$interval = undef,
$notes = undef,
) {
include consul
$id = $title

$basic_hash = {
'id' => $id,
'name' => $name,
}

if $ttl and $interval {
fail("Only one of ttl and interval can be defined")
}

if $ttl {
if $script {
fail("script must not be defined for ttl checks")
}
$check_definition = {
ttl => $ttl,
}
} elsif $interval {
if (! $script) {
fail("script must be defined for interval checks")
}
$check_definition = {
script => $script,
interval => $interval,
}
} else {
fail("One of ttl or interval must be defined.")
}

if $notes {
$notes_hash = {
notes => $notes
}
} else {
$notes_hash = {}
}

$check_hash = {
check => merge($basic_hash, $check_definition, $port_hash)
}

File[$consul::config_dir] ->
file { "${consul::config_dir}/check_${id}.json":
content => template('consul/check.json.erb'),
} ~> Class['consul::run_service']
}
2 changes: 1 addition & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

class { 'consul::install': } ->
class { 'consul::config': } ~>
class { 'consul::service': } ->
class { 'consul::run_service': } ->
Class['consul']

}
13 changes: 13 additions & 0 deletions manifests/run_service.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# == Class consul::service
#
# This class is meant to be called from consul
# It ensure the service is running
#
class consul::run_service {

service { 'consul':
ensure => $consul::service_ensure,
enable => $consul::service_enable,
}

}
67 changes: 57 additions & 10 deletions manifests/service.pp
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
# == Class consul::service
#
# This class is meant to be called from consul
# It ensure the service is running
#
class consul::service {
define consul::service(
$tags = [],
$port = undef,
$check_ttl = undef,
$check_script = undef,
$check_interval = undef,
) {
include consul
$id = $title

service { 'consul':
ensure => $consul::service_ensure,
enable => $consul::service_enable,
$basic_hash = {
'id' => $id,
'name' => $name,
'tags' => $tags,
}

}
if $check_ttl and $check_interval {
fail("Only one of check_ttl and check_interval can be defined")
}

if $check_ttl {
if $check_script {
fail("check_script must not be defined for ttl checks")
}
$check_hash = {
check => {
ttl => $check_ttl,
}
}
} elsif $check_interval {
if (! $check_script) {
fail("check_script must be defined for interval checks")
}
$check_hash = {
check => {
script => $check_script,
interval => $check_interval,
}
}
} else {
$check_hash = {}
}

if $port {
$port_hash = {
port => $port
}
} else {
$port_hash = {}
}

$service_hash = {
service => merge($basic_hash, $check_hash, $port_hash)
}

File[$consul::config_dir] ->
file { "${consul::config_dir}/service_${id}.json":
content => template('consul/service.json.erb'),
} ~> Class['consul::run_service']
}
66 changes: 66 additions & 0 deletions spec/defines/consul_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'spec_helper'

describe 'consul::check' do
let(:facts) {{ :architecture => 'x86_64' }}
let(:title) { "my_check" }

describe 'with no args' do
let(:params) {{}}

it {
expect { should raise_error(Puppet::Error) }
}
end
describe 'with interval' do
let(:params) {{
'interval' => '30s',
'script' => 'true'
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/)
.with_content(/"name" *: *"my_check"/)
.with_content(/"check" *: *{/)
.with_content(/"interval" *: *"30s"/)
.with_content(/"script" *: *"true"/)
}
end
describe 'with ttl' do
let(:params) {{
'ttl' => '30s',
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/)
.with_content(/"name" *: *"my_check"/)
.with_content(/"check" *: *{/)
.with_content(/"ttl" *: *"30s"/)
}
end
describe 'with both ttl and interval' do
let(:params) {{
'ttl' => '30s',
'interval' => '60s'
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
describe 'with both ttl and script' do
let(:params) {{
'ttl' => '30s',
'script' => 'true'
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
describe 'with interval but no script' do
let(:params) {{
'interval' => '30s',
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
end
65 changes: 65 additions & 0 deletions spec/defines/consul_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'spec_helper'

describe 'consul::service' do
let(:facts) {{ :architecture => 'x86_64' }}
let(:title) { "my_service" }

describe 'with no args' do
let(:params) {{}}

it {
should contain_file("/etc/consul/service_my_service.json")
.with_content(/"service" *: *{/)
.with_content(/"id" *: *"my_service"/)
.with_content(/"name" *: *"my_service"/)
}
end
describe 'with interval' do
let(:params) {{
'check_interval' => '30s',
'check_script' => 'true'
}}
it {
should contain_file("/etc/consul/service_my_service.json") \
.with_content(/"check" *: *{/)
.with_content(/"interval" *: *"30s"/)
.with_content(/"script" *: *"true"/)
}
end
describe 'with ttl' do
let(:params) {{
'check_ttl' => '30s',
}}
it {
should contain_file("/etc/consul/service_my_service.json") \
.with_content(/"check" *: *{/)
.with_content(/"ttl" *: *"30s"/)
}
end
describe 'with both ttl and interval' do
let(:params) {{
'check_ttl' => '30s',
'check_interval' => '60s'
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
describe 'with both ttl and script' do
let(:params) {{
'check_ttl' => '30s',
'check_script' => 'true'
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
describe 'with interval but no script' do
let(:params) {{
'interval' => '30s',
}}
it {
expect { should raise_error(Puppet::Error) }
}
end
end
2 changes: 2 additions & 0 deletions templates/check.json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- require 'json' -%>
<%= JSON.pretty_generate(Hash[@check_hash.sort]) %>
2 changes: 2 additions & 0 deletions templates/service.json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- require 'json' -%>
<%= JSON.pretty_generate(Hash[@service_hash.sort]) %>