volume.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package convert
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. "github.com/compose-spec/compose-go/types"
  9. )
  10. // GetRunVolumes return volume configurations for a project and a single service
  11. // this is meant to be used as a compose project of a single service
  12. func GetRunVolumes(volumes []string) (map[string]types.VolumeConfig, []types.ServiceVolumeConfig, error) {
  13. var serviceConfigVolumes []types.ServiceVolumeConfig
  14. projectVolumes := make(map[string]types.VolumeConfig, len(volumes))
  15. for i, v := range volumes {
  16. var vi volumeInput
  17. err := vi.parse(fmt.Sprintf("volume-%d", i), v)
  18. if err != nil {
  19. return nil, nil, err
  20. }
  21. projectVolumes[vi.name] = types.VolumeConfig{
  22. Name: vi.name,
  23. Driver: azureFileDriverName,
  24. DriverOpts: map[string]string{
  25. volumeDriveroptsAccountNameKey: vi.username,
  26. volumeDriveroptsAccountKeyKey: vi.key,
  27. volumeDriveroptsShareNameKey: vi.share,
  28. },
  29. }
  30. sv := types.ServiceVolumeConfig{
  31. Type: azureFileDriverName,
  32. Source: vi.name,
  33. Target: vi.target,
  34. }
  35. serviceConfigVolumes = append(serviceConfigVolumes, sv)
  36. }
  37. return projectVolumes, serviceConfigVolumes, nil
  38. }
  39. type volumeInput struct {
  40. name string
  41. username string
  42. key string
  43. share string
  44. target string
  45. }
  46. func escapeKeySlashes(rawURL string) (string, error) {
  47. urlSplit := strings.Split(rawURL, "@")
  48. if len(urlSplit) < 1 {
  49. return "", errors.New("invalid url format " + rawURL)
  50. }
  51. userPasswd := strings.ReplaceAll(urlSplit[0], "/", "_")
  52. scaped := userPasswd + rawURL[strings.Index(rawURL, "@"):]
  53. return scaped, nil
  54. }
  55. func unescapeKey(key string) string {
  56. return strings.ReplaceAll(key, "_", "/")
  57. }
  58. // Removes the second ':' that separates the source from target
  59. func volumeURL(pathURL string) (*url.URL, error) {
  60. scapedURL, err := escapeKeySlashes(pathURL)
  61. if err != nil {
  62. return nil, err
  63. }
  64. pathURL = "//" + scapedURL
  65. count := strings.Count(pathURL, ":")
  66. if count > 2 {
  67. return nil, fmt.Errorf("unable to parse volume mount %q", pathURL)
  68. }
  69. if count == 2 {
  70. tokens := strings.Split(pathURL, ":")
  71. pathURL = fmt.Sprintf("%s:%s%s", tokens[0], tokens[1], tokens[2])
  72. }
  73. return url.Parse(pathURL)
  74. }
  75. func (v *volumeInput) parse(name string, s string) error {
  76. volumeURL, err := volumeURL(s)
  77. if err != nil {
  78. return fmt.Errorf("volume specification %q could not be parsed %q", s, err)
  79. }
  80. v.username = volumeURL.User.Username()
  81. if v.username == "" {
  82. return fmt.Errorf("volume specification %q does not include a storage username", v)
  83. }
  84. key, ok := volumeURL.User.Password()
  85. if !ok || key == "" {
  86. return fmt.Errorf("volume specification %q does not include a storage key", v)
  87. }
  88. v.key = unescapeKey(key)
  89. v.share = volumeURL.Host
  90. if v.share == "" {
  91. return fmt.Errorf("volume specification %q does not include a storage file share", v)
  92. }
  93. v.name = name
  94. v.target = volumeURL.Path
  95. if v.target == "" {
  96. v.target = filepath.Join("/run/volumes/", v.share)
  97. }
  98. return nil
  99. }