2017-07-06 13:03:44 +02:00
|
|
|
// See cmd package.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-08 07:03:51 +02:00
|
|
|
"errors"
|
2018-08-27 19:10:55 +02:00
|
|
|
"github.com/spf13/cobra"
|
2018-08-29 22:06:24 +02:00
|
|
|
"github.com/zrepl/zrepl/client"
|
2018-08-27 22:21:45 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-08-27 19:10:55 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon"
|
2018-08-27 22:21:45 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2018-09-05 02:27:20 +02:00
|
|
|
"fmt"
|
2017-07-06 13:03:44 +02:00
|
|
|
)
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "zrepl",
|
|
|
|
Short: "ZFS dataset replication",
|
|
|
|
Long: `Replicate ZFS filesystems & volumes between pools:
|
|
|
|
|
|
|
|
- push & pull mode
|
|
|
|
- automatic snapshot creation & pruning
|
|
|
|
- local / over the network
|
|
|
|
- ACLs instead of blank SSH access`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var daemonCmd = &cobra.Command{
|
2018-08-27 22:21:45 +02:00
|
|
|
Use: "daemon",
|
2018-08-27 19:10:55 +02:00
|
|
|
Short: "daemon",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return daemon.Run(conf)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
var wakeupCmd = &cobra.Command{
|
|
|
|
Use: "wakeup",
|
|
|
|
Short: "wake up jobs",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-29 19:18:54 +02:00
|
|
|
return client.RunWakeup(conf, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-23 14:44:53 +02:00
|
|
|
var statusCmdFlags client.StatusFlags
|
|
|
|
|
2018-08-29 19:18:54 +02:00
|
|
|
var statusCmd = &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "status",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-23 14:44:53 +02:00
|
|
|
return client.RunStatus(statusCmdFlags, conf, args)
|
2018-08-27 22:21:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
var stdinserverCmd = &cobra.Command{
|
|
|
|
Use: "stdinserver CLIENT_IDENTITY",
|
|
|
|
Short: "start in stdinserver mode (from authorized_keys file)",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.RunStdinserver(conf, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-05 02:27:20 +02:00
|
|
|
|
|
|
|
var bashcompCmd = &cobra.Command{
|
|
|
|
Use: "bashcomp path/to/out/file",
|
|
|
|
Short: "generate bash completions",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
fmt.Fprintf(os.Stderr, "specify exactly one positional agument\n")
|
|
|
|
cmd.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := rootCmd.GenBashCompletionFile(args[0]); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error generating bash completion: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:32:38 +02:00
|
|
|
var configcheckCmd = &cobra.Command{
|
|
|
|
Use: "configcheck",
|
|
|
|
Short: "validate config file",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.RunConfigcheck(conf, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:52:11 +02:00
|
|
|
var versionCmdArgs client.VersionArgs
|
|
|
|
var versionCmd = &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "print version of zrepl binary (for running daemon 'zrepl control version' command)",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err == nil {
|
|
|
|
versionCmdArgs.Config = conf
|
|
|
|
}
|
|
|
|
client.RunVersion(versionCmdArgs)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-07 09:04:03 +02:00
|
|
|
var pprofCmd = &cobra.Command{
|
|
|
|
Use: "pprof off | [on TCP_LISTEN_ADDRESS]",
|
|
|
|
Short: "start a http server exposing go-tool-compatible profiling endpoints at TCP_LISTEN_ADDRESS",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
conf, err := config.ParseConfig(rootArgs.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pprofCmdArgs client.PProfArgs
|
|
|
|
if cmd.Flags().NArg() < 1 {
|
|
|
|
goto enargs
|
|
|
|
}
|
|
|
|
switch cmd.Flags().Arg(0) {
|
|
|
|
case "on":
|
|
|
|
pprofCmdArgs.Run = true
|
|
|
|
if cmd.Flags().NArg() != 2 {
|
|
|
|
return errors.New("must specify TCP_LISTEN_ADDRESS as second positional argument")
|
|
|
|
}
|
|
|
|
pprofCmdArgs.HttpListenAddress = cmd.Flags().Arg(1)
|
|
|
|
case "off":
|
|
|
|
if cmd.Flags().NArg() != 1 {
|
|
|
|
goto enargs
|
|
|
|
}
|
|
|
|
pprofCmdArgs.Run = false
|
|
|
|
}
|
|
|
|
|
|
|
|
client.RunPProf(conf, pprofCmdArgs)
|
|
|
|
return nil
|
|
|
|
enargs:
|
|
|
|
return errors.New("invalid number of positional arguments")
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
var rootArgs struct {
|
|
|
|
configFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
//cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().StringVar(&rootArgs.configFile, "config", "", "config file path")
|
|
|
|
rootCmd.AddCommand(daemonCmd)
|
2018-08-27 22:21:45 +02:00
|
|
|
rootCmd.AddCommand(wakeupCmd)
|
2018-09-23 14:44:53 +02:00
|
|
|
statusCmd.Flags().BoolVar(&statusCmdFlags.Raw, "raw", false, "dump raw response from zrepl daemon")
|
2018-08-29 19:18:54 +02:00
|
|
|
rootCmd.AddCommand(statusCmd)
|
2018-09-05 01:41:54 +02:00
|
|
|
rootCmd.AddCommand(stdinserverCmd)
|
2018-09-05 02:27:20 +02:00
|
|
|
rootCmd.AddCommand(bashcompCmd)
|
2018-09-05 17:32:38 +02:00
|
|
|
rootCmd.AddCommand(configcheckCmd)
|
2018-09-07 08:52:11 +02:00
|
|
|
versionCmd.Flags().StringVar(&versionCmdArgs.Show, "show", "", "version info to show (client|daemon)")
|
|
|
|
rootCmd.AddCommand(versionCmd)
|
2018-09-07 09:04:03 +02:00
|
|
|
rootCmd.AddCommand(pprofCmd)
|
2018-08-27 19:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 13:03:44 +02:00
|
|
|
func main() {
|
2018-08-27 19:10:55 +02:00
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2017-07-06 13:03:44 +02:00
|
|
|
log.Printf("error executing root command: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|