wsman 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08191bba813ef1cace9bdb03fa41b253ae5cd64c
4
+ data.tar.gz: 4864dccfb436376d5a7a6ce9ebd61c2439594929
5
+ SHA512:
6
+ metadata.gz: 0950a91583e94eca3cff0f3d9a73d9da41ffde431ec2617d3615d81f50ab84b76e46e82ca80870000064a01edfa4c690a5359405e437ba31e943cf5ea7bfa200
7
+ data.tar.gz: 5117b791d6f77db1e20b628f854b2705429f58d24609682ff528c33c92cb1aef5c557228cdf09dd0a1ac4bad189bb28c81b90a8961648cb089a42a1ee60a5bc5
@@ -0,0 +1,10 @@
1
+ .DS_Store
2
+
3
+ /.yardoc
4
+ /.bundle
5
+
6
+ /pkg
7
+ /doc
8
+
9
+ /*.gem
10
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,4 @@
1
+ wsman
2
+ =====
3
+
4
+ WS-MAN communication library
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler'
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts 'Run `bundle install` to install missing gems'
7
+ exit e.status_code
8
+ end
9
+
10
+ require 'bundler/gem_tasks'
11
+ require 'yard'
12
+
13
+ YARD::Rake::YardocTask.new
14
+
15
+ # require 'rspec/core'
16
+ # require 'rspec/core/rake_task'
17
+
18
+ # RSpec::Core::RakeTask.new(:spec) do |spec|
19
+ # spec.pattern = FileList['spec/**/*_spec.rb']
20
+ # spec.rspec_opts = '--color --format progress'
21
+ # end
22
+
23
+ # task default: :spec
@@ -0,0 +1,747 @@
1
+ require 'yaml'
2
+ require 'json'
3
+ require 'xmlsimple'
4
+
5
+ class Wsman
6
+ BIOS_SVC_CLASS = "DCIM_BIOSService"
7
+ RAID_SVC_CLASS = "DCIM_RAIDService"
8
+ JOB_SVC_CLASS = "DCIM_JOBService"
9
+ LC_SVC_CLASS = "DCIM_LCService"
10
+ SOFT_SVC_CLASS = "DCIM_SoftwareInstallationService"
11
+
12
+ BASE_URI = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2"
13
+ URI_NS = "#{BASE_URI}/root/dcim"
14
+
15
+ SOFT_IDEN_URI = "#{URI_NS}/DCIM_SoftwareIdentity"
16
+ SOFT_SVC_URI = "#{URI_NS}/DCIM_SoftwareInstallationService"
17
+ SYS_VIEW_URI = "#{URI_NS}/DCIM_SystemView"
18
+ BIOS_ENUM_URI = "#{URI_NS}/DCIM_BIOSEnumeration"
19
+
20
+ CHANGE_BOOT_ORDER_CMD = "ChangeBootOrderByInstanceID"
21
+ CHANGE_BOOT_STATE_CMD = "ChangeBootSourceState"
22
+
23
+ RETURN_CFG_OK = 0
24
+ RETURN_VAL_OK = '0'
25
+ RETURN_CONFIG_VAL_OK = 4096
26
+ RETURN_CFG_JOB = 4096
27
+ RETURN_VAL_FAIL = '2'
28
+ RETURN_VAL_NO_ACTION = '-1'
29
+
30
+ ENUMERATE_CMD = 'enumerate'
31
+
32
+ attr :host
33
+ attr :user
34
+ attr :password
35
+ attr :port
36
+ attr :debug_time
37
+
38
+ def initialize(opts = {})
39
+ @host = opts[:host]
40
+ @user = opts[:user]
41
+ @password = opts[:password]
42
+ @port = opts[:port] || 443
43
+ @debug_time = opts[:debug_time] || false
44
+ @debug = opts[:debug] || false
45
+ end
46
+
47
+ def self.certname(host)
48
+ "/tmp/cer-#{host}.cer"
49
+ end
50
+
51
+ def self.setup_env(host, user, password)
52
+ filename = self.class.certname(host)
53
+ return true if File.exists?(filename)
54
+
55
+ output = %x{ping -W 3 -c 2 #{host} 2>/dev/null >/dev/null}
56
+
57
+ if $?.exitstatus != 0
58
+ puts "Failed to ping host: #{host}"
59
+ return false
60
+ end
61
+
62
+ output = %x{echo | openssl s_client -connect #{host}:443 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >#{filename} 2>&1}
63
+
64
+ if $?.exitstatus != 0
65
+ puts output
66
+ return false
67
+ end
68
+
69
+ true
70
+ end
71
+
72
+ def measure_time(msg)
73
+ start = Time.now if @debug_time
74
+ yield
75
+ puts "#{msg}: #{Time.now - start}" if @debug_time
76
+ end
77
+
78
+ def setup_env
79
+ retVal = self.class.setup_env(@host, @user, @password)
80
+ return retVal
81
+ end
82
+
83
+ def command(action, url, args = "", count = 0)
84
+ retVal = self.setup_env
85
+
86
+ if !retVal
87
+ puts "Unable to ping system...exiting"
88
+ return false
89
+ end
90
+
91
+ filename = self.class.certname(@host)
92
+ output = ""
93
+ ret = 0
94
+ stdargs = "-N root/dcim -v -V -o -j utf-8 -y basic"
95
+
96
+ self.measure_time "WSMAN #{action} #{url} call" do
97
+ cmd = "wsman #{action} #{url} -h #{@host} -P #{@port} -u #{@user} -p #{@password} -c #{filename} #{stdargs} #{args} 2>&1"
98
+ output = %x{#{cmd}}
99
+ ret = $?.exitstatus
100
+ end
101
+
102
+ if ret != 0
103
+ puts "wsman command failed: #{action}"
104
+ puts output
105
+
106
+ return false
107
+ end
108
+
109
+ if output =~ /Connection failed. response code = 0/
110
+ return false if count >= 3
111
+
112
+ puts "Retrying the command: #{count} #{action}"
113
+ sleep 20
114
+
115
+ return command(action, url, args, count + 1)
116
+ end
117
+
118
+ return output
119
+ end
120
+
121
+ def schedule_job(jid, time)
122
+ job_svc_uri = find_instance_uri(JOB_SVC_CLASS)
123
+ output = self.command("invoke -a SetupJobQueue",job_svc_uri, " -k JobArray=\"#{jid}\" -k StartTimeInterval=\"#{time}\"")
124
+
125
+ return false unless output
126
+
127
+ hash = XmlSimple.xml_in(output, "ForceArray" => false)
128
+ t = hash["Body"]
129
+
130
+ if t["Fault"]
131
+ return false, t["Fault"]
132
+ end
133
+
134
+ if t["SetupJobQueue_OUTPUT"]["ReturnValue"].instance_of? Hash
135
+ return true, "nil value"
136
+ end
137
+
138
+ if t["SetupJobQueue_OUTPUT"]["ReturnValue"] != "0"
139
+ return false, t["SetupJobQueue_OUTPUT"]["Message"]
140
+ end
141
+
142
+ return true, 0
143
+ end
144
+
145
+ def clear_all_jobs
146
+ job_svc_uri = find_instance_uri(JOB_SVC_CLASS)
147
+ output = self.command("invoke -a DeleteJobQueue",job_svc_uri, " -m 256 -k JobID=\"JID_CLEARALL\"")
148
+
149
+ return false unless output
150
+
151
+ hash = XmlSimple.xml_in(output, "ForceArray" => false)
152
+ t = hash["Body"]
153
+
154
+ if t["Fault"]
155
+ return false, t["Fault"]
156
+ end
157
+
158
+ if t["DeleteJobQueue_OUTPUT"]["ReturnValue"].instance_of? Hash
159
+ return true, "nil value"
160
+ end
161
+
162
+ ret = t["DeleteJobQueue_OUTPUT"]["ReturnValue"].to_i == 0 rescue false
163
+ return ret, t["DeleteJobQueue_OUTPUT"]["Message"]
164
+ end
165
+
166
+ def get_job_status(jid)
167
+ output = self.command("get","#{URI_NS}/DCIM_LifecycleJob?InstanceID=#{jid}")
168
+ return false unless output
169
+
170
+ hash = XmlSimple.xml_in(output, "ForceArray" => false)
171
+ t = hash["Body"]
172
+ if t["Fault"]
173
+ return false, t["Fault"]["Reason"]["Text"]["content"]
174
+ end
175
+
176
+ js = t["DCIM_LifecycleJob"]["JobStatus"]
177
+ if js == "New"
178
+ if t["DCIM_LifecycleJob"]["MessageID"] == "RED023"
179
+ return false, "In Use"
180
+ end
181
+ end
182
+
183
+ return true, js
184
+ end
185
+
186
+ def is_RS_ready?
187
+ lc_svc_uri = find_instance_uri(LC_SVC_CLASS)
188
+ output = self.command("invoke -a GetRSStatus", lc_svc_uri)
189
+
190
+ return false unless output
191
+
192
+ hash = XmlSimple.xml_in(output, "ForceArray" => false)
193
+ t = hash["Body"]
194
+
195
+ if t["Fault"]
196
+ return false, t["Fault"]
197
+ end
198
+
199
+ if t["GetRSStatus_OUTPUT"]["ReturnValue"].instance_of? Hash
200
+ return true, "Ready"
201
+ end
202
+
203
+ if t["GetRSStatus_OUTPUT"]["ReturnValue"] != "0"
204
+ return false, t["GetRSStatus_OUTPUT"]["Message"]
205
+ end
206
+
207
+ status = t["GetRSStatus_OUTPUT"]["Status"]
208
+ puts "Status from getrsstatus is #{status}"
209
+
210
+ return status == "Ready", status
211
+ end
212
+
213
+ def find_instance_uri(serviceClass)
214
+ url = "#{URI_NS}/#{serviceClass}"
215
+ xml = self.command("enumerate", url, " -m 512 -M epr")
216
+ selectorStr = "#{url}?"
217
+ content = self.process_response(xml,'["Body"]["EnumerateResponse"]["Items"]')
218
+
219
+ if content
220
+ selectorSet = content["EndpointReference"]["ReferenceParameters"]["SelectorSet"]
221
+
222
+ selectorSet["Selector"].each do |selector|
223
+ selectorStr += "," unless selectorStr == "#{url}?"
224
+ selectorStr += selector["Name"] + "=" + selector["content"] unless selector["Name"] == "__cimnamespace"
225
+ end
226
+ end
227
+
228
+ selectorStr
229
+ end
230
+
231
+ def find_base_instance_uri(serviceClass)
232
+ url = "#{BASE_URI}/#{serviceClass}"
233
+ xml = self.command(ENUMERATE_CMD, url, " -m 512 -M epr")
234
+ selectorStr = "#{url}?"
235
+ content = self.process_response(xml,'["Body"]["EnumerateResponse"]["Items"]')
236
+
237
+ if content
238
+ selectorSet = content["EndpointReference"]["ReferenceParameters"]["SelectorSet"]
239
+
240
+ selectorSet["Selector"].each do |selector|
241
+ selectorStr += "," unless selectorStr == "#{url}?"
242
+ selectorStr += selector["Name"] + "=" + selector["content"] unless selector["Name"] == "__cimnamespace"
243
+ end
244
+ end
245
+
246
+ selectorStr
247
+ end
248
+
249
+ def find_instance_uri_from_objepr(content)
250
+ selectorStr = "?"
251
+
252
+ if content
253
+ selectorSet = content["EndpointReference"]["ReferenceParameters"]["SelectorSet"]
254
+
255
+ selectorSet["Selector"].each do |selector|
256
+ selectorStr += "," unless selectorStr == "?"
257
+ selectorStr += selector["Name"] + "=" + selector["content"] unless selector["Name"] == "__cimnamespace"
258
+ end
259
+ end
260
+
261
+ selectorStr
262
+ end
263
+
264
+ def get_selector_string(instHash)
265
+ selectorStr = ""
266
+ selectorSet = instHash["EndpointReference"]["ReferenceParameters"]["SelectorSet"]
267
+
268
+ if selectorSet["Selector"].is_a?(Array)
269
+ selectorSet["Selector"].each do |selector|
270
+ if (!selector.nil?)
271
+ selectorStr += %Q[<w:Selector Name="#{selector["Name"]}">#{selector["content"]}</w:Selector>] unless selector["Name"] == "__cimnamespace"
272
+ end
273
+ end
274
+ elsif selectorSet["Selector"].is_a?(Hash)
275
+ selectorStr += %Q[<w:Selector Name="#{selectorSet["Selector"]["Name"]}">#{selectorSet["Selector"]["content"]}</w:Selector>]
276
+ end
277
+
278
+ puts "selector string is #{selectorStr}"
279
+ selectorStr
280
+ end
281
+
282
+ def get_job_id(instHash)
283
+ jobID = ""
284
+ selectorSet = ""
285
+ testFor12G = instHash["EndpointReference"]
286
+
287
+ if testFor12G.nil?
288
+ selectorSet = instHash["ReferenceParameters"]["SelectorSet"]
289
+ else
290
+ selectorSet = instHash["EndpointReference"]["ReferenceParameters"]["SelectorSet"]
291
+ end
292
+
293
+ if selectorSet["Selector"].is_a?(Array)
294
+ selectorSet["Selector"].each do |selector|
295
+ if !selector.nil?
296
+ jobID = selector["content"] if selector["Name"] != "__cimnamespace"
297
+ end
298
+ end
299
+ elsif selectorSet["Selector"].is_a?(Hash)
300
+ jobID = selectorSet["Selector"]["content"]
301
+ end
302
+
303
+ puts "parsed job id string is #{jobID}"
304
+ jobID
305
+ end
306
+
307
+ def get_job_selector(instHash)
308
+ puts "Parsing job selector string"
309
+
310
+ selectorStr = instHash["ReferenceParameters"]["ResourceURI"] + "?"
311
+ selectorSet = instHash["ReferenceParameters"]["SelectorSet"]
312
+
313
+ selectorSet["Selector"].each do |selector|
314
+ selectorStr += selector["Name"] + "=" + selector["content"] unless selector["Name"] == "__cimnamespace"
315
+ end
316
+
317
+ selectorStr
318
+ end
319
+
320
+ def power_down_system
321
+ retVal = false
322
+ method = "RequestStateChange"
323
+
324
+ xml = self.command(ENUMERATE_CMD, CS_INST_URI, " -m 512 -M objepr")
325
+ instanceList = self.process_response(xml,'["Body"]["EnumerateResponse"]["Items"]["Item"]')
326
+ instanceList = (instanceList.instance_of?(Array)) ? instanceList : [instanceList]
327
+
328
+ instanceList.each do |instance|
329
+ if instance.is_a?(Hash)
330
+ classNames = instance.keys
331
+ classNames.delete("EndpointReference")
332
+
333
+ if (instance[classNames[0]]['Dedicated']).to_i == 0
334
+ newcsinst = "#{URI_NS}/#{classNames[0]}"
335
+ csuri = newcsinst + find_instance_uri_from_objepr(instance)
336
+
337
+ xml = self.command("#{INVOKE_CMD} -a #{method} -k RequestedState='3'", csuri)
338
+ returnVal = self.current_value(xml,method)
339
+
340
+ if returnVal.to_i == RETURN_CFG_OK
341
+ retVal = true
342
+ end
343
+ end
344
+ end
345
+ end
346
+
347
+ retVal
348
+ end
349
+
350
+ def create_update_reboot_job
351
+ retVal = false
352
+ jobID = ""
353
+
354
+ puts "Creating reboot job for updates..."
355
+
356
+ method = "CreateRebootJob"
357
+ inputFile = "/tmp/#{method}.xml"
358
+
359
+ File.open(inputFile, "w+") do |ff|
360
+ ff.write %Q[
361
+ <p:#{method}_INPUT xmlns:p="#{URI_NS}/DCIM_SoftwareInstallationService">
362
+ <p:RebootJobType>3</p:RebootJobType>
363
+ </p:#{method}_INPUT>
364
+ ]
365
+ end
366
+
367
+ cmd = "#{INVOKE_CMD} -a #{method}"
368
+ instURI = find_instance_uri(SOFT_SVC_CLASS)
369
+ output = self.command(cmd, instURI , "-J #{inputFile}")
370
+
371
+ puts "Debug: create reboot job failed no output" unless output
372
+ return [ false, "Failed to create update job" ] unless output
373
+
374
+ retVal = self.current_value(output,method)
375
+
376
+ if retVal.to_i == RETURN_CFG_OK
377
+ puts "No RID returned...invocation of downgrade failed"
378
+ elsif retVal.to_i == RETURN_CFG_JOB
379
+ wsInstance = self.process_response(output, '["Body"]["CreateRebootJob_OUTPUT"]["RebootJobID"]')
380
+ jobID = get_job_id(wsInstance)
381
+ retVal = true
382
+ else
383
+ puts "Error encountered in job creation..."
384
+ end
385
+
386
+ [retVal, jobID]
387
+ end
388
+
389
+ def create_reboot_job
390
+ retVal = false
391
+ jobID = ""
392
+
393
+ puts "Creating reboot job for stacking in job queue..."
394
+
395
+ method = "CreateRebootJob"
396
+ cmd = "#{INVOKE_CMD} -a #{method}"
397
+ instURI = find_instance_uri(JOB_SVC_CLASS)
398
+ output = self.command(cmd, instURI , "-k RebootJobType=3")
399
+
400
+ puts "Debug: create reboot job failed no output" unless output
401
+ return [ false, "Failed to create update job" ] unless output
402
+
403
+ retVal = self.current_value(output,method)
404
+
405
+ if retVal.to_i == RETURN_CFG_OK
406
+ puts "No RID returned...invocation of createrebootjob failed"
407
+ elsif retVal.to_i == RETURN_CFG_JOB
408
+ wsInstance = self.process_response(output, '["Body"]["CreateRebootJob_OUTPUT"]["Job"]')
409
+ jobID = get_job_id(wsInstance)
410
+ retVal = true
411
+ else
412
+ puts "Error encountered in job creation..."
413
+ end
414
+
415
+ [retVal, jobID]
416
+ end
417
+
418
+ def setup_job_queue_multi(jobArray)
419
+ retVal = false
420
+ jobStr = ""
421
+ method = "SetupJobQueue"
422
+
423
+ if jobArray.nil?
424
+ puts "Input job array is nil...exiting"
425
+ else
426
+ inputFile = "/tmp/#{method}.xml"
427
+
428
+ File.open("#{inputFile}", "w+") do |ff|
429
+ ff.write %Q[<p:#{method}_INPUT xmlns:p="#{URI_NS}/#{JOB_SVC_CLASS}">]
430
+
431
+ jobArray.each do |jobID|
432
+ ff.write %Q[<p:JobArray>#{jobID}</p:JobArray>]
433
+ end
434
+
435
+ ff.write %Q[<p:StartTimeInterval>TIME_NOW</p:StartTimeInterval>]
436
+ ff.write %Q[</p:#{method}_INPUT>]
437
+ end
438
+
439
+ cmd = "#{INVOKE_CMD} -a #{method}"
440
+ svcUri = find_instance_uri(JOB_SVC_CLASS)
441
+ output = self.command(cmd, svcUri, "-J #{inputFile}")
442
+
443
+ puts output
444
+
445
+ returnVal = self.current_value(output,method)
446
+
447
+ if returnVal.to_i == RETURN_CFG_OK
448
+ puts "Successfully set up job queue"
449
+ retVal = true
450
+ end
451
+ end
452
+
453
+ retVal
454
+ end
455
+
456
+ def create_targeted_config_job(svc_class_uri, fqdd)
457
+ puts "Creating targeted config job..."
458
+
459
+ cmd = "#{INVOKE_CMD} -a CreateTargetedConfigJob -k Target=#{fqdd} -k ScheduledStartTime=TIME_NOW -k RebootJobType=1"
460
+ output = self.command(cmd, svc_class_uri)
461
+ returnVal = self.current_value(output, "CreateTargetedConfigJob")
462
+
463
+ if returnVal.to_i == RETURN_CFG_JOB
464
+ puts "Successfully created a config job..."
465
+
466
+ wsInstance = self.process_response(output,'["Body"]["CreateTargetedConfigJob_OUTPUT"]["Job"]')
467
+ jobURI = get_job_selector(wsInstance)
468
+
469
+ if jobURI.nil?
470
+ puts "Unable to parse JID for targeted config job on #{fqdd}...Exiting"
471
+ else
472
+ poll_job_for_completion(jobURI)
473
+ end
474
+ end
475
+ end
476
+
477
+ def poll_multiple_jobs(jobArray)
478
+ while jobArray.length != 0
479
+ jobArray.each do |jobID|
480
+ puts "Polling job id #{jobID}"
481
+
482
+ jobURI = "#{URI_NS}/DCIM_LifecycleJob?InstanceID=#{jobID}"
483
+ output = self.command("get", jobURI)
484
+ jobStatus = self.process_response(output,'["Body"]["DCIM_LifecycleJob"]["JobStatus"]')
485
+
486
+ if jobStatus.downcase =~ /.*completed.*/
487
+ puts "Job #{jobID} completed successfully...."
488
+ jobArray.delete(jobID)
489
+ elsif jobStatus.downcase =~ /fail/
490
+ failReason = self.process_response(output,'["Body"]["DCIM_LifecycleJob"]["Message"]')
491
+ failReasonID = self.process_response(output,'["Body"]["DCIM_LifecycleJob"]["MessageID"]')
492
+ puts "Job failed..#{failReasonID}:#{failReason}"
493
+ jobArray.delete(jobID)
494
+ else
495
+ puts "Job status = #{jobStatus}...Continue to poll"
496
+ sleep(30)
497
+ end
498
+ end
499
+ end
500
+ end
501
+
502
+ def poll_job_for_completion(uri)
503
+ puts "Polling config job status... "
504
+
505
+ for i in 0..20
506
+ output = self.command("get", uri)
507
+ jobStatus = self.process_response(output, '["Body"]["DCIM_LifecycleJob"]["JobStatus"]')
508
+
509
+ if jobStatus.downcase =~ /completed/
510
+ puts "Job completed successfully...."
511
+ break
512
+ elsif jobStatus.downcase =~ /fail/
513
+ failReason = self.process_response(output,'["Body"]["DCIM_LifecycleJob"]["Message"]')
514
+ failReasonID = self.process_response(output,'["Body"]["DCIM_LifecycleJob"]["MessageID"]')
515
+
516
+ puts "Job failed..#{failReasonID}:#{failReason}"
517
+ break
518
+ else
519
+ puts "Job status = #{jobStatus}...Continue to poll"
520
+ sleep(30)
521
+ end
522
+ end
523
+ end
524
+
525
+ def get_current_and_pending_bootmode
526
+ current_mode = nil
527
+ pending_mode = nil
528
+
529
+ begin
530
+ output = self.command(
531
+ ENUMERATE_CMD,
532
+ BIOS_ENUM_URI,
533
+ " -m 512 --dialect \"http://schemas.dmtf.org/wbem/cql/1/dsp0202.pdf\" --filter \"select * from DCIM_BIOSEnumeration where AttributeName='BootMode'\" "
534
+ )
535
+
536
+ if output
537
+ wsInstance = self.process_response(output, '["Body"]["EnumerateResponse"]["Items"]')
538
+ current_mode = wsInstance["DCIM_BIOSEnumeration"]["CurrentValue"]
539
+ pending_mode = wsInstance["DCIM_BIOSEnumeration"]["PendingValue"]
540
+ else
541
+ puts "No data returned from enumeration of boot mode attribute"
542
+ end
543
+ rescue Exception => e
544
+ puts "Exception determining boot mode...#{e.message}"
545
+ end
546
+
547
+ [current_mode, pending_mode]
548
+ end
549
+
550
+ def get_uefi_boot_source_settings
551
+ puts "Determining UEFI boot source settings."
552
+
553
+ bss = nil
554
+ uefibss = []
555
+
556
+ url = "#{URI_NS}/DCIM_BootSourceSetting"
557
+ xml = self.command(ENUMERATE_CMD, url, "-m 512")
558
+
559
+ if xml
560
+ bss = self.process_response(xml, '["Body"]["EnumerateResponse"]["Items"]["DCIM_BootSourceSetting"]')
561
+
562
+ if bss
563
+ bss.each do |setting|
564
+ if setting['BootSourceType'] and setting['BootSourceType'] == "UEFI"
565
+ uefibss << setting
566
+ end
567
+ end
568
+ end
569
+ else
570
+ puts "No boot source settings found...Returning empty array of boot source settings"
571
+ end
572
+
573
+ uefibss
574
+ end
575
+
576
+ def get_bios_boot_source_settings
577
+ puts "Determining BIOS boot source settings."
578
+
579
+ bss = nil
580
+ biosbss = []
581
+
582
+ url = "#{URI_NS}/DCIM_BootSourceSetting"
583
+ xml = self.command(ENUMERATE_CMD, url, "-m 512")
584
+
585
+ if xml
586
+ bss = self.process_response(xml, '["Body"]["EnumerateResponse"]["Items"]["DCIM_BootSourceSetting"]')
587
+
588
+ if bss
589
+ bss.each do |setting|
590
+ if setting['BootSourceType'] and (setting['BootSourceType'] == "IPL" or setting['BootSourceType'] == "BCV")
591
+ biosbss << setting
592
+ end
593
+ end
594
+ end
595
+ else
596
+ puts "No boot source settings found...Returning empty array of boot source settings"
597
+ end
598
+
599
+ biosbss
600
+ end
601
+
602
+ def set_boot_sources(boot_mode, boot_source_settings, nic_first = true)
603
+ puts "Setting boot sources - #{boot_mode}, #{nic_first}"
604
+
605
+ boot_source_list = []
606
+ emb_nics = []
607
+ int_nics = []
608
+ all_other_nics = []
609
+ other_boot_srcs = []
610
+ enable_nic_srcs = []
611
+
612
+ return boot_source_list if not boot_source_settings or boot_source_settings.length == 0
613
+ return boot_source_list if boot_mode != "UEFI" and boot_mode != "BIOS"
614
+
615
+ if nic_first
616
+ boot_source_settings.each do |bss|
617
+ boot_src_instance_id = bss['InstanceID']
618
+ puts "DBG: Processing boot source - #{boot_src_instance_id}"
619
+
620
+ emb_nics << boot_src_instance_id if boot_src_instance_id.include?("NIC.Embedded")
621
+ int_nics << boot_src_instance_id if boot_src_instance_id.include?("NIC.Integrated")
622
+ all_other_nics << boot_src_instance_id if boot_src_instance_id.include?("NIC") and !emb_nics.include?(boot_src_instance_id) and !int_nics.include?(boot_src_instance_id)
623
+ other_boot_srcs << boot_src_instance_id if !emb_nics.include?(boot_src_instance_id) and !int_nics.include?(boot_src_instance_id) and !all_other_nics.include?(boot_src_instance_id)
624
+
625
+ if boot_src_instance_id and !other_boot_srcs.include?(boot_src_instance_id)
626
+ if boot_mode == "UEFI"
627
+ puts "DBG: Current state of #{boot_src_instance_id} is #{bss['CurrentEnabledStatus'].to_i}"
628
+ enable_nic_srcs << boot_src_instance_id if bss['CurrentEnabledStatus'].to_i == 0
629
+ else
630
+ puts "Not enabling or checking disabled boot sources for BIOS boot mode"
631
+ end
632
+ end
633
+ end
634
+
635
+ boot_source_list = emb_nics.sort if emb_nics and emb_nics.length > 0
636
+ boot_source_list = boot_source_list | int_nics.sort if int_nics and int_nics.length > 0
637
+ boot_source_list = boot_source_list | all_other_nics.sort if all_other_nics and all_other_nics.length > 0
638
+ boot_source_list = boot_source_list | other_boot_srcs.sort if other_boot_srcs and other_boot_srcs.length > 0
639
+ else
640
+ puts "nic_first = false. Returning current boot order"
641
+ boot_source_list = boot_source_settings
642
+ end
643
+
644
+ puts "DBG: Re-ordered boot source settings list is #{boot_source_list}"
645
+ puts "DBG: Disabled boot source list is #{enable_nic_srcs}"
646
+
647
+ [boot_source_list, enable_nic_srcs]
648
+ end
649
+
650
+ def writeBootSourceFile(input_file, instance_ids)
651
+ File.open(input_file, "w+") do |f|
652
+ f.write %Q[
653
+ <p:#{CHANGE_BOOT_ORDER_CMD}_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/root/dcim/DCIM_BootConfigSetting">
654
+ ]
655
+
656
+ instance_ids.each do |instance_id|
657
+ next unless instance_id
658
+ next if instance_id.empty?
659
+
660
+ f.write %Q[
661
+ <p:source>#{instance_id}</p:source>
662
+ ]
663
+ end
664
+
665
+ f.write %Q[
666
+ </p:#{CHANGE_BOOT_ORDER_CMD}_INPUT>
667
+ ]
668
+ end
669
+
670
+ true
671
+ end
672
+
673
+ def enable_boot_sources(boot_src_list)
674
+ return_val = false
675
+ flip_state = 1
676
+
677
+ if boot_src_list and boot_src_list.length > 0
678
+ return_val = change_boot_source_state(boot_src_list, flip_state)
679
+ else
680
+ puts "DBG: No boot sources to enable"
681
+ end
682
+
683
+ return_val
684
+ end
685
+
686
+ def change_boot_source_state(boot_src_list, state)
687
+ ret_val = false
688
+ return_val = RETURN_VAL_FAIL
689
+
690
+ boot_svc_uri = "#{URI_NS}/DCIM_BootConfigSetting?InstanceID=UEFI"
691
+
692
+ cmd = "invoke -a #{CHANGE_BOOT_STATE_CMD}"
693
+ input_file = "/tmp/#{CHANGE_BOOT_STATE_CMD}.xml"
694
+ if boot_src_list and boot_src_list.length > 0
695
+ ret_val = write_enable_boot_src_file(input_file, state, boot_src_list)
696
+
697
+ if ret_val
698
+ output = self.command(cmd, boot_svc_uri , "-J #{input_file}")
699
+
700
+ if output
701
+ puts "DBG: Output from #{CHANGE_BOOT_STATE_CMD} is #{output}"
702
+ return_val = self.current_value(output, CHANGE_BOOT_STATE_CMD)
703
+ end
704
+ else
705
+ puts "DBG: Failed to create boot source enablement input file"
706
+ end
707
+ else
708
+ puts "DBG: No boot sources to change state on"
709
+ end
710
+
711
+ return_val
712
+ end
713
+
714
+ def write_enable_boot_src_file(input_file, state, instance_ids)
715
+ File.open(input_file, "w+") do |f|
716
+ f.write %Q[
717
+ <p:#{CHANGE_BOOT_STATE_CMD}_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/root/dcim/DCIM_BootConfigSetting">
718
+ <p:EnabledState>#{state}</p:EnabledState>
719
+ ]
720
+
721
+ instance_ids.each do |instance_id|
722
+ next unless instance_id
723
+ next if instance_id.empty?
724
+
725
+ f.write %Q[
726
+ <p:source>#{instance_id}</p:source>
727
+ ]
728
+ end
729
+
730
+ f.write %Q[
731
+ </p:#{CHANGE_BOOT_STATE_CMD}_INPUT>
732
+ ]
733
+ end
734
+
735
+ true
736
+ end
737
+
738
+ def process_response(xml, path, options = { "ForceArray" => false })
739
+ hash = XmlSimple.xml_in(xml, options)
740
+ eval("hash#{path}")
741
+ end
742
+
743
+ def current_value(xml,cmd)
744
+ path = '["Body"]["' + cmd + '_OUTPUT"]["ReturnValue"]'
745
+ process_response(xml , path)
746
+ end
747
+ end
@@ -0,0 +1,3 @@
1
+ class Wsman
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'wsman/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'wsman'
6
+ s.version = Wsman::VERSION
7
+ s.authors = ['Thomas Boerger', 'David Paterson']
8
+ s.homepage = 'https://github.com/crowbar/wsman'
9
+ s.license = 'Apache-2.0'
10
+ s.summary = 'WS-MAN library'
11
+ s.description = 'WS-MAN communication library'
12
+ s.email = 'tboerger@suse.de'
13
+
14
+ s.files = `git ls-files`.split($/)
15
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+ s.require_paths = ['lib']
18
+
19
+ s.post_install_message = File.read('UPGRADING') if File.exists?('UPGRADING')
20
+
21
+ s.add_runtime_dependency 'xml-simple', '>= 1.0.0'
22
+
23
+ s.add_development_dependency 'bundler'
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'yard'
26
+ s.add_development_dependency 'gem-release'
27
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wsman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Boerger
8
+ - David Paterson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: xml-simple
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: 1.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: yard
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: gem-release
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: WS-MAN communication library
85
+ email: tboerger@suse.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - lib/wsman.rb
96
+ - lib/wsman/version.rb
97
+ - wsman.gemspec
98
+ homepage: https://github.com/crowbar/wsman
99
+ licenses:
100
+ - Apache-2.0
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: WS-MAN library
122
+ test_files: []
123
+ has_rdoc: