watch.go 14 KB

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