lita-pulp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +3 -0
  4. data/README.md +35 -0
  5. data/Rakefile +8 -0
  6. data/lib/lita-pulp.rb +15 -0
  7. data/lib/lita/handlers/pulp.rb +369 -0
  8. data/lib/pulphelper/misc.rb +16 -0
  9. data/lib/pulphelper/repo.rb +356 -0
  10. data/lib/pulphelper/unit.rb +107 -0
  11. data/lita-pulp.gemspec +28 -0
  12. data/locales/en.yml +23 -0
  13. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_copy_puppet_between_repo/copy_puppet_module.yml +86 -0
  14. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_copy_puppet_between_repo/copy_puppet_module_delete_new_and_publish.yml +217 -0
  15. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_copy_rpm_between_repo/copy_rpm_package.yml +86 -0
  16. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_copy_rpm_between_repo/copy_rpm_package_delete_new_and_publish.yml +264 -0
  17. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_puppet_repos/list_puppet_repos.yml +88 -0
  18. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_puppet_search/search_puppet_with_repo.yml +134 -0
  19. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_puppet_search/search_puppet_without_author_name.yml +134 -0
  20. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_puppet_search/search_puppet_without_repo.yml +134 -0
  21. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_rpm_repos/list_rpm_repos.yml +593 -0
  22. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_rpm_search/search_rpm_with_repo.yml +152 -0
  23. data/spec/fixtures/cassettes/Lita_Handlers_Pulp/_rpm_search/search_rpm_without_repo.yml +152 -0
  24. data/spec/lita/handlers/pulp_repo_spec.rb +35 -0
  25. data/spec/lita/handlers/pulp_unit_spec.rb +79 -0
  26. data/spec/spec_helper.rb +28 -0
  27. data/templates/.gitkeep +0 -0
  28. metadata +226 -0
@@ -0,0 +1,16 @@
1
+ require 'runcible'
2
+
3
+ module PulpHelper
4
+ module Misc
5
+ def client
6
+ Runcible::Instance.new({
7
+ :url => config.url,
8
+ :api_path => config.api_path||'pulp/api/v2',
9
+ :user => config.username,
10
+ :http_auth => {:password => config.password},
11
+ :verify_ssl => config.verify_ssl || false,
12
+ :logger => "",
13
+ })
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,356 @@
1
+ require 'json'
2
+ require 'runcible'
3
+
4
+ module PulpHelper
5
+ module Repo
6
+ REPO_TYPE_RPM="rpm-repo"
7
+ REPO_TYPE_PUPPET="puppet-repo"
8
+
9
+ def list_repo(type)
10
+ criteria = {
11
+ "filters" => {
12
+ "notes._repo-type" => {
13
+ "$in" => [type]
14
+ }
15
+ }
16
+ }
17
+ #puts "criteria:#{criteria}"
18
+ response=client.resources.repository.search(criteria)
19
+ code=response.code
20
+ body=response.body
21
+ result=[]
22
+ case code
23
+ when 200
24
+ repos=JSON.parse(body.to_json)
25
+ #puts repos
26
+ repos.each do |repo|
27
+ repo_data={
28
+ :id => repo["id"],
29
+ :name => repo["display_name"],
30
+ :description => repo["description"]
31
+ }
32
+ #puts repos
33
+ result << repo_data
34
+ end
35
+ else
36
+ raise "Exception: cannot list repository: response code :#{code}"
37
+ end
38
+ return result
39
+ end#list_repo
40
+
41
+ #yum repo
42
+ # distributors
43
+ # distributor_type_id : yum_distributor
44
+ # auto_publish: true or false
45
+ # last_publish:
46
+ # config:
47
+ # http:
48
+ # https:
49
+ # relative_url
50
+ # importers
51
+ # importer_type_id: yum_importer
52
+ # last_sync:
53
+ # config:
54
+ # feed
55
+
56
+ #puppet repo
57
+ # distributors:
58
+ # distributor_type_id: "puppet_distributor"
59
+ # auto_publish:
60
+ # last_publish
61
+ # config :
62
+ # importers:
63
+ # importer_type_id: "puppet_importer"
64
+ def list_repo_details(type)
65
+ criteria = {
66
+ "filters" => {
67
+ "notes._repo-type" => {
68
+ "$in" => [type]
69
+ }
70
+ }
71
+ }
72
+ #puts "criteria:#{criteria}"
73
+ response=client.resources.repository.search(criteria, {"details" => true})
74
+ code=response.code
75
+ body=response.body
76
+ result=[]
77
+ case code
78
+ when 200
79
+ repos=JSON.parse(body.to_json)
80
+ #puts repos
81
+ repos.each do |repo|
82
+ case type
83
+ when REPO_TYPE_RPM
84
+ yum_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_distributor'}[0]
85
+ yum_importer = repo["distributors"].select{ |d| d["distributor_type_id"] == 'yum_importer'}[0]
86
+
87
+ distributor = nil
88
+ if yum_distributor
89
+ distributor = {
90
+ :auto_publish => yum_distributor["auto_publish"],
91
+ :last_publish => yum_distributor["last_publish"],
92
+ :config => yum_distributor["config"]
93
+ }
94
+ end
95
+ importer = nil
96
+ if yum_importer
97
+ importer = {
98
+ :last_sync => yum_importer["last_sync"],
99
+ :config => yum_importer["config"]
100
+ }
101
+ end
102
+
103
+ repo_data={
104
+ :id => repo["id"],
105
+ :name => repo["display_name"],
106
+ :description => repo["description"],
107
+ :content_unit_counts => repo["content_unit_counts"],
108
+ :type => REPO_TYPE_RPM,
109
+ :distributor => distributor,
110
+ :importer => importer,
111
+ }
112
+ #puts repos
113
+ result << repo_data
114
+ when REPO_TYPE_PUPPET
115
+ puppet_distributor = repo["distributors"].select{ |d| d["distributor_type_id"] == 'puppet_distributor'}[0]
116
+ distributor =nil
117
+ if puppet_distributor
118
+ distributor = {
119
+ :auto_publish => yum_distributor["auto_publish"],
120
+ :last_publish => yum_distributor["last_publish"],
121
+ :config => yum_distributor["config"]
122
+ }
123
+ end
124
+ repo_data={
125
+ :id => repo["id"],
126
+ :name => repo["display_name"],
127
+ :description => repo["description"],
128
+ :content_unit_counts => repo["content_unit_counts"],
129
+ :type => REPO_TYPE_PUPPET,
130
+ :distributor => distributor
131
+ }
132
+ #puts repos
133
+ result << repo_data
134
+ else
135
+ end
136
+ end
137
+ else
138
+ raise "Exception: cannot list repository: response code :#{code}"
139
+ end
140
+ return result
141
+ end#list_repo_details
142
+
143
+ def publish_repo!(forge_id)
144
+ message = "Publish #{forge_id} submitted successfully"
145
+ begin
146
+ publish_response=client.extensions.repository.publish_all(forge_id)
147
+
148
+ ##
149
+ ## Runcilbe does not proper include respone code
150
+ ##
151
+ if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
152
+ raise "Publish #{forge_id} failed with http response code: #{publish_response.code}"
153
+ end
154
+ rescue StandardError => e
155
+ raise "Excpetion: Failed to publish, #{e.message}"
156
+ end
157
+ end#publish_repo
158
+
159
+ def delete_rpm_newer!(forge_id, name, version, release, arch, auto_publish=false)
160
+ criteria = get_rpm_unit_ass_criteria(name, version, release, arch)
161
+ begin
162
+ unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
163
+ #pulp api documented response code
164
+ if unassociate_response.code !=202
165
+ raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
166
+ end
167
+ if auto_publish
168
+ publish_response=client.extensions.repository.publish_all(forge_id)
169
+ presponse=JSON.parse(publish_response.to_json)
170
+ if presponse.nil? || presponse[0]["spawned_tasks"].length<1
171
+ raise "Exception: repo publish requeste failed, response : #{publish_response}"
172
+ end
173
+
174
+ # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
175
+ # raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
176
+ # end
177
+ end
178
+ rescue StandardError => e
179
+ raise "Error delete rpm pakcage older than #{name}-#{version}-#{release} from repo #{forge_id}: #{e.message}"
180
+ end
181
+ end#delete_rpm_newer
182
+
183
+ def copy_rpm_between_repo!(from_repo, to_repo, name, version, release, arch, delete_new=false, auto_publish=true)
184
+ search_params=get_rpm_search_params(name, version, release, arch)
185
+ begin
186
+ unit_search_response=client.resources.unit.search('rpm', search_params[:criteria], search_params[:optional])
187
+
188
+ if unit_search_response.code !=200
189
+ raise "Exception: Cannot find unit"
190
+ end
191
+ found_units=JSON.parse(unit_search_response.to_json)
192
+ unit_id=found_units.first['_id']
193
+
194
+ copy_unit_ids= {
195
+ :ids => [unit_id]
196
+ }
197
+ copy_response=client.extensions.rpm.copy(from_repo, to_repo, copy_unit_ids)
198
+ if copy_response.code !=200 && copy_response.code !=202
199
+ raise "Exception: unit copy failed with code #{copy_response.code}"
200
+ end
201
+
202
+ if(delete_new)
203
+ delete_rpm_newer!(to_repo, name, version, release, arch, auto_publish)
204
+ end
205
+ rescue StandardError => e
206
+ raise "Exception: Error copy module between repo: #{e.to_s}"
207
+ end
208
+ end#copy_rpm_between_repo
209
+
210
+ def delete_puppet_newer!(forge_id, author, name, version, auto_publish=false)
211
+ #/pulp/api/v2/repositories/<repo_id>/actions/unassociate/
212
+ criteria = get_puppet_unit_assoc_criteria(author, name, version)
213
+ begin
214
+ unassociate_response=client.resources.repository.unassociate_units(forge_id, criteria)
215
+ #pulp api documented response code
216
+ if unassociate_response.code !=202
217
+ raise "Exception: cannot unassociate unit, response code: #{unassociate_response.code}"
218
+ end
219
+ if auto_publish
220
+ publish_response=client.extensions.repository.publish_all(forge_id)
221
+
222
+ presponse=JSON.parse(publish_response.to_json)
223
+ if presponse.nil? || presponse[0]["spawned_tasks"].length<1
224
+ raise "Exception: repo publish requeste failed, response : #{publish_response}"
225
+ end
226
+ # if publish_response.code !=202 && !"#{publish_response.code}".start_with?("20")
227
+ # raise "Exception: repo publish requeste failed, response code : #{publish_response.code}"
228
+ # end
229
+ end
230
+ rescue StandardError => e
231
+ raise "Exception: Error delete module newer than #{author}-#{name}-#{version} from repo #{forge_id}: #{e.message}"
232
+ end
233
+ end#function
234
+
235
+ def copy_puppet_between_repo!(from_repo, to_repo, author,name, version, delete_new=false, auto_publish=true)
236
+ search_params = get_puppet_search_params(author, name, version)
237
+ begin
238
+ puts "search_params :#{search_params}"
239
+ unit_search_response=client.resources.unit.search("puppet_module", search_params[:criteria], search_params[:optional])
240
+ if unit_search_response.code !=200
241
+ raise "Exception: unit search faild with code #{unit_search_response.code}"
242
+ end
243
+ puts "search_response : #{unit_search_response.to_json}"
244
+ found_units=JSON.parse(unit_search_response.to_json)
245
+ unit_id=found_units.first['_id']
246
+
247
+ unless unit_id && unit_id.length >0
248
+ raise "No puppet module found to copy"
249
+ end
250
+ copy_unit_ids= {
251
+ :ids => [unit_id]
252
+ }
253
+ copy_response=client.extensions.puppet_module.copy(from_repo, to_repo, copy_unit_ids)
254
+ if copy_response.code !=200 && copy_response.code !=202
255
+ raise "Exception: unit copy failed with code #{copy_response.code}"
256
+ end
257
+ if(delete_new)
258
+ delete_puppet_newer!(to_repo,author, name, version, auto_publish )
259
+ end
260
+ rescue StandardError => e
261
+ raise "Error copy module between repo: #{e.to_s}"
262
+ end
263
+ end# copy puppet
264
+
265
+
266
+ private
267
+ def get_rpm_search_params (name, version, release, arch)
268
+ search_optional= {
269
+ :include_repos => true
270
+ }
271
+ search_criteria ={
272
+ :filters => {
273
+ :name => name,
274
+ :version=> version,
275
+ :release => release,
276
+ :arch => arch,
277
+ },
278
+ :sort => [["version", "descending"]],
279
+ :limit => 1,
280
+ :fields => ["name", "version", "release", "arch"]
281
+ }
282
+
283
+ search_params= {
284
+ :optional => search_optional,
285
+ :criteria => search_criteria,
286
+ }
287
+
288
+ return search_params
289
+ end# get_rpm_search_params
290
+
291
+ def get_rpm_unit_ass_criteria(name, version, release, arch)
292
+ #/pulp/api/v2/repositories/<repo_id>/actions/unassociate/
293
+ criteria= {
294
+ :filters => {
295
+ :unit => {
296
+ '$and' => [
297
+ {:name => name},
298
+ {:arch => arch},
299
+ '$or' => [
300
+ :version=> {
301
+ '$gt' => version
302
+ },
303
+ :release => {
304
+ '$gt' => release
305
+ }
306
+ ]
307
+ ]
308
+ }
309
+ },
310
+ :type_ids => ["rpm"]
311
+ }
312
+
313
+ return criteria
314
+ end#function
315
+
316
+ def get_puppet_search_params(author, name, version)
317
+ search_optional= {
318
+ :include_repos => true
319
+ }
320
+ search_criteria ={
321
+ :filters => {
322
+ :author => author,
323
+ :name => name,
324
+ :version=> version
325
+ },
326
+ :sort => [["version", "descending"]],
327
+ :limit => 1,
328
+ :fields => ["author", "name", "version"]
329
+ }
330
+
331
+ search_params={
332
+ :criteria => search_criteria,
333
+ :optional => search_optional,
334
+ }
335
+
336
+ return search_params
337
+ end#function
338
+ def get_puppet_unit_assoc_criteria(author, name, version)
339
+ criteria= {
340
+ :filters => {
341
+ :unit => {
342
+ '$and' => [
343
+ {:name => name},
344
+ {:author => author},
345
+ :version=> {
346
+ '$gt' => version
347
+ }
348
+ ]
349
+ }
350
+ },
351
+ :type_ids => ["puppet_module"]
352
+ }
353
+ return criteria
354
+ end#function
355
+ end#module
356
+ end#module
@@ -0,0 +1,107 @@
1
+ require 'json'
2
+ module PulpHelper
3
+ module Unit
4
+ def search_rpm(name, repo)
5
+ criteria = {
6
+ "fields" => ["name", "version", "release", "epoch", "checksum"],
7
+ "filters" => {
8
+ "name" => {
9
+ "$regex" => name
10
+ }
11
+ },
12
+ "sort" => [["epoch", "descending"], ["version", "descending"], ["release", "descending"]]
13
+ }
14
+ #puts "search criteria #{criteria} in repo : #{repo}"
15
+ response = client.resources.unit.search("rpm", criteria, {"include_repos" => true})
16
+ code = response.code
17
+ body = response.body
18
+ #puts body
19
+ results =[]
20
+ case code
21
+ when 200
22
+ units = JSON.parse(body.to_json)
23
+ units.each do |unit|
24
+ result = {
25
+ :name => unit["name"],
26
+ :epoch => unit["epoch"],
27
+ :version => unit["version"],
28
+ :release => unit["release"],
29
+ :checksum => unit["checksum"],
30
+ :repos => unit["repository_memberships"]
31
+ }
32
+ results << result
33
+ end
34
+ else
35
+ raise "Excpetion:response code = #{code}"
36
+ end
37
+ #filter by repo
38
+ if repo
39
+ results =results.select {|r|
40
+ r[:repos].include? repo
41
+ }
42
+ end
43
+ #puts "result:#{results}"
44
+ results
45
+ end # end search
46
+ def search_puppet(author, name, repo)
47
+ if author.nil?
48
+ criteria = {
49
+ "fields" => ["author","name", "version"],
50
+ "filters" => {
51
+ "name" => {
52
+ "$regex" => name
53
+ }
54
+ },
55
+ "sort" => [["author", "descending"],["name", "descending"],["version", "descending"]]
56
+ }
57
+ else
58
+ criteria = {
59
+ "fields" => ["author","name", "version"],
60
+ "filters" => {
61
+ "$and" => [
62
+ {
63
+ "name" => {
64
+ "$regex" => name
65
+ }
66
+ },
67
+ {
68
+ "author" => author
69
+ }
70
+ ]
71
+ },
72
+ "sort" => [["author", "descending"],["name", "descending"],["version", "descending"]]
73
+ }
74
+ end
75
+ #puts "search criteria #{criteria}"
76
+ response = client.resources.unit.search("puppet_module", criteria, {"include_repos" => true})
77
+ code = response.code
78
+ body = response.body
79
+ #puts body
80
+ results =[]
81
+ case code
82
+ when 200
83
+ units = JSON.parse(body.to_json)
84
+ units.each do |unit|
85
+ result ={
86
+ :name => unit["name"],
87
+ :version => unit["version"],
88
+ :author => unit["author"],
89
+ :repos => unit["repository_memberships"]
90
+ }
91
+ results << result
92
+ end
93
+ else
94
+ raise "Exception: search response code = #{code}"
95
+ end
96
+ #filter by repo
97
+ if repo
98
+ results =results.select {|r|
99
+ r[:repos].include? repo
100
+ }
101
+ end
102
+ #puts results
103
+ results
104
+ end
105
+
106
+ end
107
+ end