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

Discussion : Wait for installer state to be ok before continuing. #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AllCops:
Exclude:
- 'pkg/**/*'
- 'vendor/**/*'
TargetRubyVersion: 2.0
TargetRubyVersion: 2.2

Metrics/BlockLength:
Exclude:
Expand Down
46 changes: 46 additions & 0 deletions lib/puppet/provider/aem_crx_package/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,49 @@ 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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixed port '4502' should come from the "port" parameter -> see build_cfg method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but for that i needed to modify the 'build_client' function, which i did not want to do for this PR. It is mainly intended as a discussion entry-point and definitely not as a final clean-code PR.

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])
begin
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
raise "wait_for_install_ok Response '#{response.code}' is not a Net::HTTPSuccess" unless response.is_a?(Net::HTTPSuccess)
data = JSON.parse(response.body)
check_install_status data
rescue Errno::EADDRNOTAVAIL, JSON::ParserError, RuntimeError => e
Puppet.info("wait_for_install_ok exception 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 check_install_status(data)
if data['Active'] == true || data['ActiveResourceCount'] != 0
@installed_resource_count = nil
raise "Active: #{data['Active']} (req: false), ActiveResourceCount: #{data['ActiveResourceCount']} (req: 0)"
elsif @installed_resource_count.nil? || @installed_resource_count != data['InstalledResourceCount']
@installed_resource_count = data['InstalledResourceCount']
raise "Adding timeout to wait for 'InstalledResourceCount' (#{@installed_resource_count}) to stabilise"
end
end

def find_package
wait_for_install_ok
client = build_client

path = "/etc/packages/#{@resource[:group]}/#{@resource[:pkg]}-.zip"
Expand Down Expand Up @@ -145,22 +187,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
Expand Down