script.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/pkg/errors"
  20. comapi "yunion.io/x/onecloud/pkg/apis/compute"
  21. api "yunion.io/x/onecloud/pkg/apis/devtool"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/devtool/utils"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules/ansible"
  27. "yunion.io/x/onecloud/pkg/util/stringutils2"
  28. )
  29. type SScript struct {
  30. db.SSharableVirtualResourceBase
  31. // remote
  32. Type string `width:"16" nullable:"false"`
  33. PlaybookReferenceId string `width:"128" nullable:"false"`
  34. MaxTryTimes int `default:"1"`
  35. }
  36. type SScriptManager struct {
  37. db.SSharableVirtualResourceBaseManager
  38. }
  39. var ScriptManager *SScriptManager
  40. func init() {
  41. ScriptManager = &SScriptManager{
  42. SSharableVirtualResourceBaseManager: db.NewSharableVirtualResourceBaseManager(
  43. SScript{},
  44. "script_tbl",
  45. "script",
  46. "scripts",
  47. ),
  48. }
  49. ScriptManager.SetVirtualObject(ScriptManager)
  50. utils.RegisterArgGenerator(MonitorAgent, utils.GetArgs)
  51. }
  52. var MonitorAgent = "monitor agent"
  53. func (sm *SScriptManager) InitializeData() error {
  54. q := sm.Query().Equals("playbook_reference", MonitorAgent)
  55. n, err := q.CountWithError()
  56. if err != nil {
  57. return err
  58. }
  59. if n > 0 {
  60. return nil
  61. }
  62. s := SScript{
  63. PlaybookReferenceId: MonitorAgent,
  64. }
  65. s.ProjectId = "system"
  66. s.IsPublic = true
  67. s.PublicScope = "system"
  68. err = sm.TableSpec().Insert(context.Background(), &s)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. func (sm *SScriptManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.ScriptCreateInput) (api.ScriptCreateInput, error) {
  75. // check ansible playbook reference
  76. session := auth.GetSessionWithInternal(ctx, userCred, "")
  77. pr, err := ansible.AnsiblePlaybookReference.Get(session, input.PlaybookReference, nil)
  78. if err != nil {
  79. return input, errors.Wrapf(err, "unable to get AnsiblePlaybookReference %q", input.PlaybookReference)
  80. }
  81. id, _ := pr.GetString("id")
  82. input.PlaybookReference = id
  83. return input, nil
  84. }
  85. func (s *SScript) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
  86. s.Status = api.SCRIPT_STATUS_READY
  87. s.PlaybookReferenceId, _ = data.GetString("playbook_reference")
  88. return nil
  89. }
  90. func (sm *SScriptManager) FetchCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, objs []interface{}, fields stringutils2.SSortedStrings, isList bool) []api.ScriptDetails {
  91. vDetails := sm.SSharableVirtualResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  92. details := make([]api.ScriptDetails, len(objs))
  93. for i := range details {
  94. details[i].SharableVirtualResourceDetails = vDetails[i]
  95. script := objs[i].(*SScript)
  96. ais, err := script.ApplyInfos()
  97. if err != nil {
  98. log.Errorf("unable to get ApplyInfos of script %s: %v", script.Id, err)
  99. }
  100. details[i].ApplyInfos = ais
  101. }
  102. return details
  103. }
  104. func (s *SScript) ApplyInfos() ([]api.SApplyInfo, error) {
  105. q := ScriptApplyManager.Query().Equals("script_id", s.Id)
  106. var sa []SScriptApply
  107. err := db.FetchModelObjects(ScriptApplyManager, q, &sa)
  108. if err != nil {
  109. return nil, err
  110. }
  111. ai := make([]api.SApplyInfo, len(sa))
  112. for i := range ai {
  113. ai[i].ServerId = sa[i].GuestId
  114. ai[i].TryTimes = sa[i].TryTimes
  115. }
  116. return ai, nil
  117. }
  118. func (s *SScript) PerformApply(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ScriptApplyInput) (api.ScriptApplyOutput, error) {
  119. output := api.ScriptApplyOutput{}
  120. var argsGenerator string
  121. if s.Name == MonitorAgent {
  122. argsGenerator = MonitorAgent
  123. }
  124. sa, err := ScriptApplyManager.createScriptApply(ctx, s.Id, input.ServerID, nil, argsGenerator)
  125. if err != nil {
  126. return output, errors.Wrapf(err, "unable to apply script to server %s", input.ServerID)
  127. }
  128. err = sa.StartApply(ctx, userCred)
  129. if err != nil {
  130. return output, errors.Wrapf(err, "unable to apply script to server %s", input.ServerID)
  131. }
  132. output.ScriptApplyId = sa.Id
  133. return output, nil
  134. }
  135. func (s *SScript) PerformBatchApply(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ScriptBatchApplyInput) (api.ScriptBatchApplyOutput, error) {
  136. output := api.ScriptBatchApplyOutput{
  137. Results: make([]api.ScriptBatchApplyResult, len(input.ServerIds)),
  138. }
  139. var argsGenerator string
  140. if s.Name == MonitorAgent {
  141. argsGenerator = MonitorAgent
  142. }
  143. for i, serverId := range input.ServerIds {
  144. output.Results[i].ServerId = serverId
  145. sa, err := ScriptApplyManager.createScriptApply(ctx, s.Id, serverId, nil, argsGenerator)
  146. if err != nil {
  147. output.Results[i].Reason = err.Error()
  148. continue
  149. }
  150. err = sa.StartApply(ctx, userCred)
  151. if err != nil {
  152. output.Results[i].Reason = err.Error()
  153. continue
  154. }
  155. output.Results[i].Succeed = true
  156. output.Results[i].ScriptApplyId = sa.Id
  157. }
  158. return output, nil
  159. }
  160. type sServerInfo struct {
  161. ServerId string
  162. VpcId string
  163. NetworkIds []string
  164. serverDetails *comapi.ServerDetails
  165. }