watch.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. "errors"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/compose-spec/compose-go/v2/types"
  27. "github.com/docker/compose/v2/internal/sync"
  28. "github.com/docker/compose/v2/pkg/api"
  29. "github.com/docker/compose/v2/pkg/watch"
  30. moby "github.com/docker/docker/api/types"
  31. "github.com/jonboulle/clockwork"
  32. "github.com/mitchellh/mapstructure"
  33. "github.com/sirupsen/logrus"
  34. "golang.org/x/sync/errgroup"
  35. )
  36. const quietPeriod = 500 * time.Millisecond
  37. // fileEvent contains the Compose service and modified host system path.
  38. type fileEvent struct {
  39. sync.PathMapping
  40. Action types.WatchAction
  41. }
  42. // getSyncImplementation returns an appropriate sync implementation for the
  43. // project.
  44. //
  45. // Currently, an implementation that batches files and transfers them using
  46. // the Moby `Untar` API.
  47. func (s *composeService) getSyncImplementation(project *types.Project) (sync.Syncer, error) {
  48. var useTar bool
  49. if useTarEnv, ok := os.LookupEnv("COMPOSE_EXPERIMENTAL_WATCH_TAR"); ok {
  50. useTar, _ = strconv.ParseBool(useTarEnv)
  51. } else {
  52. useTar = true
  53. }
  54. if !useTar {
  55. return nil, errors.New("no available sync implementation")
  56. }
  57. return sync.NewTar(project.Name, tarDockerClient{s: s}), nil
  58. }
  59. func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error { //nolint: gocyclo
  60. var err error
  61. if project, err = project.WithSelectedServices(services); err != nil {
  62. return err
  63. }
  64. syncer, err := s.getSyncImplementation(project)
  65. if err != nil {
  66. return err
  67. }
  68. eg, ctx := errgroup.WithContext(ctx)
  69. watching := false
  70. options.LogTo.Register(api.WatchLogger)
  71. for i := range project.Services {
  72. service := project.Services[i]
  73. config, err := loadDevelopmentConfig(service, project)
  74. if err != nil {
  75. return err
  76. }
  77. if service.Develop != nil {
  78. config = service.Develop
  79. }
  80. if config == nil {
  81. continue
  82. }
  83. for _, trigger := range config.Watch {
  84. if trigger.Action == types.WatchActionRebuild {
  85. if service.Build == nil {
  86. return fmt.Errorf("can't watch service %q with action %s without a build context", service.Name, types.WatchActionRebuild)
  87. }
  88. if options.Build == nil {
  89. return fmt.Errorf("--no-build is incompatible with watch action %s in service %s", types.WatchActionRebuild, service.Name)
  90. }
  91. }
  92. }
  93. if len(services) > 0 && service.Build == nil {
  94. // service explicitly selected for watch has no build section
  95. return fmt.Errorf("can't watch service %q without a build context", service.Name)
  96. }
  97. if len(services) == 0 && service.Build == nil {
  98. continue
  99. }
  100. // set the service to always be built - watch triggers `Up()` when it receives a rebuild event
  101. service.PullPolicy = types.PullPolicyBuild
  102. project.Services[i] = service
  103. dockerIgnores, err := watch.LoadDockerIgnore(service.Build.Context)
  104. if err != nil {
  105. return err
  106. }
  107. // add a hardcoded set of ignores on top of what came from .dockerignore
  108. // some of this should likely be configurable (e.g. there could be cases
  109. // where you want `.git` to be synced) but this is suitable for now
  110. dotGitIgnore, err := watch.NewDockerPatternMatcher("/", []string{".git/"})
  111. if err != nil {
  112. return err
  113. }
  114. ignore := watch.NewCompositeMatcher(
  115. dockerIgnores,
  116. watch.EphemeralPathMatcher(),
  117. dotGitIgnore,
  118. )
  119. var paths, pathLogs []string
  120. for _, trigger := range config.Watch {
  121. if checkIfPathAlreadyBindMounted(trigger.Path, service.Volumes) {
  122. logrus.Warnf("path '%s' also declared by a bind mount volume, this path won't be monitored!\n", trigger.Path)
  123. continue
  124. }
  125. paths = append(paths, trigger.Path)
  126. pathLogs = append(pathLogs, fmt.Sprintf("Action %s for path %q", trigger.Action, trigger.Path))
  127. }
  128. watcher, err := watch.NewWatcher(paths, ignore)
  129. if err != nil {
  130. return err
  131. }
  132. logrus.Debugf("Watch configuration for service %q:%s\n",
  133. service.Name,
  134. strings.Join(append([]string{""}, pathLogs...), "\n - "),
  135. )
  136. err = watcher.Start()
  137. if err != nil {
  138. return err
  139. }
  140. watching = true
  141. eg.Go(func() error {
  142. defer watcher.Close() //nolint:errcheck
  143. return s.watch(ctx, project, service.Name, options, watcher, syncer, config.Watch)
  144. })
  145. }
  146. if !watching {
  147. return fmt.Errorf("none of the selected services is configured for watch, consider setting an 'develop' section")
  148. }
  149. options.LogTo.Log(api.WatchLogger, "watch enabled")
  150. return eg.Wait()
  151. }
  152. func (s *composeService) watch(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []types.Trigger) error {
  153. ctx, cancel := context.WithCancel(ctx)
  154. defer cancel()
  155. ignores := make([]watch.PathMatcher, len(triggers))
  156. for i, trigger := range triggers {
  157. ignore, err := watch.NewDockerPatternMatcher(trigger.Path, trigger.Ignore)
  158. if err != nil {
  159. return err
  160. }
  161. ignores[i] = ignore
  162. }
  163. events := make(chan fileEvent)
  164. batchEvents := batchDebounceEvents(ctx, s.clock, quietPeriod, events)
  165. go func() {
  166. for {
  167. select {
  168. case <-ctx.Done():
  169. return
  170. case batch := <-batchEvents:
  171. start := time.Now()
  172. logrus.Debugf("batch start: service[%s] count[%d]", name, len(batch))
  173. if err := s.handleWatchBatch(ctx, project, name, options, batch, syncer); err != nil {
  174. logrus.Warnf("Error handling changed files for service %s: %v", name, err)
  175. }
  176. logrus.Debugf("batch complete: service[%s] duration[%s] count[%d]",
  177. name, time.Since(start), len(batch))
  178. }
  179. }
  180. }()
  181. for {
  182. select {
  183. case <-ctx.Done():
  184. return nil
  185. case err := <-watcher.Errors():
  186. return err
  187. case event := <-watcher.Events():
  188. hostPath := event.Path()
  189. for i, trigger := range triggers {
  190. logrus.Debugf("change for %s - comparing with %s", hostPath, trigger.Path)
  191. if fileEvent := maybeFileEvent(trigger, hostPath, ignores[i]); fileEvent != nil {
  192. events <- *fileEvent
  193. }
  194. }
  195. }
  196. }
  197. }
  198. // maybeFileEvent returns a file event object if hostPath is valid for the provided trigger and ignore
  199. // rules.
  200. //
  201. // Any errors are logged as warnings and nil (no file event) is returned.
  202. func maybeFileEvent(trigger types.Trigger, hostPath string, ignore watch.PathMatcher) *fileEvent {
  203. if !watch.IsChild(trigger.Path, hostPath) {
  204. return nil
  205. }
  206. isIgnored, err := ignore.Matches(hostPath)
  207. if err != nil {
  208. logrus.Warnf("error ignore matching %q: %v", hostPath, err)
  209. return nil
  210. }
  211. if isIgnored {
  212. logrus.Debugf("%s is matching ignore pattern", hostPath)
  213. return nil
  214. }
  215. var containerPath string
  216. if trigger.Target != "" {
  217. rel, err := filepath.Rel(trigger.Path, hostPath)
  218. if err != nil {
  219. logrus.Warnf("error making %s relative to %s: %v", hostPath, trigger.Path, err)
  220. return nil
  221. }
  222. // always use Unix-style paths for inside the container
  223. containerPath = path.Join(trigger.Target, rel)
  224. }
  225. return &fileEvent{
  226. Action: trigger.Action,
  227. PathMapping: sync.PathMapping{
  228. HostPath: hostPath,
  229. ContainerPath: containerPath,
  230. },
  231. }
  232. }
  233. func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*types.DevelopConfig, error) {
  234. var config types.DevelopConfig
  235. y, ok := service.Extensions["x-develop"]
  236. if !ok {
  237. return nil, nil
  238. }
  239. logrus.Warnf("x-develop is DEPRECATED, please use the official `develop` attribute")
  240. err := mapstructure.Decode(y, &config)
  241. if err != nil {
  242. return nil, err
  243. }
  244. baseDir, err := filepath.EvalSymlinks(project.WorkingDir)
  245. if err != nil {
  246. return nil, fmt.Errorf("resolving symlink for %q: %w", project.WorkingDir, err)
  247. }
  248. for i, trigger := range config.Watch {
  249. if !filepath.IsAbs(trigger.Path) {
  250. trigger.Path = filepath.Join(baseDir, trigger.Path)
  251. }
  252. if p, err := filepath.EvalSymlinks(trigger.Path); err == nil {
  253. // this might fail because the path doesn't exist, etc.
  254. trigger.Path = p
  255. }
  256. trigger.Path = filepath.Clean(trigger.Path)
  257. if trigger.Path == "" {
  258. return nil, errors.New("watch rules MUST define a path")
  259. }
  260. if trigger.Action == types.WatchActionRebuild && service.Build == nil {
  261. return nil, fmt.Errorf("service %s doesn't have a build section, can't apply 'rebuild' on watch", service.Name)
  262. }
  263. config.Watch[i] = trigger
  264. }
  265. return &config, nil
  266. }
  267. // batchDebounceEvents groups identical file events within a sliding time window and writes the results to the returned
  268. // channel.
  269. //
  270. // The returned channel is closed when the debouncer is stopped via context cancellation or by closing the input channel.
  271. func batchDebounceEvents(ctx context.Context, clock clockwork.Clock, delay time.Duration, input <-chan fileEvent) <-chan []fileEvent {
  272. out := make(chan []fileEvent)
  273. go func() {
  274. defer close(out)
  275. seen := make(map[fileEvent]time.Time)
  276. flushEvents := func() {
  277. if len(seen) == 0 {
  278. return
  279. }
  280. events := make([]fileEvent, 0, len(seen))
  281. for e := range seen {
  282. events = append(events, e)
  283. }
  284. // sort batch by oldest -> newest
  285. // (if an event is seen > 1 per batch, it gets the latest timestamp)
  286. sort.SliceStable(events, func(i, j int) bool {
  287. x := events[i]
  288. y := events[j]
  289. return seen[x].Before(seen[y])
  290. })
  291. out <- events
  292. seen = make(map[fileEvent]time.Time)
  293. }
  294. t := clock.NewTicker(delay)
  295. defer t.Stop()
  296. for {
  297. select {
  298. case <-ctx.Done():
  299. return
  300. case <-t.Chan():
  301. flushEvents()
  302. case e, ok := <-input:
  303. if !ok {
  304. // input channel was closed
  305. flushEvents()
  306. return
  307. }
  308. seen[e] = time.Now()
  309. t.Reset(delay)
  310. }
  311. }
  312. }()
  313. return out
  314. }
  315. func checkIfPathAlreadyBindMounted(watchPath string, volumes []types.ServiceVolumeConfig) bool {
  316. for _, volume := range volumes {
  317. if volume.Bind != nil && strings.HasPrefix(watchPath, volume.Source) {
  318. return true
  319. }
  320. }
  321. return false
  322. }
  323. type tarDockerClient struct {
  324. s *composeService
  325. }
  326. func (t tarDockerClient) ContainersForService(ctx context.Context, projectName string, serviceName string) ([]moby.Container, error) {
  327. containers, err := t.s.getContainers(ctx, projectName, oneOffExclude, true, serviceName)
  328. if err != nil {
  329. return nil, err
  330. }
  331. return containers, nil
  332. }
  333. func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []string, in io.Reader) error {
  334. execCfg := moby.ExecConfig{
  335. Cmd: cmd,
  336. AttachStdout: false,
  337. AttachStderr: true,
  338. AttachStdin: in != nil,
  339. Tty: false,
  340. }
  341. execCreateResp, err := t.s.apiClient().ContainerExecCreate(ctx, containerID, execCfg)
  342. if err != nil {
  343. return err
  344. }
  345. startCheck := moby.ExecStartCheck{Tty: false, Detach: false}
  346. conn, err := t.s.apiClient().ContainerExecAttach(ctx, execCreateResp.ID, startCheck)
  347. if err != nil {
  348. return err
  349. }
  350. defer conn.Close()
  351. var eg errgroup.Group
  352. if in != nil {
  353. eg.Go(func() error {
  354. defer func() {
  355. _ = conn.CloseWrite()
  356. }()
  357. _, err := io.Copy(conn.Conn, in)
  358. return err
  359. })
  360. }
  361. eg.Go(func() error {
  362. _, err := io.Copy(t.s.stdinfo(), conn.Reader)
  363. return err
  364. })
  365. err = t.s.apiClient().ContainerExecStart(ctx, execCreateResp.ID, startCheck)
  366. if err != nil {
  367. return err
  368. }
  369. // although the errgroup is not tied directly to the context, the operations
  370. // in it are reading/writing to the connection, which is tied to the context,
  371. // so they won't block indefinitely
  372. if err := eg.Wait(); err != nil {
  373. return err
  374. }
  375. execResult, err := t.s.apiClient().ContainerExecInspect(ctx, execCreateResp.ID)
  376. if err != nil {
  377. return err
  378. }
  379. if execResult.Running {
  380. return errors.New("process still running")
  381. }
  382. if execResult.ExitCode != 0 {
  383. return fmt.Errorf("exit code %d", execResult.ExitCode)
  384. }
  385. return nil
  386. }
  387. func (t tarDockerClient) Untar(ctx context.Context, id string, archive io.ReadCloser) error {
  388. return t.s.apiClient().CopyToContainer(ctx, id, "/", archive, moby.CopyToContainerOptions{
  389. CopyUIDGID: true,
  390. })
  391. }
  392. func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, serviceName string, options api.WatchOptions, batch []fileEvent, syncer sync.Syncer) error {
  393. pathMappings := make([]sync.PathMapping, len(batch))
  394. restartService := false
  395. for i := range batch {
  396. if batch[i].Action == types.WatchActionRebuild {
  397. options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Rebuilding service %q after changes were detected...", serviceName))
  398. // restrict the build to ONLY this service, not any of its dependencies
  399. options.Build.Services = []string{serviceName}
  400. _, err := s.build(ctx, project, *options.Build, nil)
  401. if err != nil {
  402. options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Build failed. Error: %v", err))
  403. return err
  404. }
  405. options.LogTo.Log(api.WatchLogger, fmt.Sprintf("service %q successfully built", serviceName))
  406. err = s.create(ctx, project, api.CreateOptions{
  407. Services: []string{serviceName},
  408. Inherit: true,
  409. Recreate: api.RecreateForce,
  410. })
  411. if err != nil {
  412. options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Failed to recreate service after update. Error: %v", err))
  413. return err
  414. }
  415. err = s.start(ctx, project.Name, api.StartOptions{
  416. Project: project,
  417. Services: []string{serviceName},
  418. }, nil)
  419. if err != nil {
  420. options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Application failed to start after update. Error: %v", err))
  421. }
  422. return nil
  423. }
  424. if batch[i].Action == types.WatchActionSyncRestart {
  425. restartService = true
  426. }
  427. pathMappings[i] = batch[i].PathMapping
  428. }
  429. writeWatchSyncMessage(options.LogTo, serviceName, pathMappings)
  430. service, err := project.GetService(serviceName)
  431. if err != nil {
  432. return err
  433. }
  434. if err := syncer.Sync(ctx, service, pathMappings); err != nil {
  435. return err
  436. }
  437. if restartService {
  438. return s.Restart(ctx, project.Name, api.RestartOptions{
  439. Services: []string{serviceName},
  440. Project: project,
  441. NoDeps: false,
  442. })
  443. }
  444. return nil
  445. }
  446. // writeWatchSyncMessage prints out a message about the sync for the changed paths.
  447. func writeWatchSyncMessage(log api.LogConsumer, serviceName string, pathMappings []sync.PathMapping) {
  448. const maxPathsToShow = 10
  449. if len(pathMappings) <= maxPathsToShow || logrus.IsLevelEnabled(logrus.DebugLevel) {
  450. hostPathsToSync := make([]string, len(pathMappings))
  451. for i := range pathMappings {
  452. hostPathsToSync[i] = pathMappings[i].HostPath
  453. }
  454. log.Log(api.WatchLogger, fmt.Sprintf("Syncing %q after changes were detected", serviceName))
  455. } else {
  456. hostPathsToSync := make([]string, len(pathMappings))
  457. for i := range pathMappings {
  458. hostPathsToSync[i] = pathMappings[i].HostPath
  459. }
  460. log.Log(api.WatchLogger, fmt.Sprintf("Syncing service %q after %d changes were detected", serviceName, len(pathMappings)))
  461. }
  462. }