From 783b595fc7fa4c34553d19dac9b66e80c5779b69 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Tue, 16 Oct 2012 17:39:47 -0700 Subject: [PATCH] Make puppetdb startup timeout configurable In some environments, puppetdb can take longer than 10 seconds to start up. Prior to this commit, that value was hard coded and the module would sometimes fail when it wouldn't have failed with a slightly larger timeout. This commit makes the timeout configurable, and also increases the default value to 15 seconds. --- .../puppetdb_conn_validator/puppet_https.rb | 27 ++++++++++++------- lib/puppet/type/puppetdb_conn_validator.rb | 14 ++++++++++ manifests/master/config.pp | 24 ++++++++++------- manifests/params.pp | 3 ++- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/lib/puppet/provider/puppetdb_conn_validator/puppet_https.rb b/lib/puppet/provider/puppetdb_conn_validator/puppet_https.rb index 95d5a9cb..1cb788f4 100644 --- a/lib/puppet/provider/puppetdb_conn_validator/puppet_https.rb +++ b/lib/puppet/provider/puppetdb_conn_validator/puppet_https.rb @@ -26,7 +26,7 @@ def attempt_connection end return true rescue Errno::ECONNREFUSED => e - Puppet.warning "Unable to connect to puppetdb server (#{host}:#{port}): #{e.inspect} " + Puppet.notice "Unable to connect to puppetdb server (#{host}:#{port}): #{e.inspect} " return false end end @@ -38,17 +38,26 @@ def attempt_connection setup from the local puppet environment to authenticate." def exists? + start_time = Time.now + timeout = resource[:timeout] + success = attempt_connection unless success - # It can take several seconds for the puppetdb server to start up; - # especially on the first install. Therefore, our first connection attempt - # may fail. Here we have somewhat arbitrarily chosen to retry one time - # after ten seconds if that situation arises. May want to revisit this, - # but it seems to work OK for the common use case. - Puppet.notice("Failed to connect to puppetdb; sleeping 10 seconds before retry") - sleep 10 - success = attempt_connection + while (Time.now - start_time) < timeout + # It can take several seconds for the puppetdb server to start up; + # especially on the first install. Therefore, our first connection attempt + # may fail. Here we have somewhat arbitrarily chosen to retry every 10 + # seconds until the configurable timeout has expired. + Puppet.notice("Failed to connect to puppetdb; sleeping 2 seconds before retry") + sleep 2 + success = attempt_connection + end end + + unless success + Puppet.notice("Failed to connect to puppetdb within timeout window of #{timeout} seconds; giving up.") + end + success end diff --git a/lib/puppet/type/puppetdb_conn_validator.rb b/lib/puppet/type/puppetdb_conn_validator.rb index caaf346f..48cb8e91 100644 --- a/lib/puppet/type/puppetdb_conn_validator.rb +++ b/lib/puppet/type/puppetdb_conn_validator.rb @@ -23,4 +23,18 @@ desc 'The port that the puppetdb server should be listening on.' end + newparam(:timeout) do + desc 'The max number of seconds that the validator should wait before giving up and deciding that puppetdb is not running; defaults to 15 seconds.' + defaultto 15 + + validate do |value| + # This will raise an error if the string is not convertible to an integer + Integer(value) + end + + munge do |value| + Integer(value) + end + end + end diff --git a/manifests/master/config.pp b/manifests/master/config.pp index dc6d76de..072f237a 100644 --- a/manifests/master/config.pp +++ b/manifests/master/config.pp @@ -23,6 +23,10 @@ # be installed. You may specify an explicit version # number, 'present', or 'latest'. Defaults to # 'present'. +# ['puppetdb_startup_timeout'] - The maximum amount of time that the module +# should wait for puppetdb to start up; this is most +# important during the initial install of puppetdb. +# Defaults to 15 seconds. # ['restart_puppet'] - If true, the module will restart the puppet master when # necessary. The default is 'true'. If set to 'false', # you must restart the service manually in order to pick @@ -43,15 +47,16 @@ # TODO: finish porting this to use params # class puppetdb::master::config( - $puppetdb_server = $::clientcert, - $puppetdb_port = 8081, - $manage_routes = true, - $manage_storeconfigs = true, - $puppet_confdir = $puppetdb::params::puppet_confdir, - $puppet_conf = $puppetdb::params::puppet_conf, - $puppetdb_version = $puppetdb::params::puppetdb_version, - $terminus_package = $puppetdb::params::terminus_package, - $puppet_service_name = $puppetdb::params::puppet_service_name, + $puppetdb_server = $::clientcert, + $puppetdb_port = 8081, + $manage_routes = true, + $manage_storeconfigs = true, + $puppet_confdir = $puppetdb::params::puppet_confdir, + $puppet_conf = $puppetdb::params::puppet_conf, + $puppetdb_version = $puppetdb::params::puppetdb_version, + $terminus_package = $puppetdb::params::terminus_package, + $puppet_service_name = $puppetdb::params::puppet_service_name, + $puppetdb_startup_timeout = $puppetdb::params::puppetdb_startup_timeout, $restart_puppet = true, ) inherits puppetdb::params { @@ -64,6 +69,7 @@ puppetdb_conn_validator { 'puppetdb_conn': puppetdb_server => $puppetdb_server, puppetdb_port => $puppetdb_port, + timeout => $puppetdb_startup_timeout, require => Package[$terminus_package], } diff --git a/manifests/params.pp b/manifests/params.pp index 48a960c2..4a9992ca 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -62,5 +62,6 @@ $terminus_package = 'puppetdb-terminus' } - $puppet_conf = "${puppet_confdir}/puppet.conf" + $puppet_conf = "${puppet_confdir}/puppet.conf" + $puppetdb_startup_timeout = 15 }