github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/exec.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  	"github.com/alibaba/sealer/pkg/exec"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  var roles string
    23  
    24  // execCmd represents the exec command
    25  var execCmd = &cobra.Command{
    26  	Use:   "exec",
    27  	Short: "exec a shell command or script on all node.",
    28  	Example: `
    29  exec to default cluster: my-cluster
    30  	sealer exec "cat /etc/hosts"
    31  specify the cluster name(If there is only one cluster in the $HOME/.sealer directory, it should be applied. ):
    32      sealer exec -c my-cluster "cat /etc/hosts"
    33  set role label to exec cmd:
    34      sealer exec -c my-cluster -r master,slave,node1 "cat /etc/hosts"		
    35  `,
    36  	Args: cobra.ExactArgs(1),
    37  	RunE: func(cmd *cobra.Command, args []string) error {
    38  		execCmd, err := exec.NewExecCmd(clusterName, roles)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		return execCmd.RunCmd(args[0])
    43  	},
    44  }
    45  
    46  func init() {
    47  	rootCmd.AddCommand(execCmd)
    48  	execCmd.Flags().StringVarP(&clusterName, "cluster-name", "c", "", "submit one cluster name")
    49  	execCmd.Flags().StringVarP(&roles, "roles", "r", "", "set role label to roles")
    50  }