|
| 1 | +package goipmi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/bmc-toolbox/bmclib/v2/providers" |
| 10 | + "github.com/bougou/go-ipmi" |
| 11 | + "github.com/go-logr/logr" |
| 12 | + "github.com/jacobweinstock/registrar" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // ProviderName for the provider pure go ipmi (go-ipmi) implementation. |
| 17 | + ProviderName = "goipmi" |
| 18 | + // ProviderProtocol for the provider pure go ipmi (go-ipmi) implementation. |
| 19 | + ProviderProtocol = "ipmi" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + // Features implemented by goipmi. |
| 24 | + Features = registrar.Features{ |
| 25 | + providers.FeaturePowerSet, |
| 26 | + providers.FeaturePowerState, |
| 27 | + providers.FeatureBootDeviceSet, |
| 28 | + providers.FeatureUserRead, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +// Config for goipmi provider. |
| 33 | +type Config struct { |
| 34 | + CipherSuite int |
| 35 | + Log logr.Logger |
| 36 | + Port int |
| 37 | + |
| 38 | + client *ipmi.Client |
| 39 | +} |
| 40 | + |
| 41 | +// Option for setting optional Client values. |
| 42 | +type Option func(*Config) |
| 43 | + |
| 44 | +func New(host string, port int, user, pass string, opts ...Option) (*Config, error) { |
| 45 | + cl, err := ipmi.NewClient(host, port, user, pass) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + c := &Config{ |
| 50 | + CipherSuite: 3, |
| 51 | + Log: logr.Discard(), |
| 52 | + client: cl, |
| 53 | + } |
| 54 | + for _, opt := range opts { |
| 55 | + opt(c) |
| 56 | + } |
| 57 | + c.client.WithInterface(ipmi.InterfaceLanplus) |
| 58 | + c.client.WithCipherSuiteID(toCipherSuiteID(c.CipherSuite)) |
| 59 | + |
| 60 | + return c, nil |
| 61 | +} |
| 62 | + |
| 63 | +func WithCipherSuite(cipherSuite int) Option { |
| 64 | + return func(c *Config) { |
| 65 | + c.CipherSuite = cipherSuite |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (c *Config) Name() string { |
| 70 | + return ProviderName |
| 71 | +} |
| 72 | + |
| 73 | +func (c *Config) Open(ctx context.Context) error { |
| 74 | + c.client.WithTimeout(getTimeout(ctx)) |
| 75 | + return c.client.Connect() |
| 76 | +} |
| 77 | + |
| 78 | +func getTimeout(ctx context.Context) time.Duration { |
| 79 | + deadline, ok := ctx.Deadline() |
| 80 | + if !ok { |
| 81 | + return 30 * time.Second |
| 82 | + } |
| 83 | + |
| 84 | + return time.Until(deadline) |
| 85 | +} |
| 86 | + |
| 87 | +func (c *Config) Close(_ context.Context) error { |
| 88 | + return c.client.Close() |
| 89 | +} |
| 90 | + |
| 91 | +func (c *Config) PowerStateGet(_ context.Context) (string, error) { |
| 92 | + r, err := c.client.GetChassisStatus() |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + state := "off" |
| 97 | + if r.PowerIsOn { |
| 98 | + state = "on" |
| 99 | + } |
| 100 | + |
| 101 | + return state, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *Config) PowerSet(_ context.Context, state string) (bool, error) { |
| 105 | + var action ipmi.ChassisControl |
| 106 | + switch strings.ToLower(state) { |
| 107 | + case "on": |
| 108 | + action = ipmi.ChassisControlPowerUp |
| 109 | + case "off": |
| 110 | + action = ipmi.ChassisControlPowerDown |
| 111 | + case "soft": |
| 112 | + action = ipmi.ChassisControlSoftShutdown |
| 113 | + case "reset": |
| 114 | + action = ipmi.ChassisControlHardwareRest |
| 115 | + case "cycle": |
| 116 | + action = ipmi.ChassisControlPowerCycle |
| 117 | + default: |
| 118 | + return false, fmt.Errorf("unknown or unimplemented state request: %v", state) |
| 119 | + } |
| 120 | + |
| 121 | + // ipmi.ChassisControlResponse is an empty struct. |
| 122 | + // No methods return any actual response. So we ignore it. |
| 123 | + _, err := c.client.ChassisControl(action) |
| 124 | + if err != nil { |
| 125 | + return false, err |
| 126 | + } |
| 127 | + |
| 128 | + return true, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (c *Config) BootDeviceSet(_ context.Context, bootDevice string, setPersistent, efiBoot bool) (ok bool, err error) { |
| 132 | + d := ipmi.BootDeviceSelectorNoOverride |
| 133 | + switch strings.ToLower(bootDevice) { |
| 134 | + case "pxe": |
| 135 | + d = ipmi.BootDeviceSelectorForcePXE |
| 136 | + case "disk": |
| 137 | + d = ipmi.BootDeviceSelectorForceHardDrive |
| 138 | + case "safe": |
| 139 | + d = ipmi.BootDeviceSelectorForceHardDriveSafe |
| 140 | + case "diag": |
| 141 | + d = ipmi.BootDeviceSelectorForceDiagnosticPartition |
| 142 | + case "cdrom": |
| 143 | + d = ipmi.BootDeviceSelectorForceCDROM |
| 144 | + case "bios": |
| 145 | + d = ipmi.BootDeviceSelectorForceBIOSSetup |
| 146 | + case "floppy": |
| 147 | + d = ipmi.BootDeviceSelectorForceFloppy |
| 148 | + } |
| 149 | + bt := ipmi.BIOSBootTypeLegacy |
| 150 | + if efiBoot { |
| 151 | + bt = ipmi.BIOSBootTypeEFI |
| 152 | + } |
| 153 | + |
| 154 | + if err := c.client.SetBootDevice(d, bt, setPersistent); err != nil { |
| 155 | + return false, err |
| 156 | + } |
| 157 | + |
| 158 | + return true, nil |
| 159 | +} |
| 160 | + |
| 161 | +func (c *Config) UserRead(_ context.Context) (users []map[string]string, err error) { |
| 162 | + u, err := c.client.ListUser(0) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + |
| 167 | + for _, v := range u { |
| 168 | + if v.Name == "" { |
| 169 | + continue |
| 170 | + } |
| 171 | + users = append(users, map[string]string{ |
| 172 | + "id": fmt.Sprintf("%v", v.ID), |
| 173 | + "name": v.Name, |
| 174 | + "callin": fmt.Sprintf("%v", v.Callin), |
| 175 | + "linkAuth": fmt.Sprintf("%v", v.LinkAuthEnabled), |
| 176 | + "ipmiMsg": fmt.Sprintf("%v", v.IPMIMessagingEnabled), |
| 177 | + "channelPrivLimit": fmt.Sprintf("%v", v.MaxPrivLevel), |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + return users, nil |
| 182 | +} |
| 183 | + |
| 184 | +func toCipherSuiteID(c int) ipmi.CipherSuiteID { |
| 185 | + switch c { |
| 186 | + case 0: |
| 187 | + return ipmi.CipherSuiteID0 |
| 188 | + case 1: |
| 189 | + return ipmi.CipherSuiteID1 |
| 190 | + case 2: |
| 191 | + return ipmi.CipherSuiteID2 |
| 192 | + case 3: |
| 193 | + return ipmi.CipherSuiteID3 |
| 194 | + case 4: |
| 195 | + return ipmi.CipherSuiteID4 |
| 196 | + case 5: |
| 197 | + return ipmi.CipherSuiteID5 |
| 198 | + case 6: |
| 199 | + return ipmi.CipherSuiteID6 |
| 200 | + case 7: |
| 201 | + return ipmi.CipherSuiteID7 |
| 202 | + case 8: |
| 203 | + return ipmi.CipherSuiteID8 |
| 204 | + case 9: |
| 205 | + return ipmi.CipherSuiteID9 |
| 206 | + case 10: |
| 207 | + return ipmi.CipherSuiteID10 |
| 208 | + case 11: |
| 209 | + return ipmi.CipherSuiteID11 |
| 210 | + case 12: |
| 211 | + return ipmi.CipherSuiteID12 |
| 212 | + case 13: |
| 213 | + return ipmi.CipherSuiteID13 |
| 214 | + case 14: |
| 215 | + return ipmi.CipherSuiteID14 |
| 216 | + case 15: |
| 217 | + return ipmi.CipherSuiteID15 |
| 218 | + case 16: |
| 219 | + return ipmi.CipherSuiteID16 |
| 220 | + case 17: |
| 221 | + return ipmi.CipherSuiteID17 |
| 222 | + case 18: |
| 223 | + return ipmi.CipherSuiteID18 |
| 224 | + case 19: |
| 225 | + return ipmi.CipherSuiteID19 |
| 226 | + default: |
| 227 | + return ipmi.CipherSuiteID3 |
| 228 | + } |
| 229 | + |
| 230 | +} |
0 commit comments