volume.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package convert
  14. import (
  15. "fmt"
  16. "net/url"
  17. "strings"
  18. "github.com/pkg/errors"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/api/errdefs"
  21. )
  22. // GetRunVolumes return volume configurations for a project and a single service
  23. // this is meant to be used as a compose project of a single service
  24. func GetRunVolumes(volumes []string) (map[string]types.VolumeConfig, []types.ServiceVolumeConfig, error) {
  25. var serviceConfigVolumes []types.ServiceVolumeConfig
  26. projectVolumes := make(map[string]types.VolumeConfig, len(volumes))
  27. for i, v := range volumes {
  28. var vi volumeInput
  29. err := vi.parse(fmt.Sprintf("volume-%d", i), v)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. projectVolumes[vi.name] = types.VolumeConfig{
  34. Name: vi.name,
  35. Driver: azureFileDriverName,
  36. DriverOpts: map[string]string{
  37. volumeDriveroptsAccountNameKey: vi.username,
  38. volumeDriveroptsAccountKeyKey: vi.key,
  39. volumeDriveroptsShareNameKey: vi.share,
  40. },
  41. }
  42. sv := types.ServiceVolumeConfig{
  43. Type: azureFileDriverName,
  44. Source: vi.name,
  45. Target: vi.target,
  46. }
  47. serviceConfigVolumes = append(serviceConfigVolumes, sv)
  48. }
  49. return projectVolumes, serviceConfigVolumes, nil
  50. }
  51. type volumeInput struct {
  52. name string
  53. username string
  54. key string
  55. share string
  56. target string
  57. }
  58. func escapeKeySlashes(rawURL string) (string, error) {
  59. urlSplit := strings.Split(rawURL, "@")
  60. if len(urlSplit) < 1 {
  61. return "", errors.Wrap(errdefs.ErrParsingFailed, "invalid url format "+rawURL)
  62. }
  63. userPasswd := strings.ReplaceAll(urlSplit[0], "/", "_")
  64. atIndex := strings.Index(rawURL, "@")
  65. if atIndex < 0 {
  66. return "", errors.Wrap(errdefs.ErrParsingFailed, "no share specified in "+rawURL)
  67. }
  68. scaped := userPasswd + rawURL[atIndex:]
  69. return scaped, nil
  70. }
  71. func unescapeKey(key string) string {
  72. return strings.ReplaceAll(key, "_", "/")
  73. }
  74. // Removes the second ':' that separates the source from target
  75. func volumeURL(pathURL string) (*url.URL, error) {
  76. scapedURL, err := escapeKeySlashes(pathURL)
  77. if err != nil {
  78. return nil, err
  79. }
  80. pathURL = "//" + scapedURL
  81. count := strings.Count(pathURL, ":")
  82. if count > 2 {
  83. return nil, errors.Wrap(errdefs.ErrParsingFailed, fmt.Sprintf("unable to parse volume mount %q", pathURL))
  84. }
  85. if count == 2 {
  86. tokens := strings.Split(pathURL, ":")
  87. pathURL = fmt.Sprintf("%s:%s%s", tokens[0], tokens[1], tokens[2])
  88. }
  89. return url.Parse(pathURL)
  90. }
  91. func (v *volumeInput) parse(name string, s string) error {
  92. volumeURL, err := volumeURL(s)
  93. if err != nil {
  94. return errors.Wrap(errdefs.ErrParsingFailed, fmt.Sprintf("volume specification %q could not be parsed %q", s, err))
  95. }
  96. v.username = volumeURL.User.Username()
  97. if v.username == "" {
  98. return errors.Wrap(errdefs.ErrParsingFailed, fmt.Sprintf("volume specification %q does not include a storage username", v))
  99. }
  100. key, ok := volumeURL.User.Password()
  101. if !ok || key == "" {
  102. return errors.Wrap(errdefs.ErrParsingFailed, fmt.Sprintf("volume specification %q does not include a storage key", v))
  103. }
  104. v.key = unescapeKey(key)
  105. v.share = volumeURL.Host
  106. if v.share == "" {
  107. return errors.Wrap(errdefs.ErrParsingFailed, fmt.Sprintf("volume specification %q does not include a storage file share", v))
  108. }
  109. v.name = name
  110. v.target = volumeURL.Path
  111. if v.target == "" {
  112. // Do not use filepath.Join, on Windows it will replace / by \
  113. v.target = "/run/volumes/" + v.share
  114. }
  115. return nil
  116. }