volume.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "strings"
  17. "github.com/pkg/errors"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/docker/compose-cli/errdefs"
  20. )
  21. // GetRunVolumes return volume configurations for a project and a single service
  22. // this is meant to be used as a compose project of a single service
  23. func GetRunVolumes(volumes []string) (map[string]types.VolumeConfig, []types.ServiceVolumeConfig, error) {
  24. var serviceConfigVolumes []types.ServiceVolumeConfig
  25. projectVolumes := make(map[string]types.VolumeConfig, len(volumes))
  26. for i, v := range volumes {
  27. var vi volumeInput
  28. err := vi.parse(fmt.Sprintf("volume-%d", i), v)
  29. if err != nil {
  30. return nil, nil, err
  31. }
  32. projectVolumes[vi.name] = types.VolumeConfig{
  33. Name: vi.name,
  34. Driver: azureFileDriverName,
  35. DriverOpts: map[string]string{
  36. volumeDriveroptsAccountNameKey: vi.username,
  37. volumeDriveroptsShareNameKey: vi.share,
  38. },
  39. }
  40. sv := types.ServiceVolumeConfig{
  41. Type: azureFileDriverName,
  42. Source: vi.name,
  43. Target: vi.target,
  44. }
  45. serviceConfigVolumes = append(serviceConfigVolumes, sv)
  46. }
  47. return projectVolumes, serviceConfigVolumes, nil
  48. }
  49. type volumeInput struct {
  50. name string
  51. username string
  52. share string
  53. target string
  54. }
  55. func (v *volumeInput) parse(name string, s string) error {
  56. v.name = name
  57. tokens := strings.Split(s, "@")
  58. if len(tokens) < 2 || tokens[0] == "" {
  59. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage account before '@'", v)
  60. }
  61. v.username = tokens[0]
  62. remaining := tokens[1]
  63. tokens = strings.Split(remaining, ":")
  64. if tokens[0] == "" {
  65. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage file share after '@'", v)
  66. }
  67. v.share = tokens[0]
  68. if len(tokens) > 1 {
  69. v.target = tokens[1]
  70. } else {
  71. v.target = "/run/volumes/" + v.share
  72. }
  73. return nil
  74. }