diff --git a/cmd/init.go b/cmd/init.go index a123679cb..ad2505858 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -18,7 +18,6 @@ import ( "fmt" "github.com/fanux/sealos/install" "github.com/spf13/cobra" - "github.com/wonderivan/logger" "os" ) @@ -45,23 +44,22 @@ var contact = ` var initCmd = &cobra.Command{ Use: "init", Short: "Simplest way to init your kubernets HA cluster", - Long: `sealos init --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 --node 192.168.0.5 --user root --passwd your-server-password`, + Long: `sealos init --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \ + --node 192.168.0.5 --user root --passwd your-server-password \ + --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz`, Run: func(cmd *cobra.Command, args []string) { c := &install.SealConfig{} + // 没有重大错误可以直接保存配置. 但是apiservercertsans为空. 但是不影响用户 clean + c.Dump("") install.BuildInit() + // 安装完成后生成完整版 c.Dump("") fmt.Println(contact) }, PreRun: func(cmd *cobra.Command, args []string) { - if len(install.MasterIPs) == 0 { - logger.Error("this command is init feature,master can't is empty.please check your command is ok?") + if install.ExitInitCase() { cmd.Help() - os.Exit(0) - } - if install.PkgUrl == "" { - logger.Error("your pkg-url is empty,please check your command is ok?") - cmd.Help() - os.Exit(0) + os.Exit(install.ErrorExitOSCase) } }, } @@ -82,7 +80,7 @@ func init() { initCmd.Flags().StringSliceVar(&install.NodeIPs, "node", []string{}, "kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5") initCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file location ex. /root/kube1.14.1.tar.gz") - initCmd.Flags().StringVar(&install.Version, "version", "v1.14.1", "version is kubernetes version") + initCmd.Flags().StringVar(&install.Version, "version", "", "version is kubernetes version") initCmd.Flags().StringVar(&install.Repo, "repo", "k8s.gcr.io", "choose a container registry to pull control plane images from") initCmd.Flags().StringVar(&install.PodCIDR, "podcidr", "100.64.0.0/10", "Specify range of IP addresses for the pod network") initCmd.Flags().StringVar(&install.SvcCIDR, "svccidr", "10.96.0.0/12", "Use alternative range of IP address for service VIPs") diff --git a/install/utils.go b/install/utils.go index 2d4f9e6e5..12654c018 100644 --- a/install/utils.go +++ b/install/utils.go @@ -13,6 +13,44 @@ import ( "time" ) +const ( + + ErrorExitOSCase = -1 // 错误直接退出类型 + + ErrorMasterEmpty = "your master is empty." // master节点ip为空 + ErrorVersionEmpty= "your kubernetes version is empty." // kubernetes 版本号为空 + ErrorPkgUrlEmpty = "your package url is empty." // 离线安装包为空 + //ErrorMessageSSHConfigEmpty = "your ssh password or private-key is empty." // ssh 密码/秘钥为空 + // ErrorMessageCommon // 其他错误消息 +) + +var message string + +// ExitOSCase is +func ExitInitCase() bool { + // 重大错误直接退出, 不保存配置文件 + if len(MasterIPs) == 0 { + message = ErrorMasterEmpty + } + if PkgUrl == "" { + message += ErrorPkgUrlEmpty + } + if Version == "" { + message += ErrorVersionEmpty + } + // 用户不写 --passwd, 默认走pk, 秘钥如果没有配置ssh互信, 则验证ssh的时候报错. 应该属于preRun里面 + // first to auth password, second auth pk. + // 如果初始状态都没写, 默认都为空. 报这个错 + //if SSHConfig.Password == "" && SSHConfig.PkFile == "" { + // message += ErrorMessageSSHConfigEmpty + //} + if message != "" { + logger.Error(message + "please check your command is ok?") + return true + } + return false +} + //VersionToInt v1.15.6 => 115 func VersionToInt(version string) int { // v1.15.6 => 1.15.6