|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/docker/cli/cli/command" |
| 25 | + "github.com/spf13/cobra" |
| 26 | + |
| 27 | + "github.com/docker/compose/v5/pkg/api" |
| 28 | + "github.com/docker/compose/v5/pkg/compose" |
| 29 | +) |
| 30 | + |
| 31 | +type deployOptions struct { |
| 32 | + *ProjectOptions |
| 33 | + composeOptions |
| 34 | + build bool |
| 35 | + noBuild bool |
| 36 | + push bool |
| 37 | + quiet bool |
| 38 | + removeOrphans bool |
| 39 | + wait bool |
| 40 | + waitTimeout int |
| 41 | +} |
| 42 | + |
| 43 | +func deployCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command { |
| 44 | + opts := deployOptions{ |
| 45 | + ProjectOptions: p, |
| 46 | + } |
| 47 | + cmd := &cobra.Command{ |
| 48 | + Use: "deploy [OPTIONS] [SERVICE...]", |
| 49 | + Short: "Deploy a Compose application to a Docker server", |
| 50 | + Long: `Deploy a Compose application to a Docker server. |
| 51 | +
|
| 52 | +This command applies the Compose project to the target Docker server, |
| 53 | +recreating containers with updated configuration and images. Images are |
| 54 | +pulled from the registry unless --build is specified. |
| 55 | +
|
| 56 | +Use health checks defined in the Compose file to ensure zero-downtime |
| 57 | +deployments by passing --wait.`, |
| 58 | + PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error { |
| 59 | + if opts.waitTimeout < 0 { |
| 60 | + return fmt.Errorf("--wait-timeout must be a non-negative integer") |
| 61 | + } |
| 62 | + if opts.build && opts.noBuild { |
| 63 | + return fmt.Errorf("--build and --no-build are incompatible") |
| 64 | + } |
| 65 | + return nil |
| 66 | + }), |
| 67 | + RunE: Adapt(func(ctx context.Context, args []string) error { |
| 68 | + return runDeploy(ctx, dockerCli, backendOptions, opts, args) |
| 69 | + }), |
| 70 | + ValidArgsFunction: completeServiceNames(dockerCli, p), |
| 71 | + } |
| 72 | + flags := cmd.Flags() |
| 73 | + flags.BoolVar(&opts.build, "build", false, "Build images before deploying") |
| 74 | + flags.BoolVar(&opts.noBuild, "no-build", false, "Do not build images even if build configuration is defined") |
| 75 | + flags.BoolVar(&opts.push, "push", false, "Push images to registry before deploying") |
| 76 | + flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress pull/push progress output") |
| 77 | + flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file") |
| 78 | + flags.BoolVar(&opts.wait, "wait", false, "Wait for services to be healthy before returning") |
| 79 | + flags.IntVar(&opts.waitTimeout, "wait-timeout", 0, "Maximum duration in seconds to wait for services to be healthy (0 = no timeout)") |
| 80 | + return cmd |
| 81 | +} |
| 82 | + |
| 83 | +func runDeploy(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts deployOptions, services []string) error { |
| 84 | + backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + project, _, err := opts.ToProject(ctx, dockerCli, backend, services) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + deployOpts := api.DeployOptions{ |
| 95 | + Push: opts.push, |
| 96 | + Quiet: opts.quiet, |
| 97 | + RemoveOrphans: opts.removeOrphans, |
| 98 | + Wait: opts.wait, |
| 99 | + Services: services, |
| 100 | + } |
| 101 | + |
| 102 | + if opts.waitTimeout > 0 { |
| 103 | + deployOpts.WaitTimeout = time.Duration(opts.waitTimeout) * time.Second |
| 104 | + } |
| 105 | + |
| 106 | + if opts.build && !opts.noBuild { |
| 107 | + deployOpts.Build = &api.BuildOptions{ |
| 108 | + Services: services, |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return backend.Deploy(ctx, project, deployOpts) |
| 113 | +} |
0 commit comments