cdrom.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "path/filepath"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/util/httputils"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. o "yunion.io/x/onecloud/pkg/baremetal/options"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  27. "yunion.io/x/onecloud/pkg/mcclient"
  28. "yunion.io/x/onecloud/pkg/util/fileutils2"
  29. "yunion.io/x/onecloud/pkg/util/redfish"
  30. )
  31. type SBaremetalCdromTask struct {
  32. SBaremetalTaskBase
  33. }
  34. func NewBaremetalCdromTask(
  35. userCred mcclient.TokenCredential,
  36. baremetal IBaremetal,
  37. taskId string,
  38. data jsonutils.JSONObject,
  39. ) ITask {
  40. task := &SBaremetalCdromTask{
  41. SBaremetalTaskBase: newBaremetalTaskBase(userCred, baremetal, taskId, data),
  42. }
  43. task.SetVirtualObject(task)
  44. log.Debugf("NewBaremetalCdromTask: %s", data)
  45. action, _ := data.GetString("action")
  46. if action == api.BAREMETAL_CDROM_ACTION_INSERT {
  47. task.SetStage(task.DoInsertISO)
  48. } else {
  49. task.SetStage(task.DoEjectISO)
  50. }
  51. return task
  52. }
  53. func (self *SBaremetalCdromTask) GetName() string {
  54. return "SBaremetalCdromTask"
  55. }
  56. func (self *SBaremetalCdromTask) getRedfishApi(ctx context.Context) (redfish.IRedfishDriver, error) {
  57. ipmiInfo := self.Baremetal.GetRawIPMIConfig()
  58. if ipmiInfo == nil {
  59. ipmiInfo = &types.SIPMIInfo{}
  60. }
  61. if ipmiInfo.IpAddr == "" {
  62. return nil, errors.Error("empty IPMI ip_addr")
  63. }
  64. if ipmiInfo.Username == "" {
  65. return nil, errors.Error("empty IPMI username")
  66. }
  67. if ipmiInfo.Password == "" {
  68. return nil, errors.Error("empty IPMI password")
  69. }
  70. if !ipmiInfo.CdromBoot {
  71. return nil, errors.Error("mount Virtual Cdrom not supported")
  72. }
  73. var endpoint = "https://" + ipmiInfo.IpAddr
  74. if strings.Contains(ipmiInfo.IpAddr, ":") {
  75. endpoint = fmt.Sprintf("https://[%s]", ipmiInfo.IpAddr)
  76. }
  77. redfishCli := redfish.NewRedfishDriver(ctx, endpoint, ipmiInfo.Username, ipmiInfo.Password, false)
  78. if redfishCli != nil {
  79. return redfishCli, nil
  80. } else {
  81. return nil, errors.Error("invalid redfish Api client")
  82. }
  83. }
  84. func (self *SBaremetalCdromTask) DoInsertISO(ctx context.Context, args interface{}) error {
  85. redfishCli, err := self.getRedfishApi(ctx)
  86. if err != nil {
  87. return errors.Wrap(err, "getRedfishApi")
  88. }
  89. imageId, _ := self.GetData().GetString("image_id")
  90. boot := jsonutils.QueryBoolean(self.GetData(), "boot", false)
  91. localImagePath := filepath.Join(o.Options.CachePath, imageId)
  92. if !fileutils2.Exists(localImagePath) {
  93. return errors.Error("image not cached")
  94. }
  95. imageBaseUrl := self.Baremetal.GetImageUrl(true)
  96. if len(imageBaseUrl) == 0 {
  97. return errors.Error("empty image base url")
  98. }
  99. cdromPath := httputils.JoinPath(imageBaseUrl, "/images/"+imageId)
  100. err = redfish.MountVirtualCdrom(ctx, redfishCli, cdromPath, boot)
  101. if err != nil {
  102. return errors.Wrap(err, "MountVirtualCdrom")
  103. }
  104. self.Baremetal.AutoSyncStatus(ctx)
  105. SetTaskComplete(self, nil)
  106. return nil
  107. }
  108. func (self *SBaremetalCdromTask) DoEjectISO(ctx context.Context, args interface{}) error {
  109. redfishCli, err := self.getRedfishApi(ctx)
  110. if err != nil {
  111. return errors.Wrap(err, "getRedfishApi")
  112. }
  113. err = redfish.UmountVirtualCdrom(ctx, redfishCli)
  114. if err != nil {
  115. return errors.Wrap(err, "UmountVirtualCdrom")
  116. }
  117. self.Baremetal.AutoSyncStatus(ctx)
  118. SetTaskComplete(self, nil)
  119. return nil
  120. }