github.com/openshift/installer@v1.4.17/cmd/node-joiner/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  	terminal "golang.org/x/term"
    11  
    12  	"github.com/openshift/installer/cmd/openshift-install/command"
    13  	"github.com/openshift/installer/pkg/nodejoiner"
    14  )
    15  
    16  func main() {
    17  	nodesAddCmd := &cobra.Command{
    18  		Use:   "add-nodes",
    19  		Short: "Generates an ISO that could be used to boot the configured nodes to let them join an existing cluster",
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			kubeConfig, err := cmd.Flags().GetString("kubeconfig")
    22  			if err != nil {
    23  				return err
    24  			}
    25  			dir, err := cmd.Flags().GetString("dir")
    26  			if err != nil {
    27  				return err
    28  			}
    29  			return nodejoiner.NewAddNodesCommand(dir, kubeConfig)
    30  		},
    31  	}
    32  
    33  	nodesMonitorCmd := &cobra.Command{
    34  		Use:   "monitor-add-nodes",
    35  		Short: "Monitors the configured nodes while they are joining an existing cluster",
    36  		RunE: func(cmd *cobra.Command, args []string) error {
    37  			dir, err := cmd.Flags().GetString("dir")
    38  			if err != nil {
    39  				return err
    40  			}
    41  
    42  			kubeConfig, err := cmd.Flags().GetString("kubeconfig")
    43  			if err != nil {
    44  				return err
    45  			}
    46  
    47  			ips := args
    48  			logrus.Infof("Monitoring IPs: %v", ips)
    49  			if len(ips) == 0 {
    50  				logrus.Fatal("At least one IP address must be specified")
    51  			}
    52  			return nodejoiner.NewMonitorAddNodesCommand(dir, kubeConfig, ips)
    53  		},
    54  	}
    55  
    56  	rootCmd := &cobra.Command{
    57  		Use:              "node-joiner",
    58  		PersistentPreRun: runRootCmd,
    59  	}
    60  	rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file.")
    61  	rootCmd.PersistentFlags().String("dir", ".", "assets directory")
    62  	rootCmd.PersistentFlags().String("log-level", "info", "log level (e.g. \"debug | info | warn | error\")")
    63  
    64  	rootCmd.AddCommand(nodesAddCmd)
    65  	rootCmd.AddCommand(nodesMonitorCmd)
    66  	if err := rootCmd.Execute(); err != nil {
    67  		logrus.Fatal(err)
    68  	}
    69  }
    70  
    71  func runRootCmd(cmd *cobra.Command, args []string) {
    72  	logrus.SetOutput(io.Discard)
    73  	logrus.SetLevel(logrus.TraceLevel)
    74  
    75  	logLevel, err := cmd.Flags().GetString("log-level")
    76  	if err != nil {
    77  		logrus.Fatal(err)
    78  	}
    79  
    80  	level, err := logrus.ParseLevel(logLevel)
    81  	if err != nil {
    82  		level = logrus.InfoLevel
    83  	}
    84  
    85  	logrus.AddHook(command.NewFileHookWithNewlineTruncate(os.Stderr, level, &logrus.TextFormatter{
    86  		// Setting ForceColors is necessary because logrus.TextFormatter determines
    87  		// whether or not to enable colors by looking at the output of the logger.
    88  		// In this case, the output is io.Discard, which is not a terminal.
    89  		// Overriding it here allows the same check to be done, but against the
    90  		// hook's output instead of the logger's output.
    91  		ForceColors:            terminal.IsTerminal(int(os.Stderr.Fd())),
    92  		DisableLevelTruncation: true,
    93  		DisableTimestamp:       false,
    94  		FullTimestamp:          true,
    95  		DisableQuote:           true,
    96  	}))
    97  
    98  	if err != nil {
    99  		logrus.Fatal(fmt.Errorf("invalid log-level: %w", err))
   100  	}
   101  }