2017-04-26 18:36:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-26 20:25:53 +02:00
|
|
|
"errors"
|
2017-04-29 19:07:47 +02:00
|
|
|
"fmt"
|
2017-04-29 20:10:09 +02:00
|
|
|
"github.com/jinzhu/copier"
|
2017-04-26 18:36:01 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-04-29 20:10:09 +02:00
|
|
|
"github.com/zrepl/zrepl/rpc"
|
|
|
|
"github.com/zrepl/zrepl/sshbytestream"
|
2017-04-26 18:36:01 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2017-04-26 20:25:53 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-04-29 20:10:09 +02:00
|
|
|
"io"
|
2017-04-26 20:25:53 +02:00
|
|
|
"io/ioutil"
|
2017-04-26 18:36:01 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pool struct {
|
2017-04-29 20:10:09 +02:00
|
|
|
Name string
|
|
|
|
Transport Transport
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
2017-04-29 20:10:09 +02:00
|
|
|
|
|
|
|
type Transport interface {
|
|
|
|
Connect() (rpc.RPCRequester, error)
|
|
|
|
}
|
|
|
|
type LocalTransport struct {
|
|
|
|
Pool string
|
|
|
|
}
|
|
|
|
type SSHTransport struct {
|
|
|
|
ZreplIdentity string
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Port uint16
|
|
|
|
TransportOpenCommand []string
|
|
|
|
Options []string
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:36:01 +02:00
|
|
|
type Push struct {
|
2017-04-29 19:07:47 +02:00
|
|
|
To *Pool
|
2017-04-26 18:36:01 +02:00
|
|
|
Datasets []zfs.DatasetPath
|
|
|
|
}
|
|
|
|
type Pull struct {
|
2017-04-29 19:07:47 +02:00
|
|
|
From *Pool
|
2017-04-26 20:25:53 +02:00
|
|
|
Mapping zfs.DatasetMapping
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
type Sink struct {
|
2017-04-26 20:25:53 +02:00
|
|
|
From string
|
|
|
|
Mapping zfs.DatasetMapping
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Pools []Pool
|
|
|
|
Pushs []Push
|
|
|
|
Pulls []Pull
|
|
|
|
Sinks []Sink
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseConfig(path string) (config Config, err error) {
|
|
|
|
|
|
|
|
c := make(map[string]interface{}, 0)
|
|
|
|
|
|
|
|
var bytes []byte
|
|
|
|
|
|
|
|
if bytes, err = ioutil.ReadFile(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = yaml.Unmarshal(bytes, &c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseMain(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMain(root map[string]interface{}) (c Config, err error) {
|
|
|
|
if c.Pools, err = parsePools(root["pools"]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-29 19:07:47 +02:00
|
|
|
|
|
|
|
poolLookup := func(name string) (*Pool, error) {
|
|
|
|
for _, pool := range c.Pools {
|
|
|
|
if pool.Name == name {
|
|
|
|
return &pool, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New(fmt.Sprintf("pool '%s' not defined", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Pushs, err = parsePushs(root["pushs"], poolLookup); err != nil {
|
2017-04-26 18:36:01 +02:00
|
|
|
return
|
|
|
|
}
|
2017-04-29 19:07:47 +02:00
|
|
|
if c.Pulls, err = parsePulls(root["pulls"], poolLookup); err != nil {
|
2017-04-26 18:36:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.Sinks, err = parseSinks(root["sinks"]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 20:10:09 +02:00
|
|
|
func parsePools(v interface{}) (pools []Pool, err error) {
|
|
|
|
|
|
|
|
asList := make([]struct {
|
|
|
|
Name string
|
|
|
|
Transport map[string]interface{}
|
|
|
|
}, 0)
|
|
|
|
if err = mapstructure.Decode(v, &asList); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pools = make([]Pool, len(asList))
|
|
|
|
for i, p := range asList {
|
|
|
|
var transport Transport
|
|
|
|
if transport, err = parseTransport(p.Transport); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pools[i] = Pool{
|
|
|
|
Name: p.Name,
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:36:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 20:10:09 +02:00
|
|
|
func parseTransport(it map[string]interface{}) (t Transport, err error) {
|
|
|
|
|
|
|
|
if len(it) != 1 {
|
|
|
|
err = errors.New("ambiguous transport type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range it {
|
|
|
|
switch key {
|
|
|
|
case "ssh":
|
|
|
|
t := SSHTransport{}
|
|
|
|
if err = mapstructure.Decode(val, &t); err != nil {
|
|
|
|
err = errors.New(fmt.Sprintf("could not parse ssh transport: %s", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
case "local":
|
|
|
|
t := LocalTransport{}
|
|
|
|
if err = mapstructure.Decode(val, &t); err != nil {
|
|
|
|
err = errors.New(fmt.Sprintf("could not parse local transport: %s", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New(fmt.Sprintf("unknown transport type '%s'\n", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return // unreachable
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:07:47 +02:00
|
|
|
type poolLookup func(name string) (*Pool, error)
|
|
|
|
|
|
|
|
func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) {
|
2017-04-26 18:36:01 +02:00
|
|
|
|
2017-04-26 20:25:53 +02:00
|
|
|
asList := make([]struct {
|
|
|
|
To string
|
2017-04-26 18:36:01 +02:00
|
|
|
Datasets []string
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
if err = mapstructure.Decode(v, &asList); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p = make([]Push, len(asList))
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
for i, e := range asList {
|
2017-04-29 19:07:47 +02:00
|
|
|
|
|
|
|
var toPool *Pool
|
|
|
|
if toPool, err = pl(e.To); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-26 18:36:01 +02:00
|
|
|
push := Push{
|
2017-04-29 19:07:47 +02:00
|
|
|
To: toPool,
|
2017-04-26 18:36:01 +02:00
|
|
|
Datasets: make([]zfs.DatasetPath, len(e.Datasets)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ds := range e.Datasets {
|
|
|
|
if push.Datasets[i], err = zfs.NewDatasetPath(ds); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
p[i] = push
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:07:47 +02:00
|
|
|
func parsePulls(v interface{}, pl poolLookup) (p []Pull, err error) {
|
2017-04-26 18:36:01 +02:00
|
|
|
|
2017-04-26 20:25:53 +02:00
|
|
|
asList := make([]struct {
|
|
|
|
From string
|
2017-04-26 18:36:01 +02:00
|
|
|
Mapping map[string]string
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
if err = mapstructure.Decode(v, &asList); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p = make([]Pull, len(asList))
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
for i, e := range asList {
|
2017-04-29 19:07:47 +02:00
|
|
|
|
|
|
|
var fromPool *Pool
|
|
|
|
if fromPool, err = pl(e.From); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-26 18:36:01 +02:00
|
|
|
pull := Pull{
|
2017-04-29 19:07:47 +02:00
|
|
|
From: fromPool,
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
if pull.Mapping, err = parseComboMapping(e.Mapping); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-29 18:26:43 +02:00
|
|
|
p[i] = pull
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
func parseSinks(v interface{}) (sinks []Sink, err error) {
|
2017-04-26 18:36:01 +02:00
|
|
|
|
|
|
|
var asList []interface{}
|
|
|
|
var ok bool
|
2017-04-26 20:25:53 +02:00
|
|
|
if asList, ok = v.([]interface{}); !ok {
|
2017-04-26 18:36:01 +02:00
|
|
|
return nil, errors.New("expected list")
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
sinks = make([]Sink, len(asList))
|
2017-04-26 18:36:01 +02:00
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
for i, s := range asList {
|
2017-04-26 18:36:01 +02:00
|
|
|
var sink Sink
|
2017-04-29 18:26:43 +02:00
|
|
|
if sink, err = parseSink(s); err != nil {
|
2017-04-26 18:36:01 +02:00
|
|
|
return
|
|
|
|
}
|
2017-04-29 18:26:43 +02:00
|
|
|
sinks[i] = sink
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSink(v interface{}) (s Sink, err error) {
|
|
|
|
t := struct {
|
2017-04-26 20:25:53 +02:00
|
|
|
From string
|
2017-04-26 18:36:01 +02:00
|
|
|
Mapping map[string]string
|
|
|
|
}{}
|
|
|
|
if err = mapstructure.Decode(v, &t); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.From = t.From
|
|
|
|
s.Mapping, err = parseComboMapping(t.Mapping)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseComboMapping(m map[string]string) (c zfs.ComboMapping, err error) {
|
|
|
|
|
|
|
|
c.Mappings = make([]zfs.DatasetMapping, len(m))
|
|
|
|
|
2017-04-26 20:25:53 +02:00
|
|
|
for lhs, rhs := range m {
|
2017-04-26 18:36:01 +02:00
|
|
|
|
|
|
|
if lhs[0] == '|' {
|
|
|
|
|
|
|
|
if len(m) != 1 {
|
|
|
|
err = errors.New("non-recursive mapping must be the only mapping for a sink")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := zfs.DirectMapping{
|
2017-04-26 20:25:53 +02:00
|
|
|
Source: nil,
|
2017-04-26 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.Target, err = zfs.NewDatasetPath(rhs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Mappings = append(c.Mappings, m)
|
|
|
|
|
|
|
|
} else if lhs[0] == '*' {
|
|
|
|
|
|
|
|
m := zfs.ExecMapping{}
|
|
|
|
fields := strings.Fields(strings.TrimPrefix(rhs, "!"))
|
|
|
|
if len(fields) < 1 {
|
|
|
|
err = errors.New("ExecMapping without acceptor path")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Name = fields[0]
|
|
|
|
m.Args = fields[1:]
|
|
|
|
|
|
|
|
c.Mappings = append(c.Mappings, m)
|
|
|
|
|
|
|
|
} else if strings.HasSuffix(lhs, "*") {
|
|
|
|
|
|
|
|
m := zfs.GlobMapping{}
|
|
|
|
|
|
|
|
m.PrefixPath, err = zfs.NewDatasetPath(strings.TrimSuffix(lhs, "*"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.TargetRoot, err = zfs.NewDatasetPath(rhs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Mappings = append(c.Mappings, m)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
2017-04-29 20:10:09 +02:00
|
|
|
|
|
|
|
func (t SSHTransport) Connect() (r rpc.RPCRequester, err error) {
|
|
|
|
var stream io.ReadWriteCloser
|
|
|
|
var rpcTransport sshbytestream.SSHTransport
|
|
|
|
copier.Copy(rpcTransport, t)
|
|
|
|
if stream, err = sshbytestream.Outgoing(rpcTransport); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return rpc.ConnectByteStreamRPC(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t LocalTransport) Connect() (r rpc.RPCRequester, err error) {
|
|
|
|
// TODO ugly hidden global variable reference
|
|
|
|
return rpc.ConnectLocalRPC(handler), nil
|
|
|
|
}
|