sshinfo_create_task.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/tristate"
  20. "yunion.io/x/onecloud/pkg/apis/devtool"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/devtool/models"
  24. "yunion.io/x/onecloud/pkg/devtool/utils"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. )
  27. type SshInfoCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(SshInfoCreateTask{})
  32. }
  33. func (self *SshInfoCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  34. sshInfo := obj.(*models.SSshInfo)
  35. serverId := sshInfo.ServerId
  36. session := auth.GetSession(ctx, self.GetUserCred(), "")
  37. sshable, cleanFunc, err := utils.CheckSSHable(session, serverId)
  38. if err != nil {
  39. sshInfo.MarkCreateFailed(err.Error())
  40. self.SetStageFailed(ctx, nil)
  41. return
  42. }
  43. _, err = db.Update(sshInfo, func() error {
  44. if cleanFunc != nil {
  45. sshInfo.NeedClean = tristate.True
  46. }
  47. sshInfo.ServerName = sshable.ServerName
  48. sshInfo.ServerHypervisor = sshable.ServerHypervisor
  49. sshInfo.Host = sshable.Host
  50. sshInfo.User = sshable.User
  51. sshInfo.Port = sshable.Port
  52. sshInfo.ForwardId = sshable.ProxyForwardId
  53. sshInfo.Status = devtool.SSHINFO_STATUS_READY
  54. return nil
  55. })
  56. if err != nil {
  57. log.Errorf("unable to update sshinfo: %v", err)
  58. }
  59. self.SetStageComplete(ctx, nil)
  60. }