devtooltemplate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/util/ansible"
  24. )
  25. type SDevtoolTemplate struct {
  26. SVSCronjob
  27. Playbook *ansible.Playbook `length:"text" nullable:"false" create:"required" get:"user" update:"user"`
  28. db.SVirtualResourceBase
  29. }
  30. type SDevtoolTemplateManager struct {
  31. db.SVirtualResourceBaseManager
  32. }
  33. var (
  34. DevtoolTemplateManager *SDevtoolTemplateManager
  35. )
  36. func init() {
  37. DevtoolTemplateManager = &SDevtoolTemplateManager{
  38. SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager(
  39. SDevtoolTemplate{},
  40. "devtool_templates_tbl",
  41. "devtool_template",
  42. "devtool_templates",
  43. ),
  44. }
  45. DevtoolTemplateManager.SetVirtualObject(DevtoolTemplateManager)
  46. }
  47. func (obj *SDevtoolTemplate) PerformBind(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  48. // * get server id
  49. // * get playbook struct and create obj
  50. // * get cronjob struct and create obj
  51. // * create playbook
  52. // taskman.TaskManager.NewTask(ctx, "KVMGuestRebuildRootTask", guest, task.GetUserCred(), task.GetParams(), task.GetTaskId(), "", nil)
  53. task, err := taskman.TaskManager.NewTask(ctx, "TemplateBindingServers", obj, userCred, data.(*jsonutils.JSONDict), "", "", nil)
  54. if err != nil {
  55. log.Errorf("register task TemplateBindingServers error %s", err)
  56. return nil, err
  57. }
  58. task.ScheduleRun(nil)
  59. return nil, nil
  60. }
  61. func (obj *SDevtoolTemplate) PerformUnbind(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  62. // * get server id
  63. // * stop and delete playbook
  64. // * stop and delete cronjob
  65. task, err := taskman.TaskManager.NewTask(ctx, "TemplateUnbindingServers", obj, userCred, data.(*jsonutils.JSONDict), "", "", nil)
  66. if err != nil {
  67. log.Errorf("register task TemplateUnbindingServers error %s", err)
  68. return nil, err
  69. }
  70. task.ScheduleRun(nil)
  71. return nil, nil
  72. }
  73. func (obj *SDevtoolTemplate) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  74. obj.SVirtualResourceBase.PostUpdate(ctx, userCred, query, data)
  75. task, err := taskman.TaskManager.NewTask(ctx, "TemplateUpdate", obj, userCred, data.(*jsonutils.JSONDict), "", "", nil)
  76. if err != nil {
  77. log.Errorf("register task TemplateUpdate error %s", err)
  78. }
  79. task.ScheduleRun(nil)
  80. }
  81. func (obj *SDevtoolTemplate) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
  82. template := obj
  83. items := make([]SCronjob, 0)
  84. q := CronjobManager.Query().Equals("template_id", template.Id)
  85. err := q.All(&items)
  86. if err != nil {
  87. log.Errorf("query cronjobs for %s error: %s", template.Id, err)
  88. return err
  89. }
  90. if len(items) > 0 {
  91. return httperrors.NewNotEmptyError("devtooltemplate")
  92. }
  93. return nil
  94. }