diff --git a/.rubocop.yml b/.rubocop.yml index 738e409..549805a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,7 +4,7 @@ AllCops: Exclude: - 'pkg/**/*' - 'vendor/**/*' - TargetRubyVersion: 2.0 + TargetRubyVersion: 2.2 Metrics/BlockLength: Exclude: diff --git a/lib/puppet/provider/aem_crx_package/ruby.rb b/lib/puppet/provider/aem_crx_package/ruby.rb index 640964f..62d143f 100644 --- a/lib/puppet/provider/aem_crx_package/ruby.rb +++ b/lib/puppet/provider/aem_crx_package/ruby.rb @@ -103,7 +103,46 @@ def build_client @client end + def wait_for_install_ok + require 'uri' + require 'json' + require 'net/http' + retries ||= @resource[:retries] + retry_timeout = @resource[:retry_timeout] + host = 'http://localhost:4502' + path = '/system/sling/monitoring/mbeans/org/apache/sling/installer/Installer/Sling+OSGi+Installer.json' + uri = URI.parse(host + path) + request = Net::HTTP::Get.new(uri) + request.basic_auth(@resource[:username], @resource[:password]) + + # try http get of Sling+OSGi+Installer info... + # retry untill http 200 or Timeout + # check untill "Active":false and "ActiveResourceCount":0 or untill Timeout + begin + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request(request) + end + data = JSON.parse(response.body) + # Maybe we will need to check for more than 1 ok result + # when installing some packages can trigger install of sub-packages... + if data['Active'] == true || data['ActiveResourceCount'] != 0 + raise "Active: #{data['Active']} (req: false), ActiveResourceCount: #{data['ActiveResourceCount']} (req: 0)" + end + rescue Errno::EADDRNOTAVAIL, JSON::ParserError, RuntimeError => e + Puppet.info("wait_for_install_ok FAIL for Aem_crx_package[#{@resource[:pkg]}]: #{e.class} : #{e.message} :") + will_retry = (retries -= 1) >= 0 + if will_retry + Puppet.debug("Waiting #{retry_timeout} seconds before retrying installer state query") + sleep retry_timeout + Puppet.debug("Retrying installer state query; remaining retries: #{retries}") + retry + end + raise + end + end + def find_package + wait_for_install_ok client = build_client path = "/etc/packages/#{@resource[:group]}/#{@resource[:pkg]}-.zip" @@ -145,22 +184,26 @@ def find_version(ary) end def upload_package(install = false) + wait_for_install_ok client = build_client file = File.new(@resource[:source]) client.service_post(file, install: install) end def install_package + wait_for_install_ok client = build_client client.service_exec('install', @resource[:pkg], @resource[:group], @resource[:version]) end def uninstall_package + wait_for_install_ok client = build_client client.service_exec('uninstall', @resource[:pkg], @resource[:group], @resource[:version]) end def remove_package + wait_for_install_ok client = build_client client.service_exec('delete', @resource[:pkg], @resource[:group], @resource[:version]) end