status.go 2.9 KB

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