localdeploy.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 deployserver
  15. import (
  16. "os"
  17. "strings"
  18. "github.com/sirupsen/logrus"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/hostman/diskutils"
  22. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  23. "yunion.io/x/onecloud/pkg/hostman/hostdeployer/consts"
  24. "yunion.io/x/onecloud/pkg/util/fileutils2"
  25. "yunion.io/x/onecloud/pkg/util/qemuimg"
  26. "yunion.io/x/onecloud/pkg/util/winutils"
  27. )
  28. type LocalDeploy struct{}
  29. func (d *LocalDeploy) DeployGuestFs(req *deployapi.DeployParams) (res *deployapi.DeployGuestFsResponse, err error) {
  30. localDisk, err := diskutils.NewKVMGuestDisk(qemuimg.SImageInfo{}, consts.DEPLOY_DRIVER_LOCAL_DISK, false)
  31. if err != nil {
  32. return nil, errors.Wrap(err, "new local disk")
  33. }
  34. if err := localDisk.Connect(req.GuestDesc); err != nil {
  35. return nil, errors.Wrapf(err, "local disk connect")
  36. }
  37. defer localDisk.Disconnect()
  38. return localDisk.DeployGuestfs(req)
  39. }
  40. func (d *LocalDeploy) ResizeFs(req *deployapi.ResizeFsParams) (res *deployapi.Empty, err error) {
  41. localDisk, err := diskutils.NewKVMGuestDisk(qemuimg.SImageInfo{}, consts.DEPLOY_DRIVER_LOCAL_DISK, false)
  42. if err != nil {
  43. return nil, errors.Wrap(err, "new local disk")
  44. }
  45. if err := localDisk.Connect(req.GuestDesc); err != nil {
  46. return nil, errors.Wrapf(err, "local disk connect")
  47. }
  48. defer localDisk.Disconnect()
  49. return localDisk.ResizeFs(req)
  50. }
  51. func (d *LocalDeploy) FormatFs(req *deployapi.FormatFsParams) (*deployapi.Empty, error) {
  52. localDisk, err := diskutils.NewKVMGuestDisk(qemuimg.SImageInfo{}, consts.DEPLOY_DRIVER_LOCAL_DISK, false)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "new local disk")
  55. }
  56. if err := localDisk.Connect(nil); err != nil {
  57. return nil, errors.Wrapf(err, "local disk connect")
  58. }
  59. defer localDisk.Disconnect()
  60. return localDisk.FormatFs(req)
  61. }
  62. func (d *LocalDeploy) SaveToGlance(req *deployapi.SaveToGlanceParams) (*deployapi.SaveToGlanceResponse, error) {
  63. localDisk, err := diskutils.NewKVMGuestDisk(qemuimg.SImageInfo{}, consts.DEPLOY_DRIVER_LOCAL_DISK, false)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "new local disk")
  66. }
  67. if err := localDisk.Connect(nil); err != nil {
  68. return nil, errors.Wrapf(err, "local disk connect")
  69. }
  70. defer localDisk.Disconnect()
  71. return localDisk.SaveToGlance(req)
  72. }
  73. func (d *LocalDeploy) ProbeImageInfo(req *deployapi.ProbeImageInfoPramas) (*deployapi.ImageInfo, error) {
  74. localDisk, err := diskutils.NewKVMGuestDisk(qemuimg.SImageInfo{}, consts.DEPLOY_DRIVER_LOCAL_DISK, false)
  75. if err != nil {
  76. return nil, errors.Wrap(err, "new local disk")
  77. }
  78. if err := localDisk.Connect(nil); err != nil {
  79. return nil, errors.Wrapf(err, "local disk connect")
  80. }
  81. defer localDisk.Disconnect()
  82. return localDisk.ProbeImageInfo(req)
  83. }
  84. func LocalInitEnv() error {
  85. f, _ := os.OpenFile("/log", os.O_CREATE|os.O_WRONLY, 0777)
  86. logrus.SetOutput(f)
  87. // /bin:/sbin:/usr/bin:/usr/sbin
  88. var paths = []string{
  89. "/bin",
  90. "/sbin",
  91. "/usr/bin",
  92. "/usr/sbin",
  93. "/opt/yunion/bin", // for zerofree and growpart command
  94. }
  95. err := os.Setenv("PATH", strings.Join(paths, ":"))
  96. if err != nil {
  97. return errors.Wrap(err, "set env path")
  98. }
  99. winutils.SetChntpwPath("/opt/yunion/bin/chntpw.static")
  100. err = InitEnvCommon()
  101. if err != nil {
  102. return err
  103. }
  104. return nil
  105. }
  106. func unmarshalDeployParams(val interface{}) error {
  107. if DeployOption.DeployParamsFile != "" {
  108. deployParams, err := fileutils2.FileGetContents(DeployOption.DeployParamsFile)
  109. if err != nil {
  110. return errors.Wrapf(err, "failed get params from %s", DeployOption.DeployParamsFile)
  111. }
  112. DeployOption.DeployParams = deployParams
  113. }
  114. jDeployParams, err := jsonutils.Parse([]byte(DeployOption.DeployParams))
  115. if err != nil {
  116. return errors.Wrap(err, "failed parse deploy json")
  117. }
  118. return jDeployParams.Unmarshal(val)
  119. }
  120. func StartLocalDeploy(deployAction string) (interface{}, error) {
  121. localDeployer := LocalDeploy{}
  122. switch deployAction {
  123. case "deploy_guest_fs":
  124. params := new(deployapi.DeployParams)
  125. if err := unmarshalDeployParams(params); err != nil {
  126. return nil, errors.Wrap(err, "unmarshal params")
  127. }
  128. return localDeployer.DeployGuestFs(params)
  129. case "resize_fs":
  130. params := new(deployapi.ResizeFsParams)
  131. if err := unmarshalDeployParams(params); err != nil {
  132. return nil, errors.Wrap(err, "unmarshal params")
  133. }
  134. return localDeployer.ResizeFs(params)
  135. case "format_fs":
  136. params := new(deployapi.FormatFsParams)
  137. if err := unmarshalDeployParams(params); err != nil {
  138. return nil, errors.Wrap(err, "unmarshal params")
  139. }
  140. return localDeployer.FormatFs(params)
  141. case "save_to_glance":
  142. params := new(deployapi.SaveToGlanceParams)
  143. if err := unmarshalDeployParams(params); err != nil {
  144. return nil, errors.Wrap(err, "unmarshal params")
  145. }
  146. return localDeployer.SaveToGlance(params)
  147. case "probe_image_info":
  148. params := new(deployapi.ProbeImageInfoPramas)
  149. if err := unmarshalDeployParams(params); err != nil {
  150. return nil, errors.Wrap(err, "unmarshal params")
  151. }
  152. return localDeployer.ProbeImageInfo(params)
  153. default:
  154. return nil, errors.Errorf("unknown deploy action")
  155. }
  156. }