volume.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "strconv"
  17. "strings"
  18. "github.com/pkg/errors"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose-cli/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. readOnly := strconv.FormatBool(vi.readonly)
  34. projectVolumes[vi.name] = types.VolumeConfig{
  35. Name: vi.name,
  36. Driver: azureFileDriverName,
  37. DriverOpts: map[string]string{
  38. volumeDriveroptsAccountNameKey: vi.storageAccount,
  39. volumeDriveroptsShareNameKey: vi.fileshare,
  40. volumeReadOnly: readOnly,
  41. },
  42. }
  43. sv := types.ServiceVolumeConfig{
  44. Type: azureFileDriverName,
  45. Source: vi.name,
  46. Target: vi.target,
  47. ReadOnly: vi.readonly,
  48. }
  49. serviceConfigVolumes = append(serviceConfigVolumes, sv)
  50. }
  51. return projectVolumes, serviceConfigVolumes, nil
  52. }
  53. type volumeInput struct {
  54. name string
  55. storageAccount string
  56. fileshare string
  57. target string
  58. readonly bool
  59. }
  60. // parse takes a candidate string and creates a volumeInput
  61. // Candidates take the form of <source>[:<target>][:<permissions>]
  62. // Source is of the form `<storage account>/<fileshare>`
  63. // If only the source is specified then the target is set to `/run/volumes/<fileshare>`
  64. // Target is an absolute path in the container of the form `/path/to/mount`
  65. // Permissions can only be set if the target is set
  66. // If set, permissions must be `rw` or `ro`
  67. func (v *volumeInput) parse(name string, candidate string) error {
  68. v.name = name
  69. tokens := strings.Split(candidate, ":")
  70. sourceTokens := strings.Split(tokens[0], "/")
  71. if len(sourceTokens) != 2 || sourceTokens[0] == "" {
  72. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage account before '/'", candidate)
  73. }
  74. if sourceTokens[1] == "" {
  75. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage file fileshare after '/'", candidate)
  76. }
  77. v.storageAccount = sourceTokens[0]
  78. v.fileshare = sourceTokens[1]
  79. switch len(tokens) {
  80. case 1: // source only
  81. v.target = "/run/volumes/" + v.fileshare
  82. case 2: // source and target
  83. v.target = tokens[1]
  84. case 3: // source, target, and permissions
  85. v.target = tokens[1]
  86. permissions := strings.ToLower(tokens[2])
  87. if permissions != "ro" && permissions != "rw" {
  88. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q has an invalid mode %q", candidate, permissions)
  89. }
  90. v.readonly = permissions == "ro"
  91. default:
  92. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q has invalid format", candidate)
  93. }
  94. return nil
  95. }