2017-04-14 19:26:32 +02:00
|
|
|
package main
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-26 20:25:53 +02:00
|
|
|
"github.com/urfave/cli"
|
2017-04-26 20:21:18 +02:00
|
|
|
"github.com/zrepl/zrepl/rpc"
|
2017-04-26 20:25:53 +02:00
|
|
|
"github.com/zrepl/zrepl/sshbytestream"
|
|
|
|
"io"
|
2017-04-26 20:21:18 +02:00
|
|
|
)
|
|
|
|
|
2017-04-14 19:26:32 +02:00
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
2017-04-26 20:25:53 +02:00
|
|
|
ROLE_IPC Role = iota
|
2017-04-14 19:26:32 +02:00
|
|
|
ROLE_ACTION Role = iota
|
|
|
|
)
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
var conf Config
|
|
|
|
var handler Handler
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func main() {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app := cli.NewApp()
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app.Name = "zrepl"
|
|
|
|
app.Usage = "replicate zfs datasets"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "config"},
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
2017-04-26 20:25:53 +02:00
|
|
|
app.Before = func(c *cli.Context) (err error) {
|
2017-04-26 20:21:18 +02:00
|
|
|
if !c.GlobalIsSet("config") {
|
|
|
|
return errors.New("config flag not set")
|
|
|
|
}
|
|
|
|
if conf, err = ParseConfig(c.GlobalString("config")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler = Handler{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.Commands = []cli.Command{
|
2017-04-26 20:25:53 +02:00
|
|
|
{
|
|
|
|
Name: "sink",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "start in sink mode",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "identity"},
|
|
|
|
},
|
|
|
|
Action: doSink,
|
2017-04-26 20:21:18 +02:00
|
|
|
},
|
2017-04-26 20:25:53 +02:00
|
|
|
{
|
|
|
|
Name: "run",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "do replication",
|
|
|
|
Action: doRun,
|
|
|
|
},
|
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app.RunAndExitOnError()
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func doSink(c *cli.Context) (err error) {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
var sshByteStream io.ReadWriteCloser
|
|
|
|
if sshByteStream, err = sshbytestream.Incoming(); err != nil {
|
|
|
|
return
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
return rpc.ListenByteStreamRPC(sshByteStream, handler)
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func doRun(c *cli.Context) error {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
fmt.Printf("%#v", conf)
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
return nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|