serviceurl_create_task.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 tasks
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/apis/ansible"
  21. "yunion.io/x/onecloud/pkg/apis/devtool"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/devtool/models"
  25. "yunion.io/x/onecloud/pkg/devtool/utils"
  26. )
  27. type ServiceUrlCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(ServiceUrlCreateTask{})
  32. }
  33. func (self *ServiceUrlCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  34. serviceUrl := obj.(*models.SServiceUrl)
  35. info, err := utils.GetServerInfo(ctx, serviceUrl.ServerId)
  36. if err != nil {
  37. serviceUrl.MarkCreateFailed(fmt.Sprintf("unable to get serverInfo of server %s: %v", serviceUrl.ServerId, err))
  38. self.SetStageFailed(ctx, nil)
  39. return
  40. }
  41. url, err := utils.GetServiceUrl(ctx, serviceUrl.Service)
  42. if err != nil {
  43. serviceUrl.MarkCreateFailed(err.Error())
  44. self.SetStageFailed(ctx, nil)
  45. return
  46. }
  47. url, err = utils.FindValidServiceUrl(ctx, utils.Service{
  48. Url: url,
  49. Name: serviceUrl.Service,
  50. }, "", info, &ansible.AnsibleHost{
  51. User: serviceUrl.ServerAnsibleInfo.User,
  52. IP: serviceUrl.ServerAnsibleInfo.IP,
  53. Port: serviceUrl.ServerAnsibleInfo.Port,
  54. Name: serviceUrl.ServerAnsibleInfo.Name,
  55. })
  56. if err != nil {
  57. serviceUrl.MarkCreateFailed(err.Error())
  58. self.SetStageFailed(ctx, nil)
  59. return
  60. }
  61. _, err = db.Update(serviceUrl, func() error {
  62. serviceUrl.Url = url
  63. serviceUrl.Status = devtool.SERVICEURL_STATUS_READY
  64. return nil
  65. })
  66. if err != nil {
  67. log.Errorf("unable to update serviceurl: %v", err)
  68. }
  69. self.SetStageComplete(ctx, nil)
  70. }