Project

General

Profile

Download (19.4 KB) Statistics
| Branch: | Tag: | Revision:

runcible / lib / runcible / extensions / repository.rb @ a5858c9b

1
module Runcible
2
  module Extensions
3
    class Repository < Runcible::Resources::Repository
4
      # Utility function that allows an importer to be added at repository creation time
5
      #
6
      # @param  [String]                               id       the id of the repository being created
7
      # @param  [Hash, Runcible::Extensions::Importer] importer either a hash representing an importer or
8
      #                                                         an Importer object
9
      # @return [RestClient::Response]                          the created repository
10
      def create_with_importer(id, importer)
11
        create_with_importer_and_distributors(id, importer)
12
      end
13

    
14
      # Utility function that allows distributors to be added at repository creation time
15
      #
16
      # @param  [String]                id            the id of the repository being created
17
      # @param  [Array]                 distributors  an array of hashes representing distributors or
18
      #                                               an array of Distributor objects
19
      # @return [RestClient::Response]                the created repository
20
      def create_with_distributors(id, distributors)
21
        create_with_importer_and_distributors(id, nil, distributors)
22
      end
23

    
24
      # Utility function that allows an importer and distributors to be added at repository creation time
25
      #
26
      # @param  [String]                                id            the id of the repository being created
27
      # @param  [Hash, Runcible::Extensions::Importer]  importer      either a hash representing an importer or
28
      #                                                               an Importer object
29
      # @param  [Array]                                 distributors  an array of hashes representing distributors or
30
      #                                                               an array of Distributor objects
31
      # @param  [Hash]                                  optional      container for all optional parameters
32
      # @return [RestClient::Response]                                the created repository
33
      def create_with_importer_and_distributors(id, importer, distributors = [], optional = {})
34
        if importer && importer.is_a?(Runcible::Models::Importer)
35
          optional[:importer_type_id] = importer.id
36
          optional[:importer_config] = importer.config
37
        elsif importer
38
          optional[:importer_type_id] = importer.delete('id') || importer.delete(:id)
39
          optional[:importer_config] = importer
40
        end
41

    
42
        repo_type = if importer.methods.include?(:repo_type)
43
                      importer.repo_type
44
                    elsif importer.is_a?(Hash) && importer.key?(:repo_type)
45
                      importer[:repo_type]
46
                    end
47

    
48
        if optional.key?(:importer_type_id) && repo_type
49
          # pulp needs _repo-type in order to determine the type of repo to create.
50
          optional[:notes] = { '_repo-type' => importer.repo_type }
51
        end
52

    
53
        optional[:distributors] = distributors.map do |d|
54
          if d.is_a?(Runcible::Models::Distributor)
55
            {'distributor_type_id' => d.type_id,
56
             'distributor_config' => d.config,
57
             'auto_publish' => d.auto_publish,
58
             'distributor_id' => d.id
59
            }
60
          else
61
            {'distributor_type_id' => d['type_id'],
62
             'distributor_config' => d['config'],
63
             'auto_publish' => d['auto_publish'],
64
             'distributor_id' => d['id']
65
            }
66
          end
67
        end unless distributors.empty?
68
        optional[:id] = id
69
        create(id, optional)
70
      end
71

    
72
      # Retrieves the sync status for a repository
73
      #
74
      # @param  [String]                repo_id the repository ID
75
      # @return [RestClient::Response]          a task representing the sync status
76
      def sync_status(repo_id)
77
        Runcible::Resources::Task.new(self.config).list(["pulp:repository:#{repo_id}", 'pulp:action:sync'])
78
      end
79

    
80
      # Retrieves the publish status for a repository
81
      #
82
      # @param  [String]                repo_id the repository ID
83
      # @return [RestClient::Response]          a task representing the sync status
84
      def publish_status(repo_id)
85
        Runcible::Resources::Task.new(self.config).list(["pulp:repository:#{repo_id}", 'pulp:action:publish'])
86
      end
87

    
88
      # Retrieves a set of repositories by their IDs
89
      #
90
      # @param  [Array]                repository_ids the repository ID
91
      # @return [RestClient::Response]                the set of repositories requested
92
      def search_by_repository_ids(repository_ids)
93
        criteria = {:filters =>
94
                      { 'id' => {'$in' => repository_ids}}
95
                   }
96
        search(criteria)
97
      end
98

    
99
      # Retrieves the RPM IDs for a single repository
100
      #
101
      # @param [String]             id the ID of the repository
102
      # @return [Array<String>]     the array of repository RPM IDs
103

    
104
      def rpm_ids(id)
105
        criteria = {:type_ids => [Runcible::Extensions::Rpm.content_type],
106
                    :fields => {:unit => [], :association => ['unit_id']}}
107
        self.unit_search(id, criteria).map { |i| i['unit_id'] }
108
      rescue RestClient::RequestTimeout
109
        self.logger.warn('Call to rpm_ids timed out')
110
        # lazy evaluated iterator from zero to infinite
111
        pages = Enumerator.new do |y|
112
          page = 0
113
          loop do
114
            y << page
115
            page += 1
116
          end
117
        end
118

    
119
        # TODO: this is hotfix, pagination support should be added to Runcible
120
        pages.reduce([]) do |rpm_ids, page|
121
          page_size = 500
122
          criteria  = { :type_ids => [Runcible::Extensions::Rpm.content_type],
123
                        :fields   => { :unit => [], :association => ['unit_id'] },
124
                        :limit    => page_size,
125
                        :skip     => 0 + page * page_size }
126
          result    = unit_search(id, criteria).map { |i| i['unit_id'] }
127
          rpm_ids.concat(result)
128
          if result.empty? || result.size < 500
129
            break rpm_ids
130
          else
131
            rpm_ids
132
          end
133
        end
134
      end
135

    
136
      # Retrieves the RPMs for a single repository
137
      #
138
      # @param  [String]                id the ID of the repository
139
      # @return [RestClient::Response]     the set of repository RPMs
140
      def rpms(id)
141
        criteria = {:type_ids => [Runcible::Extensions::Rpm.content_type]}
142
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
143
      end
144

    
145
      # Retrieves the RPMs by NVRE for a single repository
146
      #
147
      # @param  [String]                id      the ID of the repository
148
      # @param  [String]                name    the name of the RPMs
149
      # @param  [String]                version the version of the RPMs
150
      # @param  [String]                release the release of the RPMs
151
      # @param  [String]                epoch   the epoch of the RPMs
152
      # @return [RestClient::Response]          the set of repository RPMs
153
      def rpms_by_nvre(id, name, version = nil, release = nil, epoch = nil)
154
        and_condition = []
155
        and_condition << {:name => name} if name
156
        and_condition << {:version => version} if version
157
        and_condition << {:release => release} if release
158
        and_condition << {:epoch => epoch} if epoch
159

    
160
        criteria = {:type_ids => [Runcible::Extensions::Rpm.content_type],
161
                    :filters => {
162
                      :unit => {
163
                        '$and' => and_condition
164
                      }
165
                    },
166
                    :sort => {
167
                      :unit => [['name', 'ascending'], ['version', 'descending']]
168
                    }}
169
        unit_search(id, criteria).map { |p| p['metadata'].with_indifferent_access }
170
      end
171

    
172
      # Retrieves the errata IDs for a single repository
173
      #
174
      # @param  [String]                id the ID of the repository
175
      # @return [RestClient::Response]     the set of repository errata IDs
176
      def errata_ids(id)
177
        criteria = {:type_ids => [Runcible::Extensions::Errata.content_type],
178
                    :fields => {:unit => [], :association => ['unit_id']}}
179

    
180
        unit_search(id, criteria).map { |i| i['unit_id'] }
181
      end
182

    
183
      # Retrieves the errata for a single repository
184
      #
185
      # @param  [String]                id the ID of the repository
186
      # @return [RestClient::Response]     the set of repository errata
187
      def errata(id)
188
        criteria = {:type_ids => [Runcible::Extensions::Errata.content_type]}
189
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
190
      end
191

    
192
      # Retrieves the distributions for a single repository
193
      #
194
      # @param  [String]                id the ID of the repository
195
      # @return [RestClient::Response]     the set of repository distributions
196
      def distributions(id)
197
        criteria = {:type_ids => [Runcible::Extensions::Distribution.content_type]}
198

    
199
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
200
      end
201

    
202
      # Retrieves the package groups for a single repository
203
      #
204
      # @param  [String]                id the ID of the repository
205
      # @return [RestClient::Response]     the set of repository package groups
206
      def package_groups(id)
207
        criteria = {:type_ids => [Runcible::Extensions::PackageGroup.content_type]}
208

    
209
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
210
      end
211

    
212
      # Retrieves the package group categoriess for a single repository
213
      #
214
      # @param  [String]                id the ID of the repository
215
      # @return [RestClient::Response]     the set of repository package group categories
216
      def package_categories(id)
217
        criteria = {:type_ids => [Runcible::Extensions::PackageCategory.content_type]}
218
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
219
      end
220

    
221
      # Retrieves the puppet module IDs for a single repository
222
      #
223
      # @param  [String]                id the ID of the repository
224
      # @return [RestClient::Response]     the set of repository puppet module IDs
225
      def puppet_module_ids(id)
226
        criteria = {:type_ids => [Runcible::Extensions::PuppetModule.content_type],
227
                    :fields => {:unit => [], :association => ['unit_id']}}
228

    
229
        unit_search(id, criteria).map { |i| i['unit_id'] }
230
      end
231

    
232
      # Retrieves the puppet modules for a single repository
233
      #
234
      # @param  [String]                id the ID of the repository
235
      # @return [RestClient::Response]     the set of repository puppet modules
236
      def puppet_modules(id)
237
        criteria = {:type_ids => [Runcible::Extensions::PuppetModule.content_type]}
238
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
239
      end
240

    
241
      # Retrieves the ostree branch IDs for a single repository
242
      #
243
      # @param  [String]                id the ID of the repository
244
      # @return [RestClient::Response]  the set of repository ostree branch IDs
245
      def ostree_branch_ids(id)
246
        criteria = {:type_ids => [Runcible::Extensions::OstreeBranch.content_type],
247
                    :fields => {:unit => [], :association => ['unit_id']}}
248

    
249
        unit_search(id, criteria).map { |i| i['unit_id'] }
250
      end
251

    
252
      # Retrieves the ostree branches for a single repository
253
      #
254
      # @param  [String]                id the ID of the repository
255
      # @return [RestClient::Response]  the set of repository ostree branches
256
      def ostree_branches(id)
257
        criteria = {:type_ids => [Runcible::Extensions::OstreeBranch.content_type]}
258
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
259
      end
260

    
261
      # Retrieves the docker manifest IDs for a single repository
262
      #
263
      # @param  [String]                id the ID of the repository
264
      # @return [RestClient::Response]  the set of repository docker manifest IDs
265
      def docker_manifest_ids(id)
266
        criteria = {:type_ids => [Runcible::Extensions::DockerManifest.content_type],
267
                    :fields => {:unit => [], :association => ['unit_id']}}
268

    
269
        unit_search(id, criteria).map { |i| i['unit_id'] }
270
      end
271

    
272
      # Retrieves the docker manifests for a single repository
273
      #
274
      # @param  [String]                id the ID of the repository
275
      # @return [RestClient::Response]  the set of repository docker manifests
276
      def docker_manifests(id)
277
        criteria = {:type_ids => [Runcible::Extensions::DockerManifest.content_type]}
278
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
279
      end
280

    
281
      # Retrieves the docker manifest list IDs for a single repository
282
      #
283
      # @param  [String]                id the ID of the repository
284
      # @return [RestClient::Response]  the set of repository docker manifest IDs
285
      def docker_manifest_list_ids(id)
286
        criteria = {:type_ids => [Runcible::Extensions::DockerManifestList.content_type],
287
                    :fields => {:unit => [], :association => ['unit_id']}}
288

    
289
        unit_search(id, criteria).map { |i| i['unit_id'] }
290
      end
291

    
292
      # Retrieves the docker manifest lists for a single repository
293
      #
294
      # @param  [String]                id the ID of the repository
295
      # @return [RestClient::Response]  the set of repository docker manifests
296
      def docker_manifest_lists(id)
297
        criteria = {:type_ids => [Runcible::Extensions::DockerManifestList.content_type]}
298
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
299
      end
300

    
301
      # Retrieves the docker tag IDs for a single repository
302
      #
303
      # @param  [String]                id the ID of the repository
304
      # @return [RestClient::Response]  the set of repository docker tag IDs
305
      def docker_tag_ids(id)
306
        criteria = {:type_ids => [Runcible::Extensions::DockerTag.content_type],
307
                    :fields => {:unit => [], :association => ['unit_id']}}
308

    
309
        unit_search(id, criteria).map { |i| i['unit_id'] }
310
      end
311

    
312
      # Retrieves the docker tags for a single repository
313
      #
314
      # @param  [String]                id the ID of the repository
315
      # @return [RestClient::Response]  the set of repository docker tags
316
      def docker_tags(id)
317
        criteria = {:type_ids => [Runcible::Extensions::DockerTag.content_type]}
318
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
319
      end
320

    
321
      # Retrieves the docker image IDs for a single repository
322
      #
323
      # @param  [String]                id the ID of the repository
324
      # @return [RestClient::Response]  the set of repository docker image IDs
325
      def docker_image_ids(id)
326
        criteria = {:type_ids => [Runcible::Extensions::DockerImage.content_type],
327
                    :fields => {:unit => [], :association => ['unit_id']}}
328

    
329
        unit_search(id, criteria).map { |i| i['unit_id'] }
330
      end
331

    
332
      # Retrieves the docker images for a single repository
333
      #
334
      # @param  [String]                id the ID of the repository
335
      # @return [RestClient::Response]  the set of repository docker images
336
      def docker_images(id)
337
        criteria = {:type_ids => [Runcible::Extensions::DockerImage.content_type]}
338
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
339
      end
340

    
341
      # Updates the docker tags in a repo
342
      # @param  [String]  id the ID of the repository
343
      # @param [Hash]     tags for an image in the following format
344
      #                   the [{:image_id => <image hash>, :tag =>"value"}]
345
      # @return [RestClient::Response]
346
      def update_docker_tags(id, tags)
347
        update(id, :scratchpad => {:tags => tags})
348
      end
349

    
350
      # Retrieves the file IDs for a single repository
351
      #
352
      # @param  [String]                id the ID of the repository
353
      # @return [RestClient::Response]  the set of repository file IDs
354
      def file_ids(id)
355
        criteria = {:type_ids => [Runcible::Extensions::File.content_type],
356
                    :fields => {:unit => [], :association => ['unit_id']}}
357

    
358
        unit_search(id, criteria).map { |i| i['unit_id'] }
359
      end
360

    
361
      # Retrieves the files for a single repository
362
      #
363
      # @param  [String]                id the ID of the repository
364
      # @return [RestClient::Response]  the set of repository files
365
      def files(id)
366
        criteria = {:type_ids => [Runcible::Extensions::File.content_type]}
367
        unit_search(id, criteria).map { |i| i['metadata'].with_indifferent_access }
368
      end
369

    
370
      # Creates or updates a sync schedule for a repository
371
      #
372
      # @param  [String]                repo_id   the ID of the repository
373
      # @param  [String]                type      the importer type
374
      # @param  [String]                schedule  the time as an iso8601 interval
375
      # @return [RestClient::Response]            the newly created or updated schedule
376
      def create_or_update_schedule(repo_id, type, schedule)
377
        schedules = Runcible::Resources::RepositorySchedule.new(self.config).list(repo_id, type)
378
        if schedules.empty?
379
          Runcible::Resources::RepositorySchedule.new(self.config).create(repo_id, type, schedule)
380
        else
381
          Runcible::Resources::RepositorySchedule.new(self.config).update(repo_id, type,
382
                                                      schedules[0]['_id'], :schedule => schedule)
383
        end
384
      end
385

    
386
      # Removes a scheduled sync from a repository
387
      #
388
      # @param  [String]                repo_id   the ID of the repository
389
      # @param  [String]                type      the importer type
390
      # @return [RestClient::Response]
391
      def remove_schedules(repo_id, type)
392
        schedules = Runcible::Resources::RepositorySchedule.new(self.config).list(repo_id, type)
393
        schedules.each do |schedule|
394
          Runcible::Resources::RepositorySchedule.new(self.config).delete(repo_id, type, schedule['_id'])
395
        end
396
      end
397

    
398
      # Publishes a repository for all of it's distributors
399
      #
400
      # @param  [String]                repo_id the ID of the repository
401
      # @return [RestClient::Response]          set of tasks representing each publish
402
      def publish_all(repo_id)
403
        to_ret = []
404
        retrieve_with_details(repo_id)['distributors'].each do |d|
405
          to_ret << publish(repo_id, d['id'])
406
        end
407
        to_ret
408
      end
409

    
410
      # Retrieves a repository with details that include importer and distributors
411
      #
412
      # @param  [String]                repo_id the ID of the repository
413
      # @return [RestClient::Response]          the repository with full details
414
      def retrieve_with_details(repo_id)
415
        retrieve(repo_id, :details => true)
416
      end
417

    
418
      # Regenerate the applicability for consumers bound to a given set of repositories
419
      #
420
      # @param [String, Array]         ids  array of repo ids
421
      # @param [boolean]               parallel when true run the regeneration in parallel and return a task group
422
      #                                         tracking the summary,
423
      #                                         when false run this operation serially and return a list of
424
      #                                         spawned tasks that are to be tracked separately.
425
      #                                         False is the default option.
426
      # @return [RestClient::Response]
427
      def regenerate_applicability_by_ids(ids, parallel = false)
428
        criteria = {
429
          'parallel' => parallel,
430
          'repo_criteria' => { 'filters' => { 'id' => { '$in' => ids } } }
431
        }
432
        regenerate_applicability(criteria)
433
      end
434
    end
435
  end
436
end