mount_target_create_task.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 filesystem
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type MountTargetCreateTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(MountTargetCreateTask{})
  33. }
  34. func (self *MountTargetCreateTask) taskFailed(ctx context.Context, mt *models.SMountTarget, err error) {
  35. mt.SetStatus(ctx, self.UserCred, api.MOUNT_TARGET_STATUS_CREATE_FAILED, err.Error())
  36. logclient.AddActionLogWithStartable(self, mt, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *MountTargetCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. mt := obj.(*models.SMountTarget)
  41. fs, err := mt.GetFileSystem()
  42. if err != nil {
  43. self.taskFailed(ctx, mt, errors.Wrapf(err, "GetFileSystem"))
  44. return
  45. }
  46. ag, err := mt.GetAccessGroup()
  47. if err != nil {
  48. self.taskFailed(ctx, mt, errors.Wrapf(err, "mt.GetAccessGroup"))
  49. return
  50. }
  51. opts := cloudprovider.SMountTargetCreateOptions{
  52. NetworkType: mt.NetworkType,
  53. AccessGroupId: ag.ExternalId,
  54. }
  55. iFs, err := fs.GetICloudFileSystem(ctx)
  56. if err != nil {
  57. self.taskFailed(ctx, mt, errors.Wrapf(err, "fs.GetICloudFileSystem"))
  58. return
  59. }
  60. if opts.NetworkType == api.NETWORK_TYPE_VPC {
  61. network, err := mt.GetNetwork()
  62. if err != nil {
  63. self.taskFailed(ctx, mt, errors.Wrapf(err, "mt.GetNetwork"))
  64. return
  65. }
  66. opts.NetworkId = network.ExternalId
  67. vpc, err := mt.GetVpc()
  68. if err != nil {
  69. self.taskFailed(ctx, mt, errors.Wrapf(err, "mt.GetVpc"))
  70. return
  71. }
  72. opts.VpcId = vpc.ExternalId
  73. }
  74. opts.FileSystemId = fs.ExternalId
  75. iMt, err := iFs.CreateMountTarget(&opts)
  76. if err != nil {
  77. self.taskFailed(ctx, mt, errors.Wrapf(err, "iFs.CreateMountTarget"))
  78. return
  79. }
  80. cloudprovider.Wait(time.Second*10, time.Minute*3, func() (bool, error) {
  81. mts, err := iFs.GetMountTargets()
  82. if err != nil {
  83. return false, errors.Wrapf(err, "iFs.GetMountTargets")
  84. }
  85. for i := range mts {
  86. if mts[i].GetGlobalId() == iMt.GetGlobalId() {
  87. status := mts[i].GetStatus()
  88. log.Infof("expect mount point status %s current is %s", api.MOUNT_TARGET_STATUS_AVAILABLE, status)
  89. if status == api.MOUNT_TARGET_STATUS_AVAILABLE {
  90. iMt = mts[i]
  91. return true, nil
  92. }
  93. }
  94. }
  95. return false, nil
  96. })
  97. mt.SyncWithMountTarget(ctx, self.GetUserCred(), fs.ManagerId, iMt)
  98. self.SetStageComplete(ctx, nil)
  99. }