rhn_satellite 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +30 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +94 -0
  6. data/Rakefile +49 -0
  7. data/VERSION +1 -0
  8. data/lib/rhn_satellite.rb +25 -0
  9. data/lib/rhn_satellite/activation_key.rb +5 -0
  10. data/lib/rhn_satellite/api.rb +36 -0
  11. data/lib/rhn_satellite/channel.rb +5 -0
  12. data/lib/rhn_satellite/channel_access.rb +20 -0
  13. data/lib/rhn_satellite/channel_software.rb +20 -0
  14. data/lib/rhn_satellite/common/collection.rb +15 -0
  15. data/lib/rhn_satellite/common/debug.rb +35 -0
  16. data/lib/rhn_satellite/common/misc.rb +24 -0
  17. data/lib/rhn_satellite/connection/base.rb +25 -0
  18. data/lib/rhn_satellite/connection/handler.rb +116 -0
  19. data/lib/rhn_satellite/package.rb +15 -0
  20. data/lib/rhn_satellite/system.rb +83 -0
  21. data/lib/rhn_satellite/systemgroup.rb +34 -0
  22. data/rhn_satellite.gemspec +87 -0
  23. data/spec/spec.opts +6 -0
  24. data/spec/spec_helper.rb +12 -0
  25. data/spec/unit/rhn_satellite/activation_key_spec.rb +58 -0
  26. data/spec/unit/rhn_satellite/api_spec.rb +94 -0
  27. data/spec/unit/rhn_satellite/channel_access_spec.rb +61 -0
  28. data/spec/unit/rhn_satellite/channel_software_spec.rb +65 -0
  29. data/spec/unit/rhn_satellite/channel_spec.rb +58 -0
  30. data/spec/unit/rhn_satellite/common/debug_spec.rb +42 -0
  31. data/spec/unit/rhn_satellite/common/misc_spec.rb +33 -0
  32. data/spec/unit/rhn_satellite/connection/base_spec.rb +44 -0
  33. data/spec/unit/rhn_satellite/connection/handler_spec.rb +229 -0
  34. data/spec/unit/rhn_satellite/package_spec.rb +52 -0
  35. data/spec/unit/rhn_satellite/system_spec.rb +320 -0
  36. data/spec/unit/rhn_satellite/systemgroup_spec.rb +127 -0
  37. metadata +180 -0
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ gem "mocha", "~> 0.9.10"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ mocha (0.9.12)
11
+ rake (0.9.2)
12
+ rcov (0.9.10)
13
+ rspec (2.3.0)
14
+ rspec-core (~> 2.3.0)
15
+ rspec-expectations (~> 2.3.0)
16
+ rspec-mocks (~> 2.3.0)
17
+ rspec-core (2.3.1)
18
+ rspec-expectations (2.3.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.3.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.6.4)
28
+ mocha (~> 0.9.10)
29
+ rcov
30
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 duritong
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,94 @@
1
+ = rhn_satellite
2
+
3
+ RhnSatellite is a ruby api to the RedHat Satellite
4
+
5
+ It provides an easy way to interact with a RedHat Satellite API. Mainly the XMLRPC calls are wrapped with a few
6
+ methods, so that talking to the API from ruby is much easier.
7
+
8
+ It is far from complete, but most methods that have been added have been tested and used in real environments with
9
+ the latest satellite. The existing methods should get you an idea how to add missing methods.
10
+
11
+ Patches (including tests) are welcomed!
12
+
13
+ RedHat's API is documented here: http://docs.redhat.com/docs/en-US/Red_Hat_Network_Satellite/5.4.1/html/API_Overview/index.html
14
+
15
+ == Credentials
16
+
17
+ By default rhn_satellite will look for a yaml containing credentials in `~/.satellite.yaml` or in `/etc/satellite.yaml` .
18
+ This file should contain the following options:
19
+
20
+ hostname: satellite.example.com
21
+ username: foobar
22
+ password: foobar
23
+
24
+ However, you can always set later your own credentials for the whole API:
25
+
26
+ RhnSatellite::Connection::Handler.default_hostname = 'satellite2.example.com'
27
+ RhnSatellite::Connection::Handler.default_username = 'testuser'
28
+ RhnSatellite::Connection::Handler.default_password = 'testpwd'
29
+
30
+ Or different ones on subparts of the api:
31
+
32
+
33
+ RhnSatellite::Channel.hostname = 'satellite.example.com'
34
+ RhnSatellite::Channel.username = 'channel'
35
+ RhnSatellite::Channel.password = 'channel'
36
+
37
+ == Examples
38
+
39
+ A few examples should give an idea how things should work:
40
+
41
+ === Get a system
42
+
43
+ Fetch a system from the satellite.
44
+
45
+ satellite_system = RhnSatellite::System.get('somenode.example.com')
46
+
47
+ === clone channel
48
+
49
+ Clone a new channel.
50
+
51
+ RhnSatellite::ChannelSoftware.clone(
52
+ 'original_label',
53
+ 'new_label',
54
+ 'new_label,
55
+ "Some clone of original_label",
56
+ false
57
+ )
58
+
59
+ === Schedule update of all updateable packages
60
+
61
+ This will get all packages that can be upgraded for a system and will schedule the immediate upgrade of these packages.
62
+
63
+ satellite_system = RhnSatellite::System.get('somenode.example.com')
64
+ packages_to_update = RhnSatellite::System.latest_upgradable_packages(satellite_system['id']).inject({}) { |res,p|
65
+ # sometimes the satellite lists some weird packages, so we check if they actually exist.
66
+ key = "#{p['name']}-#{p['version']}-#{p['release']}"
67
+ if res[key]
68
+ res[key] = p['to_package_id'] if RhnSatellite::Package.exists?(p['to_package_id'])
69
+ else
70
+ res[key] = p['to_package_id']
71
+ end
72
+ res
73
+ }.values.compact
74
+ if packages_to_update.size > 0
75
+ now = Time.now
76
+ patch_time = XMLRPC::DateTime.new(now.year,now.month,now.day,now.hour,now.min, now.sec)
77
+ RhnSatellite::System.schedule_package_install(satellite_system['id'],packages_to_update,patch_time)
78
+ end
79
+
80
+ == Contributing to rhn_satellite
81
+
82
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
83
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
84
+ * Fork the project
85
+ * Start a feature/bugfix branch
86
+ * Commit and push until you are happy with your contribution
87
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
88
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
89
+
90
+ == Copyright
91
+
92
+ Copyright (c) 2011 duritong. See LICENSE.txt for
93
+ further details.
94
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rhn_satellite"
18
+ gem.homepage = "http://github.com/duritong/ruby-rhn_satellite"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{RhnSatellite is a ruby api to the RedHat Satellite}
21
+ gem.description = %Q{It provides an easy way to interact with a RedHat Satellite API.}
22
+ gem.email = "peter.meier@immerda.ch"
23
+ gem.authors = ["duritong"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "rhn_satellite #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ require 'xmlrpc/client'
4
+ require 'rhn_satellite/common/misc'
5
+ require 'rhn_satellite/common/debug'
6
+ require 'rhn_satellite/connection/handler'
7
+ require 'rhn_satellite/connection/base'
8
+ require 'rhn_satellite/common/collection'
9
+
10
+ require 'rhn_satellite/activation_key'
11
+ require 'rhn_satellite/api'
12
+ require 'rhn_satellite/channel'
13
+ require 'rhn_satellite/channel_software'
14
+ require 'rhn_satellite/channel_access'
15
+ require 'rhn_satellite/system'
16
+ require 'rhn_satellite/systemgroup'
17
+ require 'rhn_satellite/package'
18
+
19
+ if File.exists?(file=File.expand_path('~/.satellite.yaml')) || File.exists?(file='/etc/satellite.yaml')
20
+ require 'yaml'
21
+ global_options = YAML.load_file(file)
22
+ RhnSatellite::Connection::Handler.default_hostname = global_options['hostname']
23
+ RhnSatellite::Connection::Handler.default_username = global_options['username']
24
+ RhnSatellite::Connection::Handler.default_password = global_options['password']
25
+ end
@@ -0,0 +1,5 @@
1
+ module RhnSatellite
2
+ class ActivationKey < RhnSatellite::Connection::Base
3
+ collection 'activationkey.listActivationKeys'
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module RhnSatellite
2
+ class Api < RhnSatellite::Connection::Base
3
+ class << self
4
+ def api_version(disconnect=true)
5
+ @api_version ||= get_version('getVersion',disconnect)
6
+ end
7
+
8
+ def satellite_version(disconnect=true)
9
+ @satellite_version ||= get_version('systemVersion',disconnect)
10
+ end
11
+
12
+ def test_connection(user=nil,pwd=nil)
13
+ reset
14
+ test_base = RhnSatellite::Connection::Handler.instance_for(self.name, hostname, user||username, pwd||password, https)
15
+ test_base.connect
16
+ result = test_base.login && test_base.logout
17
+ test_base.disconnect
18
+ result
19
+ end
20
+
21
+ def reset
22
+ @api_version = @satellite_version = nil
23
+ super
24
+ end
25
+
26
+ private
27
+
28
+ def get_version(cmd,disconnect=true)
29
+ base.connect unless base.connected?
30
+ result = base.make_call("api.#{cmd}")
31
+ base.disconnect if disconnect
32
+ result
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module RhnSatellite
2
+ class Channel < RhnSatellite::Connection::Base
3
+ collection 'channel.listAllChannels'
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module RhnSatellite
2
+ class ChannelAccess < RhnSatellite::Connection::Base
3
+
4
+ class << self
5
+ def disable_user_restrictions(channel_label)
6
+ base.default_call('channel.access.disableUserRestrictions',channel_label)
7
+ end
8
+ def enable_user_restrictions(channel_label)
9
+ base.default_call('channel.access.enableUserRestrictions',channel_label)
10
+ end
11
+ def get_org_sharing(channel_label)
12
+ base.default_call('channel.access.getOrgSharing',channel_label)
13
+ end
14
+ def set_org_sharing(channel_label,access)
15
+ base.default_call('channel.access.setOrgSharing',channel_label,access)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module RhnSatellite
2
+ class ChannelSoftware < RhnSatellite::Connection::Base
3
+
4
+ class << self
5
+ def clone(original_label,name,label,summary,original_state=true,additional_options = {})
6
+ channel_details = {
7
+ 'name' => name,
8
+ 'label' => label,
9
+ 'summary' => summary
10
+ }.merge(additional_options)
11
+ base.default_call('channel.software.clone',original_label,channel_details,original_state)
12
+ end
13
+
14
+ def list_children(channel_label)
15
+ base.default_call('channel.software.listChildren',channel_label)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module RhnSatellite
2
+ module Common
3
+ module Collection
4
+ def all(&blk)
5
+ result = base.default_call(@collection_cmd).to_a
6
+ result.each {|record| yield(record) } if block_given?
7
+ result
8
+ end
9
+
10
+ def get(name)
11
+ all.find{|item| item["name"] =~ /#{name}/ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module RhnSatellite
2
+ module Common
3
+ module Debug
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def debug(msg)
9
+ self.class.debug(msg)
10
+ end
11
+
12
+ module ClassMethods
13
+ def debug_enabled
14
+ @debug_enabled ||= false
15
+ end
16
+
17
+ def debug_enabled=(enable)
18
+ @debug_enabled = enable
19
+ end
20
+
21
+ def output=(output)
22
+ @output = output
23
+ end
24
+
25
+ def output
26
+ @output ||= STDOUT
27
+ end
28
+
29
+ def debug(msg)
30
+ output.puts msg if @debug_enabled
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ module RhnSatellite
2
+ module Common
3
+ module Misc
4
+ def gen_date_time(time='now')
5
+ if time == 'now'
6
+ t = Time.now
7
+ elsif time.is_a?(DateTime) || time.is_a?(Time)
8
+ t = time
9
+ else
10
+ return time
11
+ end
12
+ XMLRPC::DateTime.new(
13
+ t.year,
14
+ t.month,
15
+ t.day,
16
+ t.hour,
17
+ t.min,
18
+ t.sec
19
+ )
20
+ end
21
+ module_function :gen_date_time
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module RhnSatellite
2
+ module Connection
3
+ class Base
4
+ class << self
5
+ attr_accessor :hostname, :username, :password, :https
6
+
7
+ attr_reader :collection_cmd
8
+
9
+ def reset
10
+ RhnSatellite::Connection::Handler.reset_instance(self.name)
11
+ end
12
+
13
+ private
14
+ def base
15
+ RhnSatellite::Connection::Handler.instance_for(self.name, hostname, username, password, https)
16
+ end
17
+
18
+ def collection(cmd)
19
+ @collection_cmd = cmd
20
+ extend RhnSatellite::Common::Collection
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end