Module: PulpHelper::Repo

Defined in:
lib/pulphelper/repo.rb

Constant Summary collapse

REPO_TYPE_RPM =
"rpm-repo"
REPO_TYPE_PUPPET =
"puppet-repo"
YUM_IMPORTER_ID =
"yum_importer"
PUPPET_IMPORTER_ID =
'puppet_importer'
YUM_DISTRIBUTOR_ID =
"yum_distributor"
PUPPET_DISTRIBUTOR_ID =
"puppet_distributor"
YUM_EXPORT_DISTRIBUTOR_ID =
"export_distributor"

Instance Method Summary collapse

Instance Method Details

#copy_puppet_between_repo!(from_repo, to_repo, author, name, version, delete_new = false, auto_publish = true) ⇒ Object

function



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/pulphelper/repo.rb', line 347

def copy_puppet_between_repo!(from_repo, to_repo, author,name, version, delete_new=false, auto_publish=true)
  search_params = get_puppet_search_params(author, name, version)
  begin
    puts "search_params :#{search_params}"
    unit_search_response=client.resources.unit.search("puppet_module", search_params[:criteria], search_params[:optional])
    if unit_search_response.code !=200
      raise "Exception: unit search faild with code #{unit_search_response.code}"
    end
    puts "search_response : #{unit_search_response.to_json}"
    found_units=JSON.parse(unit_search_response.to_json)
    unit_id=found_units.first['_id']

    unless unit_id && unit_id.length >0
      raise "No puppet module found to copy"
    end
    copy_unit_ids= {
        :ids => [unit_id]
    }
    copy_response=client.extensions.puppet_module.copy(from_repo, to_repo, copy_unit_ids)
    if copy_response.code !=200 && copy_response.code !=202
      raise "Exception: unit copy failed with code #{copy_response.code}"
    end
    if(delete_new)
      delete_puppet_newer!(to_repo,author, name, version, auto_publish )
    end
  rescue StandardError => e
    raise "Error copy module between repo: #{e.to_s}"
  end
end

#copy_rpm_between_repo!(from_repo, to_repo, name, version, release, arch, delete_new = false, auto_publish = true) ⇒ Object

delete_rpm_newer



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/pulphelper/repo.rb', line 295

def copy_rpm_between_repo!(from_repo, to_repo, name, version, release, arch, delete_new=false, auto_publish=true)
  search_params=get_rpm_search_params(name, version, release, arch)
  begin
    unit_search_response=client.resources.unit.search('rpm', search_params[:criteria], search_params[:optional])

    if unit_search_response.code !=200
      raise "Exception: Cannot find unit"
    end
    found_units=JSON.parse(unit_search_response.to_json)
    unit_id=found_units.first['_id']

    copy_unit_ids= {
        :ids => [unit_id]
    }
    copy_response=client.extensions.rpm.copy(from_repo, to_repo, copy_unit_ids)
    if copy_response.code !=200 && copy_response.code !=202
      raise "Exception: unit copy failed with code #{copy_response.code}"
    end

    if(delete_new)
      delete_rpm_newer!(to_repo, name, version, release, arch,  auto_publish)
    end
  rescue StandardError => e
    raise "Exception: Error copy module between repo: #{e.to_s}"
  end
end

#create_puppet_repo(repo_id:, display_name: nil, description: '', feed_url: nil, queries: nil, remove_missing: false, serve_http: true, serve_https: false, auto_publish: false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/pulphelper/repo.rb', line 188

def create_puppet_repo(repo_id:, display_name: nil , description: '', feed_url: nil, queries: nil,  remove_missing: false,  serve_http: true, serve_https: false, auto_publish: false)
  importer = Runcible::Models::PuppetImporter.new
  importer.feed = feed_url if feed_url
  importer.remove_missing = remove_missing
  importer.queries = queries
  puppet_distributor = Runcible::Models::PuppetDistributor.new nil, serve_http, serve_https
  puppet_distributor.auto_publish = auto_publish
  puppet_distributor.id = 'puppet_distributor'

  optional = {
    :display_name => display_name,
    :description => description,
  }

  begin
    response = client.extensions.repository.create_with_importer_and_distributors(repo_id, importer, [puppet_distributor], optional)
    code=response.code
    body=response.body
    puts "code #{code}"
    case code
    when 201
      return true
    default
      raise "Operation failed, response code:#{code}, #{response}"
    end
  rescue Exception => e
    raise "Failed to create repo, #{e.message}"
  end
end

#create_rpm_repo(repo_id:, display_name: nil, description: '', feed_url: nil, relative_url: nil, serve_http: true, serve_https: false, auto_publish: false) ⇒ Object

list_repo_details



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pulphelper/repo.rb', line 141

def create_rpm_repo(repo_id:, display_name: nil , description: '', feed_url: nil, relative_url: nil, serve_http: true, serve_https: false, auto_publish: false)

  if relative_url.nil? || relative_url.strip.length<1
    raise "Invalid relative_url"
  end
  importer = Runcible::Models::YumImporter.new
  importer.feed = feed_url if feed_url
  yum_distributor = Runcible::Models::YumDistributor.new relative_url, serve_http, serve_https
  yum_distributor.auto_publish = auto_publish
  yum_distributor.id = 'yum_distributor'

  export_distributor = Runcible::Models::ExportDistributor.new serve_http, serve_https, relative_url
  export_distributor.auto_publish = auto_publish
  export_distributor.id = 'export_distributor'

  optional = {
    :display_name => display_name,
    :description => description,
  }

  begin
    version = get_version #get server version
    # pulp issue https://pulp.plan.io/issues/1520
    if Gem::Version.new('2.8.0') > Gem::Version.new(version)
      def export_distributor.config
        to_ret = as_json
        to_ret.delete('auto_publish')
        to_ret.delete('id')
        to_ret.delete('relative_url')
        to_ret
      end
    end
    response = client.extensions.repository.create_with_importer_and_distributors(repo_id, importer, [yum_distributor, export_distributor], optional)
    code=response.code
    body=response.body
    puts "code #{code}"
    case code
    when 201
      return true
    default
      raise "Operation failed, response code:#{code}, #{response}"
    end
  rescue Exception => e
    raise "Failed to create repo, #{e.message}"
  end
end

#delete_puppet_newer!(forge_id, author, name, version, auto_publish = false) ⇒ Object

copy_rpm_between_repo



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/pulphelper/repo.rb', line 322

def delete_puppet_newer!(forge_id, author, name, version, auto_publish=false)
  #/pulp/api/v2/repositories/<repo_id>/actions/unassociate/
  criteria = get_puppet_unit_assoc_criteria(author, name, version)
  begin
    unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
    #pulp api documented response code
    if unassociate_response.code !=202
      raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
    end
    if auto_publish
      publish_response=client.extensions.repository.publish_all(forge_id)

      presponse=JSON.parse(publish_response.to_json)
      if presponse.nil? || presponse[0]["spawned_tasks"].length<1
        raise "Exception: repo publish requeste failed, response : #{publish_response}"
      end
      # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
      #    raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
      # end
    end
  rescue StandardError => e
    raise "Exception: Error delete module newer than #{author}-#{name}-#{version} from repo #{forge_id}: #{e.message}"
  end
end

#delete_repository(repo_id) ⇒ Object

copy puppet



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/pulphelper/repo.rb', line 377

def delete_repository(repo_id)
  begin
    response=client.resources.repository.delete(repo_id)
    case response.code
    when 202
      true
    when 404
      rase "Repository does not exist."
    default
      raise "Failed to delete repository."
    end
  rescue StandardError => e
    raise "Excpetion: Failed to delete, #{e.message}"
  end
end

#delete_rpm_newer!(forge_id, name, version, release, arch, auto_publish = false) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/pulphelper/repo.rb', line 271

def delete_rpm_newer!(forge_id, name, version, release, arch, auto_publish=false)
  criteria = get_rpm_unit_ass_criteria(name, version, release, arch)
  begin
    unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
    #pulp api documented response code
    if unassociate_response.code !=202
      raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
    end
    if auto_publish
      publish_response=client.extensions.repository.publish_all(forge_id)
      presponse=JSON.parse(publish_response.to_json)
      if presponse.nil? || presponse[0]["spawned_tasks"].length<1
        raise "Exception: repo publish requeste failed, response : #{publish_response}"
      end

      # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
      #    raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
      # end
    end
  rescue StandardError => e
    raise "Error delete rpm pakcage older than #{name}-#{version}-#{release} from repo #{forge_id}: #{e.message}"
  end
end

#get_repo(repo_id) ⇒ Object

puppet repo distributors:

distributor_type_id: "puppet_distributor"
  auto_publish:
  last_publish
  config :

importers:

importer_type_id: "puppet_importer"


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pulphelper/repo.rb', line 71

def get_repo(repo_id)
  response=client.extensions.repository.retrieve_with_details(repo_id)
  code=response.code
  body=response.body
  case code
  when 200
    repo=JSON.parse(body.to_json)
    type = repo["notes"]["_repo-type"]
    #puts repos
    repo_data=nil
    case type
      when REPO_TYPE_RPM
        yum_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_distributor'}[0]
        yum_importer = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_importer'}[0]
        distributor = nil
        if  yum_distributor
          distributor = {
            :auto_publish => yum_distributor["auto_publish"],
            :last_publish => yum_distributor["last_publish"],
            :config => yum_distributor["config"]
          }
        end
        importer = nil
        if yum_importer
          importer = {
            :last_sync => yum_importer["last_sync"],
            :config => yum_importer["config"]
          }
        end

        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"],
          :content_unit_counts => repo["content_unit_counts"],
          :type => REPO_TYPE_RPM,
          :last_unit_removed => repo["last_unit_removed"],
          :last_unit_added => repo["last_unit_added"],
          :distributor => distributor,
          :importer => importer,
        }
        #puts repos
      when REPO_TYPE_PUPPET
        puppet_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'puppet_distributor'}[0]
        distributor =nil
        if puppet_distributor
          distributor = {
            :auto_publish => puppet_distributor["auto_publish"],
            :last_publish => puppet_distributor["last_publish"],
            :config => puppet_distributor["config"]
          }
        end
        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"],
          :content_unit_counts => repo["content_unit_counts"],
          :type => REPO_TYPE_PUPPET,
          :last_unit_removed => repo["last_unit_removed"],
          :last_unit_added => repo["last_unit_added"],
          :distributor => distributor
        }
      else
      end
      repo_data
  else
    raise "Exception: cannot get repository detail: response code :#{code}"
  end
end

#list_repo(type) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pulphelper/repo.rb', line 16

def list_repo(type)
  # use $in seems buggy
  criteria = {
    "filters" => {
      "notes._repo-type" => type
    }
  }
  #puts "criteria:#{criteria}"
  response=client.resources.repository.search(criteria)
  code=response.code
  body=response.body
  #puts "list result: #{body}"
  result=[]
  case code
  when 200
    repos=JSON.parse(body.to_json)
    #puts repos
    repos.each do |repo|
        repo_data={
          :id => repo["id"],
          :name => repo["display_name"],
          :description => repo["description"]
        }
        #puts repos
        result << repo_data
    end
  else
    raise "Exception: cannot list repository: response code :#{code}"
  end
  return result
end

#publish_repo!(forge_id) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/pulphelper/repo.rb', line 218

def publish_repo!(forge_id)
  message = "Publish #{forge_id} submitted successfully"
  begin
    publish_response=client.extensions.repository.publish_all(forge_id)

    ##
    ## Runcilbe does not proper include respone code
    ##
    presponse=JSON.parse(publish_response.to_json)
    if presponse.nil? || presponse[0]["spawned_tasks"].length<1
      raise "Exception: repo publish requeste failed, response : #{publish_response}"
    end
    publish_response
  rescue StandardError => e
    raise "Excpetion: Failed to publish, #{e.message}"
  end
end

#sync_repo!(forge_id) ⇒ Object

publish_repo



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/pulphelper/repo.rb', line 236

def sync_repo!(forge_id)
  begin
    sync_response=client.extensions.repository.sync(forge_id)
    ##
    ## Runcilbe does not proper include respone code
    ##
    if sync_response.code !=201 && !"#{sync_response.code}".start_with?("20")
      if sync_response.code == 404
        raise "repository #{forge_id} not found"
      end
      raise "Sync #{forge_id} failed with http response code: #{sync_response.code}"
    end
    sync_response
  rescue StandardError => e
    raise "Excpetion: Failed to Sync, #{e.message}"
  end
end

#sync_status(forge_id) ⇒ Object

sync_repo



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/pulphelper/repo.rb', line 254

def sync_status(forge_id)
  begin
    sync_response=client.extensions.repository.sync_status(forge_id)
    ##
    ## Runcilbe does not proper include respone code
    ##
    if sync_response.code !=200 && !"#{sync_response.code}".start_with?("20")
      if sync_response.code == 404
        raise "repository #{forge_id} not found"
      end
      raise "Sync #{forge_id} failed with http response code: #{sync_response.code}"
    end
  rescue StandardError => e
    raise "Excpetion: Failed to Sync, #{e.message}"
  end
end