ansibleplaybookreference.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "os"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. api "yunion.io/x/onecloud/pkg/apis/ansible"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SAnsiblePlaybookReference struct {
  27. db.SSharableVirtualResourceBase
  28. PlaybookPath string `length:"text" nullable:"false" create:"required" get:"user" list:"user"`
  29. Method string `width:"8" nullable:"false" default:"offline" get:"user" list:"user"`
  30. DefaultParams jsonutils.JSONObject `get:"user" list:"user"`
  31. }
  32. type SAnsiblePlaybookReferenceManager struct {
  33. db.SSharableVirtualResourceBaseManager
  34. }
  35. var AnsiblePlaybookReferenceManager *SAnsiblePlaybookReferenceManager
  36. func init() {
  37. AnsiblePlaybookReferenceManager = &SAnsiblePlaybookReferenceManager{
  38. SSharableVirtualResourceBaseManager: db.NewSharableVirtualResourceBaseManager(
  39. SAnsiblePlaybookReference{},
  40. "ansibleplaybook_reference_tbl",
  41. "ansibleplaybookreference",
  42. "ansibleplaybookreferences",
  43. ),
  44. }
  45. AnsiblePlaybookReferenceManager.SetVirtualObject(AnsiblePlaybookReferenceManager)
  46. }
  47. func (arm *SAnsiblePlaybookReferenceManager) ResourceScope() rbacscope.TRbacScope {
  48. return rbacscope.ScopeSystem
  49. }
  50. var (
  51. monitorAgent = "monitor agent"
  52. monitorAgentId = "monitoragent"
  53. )
  54. func (arm *SAnsiblePlaybookReferenceManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.AnsiblePlaybookReferenceCreateInput) (api.AnsiblePlaybookReferenceCreateInput, error) {
  55. if input.Method != api.APReferenceMethodOffline {
  56. return input, httperrors.NewInputParameterError("unkown Method %q", input.Method)
  57. }
  58. if !arm.checkOfflinePath(input.PlaybookPath) {
  59. return input, httperrors.NewInputParameterError("non-existent path: %q", input.PlaybookPath)
  60. }
  61. return input, nil
  62. }
  63. func (arm *SAnsiblePlaybookReferenceManager) checkOfflinePath(path string) bool {
  64. _, err := os.Stat(path)
  65. if err != nil {
  66. if os.IsExist(err) {
  67. return true
  68. }
  69. return false
  70. }
  71. return true
  72. }
  73. func (ar *SAnsiblePlaybookReference) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
  74. if data.Contains("playbook_params") {
  75. params, _ := data.Get("playbook_params")
  76. ar.DefaultParams = params
  77. }
  78. ar.Status = api.APReferenceStatusReady
  79. return nil
  80. }
  81. func (ar *SAnsiblePlaybookReference) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.AnsiblePlaybookReferenceUpdateInput) (api.AnsiblePlaybookReferenceUpdateInput, error) {
  82. return input, httperrors.NewForbiddenError("prohibited operation")
  83. }
  84. func (ar *SAnsiblePlaybookReference) PerformRun(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.AnsiblePlaybookReferenceRunInput) (api.AnsiblePlaybookReferenceRunOutput, error) {
  85. output := api.AnsiblePlaybookReferenceRunOutput{}
  86. ai, err := AnsiblePlaybookInstanceManager.createInstance(ctx, ar.Id, input.Host, input.Args)
  87. if err != nil {
  88. return output, errors.Wrap(err, "unable to create instance")
  89. }
  90. output.AnsiblePlaybookInstanceId = ai.Id
  91. err = ai.runPlaybook(ctx, userCred, ar)
  92. if err != nil {
  93. return output, errors.Wrap(err, "unable to runPlaybook")
  94. }
  95. return output, nil
  96. }
  97. func (ar *SAnsiblePlaybookReference) PerformStop(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.AnsiblePlaybookReferenceStopInput) (jsonutils.JSONObject, error) {
  98. obj, err := AnsiblePlaybookInstanceManager.FetchById(input.AnsiblePlaybookInstanceId)
  99. if err != nil {
  100. return nil, errors.Wrap(err, "unable to fetch ansibleplaybookinstance")
  101. }
  102. ai := obj.(*SAnsiblePlaybookInstance)
  103. return nil, ai.stopPlaybook(ctx, userCred)
  104. }