| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /*
- Copyright 2020 Docker Compose CLI authors
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package compose
- import (
- "context"
- "fmt"
- "os"
- "os/signal"
- "syscall"
- "github.com/compose-spec/compose-go/v2/types"
- "github.com/docker/cli/cli"
- "github.com/docker/compose/v2/cmd/formatter"
- "github.com/docker/compose/v2/internal/tracing"
- "github.com/docker/compose/v2/pkg/api"
- "github.com/docker/compose/v2/pkg/progress"
- "github.com/eiannone/keyboard"
- "github.com/hashicorp/go-multierror"
- "github.com/sirupsen/logrus"
- )
- func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
- err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(ctx, project), func(ctx context.Context) error {
- w := progress.ContextWriter(ctx)
- err := s.create(ctx, project, options.Create, options.Start.Attach != nil)
- if err != nil {
- return err
- }
- if options.Start.Attach == nil {
- w.HasMore(false)
- return s.start(ctx, project.Name, options.Start, nil)
- }
- return nil
- }), s.stdinfo())
- if err != nil {
- return err
- }
- if options.Start.Attach == nil {
- return err
- }
- if s.dryRun {
- fmt.Fprintln(s.stdout(), "end of 'compose up' output, interactive run is not supported in dry-run mode")
- return err
- }
- var eg multierror.Group
- // if we get a second signal during shutdown, we kill the services
- // immediately, so the channel needs to have sufficient capacity or
- // we might miss a signal while setting up the second channel read
- // (this is also why signal.Notify is used vs signal.NotifyContext)
- signalChan := make(chan os.Signal, 2)
- signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
- defer close(signalChan)
- var isTerminated bool
- printer := newLogPrinter(options.Start.Attach)
- doneCh := make(chan bool)
- eg.Go(func() error {
- first := true
- gracefulTeardown := func() {
- printer.Cancel()
- formatter.ClearLine()
- fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
- eg.Go(func() error {
- err := s.Stop(context.Background(), project.Name, api.StopOptions{
- Services: options.Create.Services,
- Project: project,
- })
- isTerminated = true
- close(doneCh)
- return err
- })
- first = false
- }
- var kEvents <-chan keyboard.KeyEvent
- if options.Start.NavigationMenu {
- kEvents, err = keyboard.GetKeys(100)
- if err != nil {
- logrus.Warn("could not start menu, an error occurred while starting.")
- } else {
- isWatchConfigured := s.shouldWatch(project)
- isDockerDesktopActive := s.isDesktopIntegrationActive()
- tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured)
- formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, signalChan, s.Watch)
- if options.Start.Watch {
- formatter.KeyboardManager.StartWatch(ctx, project, options)
- }
- }
- }
- for {
- select {
- case <-doneCh:
- return nil
- case <-ctx.Done():
- if first {
- gracefulTeardown()
- }
- case <-signalChan:
- if first {
- gracefulTeardown()
- } else {
- eg.Go(func() error {
- return s.Kill(context.Background(), project.Name, api.KillOptions{
- Services: options.Create.Services,
- Project: project,
- All: true,
- })
- })
- return nil
- }
- case event := <-kEvents:
- formatter.KeyboardManager.HandleKeyEvents(event, ctx, project, options)
- }
- }
- })
- var exitCode int
- eg.Go(func() error {
- code, err := printer.Run(options.Start.OnExit, options.Start.ExitCodeFrom, func() error {
- fmt.Fprintln(s.stdinfo(), "Aborting on container exit...")
- return progress.Run(ctx, func(ctx context.Context) error {
- return s.Stop(ctx, project.Name, api.StopOptions{
- Services: options.Create.Services,
- Project: project,
- })
- }, s.stdinfo())
- })
- exitCode = code
- return err
- })
- if options.Start.Watch && !options.Start.NavigationMenu {
- eg.Go(func() error {
- buildOpts := *options.Create.Build
- buildOpts.Quiet = true
- return s.Watch(ctx, project, options.Start.Services, api.WatchOptions{
- Build: &buildOpts,
- LogTo: options.Start.Attach,
- })
- })
- }
- // We don't use parent (cancelable) context as we manage sigterm to stop the stack
- err = s.start(context.Background(), project.Name, options.Start, printer.HandleEvent)
- if err != nil && !isTerminated { // Ignore error if the process is terminated
- return err
- }
- printer.Stop()
- if !isTerminated {
- // signal for the signal-handler goroutines to stop
- close(doneCh)
- }
- err = eg.Wait().ErrorOrNil()
- if exitCode != 0 {
- errMsg := ""
- if err != nil {
- errMsg = err.Error()
- }
- return cli.StatusError{StatusCode: exitCode, Status: errMsg}
- }
- return err
- }
|