proxy.go 11 KB

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