diff --git a/manifests/check.pp b/manifests/check.pp new file mode 100644 index 00000000..8742882c --- /dev/null +++ b/manifests/check.pp @@ -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'] +} \ No newline at end of file diff --git a/manifests/init.pp b/manifests/init.pp index 57c73a5f..3e80411b 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -42,7 +42,7 @@ class { 'consul::install': } -> class { 'consul::config': } ~> - class { 'consul::service': } -> + class { 'consul::run_service': } -> Class['consul'] } diff --git a/manifests/run_service.pp b/manifests/run_service.pp new file mode 100644 index 00000000..0466cce0 --- /dev/null +++ b/manifests/run_service.pp @@ -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, + } + +} diff --git a/manifests/service.pp b/manifests/service.pp index a0d6f7ee..20038126 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -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'] +} \ No newline at end of file diff --git a/spec/defines/consul_check_spec.rb b/spec/defines/consul_check_spec.rb new file mode 100644 index 00000000..a9af0e31 --- /dev/null +++ b/spec/defines/consul_check_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/defines/consul_service_spec.rb b/spec/defines/consul_service_spec.rb new file mode 100644 index 00000000..1a16be9d --- /dev/null +++ b/spec/defines/consul_service_spec.rb @@ -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 \ No newline at end of file diff --git a/templates/check.json.erb b/templates/check.json.erb new file mode 100644 index 00000000..5772363d --- /dev/null +++ b/templates/check.json.erb @@ -0,0 +1,2 @@ +<%- require 'json' -%> +<%= JSON.pretty_generate(Hash[@check_hash.sort]) %> diff --git a/templates/service.json.erb b/templates/service.json.erb new file mode 100644 index 00000000..e69608bb --- /dev/null +++ b/templates/service.json.erb @@ -0,0 +1,2 @@ +<%- require 'json' -%> +<%= JSON.pretty_generate(Hash[@service_hash.sort]) %>