Module: Supermicro::VirtualMedia

Included in:
Client
Defined in:
lib/supermicro/virtual_media.rb

Instance Method Summary collapse

Instance Method Details

#eject_virtual_media(device: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/supermicro/virtual_media.rb', line 75

def eject_virtual_media(device: nil)
  media_list = virtual_media
  
  if device
    # Check for media with an image URL even if Inserted is false (Supermicro quirk)
    media_to_eject = media_list.find { |m| m[:device] == device && (m[:inserted] || m[:image]) }
  else
    media_to_eject = media_list.find { |m| m[:inserted] || m[:image] }
  end
  
  if media_to_eject.nil?
    debug "No media to eject#{device ? " for device #{device}" : ''}", 1, :yellow
    return false
  end
  
  debug "Ejecting #{media_to_eject[:name]} (#{media_to_eject[:image]})...", 1, :yellow
  
  # Supermicro quirk: EjectMedia action may not be available when Inserted is false
  # Try to use InsertMedia with Inserted:false to eject
  path = if media_to_eject[:eject_path] && media_to_eject[:inserted]
          media_to_eject[:eject_path]
         elsif media_to_eject[:action_path]
          # Use InsertMedia action to eject by setting Inserted to false
          media_to_eject[:action_path]
         else
          "/redfish/v1/Managers/1/VirtualMedia/#{media_to_eject[:device]}/Actions/VirtualMedia.InsertMedia"
         end
  
  # For Supermicro, use InsertMedia with a dummy image and Inserted:false to eject
  # Empty string doesn't work, so use a dummy URL
  body = if path.include?("InsertMedia")
          { 
            "Image" => "http://0.0.0.0/dummy.iso",  # Dummy URL to clear the media
            "Inserted" => false,
            "TransferMethod" => "Stream"
          }
         else
          {}
         end
  
  response = authenticated_request(
    :post,
    path,
    body: body.to_json,
    headers: { 'Content-Type': 'application/json' }
  )

  if response.status.between?(200, 299)
    debug "Media ejected successfully.", 1, :green
    return true
  else
    debug "Failed to eject media: #{response.status} - #{response.body}", 1, :red
    return false
  end
end

#find_best_virtual_media_deviceObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/supermicro/virtual_media.rb', line 337

def find_best_virtual_media_device
  media_list = virtual_media
  
  cd_device = media_list.find { |m| 
    m[:media_types]&.include?("CD") || m[:media_types]&.include?("DVD") || 
    m[:name]&.downcase&.include?("cd") || m[:device]&.downcase&.include?("cd")
  }
  
  return cd_device[:device] if cd_device
  
  removable = media_list.find { |m|
    m[:media_types]&.include?("Removable") || 
    m[:name]&.downcase&.include?("removable")
  }
  
  return removable[:device] if removable
  
  media_list.first&.dig(:device)
end

#insert_virtual_media(iso_url, device: nil) ⇒ Object

Raises:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/supermicro/virtual_media.rb', line 131

def insert_virtual_media(iso_url, device: nil)
  # Check for license if mounting HTTP/HTTPS media
  if iso_url.start_with?('http://', 'https://')
    license_info = check_virtual_media_license
    
    if license_info[:available] == false
      raise Error, "Virtual media license required: #{license_info[:message]}"
    elsif license_info[:available] == :unknown
      debug "Warning: Unable to verify virtual media license. Mount may fail if license is missing.", 1, :yellow
      debug "Supermicro requires SFT-OOB-LIC or SFT-DCMS-SINGLE for HTTP/HTTPS virtual media.", 1, :yellow
    else
      debug "Virtual media license verified: #{license_info[:licenses].join(', ')}", 2, :green
    end
  end
  
  device ||= find_best_virtual_media_device
  
  unless device
    raise Error, "No suitable virtual media device found"
  end
  
  eject_virtual_media(device: device)
  
  debug "Inserting media: #{iso_url} into #{device}...", 1, :yellow
  
  tries = 0
  max_tries = 3
  last_error = nil
  
  while tries < max_tries
    begin
      media_info = virtual_media.find { |m| m[:device] == device }
      
      unless media_info
        raise Error, "Virtual media device #{device} not found"
      end
      
      path = if media_info[:action_path]
              media_info[:action_path]
            else
              "/redfish/v1/Managers/1/VirtualMedia/#{device}/Actions/VirtualMedia.InsertMedia"
            end
      
      body = {
        "Image" => iso_url,
        "Inserted" => true,
        "WriteProtected" => true,
        "TransferMethod" => "Stream",
        "TransferProtocolType" => iso_url.start_with?("https") ? "HTTPS" : "HTTP"
      }
      
      response = authenticated_request(
        :post,
        path,
        body: body.to_json,
        headers: { 'Content-Type': 'application/json' }
      )
      
      if response.status == 202
        # Async operation - poll the task
        debug "Virtual media insert is async, polling task...", 1, :yellow
        
        task_result = wait_for_task_completion(response, timeout: 30)
        
        if task_result[:success]
          # Task completed, now verify the media is connected
          if verbosity == 0
            # Show spinner while verifying connection
            spinner = Spinner.new("Verifying connection", type: :dots, color: :yellow)
            spinner.start
          else
            debug "Task completed, verifying connection...", 1, :yellow
          end
          
          connected = false
          5.times do |i|
            sleep 1
            current_media = virtual_media
            inserted_media = current_media.find { |m| m[:device] == device }
            
            if inserted_media
              if verbosity == 0
                spinner&.update("Checking connection: #{inserted_media[:connected_via]}")
              else
                debug "  Status: ConnectedVia=#{inserted_media[:connected_via]}, Inserted=#{inserted_media[:inserted]}", 2
              end
              
              if inserted_media[:connected_via] == "URI" && inserted_media[:inserted]
                spinner&.stop("Media connected via URI", success: true) if verbosity == 0
                debug "✓ Media connected successfully via URI!", 1, :green
                connected = true
                break
              end
            end
          end
          
          spinner&.stop if spinner && verbosity == 0
          
          if connected
            return true
          else
            # Final check
            current_media = virtual_media
            inserted_media = current_media.find { |m| m[:device] == device }
            if inserted_media && inserted_media[:image] == iso_url
              if inserted_media[:connected_via] == "NotConnected"
                error_msg = "Media mounted but NOT CONNECTED! The ISO will NOT boot! ConnectedVia must be 'URI', not 'NotConnected'."
                debug "ERROR: #{error_msg}", 1, :red
                raise Error, error_msg
              else
                debug "Media mounted with status: #{inserted_media[:connected_via]}", 1, :green
                return true
              end
            else
              error_msg = "Failed to verify media mount - media not found on device #{device}"
              debug error_msg, 1, :red
              raise Error, error_msg
            end
          end
        else
          debug "Task failed or timed out", 1, :red
          last_error = task_result[:error] || "Task failed with unknown error"
          debug "Task error details: #{last_error}", 1, :red
          tries += 1
          sleep 2
          next
        end
      elsif response.status.between?(200, 299)
        # Synchronous success (rare)
        debug "Virtual media inserted synchronously.", 1, :green
        sleep 2
        current_media = virtual_media
        inserted_media = current_media.find { |m| m[:device] == device }
        
        if inserted_media && (inserted_media[:inserted] || inserted_media[:image] == iso_url)
          if inserted_media[:connected_via] == "URI"
            debug "✓ Media connected via URI", 1, :green
            return true
          elsif inserted_media[:connected_via] == "NotConnected"
            error_msg = "Media mounted but not connected! ConnectedVia is 'NotConnected' - ISO will not boot"
            debug "ERROR: #{error_msg}", 1, :red
            raise Error, error_msg
          else
            debug "Media mounted with status: #{inserted_media[:connected_via]}", 1, :yellow
            return true
          end
        else
          # If we can't verify, still treat as success but log warning
          debug "WARNING: Could not verify media mount status", 1, :yellow
          return true
        end
      elsif response.status == 400 && response.body.include?("already")
        debug "Media already inserted, ejecting and retrying...", 1, :yellow
        eject_virtual_media(device: device)
        sleep 2
        tries += 1
      else
        debug "Failed to insert media: #{response.status} - #{response.body}", 1, :red
        last_error = "HTTP #{response.status}: #{response.body}"
        tries += 1
        sleep 2
      end
    rescue => e
      debug "Error inserting media: #{e.message}", 1, :red
      last_error = e.message
      tries += 1
      sleep 2
    end
  end
  
  error_msg = "Failed to insert virtual media after #{max_tries} attempts"
  error_msg += ": #{last_error}" if last_error
  raise Error, error_msg
end

#mount_iso_and_boot(iso_url, device: nil) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/supermicro/virtual_media.rb', line 373

def mount_iso_and_boot(iso_url, device: nil)
  debug "Mounting ISO and setting boot override...", 1, :yellow
  
  insert_virtual_media(iso_url, device: device)
  
  sleep 2
  
  begin
    require_relative 'boot'
    boot_to_cd
    debug "System will boot from virtual media on next restart.", 1, :green
    return true
  rescue => e
    debug "ISO mounted but failed to set boot override: #{e.message}", 1, :yellow
    return false
  end
end

#test_iso_accessibility(iso_url) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/supermicro/virtual_media.rb', line 306

def test_iso_accessibility(iso_url)
  debug "Testing if BMC can reach ISO URL: #{iso_url}", 1, :yellow
  
  # Try to use Redfish's built-in validation if available
  path = "/redfish/v1/Managers/1/VirtualMedia/test"
  body = {
    "Image" => iso_url,
    "TestOnly" => true
  }
  
  begin
    response = authenticated_request(
      :post,
      path,
      body: body.to_json,
      headers: { 'Content-Type': 'application/json' }
    )
    
    if response.status == 200
      debug "✓ BMC can reach ISO URL", 1, :green
      return true
    else
      debug "✗ BMC cannot reach ISO URL: #{response.body}", 1, :red
      return false
    end
  rescue => e
    debug "Cannot test ISO accessibility: #{e.message}", 2, :yellow
    return nil
  end
end

#unmount_all_mediaObject



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/supermicro/virtual_media.rb', line 391

def unmount_all_media
  debug "Unmounting all virtual media...", 1, :yellow
  
  media_list = virtual_media
  mounted = media_list.select { |m| m[:inserted] }
  
  if mounted.empty?
    debug "No virtual media currently mounted.", 1, :yellow
    return true
  end
  
  success = true
  mounted.each do |media|
    if eject_virtual_media(device: media[:device])
      debug "  Ejected: #{media[:name]}", 1, :green
    else
      debug "  Failed to eject: #{media[:name]}", 1, :red
      success = false
    end
  end
  
  success
end

#virtual_mediaObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/supermicro/virtual_media.rb', line 8

def virtual_media
  response = authenticated_request(:get, "/redfish/v1/Managers/1/VirtualMedia")
  
  if response.status == 200
    begin
      data = JSON.parse(response.body)
      
      # Fetch each member individually since $expand doesn't work properly
      media = data["Members"].map do |member|
        member_path = member["@odata.id"]
        member_response = authenticated_request(:get, member_path)
        
        if member_response.status == 200
          m = JSON.parse(member_response.body)
        else
          next nil
        end
        # Check if media is actually inserted (not just a dummy URL)
        dummy_url = "http://0.0.0.0/dummy.iso"
        has_real_image = !m["Image"].nil? && !m["Image"].empty? && m["Image"] != dummy_url
        
        is_inserted = m["Inserted"] || 
                      has_real_image || 
                      (m["ConnectedVia"] && m["ConnectedVia"] != "NotConnected") ||
                      (m["ImageName"] && !m["ImageName"].empty?)
        
        # Override: if it's the dummy URL, consider it not inserted
        is_inserted = false if m["Image"] == dummy_url
        
        # Show media status in debug mode
        if is_inserted
          debug "#{m["Name"] || m["Id"]} #{m["ConnectedVia"]} #{m["Image"] || m["ImageName"]}", 1, :green
        else
          debug "#{m["Name"] || m["Id"]} #{m["ConnectedVia"] || 'NotConnected'}", 1, :yellow
        end
        
        action_path = m.dig("Actions", "#VirtualMedia.InsertMedia", "target")
        eject_path = m.dig("Actions", "#VirtualMedia.EjectMedia", "target")
        
        # Use a more descriptive name if the API returns generic names
        name = if m["Name"] == "Virtual Removable Media" && m["Id"]
                 "#{m["Name"]} (#{m["Id"]})"
               else
                 m["Name"] || m["Id"]
               end
        
        { 
          device: m["Id"],
          name: name,
          inserted: is_inserted,
          image: m["Image"] || m["ImageName"],
          connected_via: m["ConnectedVia"],
          media_types: m["MediaTypes"],
          action_path: action_path,
          eject_path: eject_path
        }
      end.compact  # Remove any nil entries
      
      return media
    rescue JSON::ParserError
      raise Error, "Failed to parse virtual media response: #{response.body}"
    end
  else
    raise Error, "Failed to get virtual media. Status code: #{response.status}"
  end
end

#virtual_media_statusObject



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/supermicro/virtual_media.rb', line 357

def virtual_media_status
  media_list = virtual_media
  
  debug "\n=== Virtual Media Status ===", 1, :green
  
  media_list.each do |media|
    debug "\nDevice: #{media[:name]} (#{media[:device]})", 1, :cyan
    debug "  Media Types: #{media[:media_types]&.join(', ')}", 1 if media[:media_types]
    debug "  Inserted: #{media[:inserted] ? 'Yes'.green : 'No'.yellow}", 1
    debug "  Image: #{media[:image]}", 1 if media[:image]
    debug "  Connected Via: #{media[:connected_via]}", 1 if media[:connected_via]
  end
  
  media_list
end