Class: Pulp::Api

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet_x/pulp/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_locationObject



75
76
77
78
# File 'lib/puppet_x/pulp/api.rb', line 75

def self.api_location
  config = global_config
  "https://#{config['server']['host']}:#{config['server']['port']}#{config['server']['api_prefix']}"
end

.bool_to_sym(value) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/puppet_x/pulp/api.rb', line 34

def self.bool_to_sym(value)
  case value
  when true then :true
  when false then :false
  else
    value
  end
end

.cert_pathObject



80
81
82
83
84
85
86
87
# File 'lib/puppet_x/pulp/api.rb', line 80

def self.cert_path
  config = global_config
  begin
    File.expand_path("#{config['filesystem']['id_cert_dir']}/#{config['filesystem']['id_cert_filename']}")
  rescue
    File.expand_path("#{ENV['USER']}/#{config['filesystem']['id_cert_filename']}")
  end
end

.check_loginObject



89
90
91
92
93
# File 'lib/puppet_x/pulp/api.rb', line 89

def self.
  if renew_user_certificate?(cert_path)
    get_user_certificate(cert_path, "#{api_location}/v2/actions/login/")
  end
end

.get_repo_properties(uri, id) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/puppet_x/pulp/api.rb', line 125

def self.get_repo_properties(uri, id)
  properties = {}
  data = request(:get, "#{uri}/#{id}/", details: true)
  data.each do |k, v|
    properties[k.to_sym] = bool_to_sym(v)
  end
  properties
end

.get_rpm_reposObject



134
135
136
137
# File 'lib/puppet_x/pulp/api.rb', line 134

def self.get_rpm_repos
  params = {:criteria => {:filters => {:notes => {'_repo-type' => 'rpm-repo'}}}}
  request(:post, 'v2/repositories/search/', params)
end

.get_user_certificate(certificate_path, login_url) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/puppet_x/pulp/api.rb', line 53

def self.get_user_certificate(certificate_path, )
  home       = ENV['HOME'] || '/root'
  config     = ini_parse("#{home}/.pulp/admin.conf")
  uri        = URI.parse()
  basic_auth = {:username => config['auth']['username'], :password => config['auth']['password']}
  api = Pulp::ApiClient.new(uri.host, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE)
  response = api.post(uri.request_uri, basic_auth: basic_auth)
  File.open(certificate_path, 'w') { |f| f.puts response.values }
end

.get_yum_importer_syncs(repo_id) ⇒ Object



139
140
141
# File 'lib/puppet_x/pulp/api.rb', line 139

def self.get_yum_importer_syncs(repo_id)
  request(:get, "v2/repositories/#{repo_id}/importers/yum_importer/schedules/sync/")
end

.global_configObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet_x/pulp/api.rb', line 63

def self.global_config
  config = ini_parse('/etc/pulp/admin/admin.conf')
  config['server']                         ||= {}
  config['filesystem']                     ||= {}
  config['server']['host']                 ||= Facter['fqdn'].value
  config['server']['port']                 ||= 443
  config['server']['api_prefix']           ||= "/pulp/api"
  config['filesystem']['id_cert_dir']      ||= "~/.pulp"
  config['filesystem']['id_cert_filename'] ||= "user-cert.pem"
  config
end

.ini_parse(file) ⇒ Object

Modified method from the AWS SDK, rather than including an extra library just to parse an ini file



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet_x/pulp/api.rb', line 12

def self.ini_parse(file)
  file = File.new(file)
  current_section = {}
  map = {}
  file.rewind
  file.each_line do |line|
    next if line.empty? || line =~ /^\s*#/
    line = line.split(/^|\s;/).first
    section = line.match(/^\s*\[([^\[\]]+)\]\s*$/) unless line.nil?
    if section
      current_section = section[1]
    elsif current_section
      item = line.match(/^\s*(.+?)\s*(:|=)\s*(.+?)\s*$/) unless line.nil?
      if item
        map[current_section] = map[current_section] || {}
        map[current_section][item[1]] = item[3]
      end
    end
  end
  map
end

.renew_user_certificate?(path) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/puppet_x/pulp/api.rb', line 43

def self.renew_user_certificate?(path)
  if File.file?(path)
    cert       = OpenSSL::X509::Certificate.new(File.read(path))
    expiration = cert.not_after.to_datetime
    expiration < DateTime.now
  else
    true
  end
end

.request(method, path, params = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet_x/pulp/api.rb', line 104

def self.request(method, path, params = {})
  
  uri = URI.parse("#{api_location}/#{path}")
  uri.path.squeeze!('/')
  begin
    api = Pulp::ApiClient.new(uri.host, uri.port, {
      use_ssl: true,
      verify_mode: OpenSSL::SSL::VERIFY_NONE,
      cert: user_auth[:cert],
      key: user_auth[:key]
    })

    Puppet.debug "Sending #{method.upcase} request to #{uri}"
    Puppet.debug "=> Params: #{params.to_json}" unless params.empty?
    api.send(method.to_s.downcase, uri.path, params)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise Puppet::Error, "#{method.upcase} request to #{uri} failed: #{e}"
  end
end

.user_authObject



95
96
97
98
99
100
101
102
# File 'lib/puppet_x/pulp/api.rb', line 95

def self.user_auth
  auth = {:cert => nil, :key => nil}
  if File.file?(cert_path)
    auth[:cert] = OpenSSL::X509::Certificate.new(File.read(cert_path))
    auth[:key]  = OpenSSL::PKey::RSA.new(File.read(cert_path))
  end
  auth
end

Instance Method Details

#get_repo(repo_id) ⇒ Object



160
161
162
# File 'lib/puppet_x/pulp/api.rb', line 160

def get_repo(repo_id)
  request(:get, "v2/repositories/#{repo_id}/")
end

#get_reposObject



156
157
158
# File 'lib/puppet_x/pulp/api.rb', line 156

def get_repos
  request(:get, 'v2/repositories/')
end

#request(*args) ⇒ Object



143
144
145
# File 'lib/puppet_x/pulp/api.rb', line 143

def request(*args)
  self.class.request(*args)
end

#sym_to_bool(value) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/puppet_x/pulp/api.rb', line 147

def sym_to_bool(value)
  case value
  when 'true', :true then true
  when 'false', :false then false
  else
    value
  end
end