2017-07-06 13:03:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zrepl/zrepl/rpc"
|
|
|
|
"github.com/zrepl/zrepl/sshbytestream"
|
|
|
|
"io"
|
|
|
|
golog "log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var StdinserverCmd = &cobra.Command{
|
2017-07-06 15:36:53 +02:00
|
|
|
Use: "stdinserver CLIENT_IDENTITY",
|
2017-07-06 13:03:44 +02:00
|
|
|
Short: "start in stdin server mode (from authorized_keys file)",
|
|
|
|
Run: cmdStdinServer,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(StdinserverCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdStdinServer(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("stdinserver exiting with error: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-07-06 15:36:53 +02:00
|
|
|
if len(args) != 1 || args[0] == "" {
|
|
|
|
err = fmt.Errorf("must specify client identity as positional argument")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
identity := args[0]
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
pullACL, ok := conf.PullACLs[identity]
|
|
|
|
if !ok {
|
2017-07-06 15:36:53 +02:00
|
|
|
err = fmt.Errorf("could not find PullACL for identity '%s'", identity)
|
2017-07-06 13:03:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sshByteStream io.ReadWriteCloser
|
|
|
|
if sshByteStream, err = sshbytestream.Incoming(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
sinkMapping := func(identity string) (m DatasetMapping, err error) {
|
|
|
|
sink, ok := conf.Sinks[identity]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not find sink for identity '%s'", identity)
|
2017-07-06 13:03:44 +02:00
|
|
|
}
|
2017-08-05 21:15:37 +02:00
|
|
|
return sink, nil
|
2017-07-06 13:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sinkLogger := golog.New(logOut, fmt.Sprintf("sink[%s] ", identity), logFlags)
|
|
|
|
handler := Handler{
|
|
|
|
Logger: sinkLogger,
|
|
|
|
SinkMappingFunc: sinkMapping,
|
2017-08-05 21:15:37 +02:00
|
|
|
PullACL: pullACL,
|
2017-07-06 13:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = rpc.ListenByteStreamRPC(sshByteStream, identity, handler, sinkLogger); err != nil {
|
|
|
|
log.Printf("listenbytestreamerror: %#v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|