manager.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 shell
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/printutils"
  21. "yunion.io/x/pkg/util/shellutils"
  22. "yunion.io/x/pkg/util/timeutils"
  23. "yunion.io/x/onecloud/pkg/util/fileutils2"
  24. "yunion.io/x/onecloud/pkg/util/redfish"
  25. )
  26. func init() {
  27. type LanConfigGetOptions struct {
  28. }
  29. shellutils.R(&LanConfigGetOptions{}, "lan-get", "Get configuration of BMC lan", func(cli redfish.IRedfishDriver, args *LanConfigGetOptions) error {
  30. confs, err := cli.GetLanConfigs(context.Background())
  31. if err != nil {
  32. return err
  33. }
  34. printutils.PrintInterfaceList(confs, 0, 0, 0, nil)
  35. return nil
  36. })
  37. type VirtualMediaGetOptions struct {
  38. }
  39. shellutils.R(&VirtualMediaGetOptions{}, "cdrom-get", "Get details of manager virtual media", func(cli redfish.IRedfishDriver, args *VirtualMediaGetOptions) error {
  40. path, image, err := cli.GetVirtualCdromInfo(context.Background())
  41. if err != nil {
  42. return err
  43. }
  44. fmt.Println("Path:", path)
  45. fmt.Println("Image:", image.Image)
  46. fmt.Println("ApiSupport:", image.SupportAction)
  47. return nil
  48. })
  49. type VirtualMediaMountOptions struct {
  50. URL string `help:"cdrom http URL"`
  51. Boot bool `help:"set boot from virtualmedia on next boot"`
  52. }
  53. shellutils.R(&VirtualMediaMountOptions{}, "cdrom-insert", "Insert iso into virtual CD-ROM", func(cli redfish.IRedfishDriver, args *VirtualMediaMountOptions) error {
  54. ctx := context.Background()
  55. err := redfish.MountVirtualCdrom(ctx, cli, args.URL, args.Boot)
  56. if err != nil {
  57. return err
  58. }
  59. fmt.Println("Success!")
  60. return nil
  61. })
  62. type VirtualMediaUmountOptions struct {
  63. }
  64. shellutils.R(&VirtualMediaUmountOptions{}, "cdrom-eject", "Eject iso from virtual CD-ROM", func(cli redfish.IRedfishDriver, args *VirtualMediaUmountOptions) error {
  65. ctx := context.Background()
  66. err := redfish.UmountVirtualCdrom(ctx, cli)
  67. if err != nil {
  68. return err
  69. }
  70. fmt.Println("Success!")
  71. return nil
  72. })
  73. type ReadLogsOptions struct {
  74. Since string `help:"read logs after, format: YYYY-MM-DD"`
  75. }
  76. shellutils.R(&ReadLogsOptions{}, "system-logs", "Read system logs", func(cli redfish.IRedfishDriver, args *ReadLogsOptions) error {
  77. var sinceTm time.Time
  78. var err error
  79. if len(args.Since) > 0 {
  80. sinceTm, err = timeutils.ParseTimeStr(args.Since)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. events, err := cli.ReadSystemLogs(context.Background(), sinceTm)
  86. if err != nil {
  87. return err
  88. }
  89. printutils.PrintInterfaceList(events, 0, 0, 0, nil)
  90. return nil
  91. })
  92. shellutils.R(&ReadLogsOptions{}, "manager-logs", "Read manager logs", func(cli redfish.IRedfishDriver, args *ReadLogsOptions) error {
  93. var sinceTm time.Time
  94. var err error
  95. if len(args.Since) > 0 {
  96. sinceTm, err = timeutils.ParseTimeStr(args.Since)
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. events, err := cli.ReadManagerLogs(context.Background(), sinceTm)
  102. if err != nil {
  103. return err
  104. }
  105. printutils.PrintInterfaceList(events, 0, 0, 0, nil)
  106. return nil
  107. })
  108. shellutils.R(&ReadLogsOptions{}, "system-logs-clear", "Clear system logs", func(cli redfish.IRedfishDriver, args *ReadLogsOptions) error {
  109. err := cli.ClearSystemLogs(context.Background())
  110. if err != nil {
  111. return err
  112. }
  113. return nil
  114. })
  115. shellutils.R(&ReadLogsOptions{}, "manager-logs-clear", "Clear manager logs", func(cli redfish.IRedfishDriver, args *ReadLogsOptions) error {
  116. err := cli.ClearManagerLogs(context.Background())
  117. if err != nil {
  118. return err
  119. }
  120. return nil
  121. })
  122. shellutils.R(&ReadLogsOptions{}, "bmc-reset", "reset bmc", func(cli redfish.IRedfishDriver, args *ReadLogsOptions) error {
  123. err := cli.BmcReset(context.Background())
  124. if err != nil {
  125. return err
  126. }
  127. fmt.Println("Success!")
  128. return nil
  129. })
  130. type NtpConfGetOptions struct {
  131. }
  132. shellutils.R(&NtpConfGetOptions{}, "ntp-get", "Get ntp configuration", func(cli redfish.IRedfishDriver, args *NtpConfGetOptions) error {
  133. conf, err := cli.GetNTPConf(context.Background())
  134. if err != nil {
  135. return err
  136. }
  137. fmt.Println(jsonutils.Marshal(conf).PrettyString())
  138. return nil
  139. })
  140. type NtpConfSetOptions struct {
  141. SERVER []string `help:"ntp servers"`
  142. TimeZone string `help:"time zone, e.g. Asia/Shanghai"`
  143. }
  144. shellutils.R(&NtpConfSetOptions{}, "ntp-set", "Set ntp configuration", func(cli redfish.IRedfishDriver, args *NtpConfSetOptions) error {
  145. ntpConf := redfish.SNTPConf{}
  146. ntpConf.ProtocolEnabled = true
  147. ntpConf.NTPServers = args.SERVER
  148. ntpConf.TimeZone = args.TimeZone
  149. err := cli.SetNTPConf(context.Background(), ntpConf)
  150. if err != nil {
  151. return err
  152. }
  153. fmt.Println("Success!")
  154. return nil
  155. })
  156. type JNLPGetOptions struct {
  157. Save string `help:"save to file"`
  158. }
  159. shellutils.R(&JNLPGetOptions{}, "jnlp-get", "Get Java Console JNLP file", func(cli redfish.IRedfishDriver, args *JNLPGetOptions) error {
  160. jnlp, err := cli.GetConsoleJNLP(context.Background())
  161. if err != nil {
  162. return err
  163. }
  164. if len(args.Save) > 0 {
  165. return fileutils2.FilePutContents(args.Save, jnlp, false)
  166. } else {
  167. fmt.Println(jnlp)
  168. return nil
  169. }
  170. })
  171. }