wbem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == 0.1.0
2
+
3
+ * First public version
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+
2
+ = ruby-wbem
3
+
4
+ * http://github.com/kkaempf/ruby-wbem
5
+
6
+ == INTRODUCTION
7
+
8
+ ruby-wbem provides an abstraction layer on top of ruby-sfcc (CIM/XML)
9
+ and openwsman (WS-Management)
10
+
11
+ You can use it to connect to any CIMOM talking CIM-XML or WS-Management
12
+
13
+ == Requirements
14
+
15
+ === Ruby interpreters
16
+
17
+ ruby-wbem has been built and the testsuite ran against:
18
+
19
+ * ruby 1.8.7
20
+ * ruby 1.9.2
21
+
22
+ == Quick Start
23
+
24
+ You can create a gem by running rake gem.
25
+
26
+ See the testsuite for more examples. Or run rake docs.
27
+
28
+ require 'rubygems'
29
+ require 'wbem'
30
+
31
+ client = Wbem.connect('http://user:password@hostname:5988')
32
+ client.namespaces.each do |ns|
33
+ puts "Namespace: #{ns}"
34
+ end
35
+
36
+ == Links
37
+
38
+ === Documentation
39
+
40
+ * http://rdoc.info/projects/kkaempf/ruby-wbem
41
+
42
+ === Code Metrics
43
+
44
+ * http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fkkaempf%2Fruby-wbem.git
45
+
46
+ == Authors
47
+
48
+ * Klaus Kämpf <kkaempf@suse.de>
@@ -0,0 +1,79 @@
1
+ #
2
+ # wbem/cimxml.rb
3
+ # CimxmlClient implementation for ruby-wbem
4
+ #
5
+ # Copyright (c) SUSE Linux Products GmbH 2011
6
+ # Written by Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the MIT license
9
+ #
10
+ require "wbem/wbem"
11
+
12
+ module Wbem
13
+ class CimxmlClient < WbemClient
14
+ require "sfcc"
15
+
16
+ def initialize url
17
+ super url
18
+ @client = Sfcc::Cim::Client.connect url
19
+ end
20
+
21
+ def objectpath namespace, classname = nil
22
+ Sfcc::Cim::ObjectPath.new(namespace, classname)
23
+ end
24
+
25
+ #
26
+ # identify client
27
+ # return identification string
28
+ # on error return nil and set @response to http response code
29
+ #
30
+ def identify
31
+ "CIM/XML client at #{@url.host}:#{@url.port}"
32
+ end
33
+
34
+ #
35
+ # Return instances for namespace and classname
36
+ #
37
+ def each_instance( ns, cn )
38
+ op = Sfcc::Cim::ObjectPath.new(ns, cn)
39
+ begin
40
+ @client.instances(op).each do |inst|
41
+ yield inst
42
+ end
43
+ rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
44
+ end
45
+ end
46
+
47
+ #
48
+ # Return list of classnames for given namespace
49
+ #
50
+ def classnames namespace, deep_inheritance = false
51
+ STDERR.puts "#{@client}.classnames(#{namespace})"
52
+ ret = []
53
+ op = Sfcc::Cim::ObjectPath.new(namespace)
54
+ flags = deep_inheritance ? Sfcc::Flags::DeepInheritance : 0
55
+ begin
56
+ @client.class_names(op,flags).each do |name|
57
+ ret << name.to_s
58
+ end
59
+ rescue Sfcc::Cim::ErrorInvalidNamespace
60
+ end
61
+ ret
62
+ end
63
+
64
+ #
65
+ # Return list of instance_names (object pathes) for given objectpath
66
+ #
67
+ def instance_names objectpath
68
+ STDERR.puts "#{@client}.instance_names(#{objectpath})"
69
+ ret = []
70
+ begin
71
+ @client.instance_names(objectpath).each do |path|
72
+ ret << path
73
+ end
74
+ rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
75
+ end
76
+ ret
77
+ end
78
+ end # class
79
+ end # module
@@ -0,0 +1,3 @@
1
+ module Wbem
2
+ VERSION = "0.1.0"
3
+ end
data/lib/wbem/wbem.rb ADDED
@@ -0,0 +1,70 @@
1
+ #
2
+ # wbem/wbem.rb
3
+ # WbemClient implementation for ruby-wbem
4
+ #
5
+ # Copyright (c) SUSE Linux Products GmbH 2011
6
+ # Written by Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the MIT license
9
+ #
10
+ module Wbem
11
+ #
12
+ # WbemClient - base class for CimxmlClient and WsmanClient
13
+ #
14
+ class WbemClient
15
+ require 'uri'
16
+
17
+ attr_reader :url, :response
18
+
19
+ def initialize url
20
+ @url = (url.is_a? URI) ? url : URI.parse(url)
21
+ end
22
+
23
+ def response_code
24
+ @client.response_code
25
+ end
26
+
27
+ def fault_string
28
+ @client.fault_string
29
+ end
30
+
31
+ def objectpath namespace, classname
32
+ raise "#{self.class}.objectpath not implemented"
33
+ end
34
+
35
+ private
36
+ def _namespaces ns, cn
37
+ result = nil
38
+ each_instance( ns, cn ) do |inst|
39
+ result ||= [ns]
40
+ name = "#{ns}/#{inst.Name}"
41
+ unless result.include? name
42
+ result << name
43
+ result.concat(_namespaces name, cn)
44
+ end
45
+ end
46
+ result || []
47
+ end
48
+ public
49
+ # return list of namespaces
50
+ def namespaces
51
+ STDERR.puts "Namespaces for #{@url}"
52
+ result = []
53
+ ['root', 'Interop', 'interop'].each do |ns|
54
+ ["CIM_Namespace", "__Namespace", "__NAMESPACE"].each do |cn|
55
+ result.concat(_namespaces ns, cn)
56
+ end
57
+ end
58
+ result.uniq
59
+ end
60
+
61
+ # return list of classnames for namespace ns
62
+ def classnames ns, deep_inheritance=false
63
+ raise "#{self.class}.classnames not implemented"
64
+ end
65
+
66
+ def instance_names classname
67
+ raise "#{self.class}.instance_names not implemented"
68
+ end
69
+ end # Class
70
+ end # Module
data/lib/wbem/wsman.rb ADDED
@@ -0,0 +1,187 @@
1
+ #
2
+ # wbem/wsman.rb
3
+ # WsmanClient implementation for ruby-wbem
4
+ #
5
+ # Copyright (c) SUSE Linux Products GmbH 2011
6
+ # Written by Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the MIT license
9
+ #
10
+ require "wbem/wbem"
11
+ require "openwsman"
12
+
13
+ class AuthError < StandardError
14
+ end
15
+
16
+ module Openwsman
17
+ class Transport
18
+ def Transport.auth_request_callback( client, auth_type )
19
+ STDERR.puts "\t *** Transport.auth_request_callback"
20
+ return nil
21
+ end
22
+ end
23
+ class ObjectPath
24
+ attr_reader :namespace, :classname
25
+ def initialize namespace, classname = nil
26
+ @namespace = namespace
27
+ @classname = classname
28
+ end
29
+ end
30
+ end
31
+
32
+ module Wbem
33
+ class WsmanClient < WbemClient
34
+ def initialize uri
35
+ super uri
36
+ @url.path = "/wsman" if @url.path.nil? || @url.path.empty?
37
+ # Openwsman::debug = -1
38
+ STDERR.puts "WsmanClient connecting to #{uri}" if Wbem.debug
39
+
40
+ @client = Openwsman::Client.new @url.to_s
41
+ raise "Cannot create Openwsman client" unless @client
42
+ @client.transport.timeout = 5
43
+ @client.transport.verify_peer = 0
44
+ @client.transport.verify_host = 0
45
+ # FIXME
46
+ # @client.transport.auth_method = (@url.scheme == 'http') ? Openwsman::BASIC_AUTH_STR : Openwsman::DIGEST_AUTH_STR
47
+ @client.transport.auth_method = Openwsman::BASIC_AUTH_STR
48
+ @options = Openwsman::ClientOptions.new
49
+
50
+ doc = identify
51
+ # STDERR.puts doc.to_xml
52
+ @protocol_version = doc.ProtocolVersion.text
53
+ @product_vendor = doc.ProductVendor.text
54
+ @product_version = doc.ProductVersion.text
55
+ if Wbem.debug
56
+ STDERR.puts "Protocol_version '#{@protocol_version}'"
57
+ STDERR.puts "Product vendor '#{@product_vendor}'"
58
+ STDERR.puts "Product version '#{@product_version}'"
59
+ end
60
+ #
61
+ # Windows winrm 2.0
62
+ # Protocol "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
63
+ # Vendor "Microsoft Corporation"
64
+ # Version "OS: 5.2.3790 SP: 2.0 Stack: 2.0"
65
+ #
66
+ # Windows winrm 1.1
67
+ # Protocol "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
68
+ # Vendor "Microsoft Corporation"
69
+ # Version "OS: 5.1.2600 SP: 3.0 Stack: 1.1"
70
+ #
71
+ # Openwsman 2.2
72
+ # Protocol "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
73
+ # Vendor "Openwsman Project"
74
+ # Version "2.2"
75
+ #
76
+
77
+ unless @protocol_version == Openwsman::XML_NS_WS_MAN
78
+ raise "Unsupported WS-Management protocol '#{@protocol_version}'"
79
+ end
80
+
81
+ case @product_vendor
82
+ when "Microsoft Corporation"
83
+ @prefix = "http://schemas.microsoft.com/wbem/wsman/1/wmi/"
84
+ if @product_version =~ /^OS:\s([\d\.]+)\sSP:\s([\d\.]+)\sStack:\s([\d\.]+)$/
85
+ @product_version = $3
86
+ else
87
+ STDERR.puts "Unrecognized product version #{@product_version}"
88
+ end
89
+ @product = :winrm
90
+ when "Openwsman Project"
91
+ @prefix = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
92
+ @product = :openwsman
93
+ else
94
+ STDERR.puts "Unsupported WS-Management vendor #{@product_vendor}"
95
+ @prefix = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
96
+ @product = :unknown
97
+ end
98
+ STDERR.puts "Connected to vendor '#{@product_vendor}', Version #{@product_version}" if Wbem.debug
99
+
100
+ end
101
+
102
+ def objectpath classname, namespace
103
+ Openwsman::ObjectPath.new classname, namespace
104
+ end
105
+
106
+ #
107
+ # WS-Identify
108
+ # returns Openwsman::XmlDoc
109
+ #
110
+ def identify
111
+ STDERR.puts "Identify client #{@client} with #{@options}" if Wbem.debug
112
+ doc = @client.identify( @options )
113
+ unless doc
114
+ raise RuntimeError.new "Identify failed: HTTP #{@client.response_code}, Err #{@client.last_error}:#{@client.fault_string}"
115
+ end
116
+ if doc.fault?
117
+ fault = doc.fault
118
+ STDERR.puts "Fault: #{fault.to_xml}"
119
+ raise fault.to_s
120
+ end
121
+ # STDERR.puts "Return #{doc.to_xml}"
122
+ doc
123
+ end
124
+
125
+ def each_instance( ns, cn )
126
+ @options.flags = Openwsman::FLAG_ENUMERATION_OPTIMIZATION
127
+ @options.max_elements = 999
128
+ resource = "#{@prefix}#{ns}/#{cn}"
129
+ result = @client.enumerate( @options, nil, resource )
130
+ return unless result
131
+ # STDERR.puts "Result '#{result.to_xml}'"
132
+ return if result.fault?
133
+ items = result.body.EnumerateResponse.Items rescue nil
134
+ items.each do |inst|
135
+ yield inst
136
+ end if items
137
+ end
138
+
139
+ def classnames namespace, deep_inheritance
140
+ # enum_classnames is Openwsman-specific
141
+ unless @product_vendor =~ /Openwsman/ && @product_version >= "2.2"
142
+ STDERR.puts "ENUMERATE_CLASS_NAMES unsupported for #{@product_vendor} #{@product_version}"
143
+ return []
144
+ end
145
+ @options.flags = Openwsman::FLAG_ENUMERATION_OPTIMIZATION
146
+ @options.max_elements = 999
147
+ @options.cim_namespace = namespace
148
+ method = Openwsman::CIM_ACTION_ENUMERATE_CLASS_NAMES
149
+ uri = Openwsman::XML_NS_CIM_INTRINSIC
150
+ result = @client.invoke( @options, uri, method )
151
+ if result.fault?
152
+ puts "Enumerate class names (#{uri}) failed:\n\tResult code #{@client.response_code}, Fault: #{@client.fault_string}"
153
+ return []
154
+ end
155
+ output = result.body[method]
156
+ classes = []
157
+ output.each do |c|
158
+ classes << c.to_s
159
+ end
160
+ return classes
161
+ end
162
+
163
+ def instance_names object_path
164
+ @options.flags = Openwsman::FLAG_ENUMERATION_OPTIMIZATION
165
+ @options.max_elements = 999
166
+ @options.cim_namespace = object_path.namespace
167
+ # http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/
168
+ # CIM=http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2
169
+ # OpenWBEM=http://schema.openwbem.org/wbem/wscim/1/cim-schema/2
170
+ # Linux=http://sblim.sf.net/wbem/wscim/1/cim-schema/2
171
+ # OMC=http://schema.omc-project.org/wbem/wscim/1/cim-schema/2
172
+ # PG=http://schema.openpegasus.org/wbem/wscim/1/cim-schema/2
173
+ uri = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"+object_path.classname
174
+ result = @client.enumerate( @options, nil, uri )
175
+ if result.fault?
176
+ puts "Enumerate instances (#{uri}) failed:\n\tResult code #{@client.response_code}, Fault: #{@client.fault_string}"
177
+ return []
178
+ end
179
+ output = result.body[method]
180
+ instances = []
181
+ output.each do |i|
182
+ instances << i.to_s
183
+ end
184
+ return instances
185
+ end
186
+ end
187
+ end # module
data/lib/wbem.rb ADDED
@@ -0,0 +1,61 @@
1
+ module Wbem
2
+ @@debug = nil
3
+ def Wbem.debug
4
+ @@debug
5
+ end
6
+ def Wbem.debug= level
7
+ @@debug = (level == 0) ? nil : level
8
+ end
9
+ class Client
10
+ require 'uri'
11
+ require 'wbem/wsman'
12
+ require 'wbem/cimxml'
13
+
14
+ attr_reader :url, :response
15
+
16
+ #
17
+ # Wbem::Client.connect uri, protocol = nil
18
+ #
19
+ # Connect to remote client identified by uri and protocol
20
+ # Possible values for protocol:
21
+ # :cimxml - connect via CIM/XML
22
+ # :wsman - connect via WS-Management
23
+ # else - probe connection (cim/xml first)
24
+ #
25
+ def self.connect uri, protocol = nil
26
+ STDERR.puts "Wbem::Client.connect(#{uri},#{protocol})" if Wbem.debug
27
+ u = uri.is_a?(URI) ? uri : URI.parse(uri)
28
+ case protocol
29
+ when :wsman
30
+ return WsmanClient.new u
31
+ when :cimxml
32
+ return CimxmlClient.new u
33
+ end
34
+ STDERR.puts "no connect, check known ports"
35
+ # no connect, check known ports
36
+ case u.port
37
+ when 8888, 8889, 5985, 5986
38
+ return WsmanClient.new u
39
+ when 5988, 5989
40
+ return CimxmlClient.new u
41
+ end
42
+ STDERR.puts "no known ports"
43
+ port = u.port # keep orig port as we change u.port below
44
+ [:wsman, :cimxml].each do |protocol|
45
+ # enforce port if uri provides scheme and host only
46
+ if port == 80 && u.scheme == 'http' # http://hostname
47
+ u.port = (protocol == :cimxml) ? 5988: 5985
48
+ end
49
+ if port == 443 && u.scheme == 'https' # https://hostname
50
+ u.port = (protocol == :cimxml) ? 5989: 5986
51
+ end
52
+ c = Wbem::Client.connect u, protocol
53
+ if c
54
+ STDERR.puts "Connect #{u} as #{c}"
55
+ return c
56
+ end
57
+ end
58
+ end
59
+
60
+ end # Class
61
+ end # Module
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbem
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "Klaus K\xC3\xA4mpf"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 0
32
+ - 5
33
+ version: "0.5"
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: ruby-wbem allows to access a CIMOM transparently through CIM/XML or WS-Management
37
+ email:
38
+ - kkaempf@suse.de
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/wbem.rb
47
+ - lib/wbem/wbem.rb
48
+ - lib/wbem/cimxml.rb
49
+ - lib/wbem/version.rb
50
+ - lib/wbem/wsman.rb
51
+ - CHANGELOG.rdoc
52
+ - README.rdoc
53
+ has_rdoc: true
54
+ homepage: http://www.github.com/kkaempf/ruby-wbem
55
+ licenses: []
56
+
57
+ post_install_message: " ____\n\
58
+ /@ ~-.\n\
59
+ / __ .- | remember to have fun! \n // // @ \n\n"
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 23
79
+ segments:
80
+ - 1
81
+ - 3
82
+ - 6
83
+ version: 1.3.6
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.5.0
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: WBEM client for Ruby based on ruby-sfcc and openwsman
91
+ test_files: []
92
+