proxy.go 12 KB

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