-
Notifications
You must be signed in to change notification settings - Fork 2
/
power.go
153 lines (130 loc) · 4.47 KB
/
power.go
1
2
3
4
5
6
7
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
74
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package amt
import (
"fmt"
"github.com/ammmze/go-amt/cim"
)
// PowerOn will power on a given machine.
func (c *Client) PowerOn() error {
return powerOn(c)
}
// PowerOff will power off a given machine.
func (c *Client) PowerOff() error {
return powerOff(c)
}
// PowerCycle will power cycle a given machine.
func (c *Client) PowerCycle() error {
return powerCycle(c)
}
// IsPoweredOn checks current power state.
func (c *Client) IsPoweredOn() (bool, error) {
return isPoweredOn(c)
}
func getPowerStatus(client *Client) (*cim.AssociatedPowerManagementServiceType, error) {
// https://software.intel.com/sites/manageability/AMT_Implementation_and_Reference_Guide/default.htm?turl=WordDocuments%2Fgetsystempowerstate.htm
items, err := client.AssociatedPowerManagementService().EnumerateObjectAndEPR()
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, fmt.Errorf("could not find the associated power management service")
}
return &items[0].Service, nil
}
func powerOn(client *Client) error {
isOn, err := isPoweredOn(client)
if err != nil {
return err
}
if isOn {
return nil
}
return requestPowerState(client, cim.RequestedPowerStateOn)
}
func powerOff(client *Client) error {
status, err := getPowerStatus(client)
if err != nil {
return err
}
if isPoweredOnGivenStatus(status) {
request := selectNextState(getPowerOffStates(), status.AvailableRequestedPowerStates)
if request != cim.RequestedPowerStateUnknown {
return requestPowerState(client, request)
}
return fmt.Errorf("there is no implemented transition state to power off the machine from the current machine state %d. available states are: %v", status.PowerState, status.AvailableRequestedPowerStates)
}
return nil
}
func powerCycle(client *Client) error {
status, err := getPowerStatus(client)
if err != nil {
return err
}
if !isPoweredOnGivenStatus(status) {
return powerOn(client)
}
request := selectNextState(getPowerCycleStates(), status.AvailableRequestedPowerStates)
if request >= 0 {
return requestPowerState(client, request)
}
return fmt.Errorf("there is no implemented transition state to power cycle the machine from the current machine state %d. available states are: %v", status.PowerState, status.AvailableRequestedPowerStates)
}
func isPoweredOn(client *Client) (bool, error) {
status, err := getPowerStatus(client)
if err != nil {
return false, err
}
return isPoweredOnGivenStatus(status), nil
}
func isPoweredOnGivenStatus(status *cim.AssociatedPowerManagementServiceType) bool {
// log.Printf("Current state: %s; Available states: %s\n", status.PowerState, status.AvailableRequestedPowerStates)
return status.PowerState == cim.PowerStateOn
}
// https://software.intel.com/sites/manageability/AMT_Implementation_and_Reference_Guide/default.htm?turl=WordDocuments%2Fchangesystempowerstate.htm
func requestPowerState(client *Client, requestedPowerState cim.RequestedPowerState) error {
status, err := getPowerStatus(client)
if err != nil {
return err
}
if !containsRequestedPowerState(status.AvailableRequestedPowerStates, requestedPowerState) {
return fmt.Errorf("there is no implemented transition state to <%d> from the current machine state <%d>. available states are: %v", requestedPowerState, status.PowerState, status.AvailableRequestedPowerStates)
}
err = client.PowerManagementService().RequestPowerStateChange(requestedPowerState, *status.UserOfService)
if err != nil {
return err
}
return nil
}
func getPowerOffStates() []cim.RequestedPowerState {
return []cim.RequestedPowerState{
cim.RequestedPowerStateOffSoftGraceful,
cim.RequestedPowerStateOffHardGraceful,
cim.RequestedPowerStateOffSoft,
cim.RequestedPowerStateOffHard,
}
}
func getPowerCycleStates() []cim.RequestedPowerState {
return []cim.RequestedPowerState{
cim.RequestedPowerStateMasterBusResetGraceful,
cim.RequestedPowerStatePowerCycleOffSoftGraceful,
cim.RequestedPowerStatePowerCycleOffHardGraceful,
cim.RequestedPowerStateMasterBusReset,
cim.RequestedPowerStatePowerCycleOffSoft,
cim.RequestedPowerStatePowerCycleOffHard,
}
}
func selectNextState(requestedStates []cim.RequestedPowerState, availableStates []cim.RequestedPowerState) cim.RequestedPowerState {
for _, a := range requestedStates {
if containsRequestedPowerState(availableStates, a) {
return a
}
}
return cim.RequestedPowerStateUnknown
}
func containsRequestedPowerState(s []cim.RequestedPowerState, e cim.RequestedPowerState) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}