Class: AemCrxPkgMgr

Inherits:
Object
  • Object
show all
Defined in:
lib/aemcrxpkgmgr.rb,
lib/aemcrxpkgmgr/version.rb

Overview

AEM CRX Package Manager

Constant Summary collapse

VERSION =
'0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AemCrxPkgMgr

Returns a new instance of AemCrxPkgMgr.



12
13
14
15
16
17
18
19
20
# File 'lib/aemcrxpkgmgr.rb', line 12

def initialize(options = {})
  @host = options[:host] || 'http://localhost:4502'
  @user = options[:user] || 'admin'
  @pass = options[:pass]
  @includeversions = options[:includeversions] || false
  @keys_to_extract = options[:keys_to_extract]
  @output = options[:output] || 'ruby'
  @debug = options[:debug]
end

Instance Attribute Details

#includeversionsObject

Returns the value of attribute includeversions.



11
12
13
# File 'lib/aemcrxpkgmgr.rb', line 11

def includeversions
  @includeversions
end

#keys_to_extractObject

Returns the value of attribute keys_to_extract.



11
12
13
# File 'lib/aemcrxpkgmgr.rb', line 11

def keys_to_extract
  @keys_to_extract
end

#outputObject

Returns the value of attribute output.



11
12
13
# File 'lib/aemcrxpkgmgr.rb', line 11

def output
  @output
end

Instance Method Details

#delete_crx_request(uri) ⇒ Object



61
62
63
64
65
66
# File 'lib/aemcrxpkgmgr.rb', line 61

def delete_crx_request(uri)
  req = Net::HTTP::Post.new(uri)
  req.basic_auth(@user, @pass)
  req.set_form_data('cmd' => 'delete')
  req
end

#delete_crx_zip(list) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/aemcrxpkgmgr.rb', line 51

def delete_crx_zip(list)
  return unless list
  @delete_crx_zip_ok = 0
  @delete_crx_zip_status = []
  list.each do |path|
    delete_crx_zip_single path
  end
  @delete_crx_zip_ok == list.length
end

#delete_crx_zip_single(path) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/aemcrxpkgmgr.rb', line 68

def delete_crx_zip_single(path)
  uri = URI(@host + '/crx/packmgr/service/script.html' + path)
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(delete_crx_request(uri))
  end
  @delete_crx_zip_status << response
  @delete_crx_zip_ok += 1 if response.is_a? Net::HTTPSuccess
end

#extract_keysObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/aemcrxpkgmgr.rb', line 90

def extract_keys
  return if @keys_to_extract.length.zero?
  @query_data.map! do |element|
    if @keys_to_extract.length == 1
      element[@keys_to_extract.first]
    else
      element.select { |key, _| @keys_to_extract.include? key }
    end
  end
end

#format_output(data) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/aemcrxpkgmgr.rb', line 101

def format_output(data)
  case @output
  when 'single' then output_single data
  when 'yaml' then data.to_yaml
  when 'json' then data.to_json
  when 'pp' then pp(data, '')
  else
    data
  end
end

#get(uri) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aemcrxpkgmgr.rb', line 22

def get(uri)
  retries ||= 100
  retry_timeout = 3

  request = Net::HTTP::Get.new(uri)
  request.basic_auth(@user, @pass)

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end
  raise "AemCrxPkgMgr.get Response '#{response.code}' is not a Net::HTTPSuccess" unless response.is_a?(Net::HTTPSuccess)
  response
rescue Errno::EADDRNOTAVAIL, RuntimeError => e
  puts "AemCrxPkgMgr.get : #{e.class} : #{e.message}"
  will_retry = (retries -= 1) >= 0
  if will_retry
    sleep retry_timeout
    retry
  end
  raise
end

#output_single(data) ⇒ Object



112
113
114
115
# File 'lib/aemcrxpkgmgr.rb', line 112

def output_single(data)
  raise 'Resultset contains more than 1 element and "single" was requested.' if data.length != 1
  data.first
end

#pkg_query(query) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aemcrxpkgmgr.rb', line 77

def pkg_query(query)
  uri = pkg_query_uri(query)
  response = get uri

  raise "HTTP response code : #{response.code}, message : #{response.message}" unless response.is_a? Net::HTTPSuccess

  @query_data = JSON.parse(response.body)['results']

  extract_keys

  format_output @query_data unless @query_data.empty?
end

#pkg_query_uri(query) ⇒ Object



44
45
46
47
48
49
# File 'lib/aemcrxpkgmgr.rb', line 44

def pkg_query_uri(query)
  uri = URI.parse(@host + '/crx/packmgr/list.jsp')
  params = { q: query, includeVersions: @includeversions }
  uri.query = URI.encode_www_form(params)
  uri
end