ipmi_command.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "strings"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. o "yunion.io/x/onecloud/pkg/webconsole/options"
  22. )
  23. type IpmiInfo struct {
  24. IpAddr string `json:"ip_addr"`
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. Present bool `json:"present"`
  28. }
  29. type IpmitoolSol struct {
  30. *BaseCommand
  31. Info *IpmiInfo
  32. s *mcclient.ClientSession
  33. }
  34. func NewIpmitoolSolCommand(info *IpmiInfo, s *mcclient.ClientSession) (*IpmitoolSol, error) {
  35. if info.IpAddr == "" {
  36. return nil, fmt.Errorf("Empty host ip address")
  37. }
  38. if info.Username == "" {
  39. return nil, fmt.Errorf("Empty username")
  40. }
  41. if info.Password == "" {
  42. return nil, fmt.Errorf("Empty password")
  43. }
  44. name := o.Options.IpmitoolPath
  45. solArgs := []string{
  46. "-I", "lanplus",
  47. "-H", info.IpAddr,
  48. "-U", info.Username,
  49. "-P", info.Password,
  50. "sol",
  51. }
  52. cmd := NewBaseCommand(s, name, solArgs...)
  53. cmd.AppendArgs("activate")
  54. tool := &IpmitoolSol{
  55. BaseCommand: cmd,
  56. Info: info,
  57. }
  58. deactiveCmd := exec.Command(name, solArgs...)
  59. deactiveCmd.Args = append(deactiveCmd.Args, "deactivate")
  60. if err := deactiveCmd.Run(); err != nil {
  61. log.Errorf("ipmitool sol deactive %q error: %v", info.IpAddr, err)
  62. }
  63. return tool, nil
  64. }
  65. func (c *IpmitoolSol) GetCommand() *exec.Cmd {
  66. return c.BaseCommand.GetCommand()
  67. }
  68. func (c *IpmitoolSol) GetSafeCommandString() string {
  69. cmd := []string{c.BaseCommand.name}
  70. i := 0
  71. for i < len(c.BaseCommand.args) {
  72. cmd = append(cmd, c.BaseCommand.args[i])
  73. if c.BaseCommand.args[i] == "-P" {
  74. cmd = append(cmd, "******")
  75. i += 1
  76. }
  77. i += 1
  78. }
  79. return strings.Join(cmd, " ")
  80. }
  81. func (c IpmitoolSol) GetProtocol() string {
  82. return PROTOCOL_TTY
  83. }