up.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "os"
  18. "os/signal"
  19. "path/filepath"
  20. "syscall"
  21. "github.com/docker/compose-cli/api/client"
  22. "github.com/docker/compose-cli/api/compose"
  23. "github.com/docker/compose-cli/api/context/store"
  24. "github.com/docker/compose-cli/api/progress"
  25. "github.com/docker/compose-cli/cli/formatter"
  26. "github.com/compose-spec/compose-go/types"
  27. "github.com/sirupsen/logrus"
  28. "github.com/spf13/cobra"
  29. )
  30. // composeOptions hold options common to `up` and `run` to run compose project
  31. type composeOptions struct {
  32. *projectOptions
  33. Build bool
  34. // ACI only
  35. DomainName string
  36. }
  37. type upOptions struct {
  38. *composeOptions
  39. Detach bool
  40. Environment []string
  41. removeOrphans bool
  42. forceRecreate bool
  43. noRecreate bool
  44. noStart bool
  45. cascadeStop bool
  46. }
  47. func (o upOptions) recreateStrategy() string {
  48. if o.noRecreate {
  49. return compose.RecreateNever
  50. }
  51. if o.forceRecreate {
  52. return compose.RecreateForce
  53. }
  54. return compose.RecreateDiverged
  55. }
  56. func upCommand(p *projectOptions, contextType string) *cobra.Command {
  57. opts := upOptions{
  58. composeOptions: &composeOptions{
  59. projectOptions: p,
  60. },
  61. }
  62. upCmd := &cobra.Command{
  63. Use: "up [SERVICE...]",
  64. Short: "Create and start containers",
  65. RunE: func(cmd *cobra.Command, args []string) error {
  66. switch contextType {
  67. case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
  68. if opts.cascadeStop && opts.Detach {
  69. return fmt.Errorf("--abort-on-container-exit and --detach are incompatible")
  70. }
  71. if opts.forceRecreate && opts.noRecreate {
  72. return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
  73. }
  74. return runCreateStart(cmd.Context(), opts, args)
  75. default:
  76. return runUp(cmd.Context(), opts, args)
  77. }
  78. },
  79. }
  80. flags := upCmd.Flags()
  81. flags.StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  82. flags.BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
  83. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  84. flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
  85. switch contextType {
  86. case store.AciContextType:
  87. flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  88. case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
  89. flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
  90. flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
  91. flags.BoolVar(&opts.noStart, "no-start", false, "Don't start the services after creating them.")
  92. flags.BoolVar(&opts.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
  93. }
  94. return upCmd
  95. }
  96. func runUp(ctx context.Context, opts upOptions, services []string) error {
  97. c, project, err := setup(ctx, *opts.composeOptions, services)
  98. if err != nil {
  99. return err
  100. }
  101. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  102. return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
  103. Detach: opts.Detach,
  104. })
  105. })
  106. return err
  107. }
  108. func runCreateStart(ctx context.Context, opts upOptions, services []string) error {
  109. c, project, err := setup(ctx, *opts.composeOptions, services)
  110. if err != nil {
  111. return err
  112. }
  113. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  114. err := c.ComposeService().Create(ctx, project, compose.CreateOptions{
  115. RemoveOrphans: opts.removeOrphans,
  116. Recreate: opts.recreateStrategy(),
  117. })
  118. if err != nil {
  119. return "", err
  120. }
  121. if opts.Detach {
  122. err = c.ComposeService().Start(ctx, project, compose.StartOptions{})
  123. }
  124. return "", err
  125. })
  126. if err != nil {
  127. return err
  128. }
  129. if opts.noStart {
  130. return nil
  131. }
  132. if opts.Detach {
  133. return nil
  134. }
  135. queue := make(chan compose.ContainerEvent)
  136. printer := printer{
  137. queue: queue,
  138. }
  139. stopFunc := func() error {
  140. ctx := context.Background()
  141. _, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
  142. return "", c.ComposeService().Stop(ctx, project)
  143. })
  144. return err
  145. }
  146. signalChan := make(chan os.Signal, 1)
  147. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  148. go func() {
  149. <-signalChan
  150. fmt.Println("Gracefully stopping...")
  151. stopFunc() // nolint:errcheck
  152. }()
  153. err = c.ComposeService().Start(ctx, project, compose.StartOptions{
  154. Attach: queue,
  155. })
  156. if err != nil {
  157. return err
  158. }
  159. _, err = printer.run(ctx, opts.cascadeStop, stopFunc)
  160. // FIXME os.Exit
  161. return err
  162. }
  163. func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
  164. c, err := client.NewWithDefaultLocalBackend(ctx)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. project, err := opts.toProject(services)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. if opts.DomainName != "" {
  173. // arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
  174. project.Services[0].DomainName = opts.DomainName
  175. }
  176. if opts.Build {
  177. for _, service := range project.Services {
  178. service.PullPolicy = types.PullPolicyBuild
  179. }
  180. }
  181. if opts.EnvFile != "" {
  182. var services types.Services
  183. for _, s := range project.Services {
  184. ef := opts.EnvFile
  185. if ef != "" {
  186. if !filepath.IsAbs(ef) {
  187. ef = filepath.Join(project.WorkingDir, opts.EnvFile)
  188. }
  189. if s.Labels == nil {
  190. s.Labels = make(map[string]string)
  191. }
  192. s.Labels[compose.EnvironmentFileLabel] = ef
  193. services = append(services, s)
  194. }
  195. }
  196. project.Services = services
  197. }
  198. return c, project, nil
  199. }
  200. type printer struct {
  201. queue chan compose.ContainerEvent
  202. }
  203. func (p printer) run(ctx context.Context, cascadeStop bool, stopFn func() error) (int, error) { //nolint:unparam
  204. consumer := formatter.NewLogConsumer(ctx, os.Stdout)
  205. for {
  206. event := <-p.queue
  207. switch event.Type {
  208. case compose.ContainerEventExit:
  209. consumer.Status(event.Service, event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
  210. if cascadeStop {
  211. fmt.Println("Aborting on container exit...")
  212. err := stopFn()
  213. logrus.Error(event.ExitCode)
  214. return event.ExitCode, err
  215. }
  216. case compose.ContainerEventLog:
  217. consumer.Log(event.Service, event.Source, event.Line)
  218. }
  219. }
  220. }