status.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 compose
  14. import (
  15. "context"
  16. "github.com/docker/docker/api/types"
  17. "github.com/docker/docker/api/types/filters"
  18. "github.com/pkg/errors"
  19. )
  20. // ContainersKey is the context key to access context value os a ContainersStatus
  21. type ContainersKey struct{}
  22. // ContainersState state management interface
  23. type ContainersState interface {
  24. Get(string) *types.Container
  25. GetContainers() Containers
  26. Add(c types.Container)
  27. AddAll(cs Containers)
  28. Remove(string) types.Container
  29. }
  30. // NewContainersState creates a new container state manager
  31. func NewContainersState(cs Containers) ContainersState {
  32. s := containersState{
  33. observedContainers: &cs,
  34. }
  35. return &s
  36. }
  37. // ContainersStatus works as a collection container for the observed containers
  38. type containersState struct {
  39. observedContainers *Containers
  40. }
  41. func (s *containersState) AddAll(cs Containers) {
  42. for _, c := range cs {
  43. lValue := append(*s.observedContainers, c)
  44. s.observedContainers = &lValue
  45. }
  46. }
  47. func (s *containersState) Add(c types.Container) {
  48. if s.Get(c.ID) == nil {
  49. lValue := append(*s.observedContainers, c)
  50. s.observedContainers = &lValue
  51. }
  52. }
  53. func (s *containersState) Remove(id string) types.Container {
  54. var c types.Container
  55. var newObserved Containers
  56. for _, o := range *s.observedContainers {
  57. if o.ID != id {
  58. c = o
  59. continue
  60. }
  61. newObserved = append(newObserved, o)
  62. }
  63. s.observedContainers = &newObserved
  64. return c
  65. }
  66. func (s *containersState) Get(id string) *types.Container {
  67. for _, o := range *s.observedContainers {
  68. if id == o.ID {
  69. return &o
  70. }
  71. }
  72. return nil
  73. }
  74. func (s *containersState) GetContainers() Containers {
  75. if s.observedContainers != nil && *s.observedContainers != nil {
  76. return *s.observedContainers
  77. }
  78. return make(Containers, 0)
  79. }
  80. // GetContextContainerState gets the container state manager
  81. func GetContextContainerState(ctx context.Context) (ContainersState, error) {
  82. cState, ok := ctx.Value(ContainersKey{}).(*containersState)
  83. if !ok {
  84. return nil, errors.New("containers' containersState not available in context")
  85. }
  86. return cState, nil
  87. }
  88. func (s composeService) getUpdatedContainersStateContext(ctx context.Context, projectName string) (context.Context, error) {
  89. observedState, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{
  90. Filters: filters.NewArgs(
  91. projectFilter(projectName),
  92. ),
  93. All: true,
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. containerState := NewContainersState(observedState)
  99. return context.WithValue(ctx, ContainersKey{}, containerState), nil
  100. }