volumes.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 ecs
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/docker/compose-cli/api/compose"
  18. "github.com/docker/compose-cli/api/volumes"
  19. "github.com/docker/compose-cli/errdefs"
  20. "github.com/awslabs/goformation/v4/cloudformation"
  21. "github.com/awslabs/goformation/v4/cloudformation/efs"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/pkg/errors"
  24. )
  25. func (b *ecsAPIService) createNFSMountTarget(project *types.Project, resources awsResources, template *cloudformation.Template) {
  26. for volume := range project.Volumes {
  27. for _, subnet := range resources.subnets {
  28. name := fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID()))
  29. template.Resources[name] = &efs.MountTarget{
  30. FileSystemId: resources.filesystems[volume].ID(),
  31. SecurityGroups: resources.allSecurityGroups(),
  32. SubnetId: subnet.ID(),
  33. }
  34. }
  35. }
  36. }
  37. func (b *ecsAPIService) mountTargets(volume string, resources awsResources) []string {
  38. var refs []string
  39. for _, subnet := range resources.subnets {
  40. refs = append(refs, fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID())))
  41. }
  42. return refs
  43. }
  44. func (b *ecsAPIService) createAccessPoints(project *types.Project, r awsResources, template *cloudformation.Template) {
  45. for name, volume := range project.Volumes {
  46. n := fmt.Sprintf("%sAccessPoint", normalizeResourceName(name))
  47. uid := volume.DriverOpts["uid"]
  48. gid := volume.DriverOpts["gid"]
  49. permissions := volume.DriverOpts["permissions"]
  50. path := volume.DriverOpts["root_directory"]
  51. ap := efs.AccessPoint{
  52. AccessPointTags: []efs.AccessPoint_AccessPointTag{
  53. {
  54. Key: compose.ProjectTag,
  55. Value: project.Name,
  56. },
  57. {
  58. Key: compose.VolumeTag,
  59. Value: name,
  60. },
  61. {
  62. Key: "Name",
  63. Value: volume.Name,
  64. },
  65. },
  66. FileSystemId: r.filesystems[name].ID(),
  67. }
  68. if uid != "" {
  69. ap.PosixUser = &efs.AccessPoint_PosixUser{
  70. Uid: uid,
  71. Gid: gid,
  72. }
  73. }
  74. if path != "" {
  75. root := efs.AccessPoint_RootDirectory{
  76. Path: path,
  77. }
  78. ap.RootDirectory = &root
  79. if uid != "" {
  80. root.CreationInfo = &efs.AccessPoint_CreationInfo{
  81. OwnerUid: uid,
  82. OwnerGid: gid,
  83. Permissions: permissions,
  84. }
  85. }
  86. }
  87. template.Resources[n] = &ap
  88. }
  89. }
  90. // VolumeCreateOptions hold EFS filesystem creation options
  91. type VolumeCreateOptions struct {
  92. KmsKeyID string
  93. PerformanceMode string
  94. ProvisionedThroughputInMibps float64
  95. ThroughputMode string
  96. }
  97. type ecsVolumeService struct {
  98. backend *ecsAPIService
  99. }
  100. func (e ecsVolumeService) List(ctx context.Context) ([]volumes.Volume, error) {
  101. filesystems, err := e.backend.aws.ListFileSystems(ctx, nil)
  102. if err != nil {
  103. return nil, err
  104. }
  105. var vol []volumes.Volume
  106. for _, fs := range filesystems {
  107. vol = append(vol, volumes.Volume{
  108. ID: fs.ID(),
  109. Description: fs.ARN(),
  110. })
  111. }
  112. return vol, nil
  113. }
  114. func (e ecsVolumeService) Create(ctx context.Context, name string, options interface{}) (volumes.Volume, error) {
  115. fs, err := e.backend.aws.CreateFileSystem(ctx, map[string]string{
  116. "Name": name,
  117. }, options.(VolumeCreateOptions))
  118. return volumes.Volume{
  119. ID: fs.ID(),
  120. Description: fs.ARN(),
  121. }, err
  122. }
  123. func (e ecsVolumeService) Delete(ctx context.Context, volumeID string, options interface{}) error {
  124. return e.backend.aws.DeleteFileSystem(ctx, volumeID)
  125. }
  126. func (e ecsVolumeService) Inspect(ctx context.Context, volumeID string) (volumes.Volume, error) {
  127. ok, err := e.backend.aws.ResolveFileSystem(ctx, volumeID)
  128. if ok == nil {
  129. err = errors.Wrapf(errdefs.ErrNotFound, "filesystem %q does not exists", volumeID)
  130. }
  131. return volumes.Volume{
  132. ID: volumeID,
  133. Description: ok.ARN(),
  134. }, err
  135. }