watch.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "fmt"
  17. "github.com/compose-spec/compose-go/v2/types"
  18. "github.com/docker/compose/v2/cmd/formatter"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/compose/v2/internal/locker"
  21. "github.com/docker/compose/v2/pkg/api"
  22. "github.com/sirupsen/logrus"
  23. "github.com/spf13/cobra"
  24. )
  25. type watchOptions struct {
  26. *ProjectOptions
  27. prune bool
  28. noUp bool
  29. }
  30. func watchCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  31. watchOpts := watchOptions{
  32. ProjectOptions: p,
  33. }
  34. buildOpts := buildOptions{
  35. ProjectOptions: p,
  36. }
  37. cmd := &cobra.Command{
  38. Use: "watch [SERVICE...]",
  39. Short: "Watch build context for service and rebuild/refresh containers when files are updated",
  40. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  41. return nil
  42. }),
  43. RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  44. if cmd.Parent().Name() == "alpha" {
  45. logrus.Warn("watch command is now available as a top level command")
  46. }
  47. return runWatch(ctx, dockerCli, backend, watchOpts, buildOpts, args)
  48. }),
  49. ValidArgsFunction: completeServiceNames(dockerCli, p),
  50. }
  51. cmd.Flags().BoolVar(&buildOpts.quiet, "quiet", false, "hide build output")
  52. cmd.Flags().BoolVar(&watchOpts.prune, "prune", false, "Prune dangling images on rebuild")
  53. cmd.Flags().BoolVar(&watchOpts.noUp, "no-up", false, "Do not build & start services before watching")
  54. return cmd
  55. }
  56. func runWatch(ctx context.Context, dockerCli command.Cli, backend api.Service, watchOpts watchOptions, buildOpts buildOptions, services []string) error {
  57. project, _, err := watchOpts.ToProject(ctx, dockerCli, nil)
  58. if err != nil {
  59. return err
  60. }
  61. if err := applyPlatforms(project, true); err != nil {
  62. return err
  63. }
  64. build, err := buildOpts.toAPIBuildOptions(nil)
  65. if err != nil {
  66. return err
  67. }
  68. // validation done -- ensure we have the lockfile for this project before doing work
  69. l, err := locker.NewPidfile(project.Name)
  70. if err != nil {
  71. return fmt.Errorf("cannot take exclusive lock for project %q: %w", project.Name, err)
  72. }
  73. if err := l.Lock(); err != nil {
  74. return fmt.Errorf("cannot take exclusive lock for project %q: %w", project.Name, err)
  75. }
  76. if !watchOpts.noUp {
  77. for index, service := range project.Services {
  78. if service.Build != nil && service.Develop != nil {
  79. service.PullPolicy = types.PullPolicyBuild
  80. }
  81. project.Services[index] = service
  82. }
  83. upOpts := api.UpOptions{
  84. Create: api.CreateOptions{
  85. Build: &build,
  86. Services: services,
  87. RemoveOrphans: false,
  88. Recreate: api.RecreateDiverged,
  89. RecreateDependencies: api.RecreateNever,
  90. Inherit: true,
  91. QuietPull: buildOpts.quiet,
  92. },
  93. Start: api.StartOptions{
  94. Project: project,
  95. Attach: nil,
  96. Services: services,
  97. },
  98. }
  99. if err := backend.Up(ctx, project, upOpts); err != nil {
  100. return err
  101. }
  102. }
  103. consumer := formatter.NewLogConsumer(ctx, dockerCli.Out(), dockerCli.Err(), false, false, false)
  104. return backend.Watch(ctx, project, services, api.WatchOptions{
  105. Build: &build,
  106. LogTo: consumer,
  107. Prune: watchOpts.prune,
  108. })
  109. }