main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 main
  15. import (
  16. "fmt"
  17. "os"
  18. "yunion.io/x/pkg/util/shellutils"
  19. "yunion.io/x/structarg"
  20. "yunion.io/x/onecloud/pkg/baremetal/utils/ipmitool"
  21. _ "yunion.io/x/onecloud/pkg/util/ipmitool/shell"
  22. "yunion.io/x/onecloud/pkg/util/ssh"
  23. )
  24. type BaseOptions struct {
  25. MODE string `help:"Execute command mode" choices:"ssh|rmcp"`
  26. HOST string `help:"IP address of remote host"`
  27. PASSWD string `help:"Password"`
  28. User string `help:"Username" short-token:"u" default:"root"`
  29. Port int `help:"Remote service port"`
  30. SUBCOMMAND string `help:"ipmicli subcommand" subcommand:"true"`
  31. }
  32. func showErrorAndExit(e error) {
  33. fmt.Fprintf(os.Stderr, "%s", e)
  34. fmt.Fprintln(os.Stderr)
  35. os.Exit(1)
  36. }
  37. func getSubcommandParser() (*structarg.ArgumentParser, error) {
  38. parser, err := structarg.NewArgumentParserWithHelp(
  39. &BaseOptions{},
  40. "ipmicli",
  41. "Command-line interface to ipmitool",
  42. `See "ipmicli COMMAND --help" for help on a specific command.`,
  43. )
  44. if err != nil {
  45. return nil, err
  46. }
  47. subcmd := parser.GetSubcommand()
  48. if subcmd == nil {
  49. return nil, fmt.Errorf("No subcommand argument.")
  50. }
  51. for _, v := range shellutils.CommandTable {
  52. _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
  53. if e != nil {
  54. return nil, e
  55. }
  56. }
  57. return parser, nil
  58. }
  59. func newExecutor(options *BaseOptions) (ipmitool.IPMIExecutor, error) {
  60. if options.MODE == "ssh" {
  61. port := 22
  62. if options.Port > 0 {
  63. port = options.Port
  64. }
  65. sshCli, err := ssh.NewClient(options.HOST, port, options.User, options.PASSWD, "")
  66. if err != nil {
  67. return nil, err
  68. }
  69. return ipmitool.NewSSHIPMI(sshCli), nil
  70. }
  71. if options.MODE == "rmcp" {
  72. port := 623
  73. if options.Port > 0 {
  74. port = options.Port
  75. }
  76. return ipmitool.NewLanPlusIPMIWithPort(options.HOST, options.User, options.PASSWD, port), nil
  77. }
  78. return nil, fmt.Errorf("Unsupported mode: %s", options.MODE)
  79. }
  80. func main() {
  81. parser, err := getSubcommandParser()
  82. if err != nil {
  83. showErrorAndExit(err)
  84. }
  85. err = parser.ParseArgs(os.Args[1:], false)
  86. options := parser.Options().(*BaseOptions)
  87. if parser.IsHelpSet() {
  88. fmt.Print(parser.HelpString())
  89. return
  90. }
  91. subcmd := parser.GetSubcommand()
  92. subparser := subcmd.GetSubParser()
  93. if err != nil || subparser == nil {
  94. if subparser != nil {
  95. fmt.Print(subparser.Usage())
  96. } else {
  97. fmt.Print(parser.Usage())
  98. }
  99. showErrorAndExit(err)
  100. return
  101. }
  102. suboptions := subparser.Options()
  103. var args []interface{}
  104. if subparser.IsHelpSet() {
  105. fmt.Print(subparser.HelpString())
  106. return
  107. }
  108. executor, err := newExecutor(options)
  109. if err != nil {
  110. showErrorAndExit(err)
  111. }
  112. args = append(args, executor, suboptions)
  113. err = subcmd.Invoke(args...)
  114. if err != nil {
  115. showErrorAndExit(err)
  116. }
  117. }