From 7f44766b9eb24ce3c9e1ec22b3cd21098711e1de Mon Sep 17 00:00:00 2001 From: Gavin Heavyside Date: Thu, 12 Mar 2015 19:39:38 +0000 Subject: [PATCH] Add support for HTTP checks --- README.md | 14 ++++++++++++-- resources/check_def.rb | 2 ++ resources/service_def.rb | 8 ++++++-- .../recipes/check_def_with_id_create.rb | 1 + spec/unit/resources/check_def_spec.rb | 9 +++++++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a88822ff..24427c30 100644 --- a/README.md +++ b/README.md @@ -419,9 +419,9 @@ the `consul::ui` recipe in your node's `run_list`. notifies :reload, 'service[consul]' end -##### Adding service with check +##### Adding services with checks - consul_service_def 'voice1' do + consul_service_def "voice1' do port 5060 tags ['_sip._udp'] check( @@ -431,6 +431,16 @@ the `consul::ui` recipe in your node's `run_list`. notifies :reload, 'service[consul]' end + consul_service_def 'server1' do + port 80 + tags ['http'] + check( + interval: '10s', + http: 'http://localhost:80' + ) + notifies :reload, 'service[consul]' + end + ##### Removing service consul_service_def 'voice1' do diff --git a/resources/check_def.rb b/resources/check_def.rb index 26850d56..1efb45ff 100644 --- a/resources/check_def.rb +++ b/resources/check_def.rb @@ -23,6 +23,7 @@ attribute :name, name_attribute: true, required: true, kind_of: String attribute :id, kind_of: String attribute :script, kind_of: String +attribute :http, kind_of: String attribute :ttl, kind_of: String attribute :interval, kind_of: String attribute :notes, kind_of: String @@ -48,6 +49,7 @@ def to_hash hash[:check][:id] = id unless id.nil? hash[:check][:script] = script unless script.nil? hash[:check][:ttl] = ttl unless ttl.nil? + hash[:check][:http] = http unless http.nil? hash[:check][:interval] = interval unless interval.nil? hash[:check][:notes] = notes unless notes.nil? hash diff --git a/resources/service_def.rb b/resources/service_def.rb index 6b1b9990..33b972d1 100644 --- a/resources/service_def.rb +++ b/resources/service_def.rb @@ -25,7 +25,7 @@ attribute :port, kind_of: Integer attribute :tags, kind_of: Array, default: nil attribute :check, kind_of: Hash, default: nil, callbacks: { - 'Checks must be a hash containing either a `:ttl` key/value or a `:script` and `:interval` key/value' => ->(check) do + 'Checks must be a hash containing either a `:ttl` key/value or a `:script/:http` and `:interval` key/value' => ->(check) do Chef::Resource::ConsulServiceDef.validate_check(check) end } @@ -35,7 +35,7 @@ def self.validate_check(check) return false end - if check.key?(:ttl) && (!check.key?(:interval) && !check.key?(:script)) + if check.key?(:ttl) && ( [:interval, :script, :http].none?{|key| check.key?(key)} ) return true end @@ -43,6 +43,10 @@ def self.validate_check(check) return true end + if check.key?(:interval) && check.key?(:http) + return true + end + false end diff --git a/spec/fixtures/cookbooks/consul_spec/recipes/check_def_with_id_create.rb b/spec/fixtures/cookbooks/consul_spec/recipes/check_def_with_id_create.rb index b332c86f..ad49a705 100644 --- a/spec/fixtures/cookbooks/consul_spec/recipes/check_def_with_id_create.rb +++ b/spec/fixtures/cookbooks/consul_spec/recipes/check_def_with_id_create.rb @@ -4,5 +4,6 @@ script "curl http://localhost:8888/health" interval "10s" ttl "50s" + http "http://localhost:8888/health" notes "Blahblah" end diff --git a/spec/unit/resources/check_def_spec.rb b/spec/unit/resources/check_def_spec.rb index 144dce13..0e037421 100644 --- a/spec/unit/resources/check_def_spec.rb +++ b/spec/unit/resources/check_def_spec.rb @@ -13,8 +13,13 @@ end it "register the check" do - ['"name": "dummy name"', '"id": "uniqueid"', '"script": "curl http://localhost:8888/health"', - '"interval": "10s"', '"ttl": "50s"', '"notes": "Blahblah"'].each do |content| + ['"name": "dummy name"', + '"id": "uniqueid"', + '"script": "curl http://localhost:8888/health"', + '"http": "http://localhost:8888/health"', + '"interval": "10s"', + '"ttl": "50s"', + '"notes": "Blahblah"'].each do |content| expect(chef_run).to render_file(check_def_path) .with_content(content) end