host_path.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 volume_mount
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/util/procutils"
  23. )
  24. func init() {
  25. RegisterDriver(newHostLocal())
  26. }
  27. type hostLocal struct{}
  28. func (h hostLocal) Mount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  29. return nil
  30. }
  31. func (h hostLocal) Unmount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error {
  32. return nil
  33. }
  34. func newHostLocal() IVolumeMount {
  35. return &hostLocal{}
  36. }
  37. func (h hostLocal) GetType() apis.ContainerVolumeMountType {
  38. return apis.CONTAINER_VOLUME_MOUNT_TYPE_HOST_PATH
  39. }
  40. func (h hostLocal) GetRuntimeMountHostPath(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) (string, error) {
  41. host := vm.HostPath
  42. if host == nil {
  43. return "", httperrors.NewNotEmptyError("host_local is nil")
  44. }
  45. if vm.FsUser != nil || vm.FsGroup != nil {
  46. return "", httperrors.NewInputParameterError("cannot use fs_user and fs_group for host_local volume")
  47. }
  48. switch host.Type {
  49. case "", apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_FILE:
  50. return h.getFilePath(host)
  51. case apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_DIRECTORY:
  52. return h.getDirectoryPath(host)
  53. }
  54. return "", httperrors.NewInputParameterError("unsupported type %q", host.Type)
  55. }
  56. func (h hostLocal) getFilePath(input *apis.ContainerVolumeMountHostPath) (string, error) {
  57. if input.Type != apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_FILE {
  58. return "", httperrors.NewInputParameterError("unsupported type %q", input.Type)
  59. }
  60. filePath := input.Path
  61. // 检查文件是否存在
  62. checkCmd := fmt.Sprintf("test -f '%s'", filePath)
  63. if _, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", checkCmd).Output(); err != nil {
  64. // 文件不存在,需要创建
  65. if !input.AutoCreate {
  66. return "", errors.Wrapf(err, "file %s does not exist and no auto_create specified", filePath)
  67. }
  68. // 先确保父目录存在
  69. parentDirCmd := fmt.Sprintf("mkdir -p '%s'", filepath.Dir(filePath))
  70. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", parentDirCmd).Output(); err != nil {
  71. return "", errors.Wrapf(err, "create parent directory for %s: %s", filePath, out)
  72. }
  73. // 创建文件
  74. createCmd := fmt.Sprintf("touch '%s'", filePath)
  75. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", createCmd).Output(); err != nil {
  76. return "", errors.Wrapf(err, "create file %s: %s", filePath, out)
  77. }
  78. if input.AutoCreateConfig != nil {
  79. // 设置权限
  80. if input.AutoCreateConfig.Permissions != "" {
  81. chmodCmd := fmt.Sprintf("chmod %s '%s'", input.AutoCreateConfig.Permissions, filePath)
  82. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", chmodCmd).Output(); err != nil {
  83. return "", errors.Wrapf(err, "chmod %s %s: %s", input.AutoCreateConfig.Permissions, filePath, out)
  84. }
  85. }
  86. // 设置所有者
  87. if input.AutoCreateConfig.Uid > 0 || input.AutoCreateConfig.Gid > 0 {
  88. chownCmd := fmt.Sprintf("chown %d:%d '%s'", input.AutoCreateConfig.Uid, input.AutoCreateConfig.Gid, filePath)
  89. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", chownCmd).Output(); err != nil {
  90. return "", errors.Wrapf(err, "chown %d:%d %s: %s", input.AutoCreateConfig.Uid, input.AutoCreateConfig.Gid, filePath, out)
  91. }
  92. }
  93. }
  94. }
  95. return filePath, nil
  96. }
  97. func (h hostLocal) getDirectoryPath(input *apis.ContainerVolumeMountHostPath) (string, error) {
  98. if input.Type != apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_DIRECTORY {
  99. return "", httperrors.NewInputParameterError("unsupported type %q", input.Type)
  100. }
  101. dirPath := input.Path
  102. // 检查目录是否存在
  103. checkCmd := fmt.Sprintf("test -d '%s'", dirPath)
  104. if _, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", checkCmd).Output(); err != nil {
  105. if !input.AutoCreate {
  106. return "", errors.Wrapf(err, "dir %s does not exist and no auto_create specified", dirPath)
  107. }
  108. // 创建目录
  109. createCmd := fmt.Sprintf("mkdir -p '%s'", dirPath)
  110. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", createCmd).Output(); err != nil {
  111. return "", errors.Wrapf(err, "create directory %s: %s", dirPath, out)
  112. }
  113. if input.AutoCreateConfig != nil {
  114. // 设置权限
  115. if input.AutoCreateConfig.Permissions != "" {
  116. chmodCmd := fmt.Sprintf("chmod %s '%s'", input.AutoCreateConfig.Permissions, dirPath)
  117. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", chmodCmd).Output(); err != nil {
  118. return "", errors.Wrapf(err, "chmod %s %s: %s", input.AutoCreateConfig.Permissions, dirPath, out)
  119. }
  120. }
  121. // 设置所有者
  122. if input.AutoCreateConfig.Uid > 0 || input.AutoCreateConfig.Gid > 0 {
  123. chownCmd := fmt.Sprintf("chown %d:%d '%s'", input.AutoCreateConfig.Uid, input.AutoCreateConfig.Gid, dirPath)
  124. if out, err := procutils.NewRemoteCommandAsFarAsPossible("sh", "-c", chownCmd).Output(); err != nil {
  125. return "", errors.Wrapf(err, "chown %d:%d %s: %s", input.AutoCreateConfig.Uid, input.AutoCreateConfig.Gid, dirPath, out)
  126. }
  127. }
  128. }
  129. }
  130. return dirPath, nil
  131. }