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

     1  /*
     2  Copyright © 2022 Alibaba Group Holding Ltd.
     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 cmd
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/alibaba/sealer/apply/processor"
    23  	"github.com/alibaba/sealer/pkg/cert"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  var flag *processor.ParserArg
    28  
    29  // genCmd represents the gen command
    30  var genCmd = &cobra.Command{
    31  	Use:   "gen",
    32  	Short: "Generate a Clusterfile to take over a normal cluster which not deployed by sealer",
    33  	Long: `sealer gen --passwd xxxx --image kubernetes:v1.19.8
    34  
    35  The takeover actually is to generate a Clusterfile by kubeconfig.
    36  Sealer will call kubernetes API to get masters and nodes IP info, then generate a Clusterfile.
    37  Also sealer will pull a CloudImage which matches the kubernetes version.
    38  
    39  Check generated Clusterfile: 'cat .sealer/<cluster name>/Clusterfile'
    40  
    41  The master should has 'node-role.kubernetes.io/master' label.
    42  
    43  Then you can use any sealer command to manage the cluster like:
    44  
    45  > Upgrade cluster
    46  	sealer upgrade --image kubernetes:v1.22.0
    47  
    48  > Scale
    49  	sealer join --node x.x.x.x
    50  
    51  > Deploy a CloudImage into the cluster
    52  	sealer run mysql-cluster:5.8`,
    53  	RunE: func(cmd *cobra.Command, args []string) error {
    54  		if flag.Passwd == "" || flag.Image == "" {
    55  			return fmt.Errorf("empty password or image name")
    56  		}
    57  		cluster, err := processor.GenerateCluster(flag)
    58  		if err != nil {
    59  			return err
    60  		}
    61  		genProcessor, err := processor.NewGenerateProcessor()
    62  		if err != nil {
    63  			return err
    64  		}
    65  		return processor.NewExecutor(genProcessor).Execute(cluster)
    66  	},
    67  }
    68  
    69  func init() {
    70  	flag = &processor.ParserArg{}
    71  	rootCmd.AddCommand(genCmd)
    72  	genCmd.Flags().Uint16Var(&flag.Port, "port", 22, "set the sshd service port number for the server (default port: 22)")
    73  	genCmd.Flags().StringVar(&flag.Pk, "pk", cert.GetUserHomeDir()+"/.ssh/id_rsa", "set server private key")
    74  	genCmd.Flags().StringVar(&flag.PkPassword, "pk-passwd", "", "set server private key password")
    75  	genCmd.Flags().StringVar(&flag.Image, "image", "", "Set taken over cloud image")
    76  	genCmd.Flags().StringVar(&flag.Name, "name", "default", "Set taken over cluster name")
    77  	genCmd.Flags().StringVar(&flag.Passwd, "passwd", "", "Set taken over ssh passwd")
    78  }