github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_up.go (about) 1 /* 2 Copyright The containerd 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 main 18 19 import ( 20 "errors" 21 "fmt" 22 "strconv" 23 "strings" 24 25 "github.com/containerd/nerdctl/pkg/clientutil" 26 "github.com/containerd/nerdctl/pkg/cmd/compose" 27 "github.com/containerd/nerdctl/pkg/composer" 28 "github.com/spf13/cobra" 29 ) 30 31 func newComposeUpCommand() *cobra.Command { 32 var composeUpCommand = &cobra.Command{ 33 Use: "up [flags] [SERVICE...]", 34 Short: "Create and start containers", 35 RunE: composeUpAction, 36 SilenceUsage: true, 37 SilenceErrors: true, 38 } 39 composeUpCommand.Flags().BoolP("detach", "d", false, "Detached mode: Run containers in the background") 40 composeUpCommand.Flags().Bool("no-build", false, "Don't build an image, even if it's missing.") 41 composeUpCommand.Flags().Bool("no-color", false, "Produce monochrome output") 42 composeUpCommand.Flags().Bool("no-log-prefix", false, "Don't print prefix in logs") 43 composeUpCommand.Flags().Bool("build", false, "Build images before starting containers.") 44 composeUpCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS during build") 45 composeUpCommand.Flags().Bool("quiet-pull", false, "Pull without printing progress information") 46 composeUpCommand.Flags().Bool("remove-orphans", false, "Remove containers for services not defined in the Compose file.") 47 composeUpCommand.Flags().StringArray("scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.") 48 return composeUpCommand 49 } 50 51 func composeUpAction(cmd *cobra.Command, services []string) error { 52 globalOptions, err := processRootCmdFlags(cmd) 53 if err != nil { 54 return err 55 } 56 detach, err := cmd.Flags().GetBool("detach") 57 if err != nil { 58 return err 59 } 60 noBuild, err := cmd.Flags().GetBool("no-build") 61 if err != nil { 62 return err 63 } 64 noColor, err := cmd.Flags().GetBool("no-color") 65 if err != nil { 66 return err 67 } 68 noLogPrefix, err := cmd.Flags().GetBool("no-log-prefix") 69 if err != nil { 70 return err 71 } 72 build, err := cmd.Flags().GetBool("build") 73 if err != nil { 74 return err 75 } 76 if build && noBuild { 77 return errors.New("--build and --no-build can not be combined") 78 } 79 enableIPFS, err := cmd.Flags().GetBool("ipfs") 80 if err != nil { 81 return err 82 } 83 quietPull, err := cmd.Flags().GetBool("quiet-pull") 84 if err != nil { 85 return err 86 } 87 removeOrphans, err := cmd.Flags().GetBool("remove-orphans") 88 if err != nil { 89 return err 90 } 91 scaleSlice, err := cmd.Flags().GetStringArray("scale") 92 if err != nil { 93 return err 94 } 95 scale := make(map[string]uint64) 96 for _, s := range scaleSlice { 97 parts := strings.Split(s, "=") 98 if len(parts) != 2 { 99 return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", s) 100 } 101 replicas, err := strconv.Atoi(parts[1]) 102 if err != nil { 103 return err 104 } 105 scale[parts[0]] = uint64(replicas) 106 } 107 108 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) 109 if err != nil { 110 return err 111 } 112 defer cancel() 113 options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental) 114 if err != nil { 115 return err 116 } 117 options.Services = services 118 c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr()) 119 if err != nil { 120 return err 121 } 122 123 uo := composer.UpOptions{ 124 Detach: detach, 125 NoBuild: noBuild, 126 NoColor: noColor, 127 NoLogPrefix: noLogPrefix, 128 ForceBuild: build, 129 IPFS: enableIPFS, 130 QuietPull: quietPull, 131 RemoveOrphans: removeOrphans, 132 Scale: scale, 133 } 134 return c.Up(ctx, uo, services) 135 }