cli

package
v0.6.0-beta.38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "sqlctl",
	Short: "SQL Channel CLI",
	Long: `
   _____ ____    __       ________                           __
  / ___// __ \  / /      / ____/ /_  ____ _____  ____  ___  / /
  \__ \/ / / / / /      / /   / __ \/ __ \/ __ \/ __ \/ _ \/ /
 ___/ / /_/ / / /___   / /___/ / / / /_/ / / / / / / /  __/ /
/____/\___\_\/_____/   \____/_/ /_/\__,_/_/ /_/_/ /_/\___/_/
									   
===============================
SQL Channel client`,
	Run: func(cmd *cobra.Command, _ []string) {
		if versionFlag {
			printVersion()
		} else {
			_ = cmd.Help()
		}
	},
}
View Source
var RunCmd = &cobra.Command{
	Use:   "run",
	Short: "Run sqlchannel and db service.",
	Example: `
sqlctl run  -- mysqld
  `,
	Args: cobra.MinimumNArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			fmt.Println("WARNING: no DB Service found.")
		}
		ctx, cancel := context.WithCancel(context.Background())

		commands, err := newCommands(ctx, &Options{
			HTTPPort:         port,
			GRPCPort:         grpcPort,
			ConfigFile:       configFile,
			Arguments:        args,
			LogLevel:         logLevel,
			ComponentsPath:   componentsPath,
			EnableAppHealth:  enableAppHealth,
			InternalGRPCPort: internalGRPCPort,
		})
		if err != nil {
			fmt.Fprint(os.Stderr, err.Error())
			os.Exit(1)
		}

		sigCh := make(chan os.Signal, 1)
		signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
		signal.Notify(commands.SigCh, syscall.SIGUSR1, syscall.SIGUSR2)

		go commands.StartSQLChannel()
		<-commands.SQLChannelStarted

		go commands.StartDBService()
		<-commands.AppStarted
		go commands.RestartDBServiceIfExited()

		if commands.AppCMD != nil {
			appCommand := strings.Join(args, " ")
			fmt.Fprintf(os.Stdout, "Start DB Service with %s.\n", appCommand)
			fmt.Fprintf(os.Stdout, "SQLChannel logs and DB logs will appear here.\n")
		} else {
			fmt.Fprintf(os.Stdout, "SQLChannel logs will appear here.\n")
		}

		sig := <-sigCh
		fmt.Printf("\n %v signal received: shutting down\n", sig)
		cancel()
		commands.WaitGroup.Wait()

		exitWithError := commands.StopSQLChannel() || commands.StopDBService()
		if exitWithError {
			os.Exit(1)
		}
	},
}
View Source
var SwitchCmd = &cobra.Command{
	Use:   "switchover",
	Short: "execute a switchover request.",
	Example: `
sqlctl switchover  --primary xxx --candidate xxx
  `,
	Args: cobra.MinimumNArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		var characterType string
		if characterType = os.Getenv("KB_SERVICE_CHARACTER_TYPE"); characterType == "" {
			fmt.Println("KB_SERVICE_CHARACTER_TYPE must be set")
			return
		}

		url := "http://" + sqlchannelAddr + "/v1.0/bindings/" + characterType
		if primary == "" && candidate == "" {
			fmt.Println("Primary or Candidate must be specified")
			return
		}

		payload := fmt.Sprintf(`{"operation": "switchover", "metadata": {"leader": "%s", "candidate": "%s"}}`, primary, candidate)

		client := http.Client{}

		req, err := http.NewRequest("POST", url, strings.NewReader(payload))
		if err != nil {
			fmt.Printf("New request error: %v", err)
		}

		resp, err := client.Do(req)
		if err != nil {
			fmt.Printf("Request SQLChannel error: %v", err)
			return
		}
		fmt.Println("SQLChannel Response:")
		bodyBytes, err := io.ReadAll(resp.Body)
		if err != nil {
			fmt.Printf("request error: %v", err)
		}
		fmt.Println(string(bodyBytes))
	},
}

Functions

func Execute

func Execute(cliVersion, apiVersion string)

Execute adds all child commands to the root command.

func GetRuntimeVersion

func GetRuntimeVersion() string

GetRuntimeVersion returns the version for the local sqlChannel runtime.

func Printf

func Printf(format string, args ...any)

Types

type Commands

type Commands struct {
	WaitGroup          sync.WaitGroup
	SQLChannelCMD      *exec.Cmd
	SQLChannelErr      error
	SQLChannelHTTPPort int
	SQLChannelGRPCPort int
	SQLChannelStarted  chan bool
	AppCMD             *exec.Cmd
	AppErr             error
	AppStarted         chan bool
	Options            *Options
	SigCh              chan os.Signal
	// contains filtered or unexported fields
}

Commands represents the managed subprocesses.

func (*Commands) RestartDBService

func (commands *Commands) RestartDBService()

func (*Commands) RestartDBServiceIfExited

func (commands *Commands) RestartDBServiceIfExited()

func (*Commands) StartDBService

func (commands *Commands) StartDBService()

func (*Commands) StartSQLChannel

func (commands *Commands) StartSQLChannel()

func (*Commands) StopDBService

func (commands *Commands) StopDBService() bool

func (*Commands) StopSQLChannel

func (commands *Commands) StopSQLChannel() bool

func (*Commands) WaitDBService

func (commands *Commands) WaitDBService()

func (*Commands) WaitSQLChannel

func (commands *Commands) WaitSQLChannel()

type Options

type Options struct {
	HTTPPort           int    `env:"DAPR_HTTP_PORT" arg:"dapr-http-port"`
	GRPCPort           int    `env:"DAPR_GRPC_PORT" arg:"dapr-grpc-port"`
	ConfigFile         string `arg:"config"`
	Protocol           string `arg:"app-protocol"`
	Arguments          []string
	LogLevel           string `arg:"log-level"`
	ComponentsPath     string `arg:"components-path"`
	InternalGRPCPort   int    `arg:"dapr-internal-grpc-port"`
	EnableAppHealth    bool   `arg:"enable-app-health-check"`
	AppHealthThreshold int    `arg:"app-health-threshold" ifneq:"0"`
}

Options represents the application configuration parameters.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL