Module: Supermicro::Power

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

Instance Method Summary collapse

Instance Method Details

#power_cycleObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/supermicro/power.rb', line 126

def power_cycle
  puts "Power cycling system...".yellow
  
  response = authenticated_request(
    :post,
    "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
    body: { "ResetType": "PowerCycle" }.to_json,
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    puts "Power cycle command sent successfully.".green
    return true
  else
    raise Error, "Failed to power cycle system: #{response.status} - #{response.body}"
  end
end

#power_off(force: false) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/supermicro/power.rb', line 49

def power_off(force: false)
  current_state = power_status
  
  if current_state == "Off"
    puts "System is already powered off.".yellow
    return true
  end
  
  reset_type = force ? "ForceOff" : "GracefulShutdown"
  puts "#{force ? 'Force powering' : 'Gracefully shutting'} off system...".yellow
  
  response = authenticated_request(
    :post,
    "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
    body: { "ResetType": reset_type }.to_json,
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    puts "Power off command sent successfully.".green
    return true
  elsif !force
    puts "Graceful shutdown failed, trying force off...".yellow
    
    response = authenticated_request(
      :post,
      "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
      body: { "ResetType": "ForceOff" }.to_json,
      headers: { 'Content-Type': 'application/json' }
    )
    
    if response.status.between?(200, 299)
      puts "Force power off command sent successfully.".green
      return true
    else
      raise Error, "Failed to power off system: #{response.status} - #{response.body}"
    end
  else
    raise Error, "Failed to force power off system: #{response.status} - #{response.body}"
  end
end

#power_onObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/supermicro/power.rb', line 24

def power_on
  current_state = power_status
  
  if current_state == "On"
    puts "System is already powered on.".yellow
    return true
  end
  
  puts "Powering on system...".yellow
  
  response = authenticated_request(
    :post,
    "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
    body: { "ResetType": "On" }.to_json,
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    puts "Power on command sent successfully.".green
    return true
  else
    raise Error, "Failed to power on system: #{response.status} - #{response.body}"
  end
end

#power_restart(force: false) ⇒ Object



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
# File 'lib/supermicro/power.rb', line 91

def power_restart(force: false)
  reset_type = force ? "ForceRestart" : "GracefulRestart"
  puts "#{force ? 'Force restarting' : 'Gracefully restarting'} system...".yellow
  
  response = authenticated_request(
    :post,
    "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
    body: { "ResetType": reset_type }.to_json,
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    puts "Restart command sent successfully.".green
    return true
  elsif !force
    puts "Graceful restart failed, trying force restart...".yellow
    
    response = authenticated_request(
      :post,
      "/redfish/v1/Systems/1/Actions/ComputerSystem.Reset",
      body: { "ResetType": "ForceRestart" }.to_json,
      headers: { 'Content-Type': 'application/json' }
    )
    
    if response.status.between?(200, 299)
      puts "Force restart command sent successfully.".green
      return true
    else
      raise Error, "Failed to restart system: #{response.status} - #{response.body}"
    end
  else
    raise Error, "Failed to force restart system: #{response.status} - #{response.body}"
  end
end

#power_statusObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/supermicro/power.rb', line 8

def power_status
  response = authenticated_request(:get, "/redfish/v1/Systems/1?$select=PowerState")
  
  if response.status == 200
    begin
      data = JSON.parse(response.body)
      power_state = data["PowerState"]
      return power_state
    rescue JSON::ParserError
      raise Error, "Failed to parse power status response: #{response.body}"
    end
  else
    raise Error, "Failed to get power status. Status code: #{response.status}"
  end
end

#reset_type_allowedObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/supermicro/power.rb', line 144

def reset_type_allowed
  response = authenticated_request(:get, "/redfish/v1/Systems/1")
  
  if response.status == 200
    begin
      data = JSON.parse(response.body)
      
      allowed_values = data.dig("Actions", "#ComputerSystem.Reset", "[email protected]")
      
      if allowed_values
        puts "Allowed reset types:".green
        allowed_values.each { |type| puts "  - #{type}" }
        return allowed_values
      else
        puts "Could not determine allowed reset types".yellow
        return []
      end
    rescue JSON::ParserError
      raise Error, "Failed to parse system response: #{response.body}"
    end
  else
    raise Error, "Failed to get system info. Status code: #{response.status}"
  end
end