github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/ctl/run.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package ctl
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"os"
    26  	"os/signal"
    27  	"strings"
    28  	"syscall"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var (
    34  	configFile       string
    35  	port             int
    36  	grpcPort         int
    37  	internalGRPCPort int
    38  	logLevel         string
    39  	componentsPath   string
    40  	enableAppHealth  bool
    41  )
    42  
    43  var RunCmd = &cobra.Command{
    44  	Use:   "run",
    45  	Short: "Run Lorry and db service.",
    46  	Example: `
    47  lorryctl run  -- mysqld
    48    `,
    49  	Args: cobra.MinimumNArgs(0),
    50  	Run: func(cmd *cobra.Command, args []string) {
    51  		if len(args) == 0 {
    52  			fmt.Println("WARNING: no DB Service found.")
    53  		}
    54  		ctx, cancel := context.WithCancel(context.Background())
    55  
    56  		commands, err := newCommands(ctx, &Options{
    57  			HTTPPort:         port,
    58  			GRPCPort:         grpcPort,
    59  			ConfigFile:       configFile,
    60  			Arguments:        args,
    61  			LogLevel:         logLevel,
    62  			ComponentsPath:   componentsPath,
    63  			EnableAppHealth:  enableAppHealth,
    64  			InternalGRPCPort: internalGRPCPort,
    65  		})
    66  		if err != nil {
    67  			fmt.Fprint(os.Stderr, err.Error())
    68  			os.Exit(1)
    69  		}
    70  
    71  		sigCh := make(chan os.Signal, 1)
    72  		signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
    73  
    74  		go commands.StartLorry()
    75  		<-commands.LorryStarted
    76  
    77  		go commands.StartDBService()
    78  		<-commands.AppStarted
    79  		go commands.RestartDBServiceIfExited()
    80  
    81  		if commands.AppCMD != nil {
    82  			appCommand := strings.Join(args, " ")
    83  			fmt.Fprintf(os.Stdout, "Start DB Service with %s.\n", appCommand)
    84  			fmt.Fprintf(os.Stdout, "Lorry logs and DB logs will appear here.\n")
    85  		} else {
    86  			fmt.Fprintf(os.Stdout, "Lorry logs will appear here.\n")
    87  		}
    88  
    89  		sig := <-sigCh
    90  		fmt.Printf("\n %v signal received: shutting down\n", sig)
    91  		cancel()
    92  		commands.WaitGroup.Wait()
    93  
    94  		exitWithError := commands.StopLorry() || commands.StopDBService()
    95  		if exitWithError {
    96  			os.Exit(1)
    97  		}
    98  	},
    99  }
   100  
   101  func init() {
   102  	RunCmd.Flags().StringVarP(&configFile, "config", "c", "/kubeblocks/config/probe/config.yaml", "Dapr configuration file")
   103  	RunCmd.Flags().IntVarP(&port, "dapr-http-port", "H", -1, "The HTTP port for Dapr to listen on")
   104  	RunCmd.Flags().IntVarP(&grpcPort, "dapr-grpc-port", "G", -1, "The gRPC port for Dapr to listen on")
   105  	RunCmd.Flags().IntVarP(&internalGRPCPort, "dapr-internal-grpc-port", "I", 56471, "The gRPC port for the Dapr internal API to listen on")
   106  	RunCmd.Flags().StringVarP(&logLevel, "log-level", "", "info", "The log verbosity. Valid values are: debug, info, warn, error, fatal, or panic")
   107  	RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", "/kubeblocks/config/probe/components", "The path for components directory")
   108  	RunCmd.Flags().BoolP("help", "h", false, "Print this help message")
   109  
   110  	RootCmd.AddCommand(RunCmd)
   111  }