mod_scheduler.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/scheduler"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/auth"
  23. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  26. )
  27. var (
  28. SchedManager SchedulerManager
  29. )
  30. func init() {
  31. SchedManager = SchedulerManager{modules.NewSchedulerManager("scheduler", "schedulers",
  32. []string{}, []string{})}
  33. modules.Register(&SchedManager)
  34. }
  35. type SchedulerManager struct {
  36. modulebase.ResourceManager
  37. }
  38. func (this *SchedulerManager) DoSchedule(s *mcclient.ClientSession, input *api.ScheduleInput, count int) (*api.ScheduleOutput, error) {
  39. url := fmt.Sprintf("/%s", this.Keyword)
  40. if count <= 0 {
  41. count = 1
  42. }
  43. input.Count = count
  44. body := input.JSON(input)
  45. ret, err := modulebase.Post(this.ResourceManager, s, url, body, "")
  46. if err != nil {
  47. return nil, err
  48. }
  49. output := new(api.ScheduleOutput)
  50. err = ret.Unmarshal(output)
  51. if err != nil {
  52. return nil, fmt.Errorf("Not a valid response: %v", err)
  53. }
  54. return output, nil
  55. }
  56. func (this *SchedulerManager) DoScheduleForecast(s *mcclient.ClientSession, params *api.ScheduleInput, count int) (bool, jsonutils.JSONObject, error) {
  57. if count <= 0 {
  58. count = 1
  59. }
  60. params.Count = count
  61. res, err := this.DoForecast(s, params.JSON(params))
  62. if err != nil {
  63. return false, nil, err
  64. }
  65. canCreate := jsonutils.QueryBoolean(res, "can_create", false)
  66. return canCreate, res, nil
  67. }
  68. func newSchedURL(action string) string {
  69. return fmt.Sprintf("/scheduler/%s", action)
  70. }
  71. func newSchedIdentURL(action, ident string) string {
  72. return fmt.Sprintf("%s/%s", newSchedURL(action), ident)
  73. }
  74. func (this *SchedulerManager) Test(s *mcclient.ClientSession, params *api.ScheduleInput) (jsonutils.JSONObject, error) {
  75. url := newSchedURL("test")
  76. _, obj, err := modulebase.JsonRequest(this.ResourceManager, s, "POST", url, nil, params.JSON(params))
  77. if err != nil {
  78. return nil, err
  79. }
  80. return obj, err
  81. }
  82. func (this *SchedulerManager) DoForecast(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  83. projectId := s.GetProjectId()
  84. domainId := s.GetProjectDomainId()
  85. cliProjectId, _ := params.GetString("project_id")
  86. if cliProjectId != "" {
  87. projectId = cliProjectId
  88. domainId = ""
  89. }
  90. if domainId == "" {
  91. adminSession := auth.GetAdminSession(context.TODO(), "")
  92. ret, err := identity.Projects.Get(adminSession, projectId, nil)
  93. if err != nil {
  94. return nil, err
  95. }
  96. domainId, _ = ret.GetString("domain_id")
  97. }
  98. data := params.(*jsonutils.JSONDict)
  99. data.Set("domain_id", jsonutils.NewString(domainId))
  100. data.Set("project_id", jsonutils.NewString(projectId))
  101. url := newSchedURL("forecast")
  102. _, obj, err := modulebase.JsonRequest(this.ResourceManager, s, "POST", url, nil, data)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return obj, err
  107. }
  108. func (this *SchedulerManager) DoHistoryList(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  109. return this.HistoryList(s, params)
  110. }
  111. func (this *SchedulerManager) DoHistoryShow(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  112. sessionId, err := params.GetString("session_id")
  113. if err != nil {
  114. return nil, httperrors.NewNotFoundError("session_id")
  115. }
  116. return this.HistoryShow(s, sessionId, params)
  117. }
  118. func (this *SchedulerManager) Cleanup(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  119. url := newSchedURL("cleanup")
  120. return modulebase.Post(this.ResourceManager, s, url, params, "")
  121. }
  122. func (this *SchedulerManager) SyncSku(s *mcclient.ClientSession, wait bool) (jsonutils.JSONObject, error) {
  123. params := jsonutils.NewDict()
  124. params.Add(jsonutils.NewBool(wait), "wait")
  125. url := newSchedURL("sync-sku")
  126. return modulebase.Post(this.ResourceManager, s, url, params, "")
  127. }
  128. func (this *SchedulerManager) Kill(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  129. return nil, fmt.Errorf("Not impl")
  130. }
  131. func (this *SchedulerManager) CandidateList(s *mcclient.ClientSession, params jsonutils.JSONObject) (obj jsonutils.JSONObject, err error) {
  132. url := newSchedURL("candidate-list")
  133. _, obj, err = modulebase.JsonRequest(this.ResourceManager, s, "POST", url, nil, params)
  134. if err != nil {
  135. return
  136. }
  137. _parse := func(property string, o jsonutils.JSONObject) (key, val string, err error) {
  138. omap, err := o.GetMap()
  139. if err != nil {
  140. return
  141. }
  142. resObj, ok := omap[property]
  143. if !ok {
  144. err = fmt.Errorf("Get key %q error", property)
  145. return
  146. }
  147. free, err := resObj.Int("free")
  148. reserverd, err := resObj.Int("reserverd")
  149. total, err := resObj.Int("total")
  150. if err != nil {
  151. return
  152. }
  153. key = fmt.Sprintf("%s(free/reserverd/total)", property)
  154. val = fmt.Sprintf("%d/%d/%d", free, reserverd, total)
  155. return
  156. }
  157. parseAdd := func(o jsonutils.JSONObject) error {
  158. odict := o.(*jsonutils.JSONDict)
  159. for _, k := range []string{"cpu", "mem", "storage"} {
  160. k, v, err := _parse(k, o)
  161. if err != nil {
  162. return err
  163. }
  164. odict.Add(jsonutils.NewString(v), k)
  165. }
  166. return nil
  167. }
  168. aggregate := func(result jsonutils.JSONObject) error {
  169. data, _ := result.GetArray("data")
  170. for _, o := range data {
  171. err := parseAdd(o)
  172. if err != nil {
  173. return err
  174. }
  175. }
  176. return nil
  177. }
  178. err = aggregate(obj)
  179. return
  180. }
  181. func (this *SchedulerManager) CandidateDetail(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  182. url := newSchedIdentURL("candidate-detail", id)
  183. return modulebase.Post(this.ResourceManager, s, url, params, "candidate")
  184. }
  185. func (this *SchedulerManager) HistoryList(s *mcclient.ClientSession, params jsonutils.JSONObject) (obj jsonutils.JSONObject, err error) {
  186. url := newSchedURL("history-list")
  187. _, obj, err = modulebase.JsonRequest(this.ResourceManager, s, "POST", url, nil, params)
  188. if err != nil {
  189. return
  190. }
  191. return
  192. }
  193. func (this *SchedulerManager) HistoryShow(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  194. url := newSchedIdentURL("history-detail", id)
  195. return modulebase.Post(this.ResourceManager, s, url, params, "history")
  196. }
  197. func (this *SchedulerManager) CleanCache(s *mcclient.ClientSession, hostId, sessionId string, sync bool) error {
  198. url := newSchedURL("clean-cache")
  199. if len(hostId) > 0 {
  200. url = fmt.Sprintf("%s/%s", url, hostId)
  201. }
  202. if len(sessionId) > 0 {
  203. url = fmt.Sprintf("%s?session=%s", url, sessionId)
  204. }
  205. if sync {
  206. url = fmt.Sprintf("%s?sync_clean=true", url)
  207. }
  208. resp, err := modulebase.RawRequest(this.ResourceManager, s, "POST", url, nil, nil)
  209. if err != nil {
  210. return err
  211. }
  212. resp.Body.Close()
  213. return nil
  214. }