main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "context"
  17. "fmt"
  18. "net/url"
  19. "os"
  20. "strings"
  21. "yunion.io/x/pkg/util/shellutils"
  22. "yunion.io/x/structarg"
  23. _ "yunion.io/x/onecloud/cmd/redfishcli/shell"
  24. "yunion.io/x/onecloud/pkg/util/fileutils2"
  25. "yunion.io/x/onecloud/pkg/util/redfish"
  26. "yunion.io/x/onecloud/pkg/util/redfish/bmconsole"
  27. _ "yunion.io/x/onecloud/pkg/util/redfish/loader"
  28. )
  29. type BaseOptions struct {
  30. Debug bool `help:"debug mode"`
  31. Endpoint string `help:"Endpoint, usually https://<host_ipmi_ip>" default:"$REDFISH_ENDPOINT" metavar:"REDFISH_ENDPOINT"`
  32. Username string `help:"Username, usually root" default:"$REDFISH_USERNAME" metavar:"REDFISH_USERNAME"`
  33. Password string `help:"Password" default:"$REDFISH_PASSWORD" metavar:"REDFISH_PASSWORD"`
  34. SUBCOMMAND string `help:"s3cli subcommand" subcommand:"true"`
  35. }
  36. var (
  37. options = &BaseOptions{}
  38. )
  39. func getSubcommandParser() (*structarg.ArgumentParser, error) {
  40. parse, e := structarg.NewArgumentParserWithHelp(options,
  41. "redfishcli",
  42. "Command-line interface to redfish API.",
  43. `See "redfishcli COMMAND --help" for help on a specific command.`)
  44. if e != nil {
  45. return nil, e
  46. }
  47. subcmd := parse.GetSubcommand()
  48. if subcmd == nil {
  49. return nil, fmt.Errorf("No subcommand argument.")
  50. }
  51. bmcJnlp()
  52. for _, v := range shellutils.CommandTable {
  53. _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
  54. if e != nil {
  55. return nil, e
  56. }
  57. }
  58. return parse, nil
  59. }
  60. func bmcJnlp() {
  61. type BmcGetOptions struct {
  62. BRAND string `help:"brand of baremetal" choices:"Lenovo|Huawei|HPE|Dell|Supermicro|Dell6|Dell7|Dell9"`
  63. Save string `help:"save to file"`
  64. Debug bool `help:"turn on debug mode"`
  65. Sku string `help:"sku"`
  66. Model string `help:"model"`
  67. }
  68. shellutils.R(&BmcGetOptions{}, "bmc-jnlp", "Get Java Console JNLP file", func(args *BmcGetOptions) error {
  69. ctx := context.Background()
  70. parts, err := url.Parse(options.Endpoint)
  71. if err != nil {
  72. return err
  73. }
  74. bmc := bmconsole.NewBMCConsole(parts.Hostname(), options.Username, options.Password, args.Debug)
  75. var jnlp string
  76. switch strings.ToLower(args.BRAND) {
  77. case "hp", "hpe":
  78. jnlp, err = bmc.GetIloConsoleJNLP(ctx)
  79. case "dell", "dell inc.":
  80. jnlp, err = bmc.GetIdracConsoleJNLP(ctx, args.Sku, args.Model)
  81. case "dell6":
  82. jnlp, err = bmc.GetIdrac6ConsoleJNLP(ctx, args.Sku, args.Model)
  83. case "dell7":
  84. jnlp, err = bmc.GetIdrac7ConsoleJNLP(ctx, args.Sku, args.Model)
  85. case "dell9":
  86. jnlp, err = bmc.GetIdrac9ConsoleJNLP(ctx)
  87. case "supermicro":
  88. jnlp, err = bmc.GetSupermicroConsoleJNLP(ctx)
  89. case "lenovo":
  90. jnlp, err = bmc.GetLenovoConsoleJNLP(ctx)
  91. }
  92. if err != nil {
  93. return err
  94. }
  95. if len(args.Save) > 0 {
  96. return fileutils2.FilePutContents(args.Save, jnlp, false)
  97. } else {
  98. fmt.Println(jnlp)
  99. return nil
  100. }
  101. })
  102. }
  103. func showErrorAndExit(e error) {
  104. fmt.Fprintf(os.Stderr, "%s", e)
  105. fmt.Fprintln(os.Stderr)
  106. os.Exit(1)
  107. }
  108. func newClient() (redfish.IRedfishDriver, error) {
  109. if len(options.Endpoint) == 0 {
  110. return nil, fmt.Errorf("Missing endpoint")
  111. }
  112. if len(options.Username) == 0 {
  113. return nil, fmt.Errorf("Missing username")
  114. }
  115. if len(options.Password) == 0 {
  116. return nil, fmt.Errorf("Missing password")
  117. }
  118. cli := redfish.NewRedfishDriver(context.Background(), options.Endpoint, options.Username, options.Password, options.Debug)
  119. if cli == nil {
  120. return nil, fmt.Errorf("no approriate driver")
  121. }
  122. return cli, nil
  123. }
  124. func main() {
  125. parser, e := getSubcommandParser()
  126. if e != nil {
  127. showErrorAndExit(e)
  128. }
  129. e = parser.ParseArgs(os.Args[1:], false)
  130. // options := parser.Options().(*BaseOptions)
  131. if parser.IsHelpSet() {
  132. fmt.Print(parser.HelpString())
  133. return
  134. }
  135. subcmd := parser.GetSubcommand()
  136. subparser := subcmd.GetSubParser()
  137. if e != nil || subparser == nil {
  138. if subparser != nil {
  139. fmt.Print(subparser.Usage())
  140. } else {
  141. fmt.Print(parser.Usage())
  142. }
  143. showErrorAndExit(e)
  144. }
  145. suboptions := subparser.Options()
  146. if subparser.IsHelpSet() {
  147. fmt.Print(subparser.HelpString())
  148. return
  149. } else if options.SUBCOMMAND == "bmc-jnlp" {
  150. e = subcmd.Invoke(suboptions)
  151. } else {
  152. var client redfish.IRedfishDriver
  153. client, e = newClient()
  154. if e != nil {
  155. showErrorAndExit(e)
  156. }
  157. e = subcmd.Invoke(client, suboptions)
  158. }
  159. if e != nil {
  160. showErrorAndExit(e)
  161. }
  162. }