schedulers.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 scheduler
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/scheduler"
  21. options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  22. )
  23. func init() {
  24. R(&options.SchedulerTestOptions{}, "scheduler-test", "Emulate schedule process",
  25. func(s *mcclient.ClientSession, args *options.SchedulerTestOptions) error {
  26. params, err := args.Params(s)
  27. if err != nil {
  28. return err
  29. }
  30. listFields := []string{"id", "name", "capacity", "count", "score"}
  31. if args.Details {
  32. listFields = append(listFields, "capacity_details", "score_details")
  33. }
  34. result, err := modules.SchedManager.Test(s, params)
  35. if err != nil {
  36. return err
  37. }
  38. printList(modulebase.JSON2ListResult(result), listFields)
  39. return nil
  40. })
  41. R(&options.SchedulerForecastOptions{}, "scheduler-forecast", "Forecat scheduler result",
  42. func(s *mcclient.ClientSession, args *options.SchedulerForecastOptions) error {
  43. params, err := args.Params(s)
  44. if err != nil {
  45. return err
  46. }
  47. result, err := modules.SchedManager.DoForecast(s, params.JSON(params))
  48. if err != nil {
  49. return err
  50. }
  51. fmt.Println(result.YAMLString())
  52. return nil
  53. })
  54. type SchedulerCandidateListOptions struct {
  55. Type string `help:"Sched type filter" choices:"baremetal|host"`
  56. Region string `help:"Cloud region ID"`
  57. Zone string `help:"Zone ID"`
  58. Limit int `default:"50" help:"Page limit"`
  59. Offset int `default:"0" help:"Page offset"`
  60. }
  61. R(&SchedulerCandidateListOptions{}, "scheduler-candidate-list", "List scheduler candidates",
  62. func(s *mcclient.ClientSession, args *SchedulerCandidateListOptions) error {
  63. params := jsonutils.NewDict()
  64. if args.Limit > 0 {
  65. params.Add(jsonutils.NewInt(int64(args.Limit)), "limit")
  66. }
  67. if args.Offset > 0 {
  68. params.Add(jsonutils.NewInt(int64(args.Offset)), "offset")
  69. }
  70. if len(args.Region) > 0 {
  71. params.Add(jsonutils.NewString(args.Region), "region")
  72. }
  73. if len(args.Zone) > 0 {
  74. params.Add(jsonutils.NewString(args.Zone), "zone")
  75. }
  76. if len(args.Type) > 0 {
  77. params.Add(jsonutils.NewString(args.Type), "type")
  78. }
  79. result, err := modules.SchedManager.CandidateList(s, params)
  80. if err != nil {
  81. return err
  82. }
  83. printList(modulebase.JSON2ListResult(result), []string{
  84. "id", "name", "host_type", "cpu(free/reserverd/total)",
  85. "mem(free/reserverd/total)", "storage(free/reserverd/total)",
  86. "status", "host_status", "enable_status"})
  87. return nil
  88. })
  89. type SchedulerCandidateShowOptions struct {
  90. ID string `help:"ID or name of host"`
  91. }
  92. R(&SchedulerCandidateShowOptions{}, "scheduler-candidate-show", "Show candidate detail",
  93. func(s *mcclient.ClientSession, args *SchedulerCandidateShowOptions) error {
  94. params := jsonutils.NewDict()
  95. result, err := modules.SchedManager.CandidateDetail(s, args.ID, params)
  96. if err != nil {
  97. return err
  98. }
  99. fmt.Println(result.YAMLString())
  100. return nil
  101. })
  102. type SchedulerCleanCacheOptions struct {
  103. HostId string `help:"ID of host" short-token:"h"`
  104. SessionId string `help:"Session id" short-token:"s"`
  105. Sync bool `help:"Sync do clean sched cache"`
  106. }
  107. R(&SchedulerCleanCacheOptions{}, "scheduler-clean-cache", "Clean scheduler hosts cache",
  108. func(s *mcclient.ClientSession, args *SchedulerCleanCacheOptions) error {
  109. err := modules.SchedManager.CleanCache(s, args.HostId, args.SessionId, args.Sync)
  110. if err != nil {
  111. return err
  112. }
  113. return nil
  114. })
  115. type SchedulerHistoryListOptions struct {
  116. Limit int `default:"50" help:"Page limit"`
  117. Offset int `default:"0" help:"Page offset"`
  118. All bool `help:"Show all histories, including scheduler-test"`
  119. IsSuggestion bool `help:"Only show forcast suggestion history"`
  120. }
  121. R(&SchedulerHistoryListOptions{}, "scheduler-history-list", "Show scheduler history list",
  122. func(s *mcclient.ClientSession, args *SchedulerHistoryListOptions) error {
  123. params := jsonutils.NewDict()
  124. if args.Limit == 0 {
  125. params.Add(jsonutils.NewInt(1024), "limit")
  126. } else {
  127. params.Add(jsonutils.NewInt(int64(args.Limit)), "limit")
  128. }
  129. if args.Offset > 0 {
  130. params.Add(jsonutils.NewInt(int64(args.Offset)), "offset")
  131. }
  132. params.Add(jsonutils.NewBool(args.All), "all")
  133. params.Add(jsonutils.NewBool(args.IsSuggestion), "is_suggestion")
  134. result, err := modules.SchedManager.HistoryList(s, params)
  135. if err != nil {
  136. return err
  137. }
  138. printList(modulebase.JSON2ListResult(result), []string{
  139. "session_id", "time", "status", "consuming", "is_suggestion",
  140. })
  141. return nil
  142. })
  143. type SchedulerHistoryShowOptions struct {
  144. ID string `help:"Session or guest ID"`
  145. Log bool `help:"Show schedule process log"`
  146. Raw bool `help:"Show raw data"`
  147. Format string `help:"Output format" choices:"json|yaml"`
  148. }
  149. R(&SchedulerHistoryShowOptions{}, "scheduler-history-show", "Show scheduler history detail",
  150. func(s *mcclient.ClientSession, args *SchedulerHistoryShowOptions) error {
  151. params := jsonutils.NewDict()
  152. if args.Log {
  153. params.Add(jsonutils.JSONTrue, "log")
  154. } else {
  155. params.Add(jsonutils.JSONFalse, "log")
  156. }
  157. if args.Raw {
  158. params.Add(jsonutils.JSONTrue, "raw")
  159. } else {
  160. params.Add(jsonutils.JSONFalse, "false")
  161. }
  162. result, err := modules.SchedManager.HistoryShow(s, args.ID, params)
  163. if err != nil {
  164. return err
  165. }
  166. if args.Format == "json" {
  167. fmt.Println(result.PrettyString())
  168. } else {
  169. fmt.Println(result.YAMLString())
  170. }
  171. return nil
  172. })
  173. type SyncOpt struct {
  174. Wait bool `help:"wait sync finish"`
  175. }
  176. R(&SyncOpt{}, "scheduler-sync-sku", "Sync scheduler SKU cache",
  177. func(s *mcclient.ClientSession, args *SyncOpt) error {
  178. result, err := modules.SchedManager.SyncSku(s, args.Wait)
  179. if err != nil {
  180. return err
  181. }
  182. fmt.Println(result.YAMLString())
  183. return nil
  184. })
  185. }