github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/save.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 "fmt" 19 "os" 20 "path/filepath" 21 22 v1 "github.com/alibaba/sealer/types/api/v1" 23 "github.com/alibaba/sealer/utils/platform" 24 25 "github.com/alibaba/sealer/pkg/image/reference" 26 "github.com/spf13/cobra" 27 28 "github.com/alibaba/sealer/logger" 29 "github.com/alibaba/sealer/pkg/image" 30 ) 31 32 type saveFlag struct { 33 ImageTar string 34 Platform string 35 } 36 37 var save saveFlag 38 39 // saveCmd represents the save command 40 var saveCmd = &cobra.Command{ 41 Use: "save", 42 Short: "save image to a tar file", 43 Long: `sealer save -o [output file name] [image name]`, 44 Example: ` 45 save kubernetes:v1.19.8 image to kubernetes.tar file: 46 47 sealer save -o kubernetes.tar kubernetes:v1.19.8`, 48 Args: cobra.ExactArgs(1), 49 RunE: func(cmd *cobra.Command, args []string) error { 50 imageTar := save.ImageTar 51 if imageTar == "" { 52 return fmt.Errorf("imagetar cannot be empty") 53 } 54 55 dir, file := filepath.Split(imageTar) 56 if dir == "" { 57 dir = "." 58 } 59 if file == "" { 60 file = fmt.Sprintf("%s.tar", args[0]) 61 } 62 imageTar = filepath.Join(dir, file) 63 // only file path like "/tmp" will lose add image tar name,make sure imageTar with full file name. 64 if filepath.Ext(imageTar) != ".tar" { 65 imageTar = filepath.Join(imageTar, fmt.Sprintf("%s.tar", args[0])) 66 } 67 68 ifs, err := image.NewImageFileService() 69 if err != nil { 70 return err 71 } 72 named, err := reference.ParseToNamed(args[0]) 73 if err != nil { 74 return err 75 } 76 77 var targetPlatforms []*v1.Platform 78 if save.Platform != "" { 79 tp, err := platform.ParsePlatforms(save.Platform) 80 if err != nil { 81 return err 82 } 83 targetPlatforms = tp 84 } 85 86 if err = ifs.Save(named.Raw(), imageTar, targetPlatforms); err != nil { 87 return fmt.Errorf("failed to save image %s: %v", args[0], err) 88 } 89 logger.Info("save image %s to %s successfully", args[0], imageTar) 90 return nil 91 }, 92 } 93 94 func init() { 95 rootCmd.AddCommand(saveCmd) 96 save = saveFlag{} 97 saveCmd.Flags().StringVarP(&save.ImageTar, "output", "o", "", "write the image to a file") 98 saveCmd.Flags().StringVar(&save.Platform, "platform", "", "set cloud image platform") 99 100 if err := saveCmd.MarkFlagRequired("output"); err != nil { 101 logger.Error("failed to init flag: %v", err) 102 os.Exit(1) 103 } 104 }