github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/build.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"os"
    19  
    20  	"github.com/alibaba/sealer/utils/platform"
    21  
    22  	"github.com/alibaba/sealer/utils"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"github.com/alibaba/sealer/build"
    27  	"github.com/alibaba/sealer/logger"
    28  )
    29  
    30  type BuildFlag struct {
    31  	ImageName    string
    32  	KubefileName string
    33  	BuildType    string
    34  	BuildArgs    []string
    35  	Platform     string
    36  	NoCache      bool
    37  	Base         bool
    38  }
    39  
    40  var buildConfig *BuildFlag
    41  
    42  // buildCmd represents the build command
    43  var buildCmd = &cobra.Command{
    44  	Use:   "build [flags] PATH",
    45  	Short: "build an cloud image from a Kubefile",
    46  	Long:  "sealer build -f Kubefile -t my-kubernetes:1.19.8 [--base=false] [--no-cache]",
    47  	Example: `the current path is the context path, default build type is lite and use build cache
    48  
    49  build:
    50  	sealer build -f Kubefile -t my-kubernetes:1.19.8 .
    51  
    52  build without cache:
    53  	sealer build -f Kubefile -t my-kubernetes:1.19.8 --no-cache .
    54  
    55  build without base:
    56  	sealer build -f Kubefile -t my-kubernetes:1.19.8 --base=false .
    57  
    58  build with args:
    59  	sealer build -f Kubefile -t my-kubernetes:1.19.8 --build-arg MY_ARG=abc,PASSWORD=Sealer123 .
    60  
    61  `,
    62  	RunE: func(cmd *cobra.Command, args []string) error {
    63  		var (
    64  			buildContext = "."
    65  		)
    66  		if len(args) != 0 {
    67  			buildContext = args[0]
    68  		}
    69  
    70  		targetPlatforms, err := platform.GetPlatform(buildConfig.Platform)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		for _, tp := range targetPlatforms {
    75  			p := tp
    76  			conf := &build.Config{
    77  				BuildType: buildConfig.BuildType,
    78  				NoCache:   buildConfig.NoCache,
    79  				ImageName: buildConfig.ImageName,
    80  				NoBase:    !buildConfig.Base,
    81  				BuildArgs: utils.ConvertEnvListToMap(buildConfig.BuildArgs),
    82  				Platform:  *p,
    83  			}
    84  			builder, err := build.NewBuilder(conf)
    85  			if err != nil {
    86  				return err
    87  			}
    88  			err = builder.Build(buildConfig.ImageName, buildContext, buildConfig.KubefileName)
    89  			if err != nil {
    90  				return err
    91  			}
    92  		}
    93  		return nil
    94  	},
    95  }
    96  
    97  func init() {
    98  	buildConfig = &BuildFlag{}
    99  	rootCmd.AddCommand(buildCmd)
   100  	buildCmd.Flags().StringVarP(&buildConfig.BuildType, "mode", "m", "lite", "cluster image build type, default is lite")
   101  	buildCmd.Flags().StringVarP(&buildConfig.KubefileName, "kubefile", "f", "Kubefile", "kubefile filepath")
   102  	buildCmd.Flags().StringVarP(&buildConfig.ImageName, "imageName", "t", "", "cluster image name")
   103  	buildCmd.Flags().BoolVar(&buildConfig.NoCache, "no-cache", false, "build without cache")
   104  	buildCmd.Flags().BoolVar(&buildConfig.Base, "base", true, "build with base image,default value is true.")
   105  	buildCmd.Flags().StringSliceVar(&buildConfig.BuildArgs, "build-arg", []string{}, "set custom build args")
   106  	buildCmd.Flags().StringVar(&buildConfig.Platform, "platform", "", "set cloud image platform,if not set,keep same platform with runtime")
   107  
   108  	if err := buildCmd.MarkFlagRequired("imageName"); err != nil {
   109  		logger.Error("failed to init flag: %v", err)
   110  		os.Exit(1)
   111  	}
   112  }