From cfb28d6d742bb58206c6bad140b19996e063f230 Mon Sep 17 00:00:00 2001 From: Jiahui Date: Wed, 12 May 2021 21:23:22 +0800 Subject: [PATCH] fix delete clusterFile (#65) * fix login; fix delete clusterFile * fix login; fix delete clusterFile * update --- apply/cloud_applier.go | 3 +-- sealer/cmd/build.go | 10 +++++++--- sealer/cmd/delete.go | 8 ++++---- sealer/cmd/list.go | 6 +++--- sealer/cmd/login.go | 15 ++++++++++++--- sealer/cmd/pull.go | 11 ++++------- sealer/cmd/push.go | 11 ++++------- sealer/cmd/rmi.go | 11 ++++------- sealer/cmd/run.go | 9 +++------ sealer/cmd/tag.go | 11 ++++------- sealer/cmd/version.go | 6 +++--- 11 files changed, 49 insertions(+), 52 deletions(-) diff --git a/apply/cloud_applier.go b/apply/cloud_applier.go index e13c3ab08fe..f6616b5648f 100644 --- a/apply/cloud_applier.go +++ b/apply/cloud_applier.go @@ -101,10 +101,9 @@ func (c *CloudApplier) Delete() error { } if err := utils.RemoveFileContent(common.EtcHosts, fmt.Sprintf("%s %s", host, common.APIServerDomain)); err != nil { logger.Warn(err) - return nil } - if err := utils.CleanFiles(common.DefaultKubeconfigDir, common.GetClusterWorkDir(c.ClusterDesired.Name), common.KubectlPath); err != nil { + if err := utils.CleanFiles(common.DefaultKubeconfigDir, common.GetClusterWorkDir(c.ClusterDesired.Name), common.TmpClusterfile, common.KubectlPath); err != nil { logger.Warn(err) return nil } diff --git a/sealer/cmd/build.go b/sealer/cmd/build.go index 0e3fb99f287..1b29becf2cc 100644 --- a/sealer/cmd/build.go +++ b/sealer/cmd/build.go @@ -34,9 +34,9 @@ var buildConfig *BuildFlag // buildCmd represents the build command var buildCmd = &cobra.Command{ - Use: "build", - Short: "cloud image local build command line", - Long: `sealer build -f Kubefile -t my-kubernetes:1.18.3 .`, + Use: "build", + Short: "cloud image local build command line", + Example: `sealer build -f Kubefile -t my-kubernetes:1.18.3 .`, Run: func(cmd *cobra.Command, args []string) { conf := &build.Config{} builder := build.NewBuilder(conf, buildConfig.BuildType) @@ -55,4 +55,8 @@ func init() { buildCmd.Flags().StringVarP(&buildConfig.KubefileName, "kubefile", "f", "Kubefile", "kubefile filepath") buildCmd.Flags().StringVarP(&buildConfig.ImageName, "imageName", "t", "", "cluster image name") buildCmd.Flags().StringVarP(&buildConfig.Context, "context", "c", ".", "cluster image build context file path") + if err := buildCmd.MarkFlagRequired("imageName"); err != nil { + logger.Error("failed to init flag: %v", err) + os.Exit(1) + } } diff --git a/sealer/cmd/delete.go b/sealer/cmd/delete.go index 7da186a2172..d1fb930628a 100644 --- a/sealer/cmd/delete.go +++ b/sealer/cmd/delete.go @@ -25,10 +25,10 @@ import ( // deleteCmd represents the delete command var deleteCmd = &cobra.Command{ - Use: "delete", - Short: "delete a cluster", - Long: `sealer delete -f /root/.sealer/mycluster/Clusterfile -if provider is BARESERVER will delete kubernetes nodes, or if provider is ALI_CLOUD, will delete all the infra resources`, + Use: "delete", + Short: "delete a cluster", + Long: `if provider is BARESERVER will delete kubernetes nodes, or if provider is ALI_CLOUD, will delete all the infra resources`, + Example: `sealer delete -f /root/.sealer/mycluster/Clusterfile`, Run: func(cmd *cobra.Command, args []string) { if err := apply.NewApplierFromFile(clusterFile).Delete(); err != nil { logger.Error(err) diff --git a/sealer/cmd/list.go b/sealer/cmd/list.go index e9c91fd29b2..e85350fb37c 100644 --- a/sealer/cmd/list.go +++ b/sealer/cmd/list.go @@ -30,9 +30,9 @@ const ( ) var listCmd = &cobra.Command{ - Use: "images", - Short: "list all cluster images", - Long: `sealer images`, + Use: "images", + Short: "list all cluster images", + Example: `sealer images`, Run: func(cmd *cobra.Command, args []string) { imageMetadataList, err := image.NewImageMetadataService().List() if err != nil { diff --git a/sealer/cmd/login.go b/sealer/cmd/login.go index ec1df6c4645..b9aea5d432d 100644 --- a/sealer/cmd/login.go +++ b/sealer/cmd/login.go @@ -32,9 +32,10 @@ type LoginFlag struct { var loginConfig *LoginFlag var loginCmd = &cobra.Command{ - Use: "login", - Short: "login image repositories", - Long: `sealer login registry.cn-qingdao.aliyuncs.com -u [username] -p [password]`, + Use: "login", + Short: "login image repositories", + Example: `sealer login registry.cn-qingdao.aliyuncs.com -u [username] -p [password]`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { logger.Error("enter the registry URL failed") @@ -52,4 +53,12 @@ func init() { rootCmd.AddCommand(loginCmd) loginCmd.Flags().StringVarP(&loginConfig.RegistryUsername, "username", "u", "", "user name for login registry") loginCmd.Flags().StringVarP(&loginConfig.RegistryPasswd, "passwd", "p", "", "password for login registry") + if err := loginCmd.MarkFlagRequired("username"); err != nil { + logger.Error("failed to init flag: %v", err) + os.Exit(1) + } + if err := loginCmd.MarkFlagRequired("passwd"); err != nil { + logger.Error("failed to init flag: %v", err) + os.Exit(1) + } } diff --git a/sealer/cmd/pull.go b/sealer/cmd/pull.go index 77a1429832f..606b9f0c9ea 100644 --- a/sealer/cmd/pull.go +++ b/sealer/cmd/pull.go @@ -26,14 +26,11 @@ import ( // pullCmd represents the pull command var pullCmd = &cobra.Command{ - Use: "pull", - Short: "pull cloud image to local", - Long: `sealer pull registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.5`, + Use: "pull", + Short: "pull cloud image to local", + Example: `sealer pull registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.5`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - logger.Error("enter the imageName") - os.Exit(1) - } if err := image.NewImageService().Pull(args[0]); err != nil { logger.Error(err) os.Exit(1) diff --git a/sealer/cmd/push.go b/sealer/cmd/push.go index d54e8a3d033..05110eb35d3 100644 --- a/sealer/cmd/push.go +++ b/sealer/cmd/push.go @@ -26,14 +26,11 @@ import ( // pushCmd represents the push command var pushCmd = &cobra.Command{ - Use: "push", - Short: "push cloud image to registry", - Long: `sealer push registry.cn-qingdao.aliyuncs.com/sealer-io/my-kuberentes-cluster-with-dashboard:latest`, + Use: "push", + Short: "push cloud image to registry", + Example: `sealer push registry.cn-qingdao.aliyuncs.com/sealer-io/my-kuberentes-cluster-with-dashboard:latest`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - logger.Error("enter the imageName") - os.Exit(1) - } if err := image.NewImageService().Push(args[0]); err != nil { logger.Error(err) os.Exit(1) diff --git a/sealer/cmd/rmi.go b/sealer/cmd/rmi.go index 5e9269f59d6..2e4ff7b8afc 100644 --- a/sealer/cmd/rmi.go +++ b/sealer/cmd/rmi.go @@ -26,14 +26,11 @@ import ( // rmiCmd represents the rmi command var rmiCmd = &cobra.Command{ - Use: "rmi", - Short: "rmi delete local image", - Long: `sealer rmi registry.cn-qingdao.aliyuncs.com/seadent/cloudrootfs:v1.16.9-alpha.5`, + Use: "rmi", + Short: "rmi delete local image", + Example: `sealer rmi registry.cn-qingdao.aliyuncs.com/sealer/cloudrootfs:v1.16.9-alpha.5`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - logger.Error("enter the imageName") - os.Exit(1) - } if err := image.NewImageService().Delete(args[0]); err != nil { logger.Error(err) os.Exit(1) diff --git a/sealer/cmd/run.go b/sealer/cmd/run.go index bb8a11455b0..03574db4b9f 100644 --- a/sealer/cmd/run.go +++ b/sealer/cmd/run.go @@ -18,8 +18,8 @@ var runArgs *common.RunArgs var runCmd = &cobra.Command{ Use: "run", Short: "run a cluster with images and arguments", - Long: `sealer run registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.7 --masters [arg] --nodes [arg] -examples: + Long: `sealer run registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.7 --masters [arg] --nodes [arg]`, + Example: ` create default cluster: sealer run registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.7 @@ -30,11 +30,8 @@ create cluster to your baremetal server, appoint the iplist: sealer run registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.7 --masters 192.168.0.2,192.168.0.3,192.168.0.4 \ --nodes 192.168.0.5,192.168.0.6,192.168.0.7 `, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - logger.Error("cluster image name not found") - os.Exit(1) - } applier, err := apply.NewApplierFromArgs(args[0], runArgs) if err != nil { logger.Error(err) diff --git a/sealer/cmd/tag.go b/sealer/cmd/tag.go index eaeb46de7f5..1ad38e1872a 100644 --- a/sealer/cmd/tag.go +++ b/sealer/cmd/tag.go @@ -24,14 +24,11 @@ import ( ) var tagCmd = &cobra.Command{ - Use: "tag", - Short: "tag IMAGE[:TAG] TARGET_IMAGE[:TAG]", - Long: `sealer tag sealer/cloudrootfs:v1.16.9-alpha.6 registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.5`, + Use: "tag", + Short: "tag IMAGE[:TAG] TARGET_IMAGE[:TAG]", + Example: `sealer tag sealer/cloudrootfs:v1.16.9-alpha.6 registry.cn-qingdao.aliyuncs.com/sealer-io/cloudrootfs:v1.16.9-alpha.5`, + Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { - if len(args) < 2 { - logger.Error("Enter imageName and target imageName") - os.Exit(1) - } err := image.NewImageMetadataService().Tag(args[0], args[1]) if err != nil { logger.Error(err) diff --git a/sealer/cmd/version.go b/sealer/cmd/version.go index 4dac163a566..c7ac643db81 100644 --- a/sealer/cmd/version.go +++ b/sealer/cmd/version.go @@ -28,9 +28,9 @@ import ( var shortPrint bool var versionCmd = &cobra.Command{ - Use: "version", - Short: "version", - Long: `sealer version`, + Use: "version", + Short: "version", + Example: `sealer version`, Run: func(cmd *cobra.Command, args []string) { marshalled, err := json.Marshal(version.Get()) if err != nil {