satops 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module Helpers
2
+ def self.filter(hash, filter)
3
+ list=[]
4
+ unless hash.nil?
5
+ hash.each do |e|
6
+ list << e[filter]
7
+ end
8
+ end
9
+ list
10
+ end
11
+ end
@@ -0,0 +1,1254 @@
1
+
2
+ class Operation
3
+ class << self
4
+ attr_accessor :update
5
+ end
6
+ @update=false
7
+
8
+ attr_reader :log
9
+
10
+ def initialize(log)
11
+ # Family is associated operation's group class: i.e 'UsersSet' for 'Users'
12
+ @family=Kernel.const_get(self.class.to_s+'Set')
13
+ @log=log
14
+ @log.info "Init #{self.class.to_s}"
15
+ end
16
+
17
+ def create(sat)
18
+ @family.class_eval { return self.new(sat) }
19
+ end
20
+
21
+ def clone(sat, src_name, dst_name)
22
+ @log.info "Cloning #{self.class}"
23
+ end
24
+
25
+ def destroy(sat)
26
+ @log.info "Deleting #{self.class}"
27
+ satobjects=create(sat)
28
+ satobjects.delete_all
29
+ end
30
+
31
+ def export(type, sat, path)
32
+ @log.info "Exporting #{self.class}"
33
+
34
+ satobjects=create(sat)
35
+ satobjects.fetch
36
+ case type
37
+ when :mrb
38
+ File.open("#{path}/#{self.class}.mrb", "w+") do |f|
39
+ Marshal.dump(satobjects.list, f)
40
+ end
41
+ when :yaml
42
+ File.open("#{path}/#{self.class}.yaml", "w+") do |f|
43
+ YAML.dump(satobjects.list, f)
44
+ end
45
+ end
46
+ end
47
+
48
+ def import(type, sat, path)
49
+ @log.info "Importing #{self.class}"
50
+ satobjects=[]
51
+ case type
52
+ when :mrb
53
+ File.open("#{path}/#{self.class}.mrb") do |f|
54
+ satobjects = Marshal.load(f)
55
+ end
56
+ when :yaml
57
+ File.open("#{path}/#{self.class}.yaml") do |f|
58
+ satobjects = YAML.load(f)
59
+ end
60
+ end
61
+
62
+ dst_satobjects=create(sat)
63
+ dst_satobjects.fetch
64
+ unless satobjects.nil?
65
+ satobjects.each do |satobject|
66
+ if self.class.update && dst_satobjects.include?(satobject)
67
+ satobject.update(sat)
68
+ else
69
+ satobject.create(sat)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ def presync(src_sat, dst_sat)
76
+ end
77
+
78
+ def postsync(src_sat, dst_sat)
79
+ end
80
+
81
+ def extra(src_sat, dst_sat)
82
+ @log.info "Applying extra #{self.class}"
83
+ src_satobjects=create(src_sat)
84
+ src_satobjects.fetch
85
+ dst_satobjects=create(dst_sat)
86
+ dst_satobjects.fetch
87
+
88
+ satobjects_extras=[]
89
+ satobjects_extras=dst_satobjects-src_satobjects
90
+ dst_satobjects.extra(satobjects_extras) unless satobjects_extras.empty?
91
+ end
92
+
93
+ def sync(src_sat, dst_sat)
94
+ @log.info "Synchronizing #{self.class}"
95
+ src_satobjects=create(src_sat)
96
+ src_satobjects.fetch
97
+ dst_satobjects=create(dst_sat)
98
+ dst_satobjects.fetch
99
+ presync(src_sat, dst_sat)
100
+ unless src_satobjects.nil?
101
+ src_satobjects.list.each do |src_satobject|
102
+ if self.class.update && dst_satobjects.include?(src_satobject)
103
+ src_satobject.update(dst_sat)
104
+ else
105
+ src_satobject.create(dst_sat)
106
+ end
107
+ end
108
+ end
109
+ postsync(src_sat, dst_sat)
110
+ end
111
+ end
112
+
113
+ class Activationkeys < Operation
114
+ end
115
+
116
+ class Channels < Operation
117
+ class << self
118
+ attr_accessor :delete, :iss
119
+ end
120
+ @delete=false
121
+ @iss=false
122
+
123
+ def presync(src_sat, dst_sat)
124
+ if Channels.iss
125
+ all_channels=Helpers.filter(src_sat.channel.listAllChannels, 'label').sort
126
+ @result=nil
127
+ 3.times do
128
+ iss_cmd="/usr/bin/ssh -q root@#{dst_sat.host.name} '/usr/bin/satellite-sync "
129
+ all_channels.each do |e|
130
+ iss_cmd << "-c #{e} "
131
+ end
132
+ iss_cmd << "; echo $?'"
133
+ @log.info iss_cmd
134
+ @result=%x(#{iss_cmd})
135
+ @log.info @result
136
+ break if @result.chomp.reverse[0,1] == '0'
137
+ end
138
+ raise "Fatal: ISS Failed" if @result.chomp.reverse[0,1] != '0'
139
+ end
140
+ rescue RuntimeError => e
141
+ @log.fatal "#{e}"
142
+ end
143
+ end
144
+
145
+ class Configchannels < Operation
146
+ class << self
147
+ attr_accessor :exclude
148
+ end
149
+ @exclude=[]
150
+ end
151
+
152
+ class Kickstarts < Operation
153
+ end
154
+
155
+ class KickstartFilepreservations < Operation
156
+ end
157
+
158
+ class KickstartKeys < Operation
159
+ class << self
160
+ attr_accessor :key_type
161
+ end
162
+ @key_type='GPG'
163
+ end
164
+
165
+ class KickstartSnippets < Operation
166
+ end
167
+
168
+ class Orgs < Operation
169
+ class << self
170
+ attr_accessor :entitlements
171
+ end
172
+ @entitlements=false
173
+ end
174
+
175
+ class OrgTrusts < Operation
176
+ end
177
+
178
+ class Systems < Operation
179
+ end
180
+
181
+ class SystemCustominfos < Operation
182
+ end
183
+
184
+ class Systemgroups < Operation
185
+ end
186
+
187
+ class Users < Operation
188
+ class << self
189
+ attr_accessor :delete, :deactivated, :exclude, :password
190
+ end
191
+ @delete=false
192
+ @deactivated=false
193
+ @exclude=[]
194
+ @password=''
195
+ end
196
+
197
+ class OperationSet
198
+ attr_reader :list
199
+
200
+ def initialize(sat)
201
+ @sat=sat
202
+ @list=[]
203
+ end
204
+
205
+ def extra(list)
206
+ list.each do |obj|
207
+ obj.delete(@sat)
208
+ end
209
+ end
210
+
211
+ def delete_all
212
+ self.fetch_all.each do |obj|
213
+ obj.delete(@sat)
214
+ end
215
+ end
216
+
217
+ def fetch
218
+ @list=fetch_all
219
+ end
220
+
221
+ # Create include methods
222
+ %w{description id key label login name}.each do |method|
223
+ define_method("include_#{method}?".to_sym) do |o|
224
+ result=false
225
+ @list.each do |e|
226
+ result=true if eval("e.#{method} == o.#{method}")
227
+ end
228
+ result
229
+ end
230
+ end
231
+
232
+ def -(val)
233
+ result=[]
234
+ @list.each do |e|
235
+ result << e unless val.include?(e)
236
+ end
237
+ result
238
+ end
239
+ end
240
+
241
+ class Activationkey
242
+ attr_reader :key, :child_channel_labels, :config_channel_labels, :packages, :entitlements, :server_group_ids
243
+
244
+ def self.reader(sat, key)
245
+ activation_key=sat.activationkey.getDetails(key)
246
+ activation_key.merge!('config_channel_labels'=>Helpers.filter(sat.activationkey.listConfigChannels(activation_key['key']), 'label'))
247
+ activation_key.merge!('server_group_names'=>ActivationkeysSet.get_server_groups_names(sat, activation_key['server_group_ids']))
248
+ activation_key.merge!('config_deployment'=>sat.activationkey.checkConfigDeployment(key))
249
+ activation_key
250
+ end
251
+
252
+ def self.remove_id(key)
253
+ str=""
254
+ if key =~ /^[0-9]*-/
255
+ # key starts with an org id so we remove it as it's added by Satellite
256
+ str=key.sub(/^[0-9]*-/, '')
257
+ else
258
+ str=key
259
+ end
260
+ end
261
+
262
+ def initialize(activation_key)
263
+ @key=activation_key['key']
264
+ @description=activation_key['description']
265
+ @base_channel_label=activation_key['base_channel_label']
266
+ @child_channel_labels=activation_key['child_channel_labels']
267
+ @config_channel_labels=activation_key['config_channel_labels']
268
+ @config_deployment=activation_key['config_deployment']
269
+ @entitlements=activation_key['entitlements']
270
+ @packages=activation_key['packages']
271
+ @server_group_ids=activation_key['server_group_ids']
272
+ @server_group_names=activation_key['server_group_names']
273
+ @universal_default=activation_key['universal_default']
274
+ @usage_limit=activation_key['usage_limit']
275
+ @disabled=activation_key['disabled']
276
+ end
277
+
278
+ def common_update(sat, key)
279
+ sat.activationkey.addEntitlements(key, @entitlements)
280
+ sat.activationkey.enableConfigDeployment(key) if @config_deployment
281
+ sat.activationkey.addChildChannels(key, @child_channel_labels)
282
+ sat.activationkey.addConfigChannels([key], @config_channel_labels, true)
283
+ sat.activationkey.addPackages(key, @packages)
284
+ sat.activationkey.addServerGroups(key, ActivationkeysSet.get_server_groups_ids(sat, @server_group_names))
285
+ end
286
+
287
+ def create(sat)
288
+ @base_channel_label="" if @base_channel_label == 'none' # RHN Default
289
+ key=sat.activationkey.create(Activationkey.remove_id(@key), @description, @base_channel_label, @usage_limit, @entitlements, @universal_default)
290
+ common_update(sat, key) if key
291
+ end
292
+
293
+ def delete(sat)
294
+ sat.activationkey.delete(@key)
295
+ end
296
+
297
+ def update(sat)
298
+ # Sync base channel field
299
+ orig=Activationkey.new(Activationkey.reader(sat, @key))
300
+ @base_channel_label='' if @base_channel_label== 'none'
301
+ @usage_limit=-1 if @usage_limit == 0
302
+ sat.activationkey.setDetails(@key, {'description' => @description, 'base_channel_label' => @base_channel_label, 'usage_limit' => @usage_limit, 'universal_default' => @universal_default, 'disabled' => @disabled})
303
+ sat.activationkey.removeChildChannels(@key, orig.child_channel_labels)
304
+ sat.activationkey.removeConfigChannels([@key], orig.config_channel_labels)
305
+ sat.activationkey.removePackages(@key, orig.packages) # must be done before removing entitlements!
306
+ sat.activationkey.removeServerGroups(@key, orig.server_group_ids)
307
+ sat.activationkey.disableConfigDeployment(@key)
308
+ sat.activationkey.removeEntitlements(@key, orig.entitlements)
309
+ common_update(sat, @key)
310
+ end
311
+ end
312
+
313
+ class ActivationkeysSet < OperationSet
314
+ # Grab server group IDs using names
315
+ def self.get_server_groups_ids(sat, names)
316
+ server_group_ids=[]
317
+ names.each do |e|
318
+ server_group_ids << sat.systemgroup.getDetails(e)['id']
319
+ end
320
+ server_group_ids
321
+ end
322
+
323
+ # Grab server group names using IDs
324
+ def self.get_server_groups_names(sat, ids)
325
+ server_group_names=Array.new
326
+ ids.each do |e|
327
+ server_group=sat.systemgroup.getDetails(e)
328
+ server_group_names << server_group['name']
329
+ end
330
+ server_group_names
331
+ end
332
+
333
+ def fetch_all
334
+ activation_keys=[]
335
+ @sat.activationkey.list.each do |activation_key|
336
+ activation_keys << Activationkey.new(Activationkey.reader(@sat, activation_key['key']))
337
+ end
338
+ activation_keys
339
+ end
340
+
341
+ def include?(arg)
342
+ self.include_key?(arg)
343
+ end
344
+ end
345
+
346
+ class Channel
347
+ attr_reader :id, :label
348
+ REDHAT="Red Hat, Inc."
349
+
350
+ def self.reader(sat, channel)
351
+ channel.merge!(sat.channelSoftware.getDetails(channel['label']))
352
+ channel.merge!({'isGloballySubscribable'=>sat.channelSoftware.isGloballySubscribable(channel['label'])})
353
+ unless channel['isGloballySubscribable']
354
+ subscribers={}
355
+ Helpers.filter(sat.user.listUsers, 'login').each do |login|
356
+ subscribers.merge!({login => sat.channelSoftware.isUserSubscribable(channel['label'], login)})
357
+ end
358
+ channel.merge!({'subscribers'=>subscribers})
359
+ end
360
+ unless channel['provider_name'] == REDHAT
361
+ managers={}
362
+ Helpers.filter(sat.user.listUsers, 'login').each do |login|
363
+ managers.merge!({login => sat.channelSoftware.isUserManageable(channel['label'], login)})
364
+ end
365
+ channel.merge!({'managers'=>managers})
366
+ end
367
+ channel
368
+ end
369
+
370
+ def initialize(channel)
371
+ @id=channel['id']
372
+ @label=channel['label']
373
+ @name=channel['name']
374
+ @arch_name=channel['arch_name']
375
+ @summary=channel['summary']
376
+ @provider_name= channel['provider_name']
377
+ @packages=channel['packages']
378
+ @systems=channel['systems']
379
+ @is_globally_subscribable=channel['isGloballySubscribable']
380
+ @subscribers=channel['subscribers']
381
+ @managers=channel['managers']
382
+ @description=channel['description']
383
+ @checksum_label=channel['checksum_label']
384
+ @last_modified=channel['last_modified']
385
+ @maintainer_name=channel['maintainer_name']
386
+ @maintainer_email=channel['maintainer_email']
387
+ @maintainer_phone=channel['maintainer_phone']
388
+ @support_policy=channel['support_policy']
389
+ @gpg_key_url=channel['gpg_key_url']
390
+ @gpg_key_id=channel['gpg_key_id']
391
+ @gpg_key_fp=channel['gpg_key_fp']
392
+ @yumrepo_source_url=channel['yumrepo_source_url']
393
+ @yumrepo_label=channel['yumrepo_label']
394
+ @yumrepo_last_sync=channel['yumrepo_last_sync']
395
+ @end_of_life=channel['end_of_life']
396
+ @parent_channel_label=channel['parent_channel_label']
397
+ @clone_original=channel['clone_original']
398
+ end
399
+
400
+ def create(sat)
401
+ # Software Channels must created via ISS (satellite-sync)
402
+ end
403
+
404
+ def delete(sat)
405
+ sat.channelSoftware.delete(@label)
406
+ end
407
+
408
+ def update(sat)
409
+ # Update details for non Red Hat channels
410
+ if @provider_name != REDHAT
411
+ # Non mandatory fields that could be nil need to be empty
412
+ @maintainer_name='' unless @maintainer_name
413
+ @maintainer_email='' unless @maintainer_email
414
+ @maintainer_phone='' unless @maintainer_phone
415
+ # Find target channel id
416
+ id=sat.channelSoftware.getDetails(@label)['id']
417
+ sat.channelSoftware.setDetails(id, {'checksum_label' => @checksum_label, 'name' => @name, 'summary' => @summary, 'description' => @description, 'maintainer_name' => @maintainer_name, 'maintainer_email' => @maintainer_email, 'maintainer_phone' => @maintainer_phone, 'gpg_key_url' => @gpg_key_url, 'gpg_key_id' => @gpg_key_id, 'gpg_key_fp' => @gpg_key_fp})
418
+
419
+ # Managers
420
+ if @managers
421
+ @managers.each do |login, value|
422
+ sat.channelSoftware.setUserManageable(@label, login, value)
423
+ end
424
+ end
425
+ end
426
+
427
+ # Globally Subscribable
428
+ sat.channelSoftware.setGloballySubscribable(@label, @is_globally_subscribable)
429
+
430
+ # Per User subscriptions
431
+ if !@is_globally_subscribable && @subscribers
432
+ @subscribers.each do |login, value|
433
+ sat.channelSoftware.setUserSubscribable(@label, login, value)
434
+ end
435
+ end
436
+
437
+ # To Do : Repos
438
+ end
439
+ end
440
+
441
+ class ChannelsSet < OperationSet
442
+ def delete_all
443
+ # Flag must be set!
444
+ super.delete_all if Channels.delete
445
+ end
446
+
447
+ def fetch_all
448
+ channels=[]
449
+ @sat.channel.listAllChannels.each do |channel|
450
+ channels << Channel.new(Channel.reader(@sat, channel))
451
+ end
452
+ channels
453
+ end
454
+
455
+ def include?(arg)
456
+ self.include_label?(arg)
457
+ end
458
+ end
459
+
460
+ class Configchannel
461
+ attr_reader :label
462
+
463
+ def self.reader(sat, id)
464
+ # Configchannel files are files, directories or symlinks
465
+ configchannel ={}
466
+ configchannel.merge!(sat.configchannel.getDetails(id))
467
+
468
+ file_revisions=Hash.new
469
+ sat.configchannel.listFiles(configchannel['label']).each do |file|
470
+ file_revisions.merge!("#{file['path']}" => sat.configchannel.getFileRevisions(configchannel['label'], file['path']))
471
+ end
472
+ configchannel.merge!({'file_revisions' => file_revisions})
473
+ configchannel
474
+ end
475
+
476
+ def initialize(configchannel)
477
+ @id=configchannel['id']
478
+ @orgId=configchannel['orgId']
479
+ @label=configchannel['label']
480
+ @name=configchannel['name']
481
+ @description=configchannel['description']
482
+ @configChannelType=configchannel['configChannelType']
483
+ @file_revisions=configchannel['file_revisions']
484
+ end
485
+
486
+ def set_files(sat, cfg_file)
487
+ case cfg_file['type']
488
+ when 'file'
489
+ sat.configchannel.createOrUpdatePath(@label, cfg_file, false)
490
+ when 'directory'
491
+ sat.configchannel.createOrUpdatePath(@label, cfg_file, true)
492
+ when 'symlink'
493
+ sat.configchannel.createOrUpdateSymlink(@label, cfg_file)
494
+ end
495
+ end
496
+
497
+ def create(sat)
498
+ sat.configchannel.create(@label, @name, @description)
499
+ # Create file revisions
500
+ @file_revisions.each do |cfg_file, revisions|
501
+ revisions.each do |file_revision|
502
+ set_files(sat, file_revision)
503
+ end
504
+ end
505
+ end
506
+
507
+ def delete(sat)
508
+ sat.configchannel.deleteChannels([@label])
509
+ end
510
+
511
+ def update(sat)
512
+ sat.configchannel.update(@label, @name, @description)
513
+
514
+ @file_revisions.each do |cfg_file, revisions|
515
+ # dst_cfg_files=sat.configchannel.deleteFiles(@label, [cfg_file])
516
+ revisions.each do |file_revision|
517
+ set_files(sat, file_revision)
518
+ end
519
+ end
520
+ end
521
+ end
522
+
523
+ class ConfigchannelsSet < OperationSet
524
+ def fetch_all
525
+ configchannels=[]
526
+ @sat.configchannel.listGlobals.each do |config_channel|
527
+ configchannels << Configchannel.new(Configchannel.reader(@sat, config_channel['id']))
528
+ end
529
+
530
+ # Apply exclude list option
531
+ if Configchannels.exclude
532
+ Configchannels.exclude.each do |exclude|
533
+ case exclude
534
+ when Regexp
535
+ configchannels.delete_if { |u| u.label =~ exclude }
536
+ when String
537
+ configchannels.delete_if { |u| u.label == exclude }
538
+ end
539
+ end
540
+ end
541
+ configchannels
542
+ end
543
+
544
+ def include?(arg)
545
+ self.include_label?(arg)
546
+ end
547
+ end
548
+
549
+ class Kickstart
550
+ attr_reader :label
551
+
552
+ def self.reader(sat, ks)
553
+ label=ks['label']
554
+ kickstart=ks
555
+ kickstart.merge!({'advanced_options'=>sat.kickstartProfile.getAdvancedOptions(label)})
556
+ kickstart.merge!({'child_channels'=>sat.kickstartProfile.getChildChannels(label)})
557
+ kickstart.merge!({'custom_options'=>sat.kickstartProfile.getCustomOptions(label)})
558
+ kickstart.merge!({'variables'=>sat.kickstartProfile.getVariables(label)})
559
+
560
+ kickstart.merge!({'config_management'=>sat.kickstartProfileSystem.checkConfigManagement(label)})
561
+ kickstart.merge!({'remote_commands'=>sat.kickstartProfileSystem.checkRemoteCommands(label)})
562
+ kickstart.merge!({'locale'=>sat.kickstartProfileSystem.getLocale(label)})
563
+ kickstart.merge!({'selinux'=>sat.kickstartProfileSystem.getSELinux(label)})
564
+ kickstart.merge!({'partitioning_scheme'=>sat.kickstartProfileSystem.getPartitioningScheme(label)})
565
+ kickstart.merge!({'registration_type'=>sat.kickstartProfileSystem.getRegistrationType(label)})
566
+ kickstart.merge!({'software_list'=>sat.kickstartProfileSoftware.getSoftwareList(label)})
567
+
568
+ kickstart.merge!({'keys'=>Helpers.filter(sat.kickstartProfileSystem.listKeys(label), 'description')})
569
+ kickstart.merge!({'file_preservations'=>Helpers.filter(sat.kickstartProfileSystem.listFilePreservations(label), 'name')})
570
+ kickstart.merge!({'scripts'=>sat.kickstartProfile.listScripts(label)})
571
+ kickstart
572
+ end
573
+
574
+ def initialize(kickstart)
575
+ @label=kickstart['label']
576
+ @tree_label=kickstart['tree_label']
577
+ @name=kickstart['name']
578
+ @advanced_mode=kickstart['advanced_mode']
579
+ @org_default=kickstart['org_default']
580
+ @active=kickstart['active']
581
+ @advanced_options=kickstart['advanced_options']
582
+ @child_channels=kickstart['child_channels']
583
+ @custom_options=kickstart['custom_options']
584
+ @variables=kickstart['variables']
585
+ @config_management=kickstart['config_management']
586
+ @remote_commands=kickstart['remote_commands']
587
+ @locale=kickstart['locale']
588
+ @selinux=kickstart['selinux']
589
+ @partitioning_scheme=kickstart['partitioning_scheme']
590
+ @registration_type=kickstart['registration_type']
591
+ @software_list=kickstart['software_list']
592
+ @keys=kickstart['keys']
593
+ @file_preservations=kickstart['file_preservations']
594
+ @scripts=kickstart['scripts']
595
+ end
596
+
597
+ def create(sat)
598
+ sat.kickstart.createProfile(@label, 'none', @tree_label, 'default', '')
599
+ sat.kickstart.disableProfile(@label, !@active)
600
+
601
+ sat.kickstartProfile.setAdvancedOptions(@label, @advanced_options)
602
+
603
+ custom_options=[]
604
+ @custom_options.each do |val|
605
+ custom_options << val['arguments']
606
+ end
607
+ sat.kickstartProfile.setCustomOptions(@label, custom_options)
608
+ sat.kickstartProfile.setVariables(@label, @variables)
609
+ sat.kickstartProfile.setChildChannels(@label, @child_channels)
610
+ sat.kickstartProfile.setKickstartTree(@label, @tree_label)
611
+ # No API to get logging options - let's activate them by default
612
+ sat.kickstartProfile.setLogging(@label, true, true)
613
+ sat.kickstartProfileSystem.setLocale(@label, @locale['locale'], @locale['useUtc'])
614
+ sat.kickstartProfileSystem.setSELinux(@label, @selinux)
615
+ sat.kickstartProfileSystem.setPartitioningScheme(@label, @partitioning_scheme)
616
+ sat.kickstartProfileSystem.setRegistrationType(@label, @registration_type)
617
+ sat.kickstartProfileSystem.addKeys(@label, @keys)
618
+ sat.kickstartProfileSystem.addFilePreservations(@label, @file_preservations)
619
+ sat.kickstartProfileSoftware.setSoftwareList(@label, @software_list)
620
+
621
+ if @config_management
622
+ sat.kickstartProfileSystem.enableConfigManagement(@label)
623
+ else
624
+ sat.kickstartProfileSystem.disableConfigManagement(@label)
625
+ end
626
+
627
+ if @remote_commands
628
+ sat.kickstartProfileSystem.enableRemoteCommands(@label)
629
+ else
630
+ sat.kickstartProfileSystem.disableRemoteCommands(@label)
631
+ end
632
+
633
+ @scripts.each do |script|
634
+ sat.kickstartProfile.addScript(@label, script['contents'], script['interpreter'], script['script_type'], script['chroot'], script['template'])
635
+ end
636
+ end
637
+
638
+ def delete(sat)
639
+ sat.kickstart.deleteProfile(@label)
640
+ end
641
+
642
+ def update(sat)
643
+ # Remove scripts first because there is no RHN API call for updating them
644
+ Helpers.filter(sat.kickstartProfile.listScripts(@label), 'id').each do |id|
645
+ sat.kickstartProfile.removeScript(@label, id)
646
+ end
647
+ # No API for updating KS profile so we overide
648
+ self.create(sat)
649
+ end
650
+ end
651
+
652
+ class KickstartsSet < OperationSet
653
+ def fetch_all
654
+ kickstarts=[]
655
+ @sat.kickstart.listKickstarts.each do |ks|
656
+ kickstarts << Kickstart.new(Kickstart.reader(@sat, ks))
657
+ end
658
+ kickstarts
659
+ end
660
+
661
+ def include?(arg)
662
+ self.include_label?(arg)
663
+ end
664
+ end
665
+
666
+ class KickstartFilepreservation
667
+ attr_reader :name
668
+
669
+ def self.reader(sat, file_preserv)
670
+ file_preserv.merge!({'file_list'=>sat.kickstartFilepreservation.get(file_preserv['name'])['file_names']})
671
+ file_preserv
672
+ end
673
+
674
+ def initialize(file_preserv)
675
+ @id=file_preserv['id']
676
+ @name=file_preserv['name']
677
+ @file_list=file_preserv['file_list']
678
+ end
679
+
680
+ def delete(sat)
681
+ sat.kickstartFilepreservation.delete(@name)
682
+ end
683
+
684
+ def create(sat)
685
+ sat.kickstartFilepreservation.create(@name, @file_list)
686
+ end
687
+
688
+ def update(sat)
689
+ # No API for update
690
+ self.delete(sat)
691
+ self.create(sat)
692
+ end
693
+ end
694
+
695
+ class KickstartFilepreservationsSet < OperationSet
696
+ def fetch_all
697
+ file_perservations=[]
698
+ @sat.kickstartFilepreservation.listAllFilePreservations.each do |file_preserv|
699
+ file_perservations << KickstartFilepreservation.new(KickstartFilepreservation.reader(@sat, file_preserv))
700
+ end
701
+ file_perservations
702
+ end
703
+
704
+ def include?(arg)
705
+ self.include_name?(arg)
706
+ end
707
+ end
708
+
709
+ # GPG/SSL Keys
710
+ class KickstartKey
711
+ attr_reader :description
712
+
713
+ def initialize(key)
714
+ @description=key['description']
715
+ @type=key['type']
716
+ @content=key['content']
717
+ end
718
+
719
+ def delete(sat)
720
+ sat.kickstartKeys.delete(@description)
721
+ end
722
+
723
+ def create(sat)
724
+ sat.kickstartKeys.create(@description, @type, @content)
725
+ end
726
+
727
+ def update(sat)
728
+ sat.kickstartKeys.update(@description, @type, @content)
729
+ end
730
+ end
731
+
732
+ class KickstartKeysSet < OperationSet
733
+ def fetch_all
734
+ ks_keys=[]
735
+ get_all.each do |ks|
736
+ ks_keys << KickstartKey.new(ks)
737
+ end
738
+ ks_keys
739
+ end
740
+
741
+ def get_all
742
+ # Fetch only kickstart keys matching key_type option
743
+ key_type=KickstartKeys.key_type
744
+ return [] unless key_type
745
+ ksdetails=[]
746
+ @sat.kickstartKeys.listAllKeys.each do |ks_key|
747
+ ksdetails.push(@sat.kickstartKeys.getDetails(ks_key['description'])) if ks_key['type'] == key_type
748
+ end
749
+ ksdetails
750
+ end
751
+
752
+ def include?(arg)
753
+ self.include_description?(arg)
754
+ end
755
+ end
756
+
757
+ class KickstartSnippet
758
+ attr_reader :name
759
+
760
+ def initialize(snippet)
761
+ @name=snippet['name']
762
+ @contents=snippet['contents']
763
+ end
764
+
765
+ def delete(sat)
766
+ sat.kickstartSnippet.delete(@name)
767
+ end
768
+
769
+ def create(sat)
770
+ sat.kickstartSnippet.createOrUpdate(@name, @contents)
771
+ end
772
+
773
+ def update(sat)
774
+ self.create(sat)
775
+ end
776
+ end
777
+
778
+ class KickstartSnippetsSet < OperationSet
779
+ def fetch_all
780
+ snippets=[]
781
+ @sat.kickstartSnippet.listCustom.each do |snippet|
782
+ snippets << KickstartSnippet.new(snippet)
783
+ end
784
+ snippets
785
+ end
786
+
787
+ def include?(arg)
788
+ self.include_name?(arg)
789
+ end
790
+ end
791
+
792
+ class Org
793
+ attr_reader :id, :name
794
+
795
+ def self.reader(sat, id)
796
+ org={}
797
+ org.merge!(sat.org.getDetails(id))
798
+ org.merge!({'users'=>sat.org.listUsers(id)})
799
+ if Orgs.entitlements
800
+ org.merge!({'system_entitlements'=>sat.org.listSystemEntitlementsForOrg(id)})
801
+
802
+ # Filter out empty software entitlements
803
+ software_entitlements=sat.org.listSoftwareEntitlementsForOrg(id)
804
+ if software_entitlements
805
+ software_entitlements.delete_if do |entitlement|
806
+ entitlement['allocated'] == 0 && entitlement['allocated_flex'] == 0
807
+ end
808
+ org.merge!({'software_entitlements'=>software_entitlements})
809
+ end
810
+ end
811
+ org
812
+ end
813
+
814
+ def initialize(org)
815
+ @id=org['id']
816
+ @name=org['name']
817
+ @active_users=org['active_users'] # Number of active users in the organization.
818
+ @systems=org['systems'] # Number of systems in the organization.
819
+ # API doesn't return trusts info wigh getDetails
820
+ # @trusts=org['trusts'] # Number of trusted organizations.
821
+ @users=org['users']
822
+ @system_groups=org['system_groups'] # Number of system groups in the organization. (optional)
823
+ @activation_keys=org['activation_keys'] # Number of activation keys in the organization. (optional)
824
+ @kickstart_profiles=org['kickstart_profiles'] # Number of kickstart profiles in the organization. (optional)
825
+ @configuration_channels=org['configuration_channels'] # Number of configuration channels in the organization. (optional)
826
+ @system_entitlements=org['system_entitlements']
827
+ @software_entitlements=org['software_entitlements']
828
+ end
829
+
830
+ def create(sat)
831
+ # Create org from the first user with admin privileges
832
+ # Never handle default org (id=1)
833
+ if @id != 1 && @active_users >= 1
834
+ admin=get_admin
835
+ if admin
836
+ # Try to find that Org
837
+ org=sat.org.getDetails(@name)
838
+
839
+ unless org
840
+ # Create Org if doesn't exit
841
+ org=sat.org.create(@name, admin['login'], admin['login'], 'Mr.', admin['name'].split(',')[1], admin['name'].split(',')[0], admin['email'], false)
842
+ end
843
+
844
+ # Entitlements option activated
845
+ if Orgs.entitlements
846
+ unless org
847
+ # case org already exist!
848
+ org=sat.org.getDetails(@name)
849
+ end
850
+
851
+ if org
852
+ # Systems Entitlements
853
+ @system_entitlements.each do |system_entitlement|
854
+ sat.org.setSystemEntitlements(org['id'], system_entitlement['label'], system_entitlement['allocated'])
855
+ end
856
+
857
+ # Software Entitlements
858
+ @software_entitlements.each do |software_entitlement|
859
+ sat.org.setSoftwareEntitlements(org['id'], software_entitlement['label'], software_entitlement['allocated'])
860
+ sat.org.setSoftwareFlexEntitlements(org['id'], software_entitlement['label'], software_entitlement['allocated_flex'])
861
+ end
862
+ end
863
+ end
864
+ end
865
+ end
866
+ end
867
+
868
+ def delete(sat)
869
+ sat.org.delete(@id) unless @id == 1
870
+ end
871
+
872
+ def get_admin
873
+ @users.each do |user|
874
+ return user if user['is_org_admin']
875
+ end
876
+ end
877
+
878
+ def update(sat)
879
+ self.create(sat)
880
+ end
881
+ end
882
+
883
+ class OrgsSet < OperationSet
884
+ def fetch_all
885
+ orgs=[]
886
+ @sat.org.listOrgs.each do |org|
887
+ orgs << Org.new(Org.reader(@sat, org['id']))
888
+ end
889
+ orgs
890
+ end
891
+
892
+ def include?(arg)
893
+ self.include_id?(arg)
894
+ end
895
+ end
896
+
897
+ class OrgTrust
898
+ attr_reader :id
899
+
900
+ def self.reader(sat, org)
901
+ org_trusts=org
902
+ # Misnomer - listTrusts actually returns all orgs!
903
+ alltrusts=sat.orgTrusts.listTrusts(org['id'])
904
+ trusts=[]
905
+ alltrusts.each do |trust|
906
+ if trust['trustEnabled']
907
+ trusts << trust
908
+ # Broken - BZ#815715
909
+ # sat.orgTrusts.getDetails(trust['orgId'])
910
+ # ...
911
+ end
912
+ end
913
+ org_trusts.merge!({'trusted_orgs'=>trusts})
914
+ org_trusts
915
+ end
916
+
917
+ def initialize(org)
918
+ @id=org['id']
919
+ @name=org['name']
920
+ @trusted_orgs=org['trusted_orgs']
921
+ end
922
+
923
+ def create(sat)
924
+ @trusted_orgs.each do |trust|
925
+ sat.orgTrusts.addTrust(@id, trust['orgId'])
926
+ end
927
+ end
928
+
929
+ def delete(sat)
930
+ @trusted_orgs.each do |trusted|
931
+ sat.orgTrusts.removeTrust(@id, trusted['orgId'])
932
+ end
933
+ end
934
+
935
+ def update(sat)
936
+ self.create(sat)
937
+ end
938
+ end
939
+
940
+ class OrgTrustsSet < OperationSet
941
+ def fetch_all
942
+ org_trusts=[]
943
+ @sat.org.listOrgs.each do |org|
944
+ if org['trusts'] > 0
945
+ org_trusts << OrgTrust.new(OrgTrust.reader(@sat, org))
946
+ end
947
+ end
948
+ org_trusts
949
+ end
950
+
951
+ def include?(arg)
952
+ self.include_id?(arg)
953
+ end
954
+ end
955
+
956
+ class System
957
+ attr_reader :id
958
+
959
+ def self.reader(sat, id)
960
+ system={}
961
+ system.merge!(sat.system.getDetails(id))
962
+ system.merge!({'connection_path'=>sat.system.getConnectionPath(id)})
963
+ system.merge!({'cpu'=>sat.system.getCpu(id)})
964
+ system.merge!({'custom_values'=>sat.system.getCustomValues(id)})
965
+ system.merge!({'devices'=>sat.system.getDevices(id)})
966
+ system.merge!({'dmi'=>sat.system.getDmi(id)})
967
+ system.merge!({'entitlements'=>sat.system.getEntitlements(id)})
968
+ system.merge!({'event_history'=>sat.system.getEventHistory(id)})
969
+ system.merge!({'memory'=>sat.system.getMemory(id)})
970
+ system.merge!({'name'=>sat.system.getName(id)})
971
+ system.merge!({'network'=>sat.system.getNetwork(id)})
972
+ system.merge!({'network_devices'=>sat.system.getNetworkDevices(id)})
973
+ system.merge!({'registration_date'=>sat.system.getRegistrationDate(id)})
974
+ system.merge!({'running_kernel'=>sat.system.getRunningKernel(id)})
975
+ system.merge!({'subscribed_base_channel'=>sat.system.getSubscribedBaseChannel(id)})
976
+ system
977
+ end
978
+
979
+ def initialize(system)
980
+ @id=system['id']
981
+ @profile_name=system['profile_name']
982
+ @base_entitlement=system['base_entitlement']
983
+ @addon_entitlement=system['']
984
+ @auto_update=system['auto_update']
985
+ @release=system['release']
986
+ @address1=system['address1']
987
+ @address2=system['address2']
988
+ @city=system['city']
989
+ @state=system['state']
990
+ @country=system['country']
991
+ @building=system['building']
992
+ @room=system['room']
993
+ @rack=system['rack']
994
+ @description=system['description']
995
+ @hostname=system['hostname']
996
+ @last_boot=system['last_boot']
997
+ @osa_satus=system['osa_status']
998
+ @lock_status=system['lock_status']
999
+ @connection_path=system['connection_path']
1000
+ @cpu=system['cpu']
1001
+ @custom_values=system['custom_values=']
1002
+ @devices=system['devices']
1003
+ @dmi=system['dmi']
1004
+ @entitlements=system['entitlements']
1005
+ @event_history=system['event_history']
1006
+ @memory=system['memory']
1007
+ @name=system['name']
1008
+ @network=system['network']
1009
+ @network_devices=system['network_devices']
1010
+ @registration_date=system['registration_date']
1011
+ @running_kernel=system['running_kernel']
1012
+ @subscribed_base_channel=system['subscribed_base_channel']
1013
+ end
1014
+
1015
+ # System profiles must be registered and cannot be created
1016
+ def create(sat)
1017
+ end
1018
+
1019
+ def update(sat)
1020
+ end
1021
+ end
1022
+
1023
+ class SystemsSet < OperationSet
1024
+ def delete(list)
1025
+ # To Test
1026
+ list=[list] if list.class != Array
1027
+ @sat.system.deleteSystems(Helpers.filter(list, 'id'))
1028
+ end
1029
+
1030
+ def delete_all
1031
+ delete(@sat.system.listSystems)
1032
+ end
1033
+
1034
+ def fetch_all
1035
+ systems=[]
1036
+ @sat.system.listSystems.each do |sys|
1037
+ systems << System.new(System.reader(@sat, sys['id']))
1038
+ end
1039
+ systems
1040
+ end
1041
+
1042
+ def include?(arg)
1043
+ self.include_id?(arg)
1044
+ end
1045
+ end
1046
+
1047
+ class SystemCustominfo
1048
+ attr_reader :id, :label
1049
+
1050
+ def initialize(system)
1051
+ @id=system['id']
1052
+ @label=system['label']
1053
+ @description=system['description']
1054
+ @last_modified=system['last_modified']
1055
+ @system_count=system['system_count']
1056
+ end
1057
+
1058
+ def delete(sat)
1059
+ sat.systemCustominfo.deleteKey(@label)
1060
+ end
1061
+
1062
+ def create(sat)
1063
+ sat.systemCustominfo.createKey(@label, @description)
1064
+ end
1065
+
1066
+ def update(sat)
1067
+ sat.systemCustominfo.updateKey(@label, @description)
1068
+ end
1069
+ end
1070
+
1071
+ class SystemCustominfosSet < OperationSet
1072
+ def fetch_all
1073
+ system_infos=[]
1074
+ @sat.systemCustominfo.listAllKeys.each do |custom_info|
1075
+ system_infos << SystemCustominfo.new(custom_info)
1076
+ end
1077
+ system_infos
1078
+ end
1079
+
1080
+ def include?(arg)
1081
+ include_label?(arg)
1082
+ end
1083
+ end
1084
+
1085
+ class Systemgroup
1086
+ attr_reader :name
1087
+
1088
+ def initialize(sysgroup)
1089
+ @id=sysgroup['id']
1090
+ @name=sysgroup['name']
1091
+ @description=sysgroup['description']
1092
+ @org_id=sysgroup['org_id']
1093
+ @system_count=sysgroup['system_count']
1094
+ end
1095
+
1096
+ def create(sat)
1097
+ sat.systemgroup.create(@name, @description)
1098
+ end
1099
+
1100
+ def delete(sat)
1101
+ sat.systemgroup.delete(@name)
1102
+ end
1103
+
1104
+ def update(sat)
1105
+ sat.systemgroup.update(@name, @description)
1106
+ end
1107
+ end
1108
+
1109
+ class SystemgroupsSet < OperationSet
1110
+ def fetch_all
1111
+ sysgroups=[]
1112
+ @sat.systemgroup.listAllGroups.each do |sysgroup|
1113
+ sysgroups << Systemgroup.new(sysgroup)
1114
+ end
1115
+ sysgroups
1116
+ end
1117
+
1118
+ def include?(arg)
1119
+ self.include_name?(arg)
1120
+ end
1121
+ end
1122
+
1123
+ class User
1124
+ attr_reader :login
1125
+
1126
+ def self.reader(sat, login)
1127
+ user={'login'=>login}
1128
+ user.merge!(sat.user.getDetails(login))
1129
+ user.merge!({'roles'=>sat.user.listRoles(login)})
1130
+ user.merge!({'assigned_system_groups'=>sat.user.listAssignedSystemGroups(login)})
1131
+ user.merge!({'default_system_groups'=>sat.user.listDefaultSystemGroups(login)})
1132
+ user
1133
+ end
1134
+
1135
+ def initialize(user)
1136
+ @login=user['login']
1137
+ @first_name=user['first_name']
1138
+ @last_name=user['last_name']
1139
+ @email=user['email']
1140
+ @org_id=user['org_id']
1141
+ @prefix=user['prefix']
1142
+ @last_login_date=user['last_login_date']
1143
+ @created_date=user['created_date']
1144
+ @enabled=user['enabled']
1145
+ @use_pam=user['use_pam']
1146
+ @roles=user['roles']
1147
+ @assigned_system_groups=user['assigned_system_groups']
1148
+ @default_system_groups=user['default_system_groups']
1149
+ end
1150
+
1151
+ def common_update(sat)
1152
+ # Enable/Disable
1153
+ if @enabled
1154
+ sat.user.enable(@login)
1155
+ else
1156
+ sat.user.disable(@login)
1157
+ end
1158
+
1159
+ # Adding roles
1160
+ @roles.each do |role|
1161
+ sat.user.addRole(@login, role)
1162
+ end
1163
+
1164
+ # Assigned System Groups
1165
+ sat.user.addAssignedSystemGroups(@login, Helpers.filter(@assigned_system_groups, 'name'), false) unless @assigned_system_groups.empty?
1166
+
1167
+ # Default System Groups
1168
+ sat.user.addDefaultSystemGroups(@login, Helpers.filter(@default_system_groups, 'name')) unless @default_system_groups.empty?
1169
+ end
1170
+
1171
+ def create(sat)
1172
+ @use_pam
1173
+ if @use_pam
1174
+ sat.user.create(@login, "", @first_name, @last_name, @email, 1)
1175
+ else
1176
+ # When creating user on target, the passwor comes from configuration
1177
+ # because there no API to read it.
1178
+ password=Users.password
1179
+ sat.user.create(@login, password, @first_name, @last_name, @email, 0)
1180
+ end
1181
+ common_update(sat)
1182
+ end
1183
+
1184
+ def update(sat)
1185
+ @prefix='' unless @prefix
1186
+ # We ignore password update - Ain't any API for it!
1187
+ sat.user.setDetails(@login, {'first_name' => @first_name, 'last_name' => @last_name, 'email' => @email, 'prefix' => @prefix})
1188
+ sat.user.listRoles(login).each do |role|
1189
+ sat.user.removeRole(login, role)
1190
+ end
1191
+ common_update(sat)
1192
+ end
1193
+ end
1194
+
1195
+ class UsersSet < OperationSet
1196
+ def delete(list)
1197
+ Users.exclude.each do |exclude|
1198
+ list.delete_if { |u| u == exclude }
1199
+ end
1200
+ list.each do |user|
1201
+ @sat.user.delete(user.login)
1202
+ end
1203
+ end
1204
+
1205
+ def delete_all
1206
+ delete(Helpers.filter(@sat.user.listUsers, 'login'))
1207
+ end
1208
+
1209
+ def disable(list)
1210
+ list.each do |user|
1211
+ # Remove Roles first
1212
+ @sat.user.listRoles(user.login).each do |role|
1213
+ @sat.user.removeRole(user.login, role)
1214
+ end
1215
+ # Disable User
1216
+ @sat.user.disable(user.login)
1217
+ end
1218
+ end
1219
+
1220
+ def extra(list)
1221
+ # Users are not deleted by default but deactivated (to keep history)
1222
+ # unless delete option is true
1223
+ if Users.delete
1224
+ delete(list)
1225
+ else
1226
+ disable(list)
1227
+ end
1228
+ end
1229
+
1230
+ def fetch_all
1231
+ user_list=[]
1232
+ user_list=@sat.user.listUsers
1233
+
1234
+ # users excluded from list option
1235
+ Users.exclude.each do |exclude|
1236
+ user_list.delete_if { |u| u['login'] == exclude }
1237
+ end
1238
+
1239
+ # Exclude deactivated users unless option activated
1240
+ unless Users.deactivated
1241
+ user_list.delete_if { |u| u['enabled'] == false }
1242
+ end
1243
+
1244
+ users=[]
1245
+ Helpers.filter(user_list, 'login').each do |login|
1246
+ users << User.new(User.reader(@sat, login))
1247
+ end
1248
+ users
1249
+ end
1250
+
1251
+ def include?(arg)
1252
+ self.include_login?(arg)
1253
+ end
1254
+ end