1
0

proxy.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 api
  14. import (
  15. "context"
  16. "github.com/compose-spec/compose-go/types"
  17. )
  18. // ServiceProxy implements Service by delegating to implementation functions. This allows lazy init and per-method overrides
  19. type ServiceProxy struct {
  20. BuildFn func(ctx context.Context, project *types.Project, options BuildOptions) error
  21. PushFn func(ctx context.Context, project *types.Project, options PushOptions) error
  22. PullFn func(ctx context.Context, project *types.Project, opts PullOptions) error
  23. CreateFn func(ctx context.Context, project *types.Project, opts CreateOptions) error
  24. StartFn func(ctx context.Context, projectName string, options StartOptions) error
  25. RestartFn func(ctx context.Context, projectName string, options RestartOptions) error
  26. StopFn func(ctx context.Context, projectName string, options StopOptions) error
  27. UpFn func(ctx context.Context, project *types.Project, options UpOptions) error
  28. DownFn func(ctx context.Context, projectName string, options DownOptions) error
  29. LogsFn func(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
  30. PsFn func(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
  31. ListFn func(ctx context.Context, options ListOptions) ([]Stack, error)
  32. ConvertFn func(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
  33. KillFn func(ctx context.Context, project string, options KillOptions) error
  34. RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
  35. RemoveFn func(ctx context.Context, project string, options RemoveOptions) error
  36. ExecFn func(ctx context.Context, project string, opts RunOptions) (int, error)
  37. CopyFn func(ctx context.Context, project string, options CopyOptions) error
  38. PauseFn func(ctx context.Context, project string, options PauseOptions) error
  39. UnPauseFn func(ctx context.Context, project string, options PauseOptions) error
  40. TopFn func(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)
  41. EventsFn func(ctx context.Context, project string, options EventsOptions) error
  42. PortFn func(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error)
  43. ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
  44. interceptors []Interceptor
  45. }
  46. // NewServiceProxy produces a ServiceProxy
  47. func NewServiceProxy() *ServiceProxy {
  48. return &ServiceProxy{}
  49. }
  50. // Interceptor allow to customize the compose types.Project before the actual Service method is executed
  51. type Interceptor func(ctx context.Context, project *types.Project)
  52. var _ Service = &ServiceProxy{}
  53. // WithService configure proxy to use specified Service as delegate
  54. func (s *ServiceProxy) WithService(service Service) *ServiceProxy {
  55. s.BuildFn = service.Build
  56. s.PushFn = service.Push
  57. s.PullFn = service.Pull
  58. s.CreateFn = service.Create
  59. s.StartFn = service.Start
  60. s.RestartFn = service.Restart
  61. s.StopFn = service.Stop
  62. s.UpFn = service.Up
  63. s.DownFn = service.Down
  64. s.LogsFn = service.Logs
  65. s.PsFn = service.Ps
  66. s.ListFn = service.List
  67. s.ConvertFn = service.Convert
  68. s.KillFn = service.Kill
  69. s.RunOneOffContainerFn = service.RunOneOffContainer
  70. s.RemoveFn = service.Remove
  71. s.ExecFn = service.Exec
  72. s.CopyFn = service.Copy
  73. s.PauseFn = service.Pause
  74. s.UnPauseFn = service.UnPause
  75. s.TopFn = service.Top
  76. s.EventsFn = service.Events
  77. s.PortFn = service.Port
  78. s.ImagesFn = service.Images
  79. return s
  80. }
  81. // WithInterceptor configures Interceptor to be applied to Service method execution
  82. func (s *ServiceProxy) WithInterceptor(interceptors ...Interceptor) *ServiceProxy {
  83. s.interceptors = append(s.interceptors, interceptors...)
  84. return s
  85. }
  86. // Build implements Service interface
  87. func (s *ServiceProxy) Build(ctx context.Context, project *types.Project, options BuildOptions) error {
  88. if s.BuildFn == nil {
  89. return ErrNotImplemented
  90. }
  91. for _, i := range s.interceptors {
  92. i(ctx, project)
  93. }
  94. return s.BuildFn(ctx, project, options)
  95. }
  96. // Push implements Service interface
  97. func (s *ServiceProxy) Push(ctx context.Context, project *types.Project, options PushOptions) error {
  98. if s.PushFn == nil {
  99. return ErrNotImplemented
  100. }
  101. for _, i := range s.interceptors {
  102. i(ctx, project)
  103. }
  104. return s.PushFn(ctx, project, options)
  105. }
  106. // Pull implements Service interface
  107. func (s *ServiceProxy) Pull(ctx context.Context, project *types.Project, options PullOptions) error {
  108. if s.PullFn == nil {
  109. return ErrNotImplemented
  110. }
  111. for _, i := range s.interceptors {
  112. i(ctx, project)
  113. }
  114. return s.PullFn(ctx, project, options)
  115. }
  116. // Create implements Service interface
  117. func (s *ServiceProxy) Create(ctx context.Context, project *types.Project, options CreateOptions) error {
  118. if s.CreateFn == nil {
  119. return ErrNotImplemented
  120. }
  121. for _, i := range s.interceptors {
  122. i(ctx, project)
  123. }
  124. return s.CreateFn(ctx, project, options)
  125. }
  126. // Start implements Service interface
  127. func (s *ServiceProxy) Start(ctx context.Context, projectName string, options StartOptions) error {
  128. if s.StartFn == nil {
  129. return ErrNotImplemented
  130. }
  131. return s.StartFn(ctx, projectName, options)
  132. }
  133. // Restart implements Service interface
  134. func (s *ServiceProxy) Restart(ctx context.Context, projectName string, options RestartOptions) error {
  135. if s.RestartFn == nil {
  136. return ErrNotImplemented
  137. }
  138. return s.RestartFn(ctx, projectName, options)
  139. }
  140. // Stop implements Service interface
  141. func (s *ServiceProxy) Stop(ctx context.Context, projectName string, options StopOptions) error {
  142. if s.StopFn == nil {
  143. return ErrNotImplemented
  144. }
  145. return s.StopFn(ctx, projectName, options)
  146. }
  147. // Up implements Service interface
  148. func (s *ServiceProxy) Up(ctx context.Context, project *types.Project, options UpOptions) error {
  149. if s.UpFn == nil {
  150. return ErrNotImplemented
  151. }
  152. for _, i := range s.interceptors {
  153. i(ctx, project)
  154. }
  155. return s.UpFn(ctx, project, options)
  156. }
  157. // Down implements Service interface
  158. func (s *ServiceProxy) Down(ctx context.Context, project string, options DownOptions) error {
  159. if s.DownFn == nil {
  160. return ErrNotImplemented
  161. }
  162. return s.DownFn(ctx, project, options)
  163. }
  164. // Logs implements Service interface
  165. func (s *ServiceProxy) Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error {
  166. if s.LogsFn == nil {
  167. return ErrNotImplemented
  168. }
  169. return s.LogsFn(ctx, projectName, consumer, options)
  170. }
  171. // Ps implements Service interface
  172. func (s *ServiceProxy) Ps(ctx context.Context, project string, options PsOptions) ([]ContainerSummary, error) {
  173. if s.PsFn == nil {
  174. return nil, ErrNotImplemented
  175. }
  176. return s.PsFn(ctx, project, options)
  177. }
  178. // List implements Service interface
  179. func (s *ServiceProxy) List(ctx context.Context, options ListOptions) ([]Stack, error) {
  180. if s.ListFn == nil {
  181. return nil, ErrNotImplemented
  182. }
  183. return s.ListFn(ctx, options)
  184. }
  185. // Convert implements Service interface
  186. func (s *ServiceProxy) Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error) {
  187. if s.ConvertFn == nil {
  188. return nil, ErrNotImplemented
  189. }
  190. for _, i := range s.interceptors {
  191. i(ctx, project)
  192. }
  193. return s.ConvertFn(ctx, project, options)
  194. }
  195. // Kill implements Service interface
  196. func (s *ServiceProxy) Kill(ctx context.Context, project string, options KillOptions) error {
  197. if s.KillFn == nil {
  198. return ErrNotImplemented
  199. }
  200. return s.KillFn(ctx, project, options)
  201. }
  202. // RunOneOffContainer implements Service interface
  203. func (s *ServiceProxy) RunOneOffContainer(ctx context.Context, project *types.Project, options RunOptions) (int, error) {
  204. if s.RunOneOffContainerFn == nil {
  205. return 0, ErrNotImplemented
  206. }
  207. for _, i := range s.interceptors {
  208. i(ctx, project)
  209. }
  210. return s.RunOneOffContainerFn(ctx, project, options)
  211. }
  212. // Remove implements Service interface
  213. func (s *ServiceProxy) Remove(ctx context.Context, project string, options RemoveOptions) error {
  214. if s.RemoveFn == nil {
  215. return ErrNotImplemented
  216. }
  217. return s.RemoveFn(ctx, project, options)
  218. }
  219. // Exec implements Service interface
  220. func (s *ServiceProxy) Exec(ctx context.Context, project string, options RunOptions) (int, error) {
  221. if s.ExecFn == nil {
  222. return 0, ErrNotImplemented
  223. }
  224. return s.ExecFn(ctx, project, options)
  225. }
  226. // Copy implements Service interface
  227. func (s *ServiceProxy) Copy(ctx context.Context, project string, options CopyOptions) error {
  228. if s.CopyFn == nil {
  229. return ErrNotImplemented
  230. }
  231. return s.CopyFn(ctx, project, options)
  232. }
  233. // Pause implements Service interface
  234. func (s *ServiceProxy) Pause(ctx context.Context, project string, options PauseOptions) error {
  235. if s.PauseFn == nil {
  236. return ErrNotImplemented
  237. }
  238. return s.PauseFn(ctx, project, options)
  239. }
  240. // UnPause implements Service interface
  241. func (s *ServiceProxy) UnPause(ctx context.Context, project string, options PauseOptions) error {
  242. if s.UnPauseFn == nil {
  243. return ErrNotImplemented
  244. }
  245. return s.UnPauseFn(ctx, project, options)
  246. }
  247. // Top implements Service interface
  248. func (s *ServiceProxy) Top(ctx context.Context, project string, services []string) ([]ContainerProcSummary, error) {
  249. if s.TopFn == nil {
  250. return nil, ErrNotImplemented
  251. }
  252. return s.TopFn(ctx, project, services)
  253. }
  254. // Events implements Service interface
  255. func (s *ServiceProxy) Events(ctx context.Context, project string, options EventsOptions) error {
  256. if s.EventsFn == nil {
  257. return ErrNotImplemented
  258. }
  259. return s.EventsFn(ctx, project, options)
  260. }
  261. // Port implements Service interface
  262. func (s *ServiceProxy) Port(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error) {
  263. if s.PortFn == nil {
  264. return "", 0, ErrNotImplemented
  265. }
  266. return s.PortFn(ctx, project, service, port, options)
  267. }
  268. // Images implements Service interface
  269. func (s *ServiceProxy) Images(ctx context.Context, project string, options ImagesOptions) ([]ImageSummary, error) {
  270. if s.ImagesFn == nil {
  271. return nil, ErrNotImplemented
  272. }
  273. return s.ImagesFn(ctx, project, options)
  274. }