volume.go 6.1 KB

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