watch.go 3.6 KB

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