volume.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "context"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  20. "github.com/Azure/go-autorest/autorest/to"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/docker/compose-cli/aci/login"
  23. "github.com/pkg/errors"
  24. "github.com/docker/compose-cli/errdefs"
  25. )
  26. const (
  27. azureFileDriverName = "azure_file"
  28. volumeDriveroptsShareNameKey = "share_name"
  29. volumeDriveroptsAccountNameKey = "storage_account_name"
  30. volumeReadOnly = "read_only"
  31. )
  32. func (p projectAciHelper) getAciFileVolumes(ctx context.Context, helper login.StorageLogin) (map[string]bool, []containerinstance.Volume, error) {
  33. azureFileVolumesMap := make(map[string]bool, len(p.Volumes))
  34. var azureFileVolumesSlice []containerinstance.Volume
  35. for name, v := range p.Volumes {
  36. if v.Driver == azureFileDriverName {
  37. shareName, ok := v.DriverOpts[volumeDriveroptsShareNameKey]
  38. if !ok {
  39. return nil, nil, fmt.Errorf("cannot retrieve fileshare name for Azurefile")
  40. }
  41. accountName, ok := v.DriverOpts[volumeDriveroptsAccountNameKey]
  42. if !ok {
  43. return nil, nil, fmt.Errorf("cannot retrieve account name for Azurefile")
  44. }
  45. readOnly, ok := v.DriverOpts[volumeReadOnly]
  46. if !ok {
  47. readOnly = "false"
  48. }
  49. ro, err := strconv.ParseBool(readOnly)
  50. if err != nil {
  51. return nil, nil, fmt.Errorf("invalid mode %q for volume", readOnly)
  52. }
  53. accountKey, err := helper.GetAzureStorageAccountKey(ctx, accountName)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. aciVolume := containerinstance.Volume{
  58. Name: to.StringPtr(name),
  59. AzureFile: &containerinstance.AzureFileVolume{
  60. ShareName: to.StringPtr(shareName),
  61. StorageAccountName: to.StringPtr(accountName),
  62. StorageAccountKey: to.StringPtr(accountKey),
  63. ReadOnly: &ro,
  64. },
  65. }
  66. azureFileVolumesMap[name] = true
  67. azureFileVolumesSlice = append(azureFileVolumesSlice, aciVolume)
  68. }
  69. }
  70. return azureFileVolumesMap, azureFileVolumesSlice, nil
  71. }
  72. func (s serviceConfigAciHelper) getAciFileVolumeMounts(volumesCache map[string]bool) ([]containerinstance.VolumeMount, error) {
  73. var aciServiceVolumes []containerinstance.VolumeMount
  74. for _, sv := range s.Volumes {
  75. if !volumesCache[sv.Source] {
  76. return []containerinstance.VolumeMount{}, fmt.Errorf("could not find volume source %q", sv.Source)
  77. }
  78. aciServiceVolumes = append(aciServiceVolumes, containerinstance.VolumeMount{
  79. Name: to.StringPtr(sv.Source),
  80. MountPath: to.StringPtr(sv.Target),
  81. })
  82. }
  83. return aciServiceVolumes, nil
  84. }
  85. // GetRunVolumes return volume configurations for a project and a single service
  86. // this is meant to be used as a compose project of a single service
  87. func GetRunVolumes(volumes []string) (map[string]types.VolumeConfig, []types.ServiceVolumeConfig, error) {
  88. var serviceConfigVolumes []types.ServiceVolumeConfig
  89. projectVolumes := make(map[string]types.VolumeConfig, len(volumes))
  90. for i, v := range volumes {
  91. var vi volumeInput
  92. err := vi.parse(fmt.Sprintf("volume-%d", i), v)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. readOnly := strconv.FormatBool(vi.readonly)
  97. projectVolumes[vi.name] = types.VolumeConfig{
  98. Name: vi.name,
  99. Driver: azureFileDriverName,
  100. DriverOpts: map[string]string{
  101. volumeDriveroptsAccountNameKey: vi.storageAccount,
  102. volumeDriveroptsShareNameKey: vi.fileshare,
  103. volumeReadOnly: readOnly,
  104. },
  105. }
  106. sv := types.ServiceVolumeConfig{
  107. Type: azureFileDriverName,
  108. Source: vi.name,
  109. Target: vi.target,
  110. ReadOnly: vi.readonly,
  111. }
  112. serviceConfigVolumes = append(serviceConfigVolumes, sv)
  113. }
  114. return projectVolumes, serviceConfigVolumes, nil
  115. }
  116. type volumeInput struct {
  117. name string
  118. storageAccount string
  119. fileshare string
  120. target string
  121. readonly bool
  122. }
  123. // parse takes a candidate string and creates a volumeInput
  124. // Candidates take the form of <source>[:<target>][:<permissions>]
  125. // Source is of the form `<storage account>/<fileshare>`
  126. // If only the source is specified then the target is set to `/run/volumes/<fileshare>`
  127. // Target is an absolute path in the container of the form `/path/to/mount`
  128. // Permissions can only be set if the target is set
  129. // If set, permissions must be `rw` or `ro`
  130. func (v *volumeInput) parse(name string, candidate string) error {
  131. v.name = name
  132. tokens := strings.Split(candidate, ":")
  133. sourceTokens := strings.Split(tokens[0], "/")
  134. if len(sourceTokens) != 2 || sourceTokens[0] == "" {
  135. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage account before '/'", candidate)
  136. }
  137. if sourceTokens[1] == "" {
  138. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q does not include a storage file fileshare after '/'", candidate)
  139. }
  140. v.storageAccount = sourceTokens[0]
  141. v.fileshare = sourceTokens[1]
  142. switch len(tokens) {
  143. case 1: // source only
  144. v.target = "/run/volumes/" + v.fileshare
  145. case 2: // source and target
  146. v.target = tokens[1]
  147. case 3: // source, target, and permissions
  148. v.target = tokens[1]
  149. permissions := strings.ToLower(tokens[2])
  150. if permissions != "ro" && permissions != "rw" {
  151. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q has an invalid mode %q", candidate, permissions)
  152. }
  153. v.readonly = permissions == "ro"
  154. default:
  155. return errors.Wrapf(errdefs.ErrParsingFailed, "volume specification %q has invalid format", candidate)
  156. }
  157. return nil
  158. }