main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/cmd/raidcli/shell"
  21. "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
  22. "yunion.io/x/onecloud/pkg/baremetal/utils/raid/drivers"
  23. "yunion.io/x/onecloud/pkg/util/ssh"
  24. )
  25. type BaseOptions struct {
  26. Debug bool `help:"debug mode"`
  27. Host string `help:"SSH Host IP" default:"$RAID_HOST" metavar:"RAID_HOST"`
  28. Username string `help:"Username, usually root" default:"$RAID_USERNAME" metavar:"RAID_USERNAME"`
  29. Password string `help:"Password" default:"$RAID_PASSWORD" metavar:"RAID_PASSWORD"`
  30. Driver string `help:"Raid driver" default:"$RAID_DRIVER" metavar:"RAID_DRIVER" choices:"MegaRaid|HPSARaid|Mpt2SAS|MarvelRaid"`
  31. LocalHost bool `help:"Run raidcli in localhost"`
  32. SUBCOMMAND string `help:"s3cli subcommand" subcommand:"true"`
  33. }
  34. var (
  35. options = &BaseOptions{}
  36. )
  37. func getSubcommandParser() (*structarg.ArgumentParser, error) {
  38. parse, e := structarg.NewArgumentParserWithHelp(options,
  39. "raidcli",
  40. "Command-line interface to test RAID drivers.",
  41. `See "raidcli COMMAND --help" for help on a specific command.`)
  42. if e != nil {
  43. return nil, e
  44. }
  45. subcmd := parse.GetSubcommand()
  46. if subcmd == nil {
  47. return nil, fmt.Errorf("No subcommand argument.")
  48. }
  49. for _, v := range shellutils.CommandTable {
  50. _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
  51. if e != nil {
  52. return nil, e
  53. }
  54. }
  55. return parse, nil
  56. }
  57. func showErrorAndExit(e error) {
  58. fmt.Fprintf(os.Stderr, "%s", e)
  59. fmt.Fprintln(os.Stderr)
  60. os.Exit(1)
  61. }
  62. func newClient() (raid.IRaidDriver, error) {
  63. if options.Debug {
  64. raid.Debug = true
  65. }
  66. if len(options.Driver) == 0 {
  67. return nil, fmt.Errorf("Missing driver")
  68. }
  69. var drv raid.IRaidDriver
  70. if !options.LocalHost {
  71. if len(options.Host) == 0 {
  72. return nil, fmt.Errorf("Missing host")
  73. }
  74. if len(options.Username) == 0 {
  75. return nil, fmt.Errorf("Missing username")
  76. }
  77. if len(options.Password) == 0 {
  78. return nil, fmt.Errorf("Missing password")
  79. }
  80. sshClient, err := ssh.NewClient(
  81. options.Host,
  82. 22,
  83. options.Username,
  84. options.Password,
  85. "",
  86. )
  87. if err != nil {
  88. return nil, fmt.Errorf("ssh client init fail: %s", err)
  89. }
  90. drv = drivers.GetDriver(options.Driver, sshClient)
  91. if drv == nil {
  92. return nil, fmt.Errorf("not supported driver %s", options.Driver)
  93. }
  94. } else {
  95. drv = drivers.GetLocalDriver(options.Driver)
  96. }
  97. err := drv.ParsePhyDevs()
  98. if err != nil {
  99. return nil, fmt.Errorf("parse phyical devices error %s", err)
  100. }
  101. return drv, nil
  102. }
  103. func main() {
  104. parser, e := getSubcommandParser()
  105. if e != nil {
  106. showErrorAndExit(e)
  107. }
  108. e = parser.ParseArgs(os.Args[1:], false)
  109. // options := parser.Options().(*BaseOptions)
  110. if parser.IsHelpSet() {
  111. fmt.Print(parser.HelpString())
  112. return
  113. }
  114. subcmd := parser.GetSubcommand()
  115. subparser := subcmd.GetSubParser()
  116. if e != nil || subparser == nil {
  117. if subparser != nil {
  118. fmt.Print(subparser.Usage())
  119. } else {
  120. fmt.Print(parser.Usage())
  121. }
  122. showErrorAndExit(e)
  123. }
  124. suboptions := subparser.Options()
  125. if subparser.IsHelpSet() {
  126. fmt.Print(subparser.HelpString())
  127. return
  128. }
  129. var client raid.IRaidDriver
  130. client, e = newClient()
  131. if e != nil {
  132. showErrorAndExit(e)
  133. }
  134. e = subcmd.Invoke(client, suboptions)
  135. if e != nil {
  136. showErrorAndExit(e)
  137. }
  138. }