2018-08-26 15:17:15 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
2018-08-26 16:44:34 +02:00
|
|
|
"github.com/zrepl/yaml-config"
|
2018-08-26 15:17:15 +02:00
|
|
|
"io/ioutil"
|
2018-08-26 16:44:34 +02:00
|
|
|
"os"
|
2018-08-31 21:51:44 +02:00
|
|
|
"reflect"
|
2018-08-26 19:20:08 +02:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2018-08-26 16:44:34 +02:00
|
|
|
"time"
|
2018-08-26 15:17:15 +02:00
|
|
|
)
|
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
type Config struct {
|
|
|
|
Jobs []JobEnum `yaml:"jobs"`
|
2018-08-31 21:51:44 +02:00
|
|
|
Global *Global `yaml:"global,optional,fromdefaults"`
|
2018-08-26 16:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 15:53:52 +02:00
|
|
|
func (c *Config) Job(name string) (*JobEnum, error) {
|
|
|
|
for _, j := range c.Jobs {
|
|
|
|
if j.Name() == name {
|
|
|
|
return &j, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("job %q not defined in config", name)
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
type JobEnum struct {
|
2018-08-26 15:17:15 +02:00
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
2018-10-13 15:53:52 +02:00
|
|
|
func (j JobEnum) Name() string {
|
|
|
|
var name string
|
|
|
|
switch v := j.Ret.(type) {
|
|
|
|
case *PushJob: name = v.Name
|
|
|
|
case *SinkJob: name = v.Name
|
|
|
|
case *PullJob: name = v.Name
|
|
|
|
case *SourceJob: name = v.Name
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknownn job type %T", v))
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2018-09-23 23:04:31 +02:00
|
|
|
type ActiveJob struct {
|
2018-08-26 23:11:50 +02:00
|
|
|
Type string `yaml:"type"`
|
|
|
|
Name string `yaml:"name"`
|
2018-09-04 23:44:45 +02:00
|
|
|
Connect ConnectEnum `yaml:"connect"`
|
2018-08-26 23:11:50 +02:00
|
|
|
Pruning PruningSenderReceiver `yaml:"pruning"`
|
2018-08-27 15:18:08 +02:00
|
|
|
Debug JobDebugSettings `yaml:"debug,optional"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 23:04:31 +02:00
|
|
|
type PassiveJob struct {
|
2018-08-27 15:18:08 +02:00
|
|
|
Type string `yaml:"type"`
|
|
|
|
Name string `yaml:"name"`
|
2018-09-04 23:44:45 +02:00
|
|
|
Serve ServeEnum `yaml:"serve"`
|
2018-08-27 15:18:08 +02:00
|
|
|
Debug JobDebugSettings `yaml:"debug,optional"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 23:04:31 +02:00
|
|
|
type PushJob struct {
|
|
|
|
ActiveJob `yaml:",inline"`
|
2018-10-11 15:22:52 +02:00
|
|
|
Snapshotting SnapshottingEnum `yaml:"snapshotting"`
|
2018-09-23 23:04:31 +02:00
|
|
|
Filesystems FilesystemsFilter `yaml:"filesystems"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 23:11:50 +02:00
|
|
|
type PullJob struct {
|
2018-09-23 23:04:31 +02:00
|
|
|
ActiveJob `yaml:",inline"`
|
2018-10-11 18:00:23 +02:00
|
|
|
RootFS string `yaml:"root_fs"`
|
|
|
|
Interval time.Duration `yaml:"interval,positive"`
|
2018-09-23 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type SinkJob struct {
|
|
|
|
PassiveJob `yaml:",inline"`
|
2018-10-11 18:00:23 +02:00
|
|
|
RootFS string `yaml:"root_fs"`
|
2018-08-26 23:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type SourceJob struct {
|
2018-09-23 23:04:31 +02:00
|
|
|
PassiveJob `yaml:",inline"`
|
2018-10-11 15:22:52 +02:00
|
|
|
Snapshotting SnapshottingEnum `yaml:"snapshotting"`
|
2018-09-23 23:04:31 +02:00
|
|
|
Filesystems FilesystemsFilter `yaml:"filesystems"`
|
2018-08-26 23:11:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 23:44:45 +02:00
|
|
|
type FilesystemsFilter map[string]bool
|
2018-08-26 23:29:57 +02:00
|
|
|
|
2018-10-11 15:22:52 +02:00
|
|
|
type SnapshottingEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SnapshottingPeriodic struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
Interval time.Duration `yaml:"interval,positive"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SnapshottingManual struct {
|
|
|
|
Type string `yaml:"type"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 23:11:50 +02:00
|
|
|
type PruningSenderReceiver struct {
|
|
|
|
KeepSender []PruningEnum `yaml:"keep_sender"`
|
|
|
|
KeepReceiver []PruningEnum `yaml:"keep_receiver"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PruningLocal struct {
|
|
|
|
Keep []PruningEnum `yaml:"keep"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
type LoggingOutletEnumList []LoggingOutletEnum
|
|
|
|
|
|
|
|
func (l *LoggingOutletEnumList) SetDefault() {
|
|
|
|
def := `
|
|
|
|
type: "stdout"
|
|
|
|
time: true
|
|
|
|
level: "warn"
|
|
|
|
format: "human"
|
|
|
|
`
|
2018-10-18 15:43:50 +02:00
|
|
|
s := &StdoutLoggingOutlet{}
|
2018-08-31 21:50:59 +02:00
|
|
|
err := yaml.UnmarshalStrict([]byte(def), &s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
*l = []LoggingOutletEnum{LoggingOutletEnum{Ret: s}}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ yaml.Defaulter = &LoggingOutletEnumList{}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type Global struct {
|
2018-08-31 21:50:59 +02:00
|
|
|
Logging *LoggingOutletEnumList `yaml:"logging,optional,fromdefaults"`
|
2018-08-31 21:51:44 +02:00
|
|
|
Monitoring []MonitoringEnum `yaml:"monitoring,optional"`
|
|
|
|
Control *GlobalControl `yaml:"control,optional,fromdefaults"`
|
|
|
|
Serve *GlobalServe `yaml:"serve,optional,fromdefaults"`
|
|
|
|
RPC *RPCConfig `yaml:"rpc,optional,fromdefaults"`
|
2018-08-31 21:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Default(i interface{}) {
|
|
|
|
v := reflect.ValueOf(i)
|
|
|
|
if v.Kind() != reflect.Ptr {
|
|
|
|
panic(v)
|
|
|
|
}
|
|
|
|
y := `{}`
|
|
|
|
err := yaml.Unmarshal([]byte(y), v.Interface())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RPCConfig struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
Timeout time.Duration `yaml:"timeout,optional,positive,default=10s"`
|
|
|
|
TxChunkSize uint32 `yaml:"tx_chunk_size,optional,default=32768"`
|
|
|
|
RxStructuredMaxLen uint32 `yaml:"rx_structured_max,optional,default=16777216"`
|
|
|
|
RxStreamChunkMaxLen uint32 `yaml:"rx_stream_chunk_max,optional,default=16777216"`
|
|
|
|
RxHeaderMaxLen uint32 `yaml:"rx_header_max,optional,default=40960"`
|
2018-09-23 20:28:30 +02:00
|
|
|
SendHeartbeatInterval time.Duration `yaml:"send_heartbeat_interval,optional,positive,default=5s"`
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:44 +02:00
|
|
|
type ConnectCommon struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
RPC *RPCConfig `yaml:"rpc,optional"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type TCPConnect struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ConnectCommon `yaml:",inline"`
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
DialTimeout time.Duration `yaml:"dial_timeout,positive,default=10s"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TLSConnect struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ConnectCommon `yaml:",inline"`
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
Ca string `yaml:"ca"`
|
|
|
|
Cert string `yaml:"cert"`
|
|
|
|
Key string `yaml:"key"`
|
|
|
|
ServerCN string `yaml:"server_cn"`
|
|
|
|
DialTimeout time.Duration `yaml:"dial_timeout,positive,default=10s"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 23:46:59 +02:00
|
|
|
type SSHStdinserverConnect struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ConnectCommon `yaml:",inline"`
|
2018-08-27 15:18:08 +02:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Port uint16 `yaml:"port"`
|
|
|
|
IdentityFile string `yaml:"identity_file"`
|
|
|
|
TransportOpenCommand []string `yaml:"transport_open_command,optional"` //TODO unused
|
|
|
|
SSHCommand string `yaml:"ssh_command,optional"` //TODO unused
|
2018-09-05 01:41:54 +02:00
|
|
|
Options []string `yaml:"options,optional"`
|
2018-08-27 15:18:08 +02:00
|
|
|
DialTimeout time.Duration `yaml:"dial_timeout,positive,default=10s"`
|
2018-08-26 23:46:59 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 14:43:53 +02:00
|
|
|
type LocalConnect struct {
|
|
|
|
ConnectCommon `yaml:",inline"`
|
2018-10-11 13:06:47 +02:00
|
|
|
ListenerName string `yaml:"listener_name"`
|
2018-09-24 14:43:53 +02:00
|
|
|
ClientIdentity string `yaml:"client_identity"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type ServeEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:44 +02:00
|
|
|
type ServeCommon struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
RPC *RPCConfig `yaml:"rpc,optional"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type TCPServe struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ServeCommon `yaml:",inline"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
Clients map[string]string `yaml:"clients"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TLSServe struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ServeCommon `yaml:",inline"`
|
2018-08-27 22:21:45 +02:00
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
Ca string `yaml:"ca"`
|
|
|
|
Cert string `yaml:"cert"`
|
|
|
|
Key string `yaml:"key"`
|
2018-09-05 01:41:54 +02:00
|
|
|
ClientCNs []string `yaml:"client_cns"`
|
2018-08-27 22:21:45 +02:00
|
|
|
HandshakeTimeout time.Duration `yaml:"handshake_timeout,positive,default=10s"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 23:46:59 +02:00
|
|
|
type StdinserverServer struct {
|
2018-08-31 21:51:44 +02:00
|
|
|
ServeCommon `yaml:",inline"`
|
2018-09-05 01:41:54 +02:00
|
|
|
ClientIdentities []string `yaml:"client_identities"`
|
2018-08-26 23:46:59 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 14:43:53 +02:00
|
|
|
type LocalServe struct {
|
|
|
|
ServeCommon `yaml:",inline"`
|
2018-10-11 13:06:47 +02:00
|
|
|
ListenerName string `yaml:"listener_name"`
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type PruningEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PruneKeepNotReplicated struct {
|
|
|
|
Type string `yaml:"type"`
|
2018-09-06 03:24:15 +02:00
|
|
|
KeepSnapshotAtCursor bool `yaml:"keep_snapshot_at_cursor,optional,default=true"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PruneKeepLastN struct {
|
2018-08-26 16:44:34 +02:00
|
|
|
Type string `yaml:"type"`
|
|
|
|
Count int `yaml:"count"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 11:44:43 +02:00
|
|
|
type PruneKeepRegex struct { // FIXME rename to KeepRegex
|
|
|
|
Type string `yaml:"type"`
|
2018-10-11 13:07:52 +02:00
|
|
|
Regex string `yaml:"regex"`
|
2018-08-27 15:18:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type LoggingOutletEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
type LoggingOutletCommon struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Level string `yaml:"level"`
|
|
|
|
Format string `yaml:"format"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
type StdoutLoggingOutlet struct {
|
2018-08-26 16:44:34 +02:00
|
|
|
LoggingOutletCommon `yaml:",inline"`
|
2018-08-27 22:21:45 +02:00
|
|
|
Time bool `yaml:"time,default=true"`
|
2018-09-05 02:25:10 +02:00
|
|
|
Color bool `yaml:"color,default=true"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type SyslogLoggingOutlet struct {
|
2018-08-26 16:44:34 +02:00
|
|
|
LoggingOutletCommon `yaml:",inline"`
|
2018-08-27 22:21:45 +02:00
|
|
|
RetryInterval time.Duration `yaml:"retry_interval,positive,default=10s"`
|
2018-08-26 16:44:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TCPLoggingOutlet struct {
|
|
|
|
LoggingOutletCommon `yaml:",inline"`
|
2018-08-26 23:11:50 +02:00
|
|
|
Address string `yaml:"address"`
|
|
|
|
Net string `yaml:"net,default=tcp"`
|
2018-08-27 22:21:45 +02:00
|
|
|
RetryInterval time.Duration `yaml:"retry_interval,positive,default=10s"`
|
2018-08-26 21:37:29 +02:00
|
|
|
TLS *TCPLoggingOutletTLS `yaml:"tls,optional"`
|
2018-08-26 16:44:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TCPLoggingOutletTLS struct {
|
|
|
|
CA string `yaml:"ca"`
|
|
|
|
Cert string `yaml:"cert"`
|
|
|
|
Key string `yaml:"key"`
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 20:25:06 +02:00
|
|
|
type MonitoringEnum struct {
|
|
|
|
Ret interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrometheusMonitoring struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
}
|
|
|
|
|
2018-08-26 23:11:50 +02:00
|
|
|
type GlobalControl struct {
|
|
|
|
SockPath string `yaml:"sockpath,default=/var/run/zrepl/control"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GlobalServe struct {
|
2018-08-31 21:50:59 +02:00
|
|
|
StdinServer *GlobalStdinServer `yaml:"stdinserver,optional,fromdefaults"`
|
2018-08-26 23:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type GlobalStdinServer struct {
|
|
|
|
SockDir string `yaml:"sockdir,default=/var/run/zrepl/stdinserver"`
|
|
|
|
}
|
|
|
|
|
2018-08-27 15:18:08 +02:00
|
|
|
type JobDebugSettings struct {
|
2018-08-31 21:50:59 +02:00
|
|
|
Conn *struct {
|
2018-08-27 15:18:08 +02:00
|
|
|
ReadDump string `yaml:"read_dump"`
|
|
|
|
WriteDump string `yaml:"write_dump"`
|
2018-08-31 21:50:59 +02:00
|
|
|
} `yaml:"conn,optional"`
|
|
|
|
RPCLog bool `yaml:"rpc_log,optional,default=false"`
|
2018-08-27 15:18:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
func enumUnmarshal(u func(interface{}, bool) error, types map[string]interface{}) (interface{}, error) {
|
|
|
|
var in struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
if err := u(&in, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if in.Type == "" {
|
2018-09-05 02:25:10 +02:00
|
|
|
return nil, &yaml.TypeError{Errors: []string{"must specify type"}}
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := types[in.Type]
|
|
|
|
if !ok {
|
2018-09-05 02:25:10 +02:00
|
|
|
return nil, &yaml.TypeError{Errors: []string{fmt.Sprintf("invalid type name %q", in.Type)}}
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
|
|
|
if err := u(v, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:44:34 +02:00
|
|
|
func (t *JobEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
2018-08-26 15:17:15 +02:00
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
2018-08-26 23:11:50 +02:00
|
|
|
"push": &PushJob{},
|
|
|
|
"sink": &SinkJob{},
|
|
|
|
"pull": &PullJob{},
|
|
|
|
"source": &SourceJob{},
|
2018-08-26 15:17:15 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ConnectEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
2018-08-27 15:18:08 +02:00
|
|
|
"tcp": &TCPConnect{},
|
|
|
|
"tls": &TLSConnect{},
|
2018-08-26 23:46:59 +02:00
|
|
|
"ssh+stdinserver": &SSHStdinserverConnect{},
|
2018-09-24 14:43:53 +02:00
|
|
|
"local": &LocalConnect{},
|
2018-08-26 15:17:15 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ServeEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
2018-08-27 15:18:08 +02:00
|
|
|
"tcp": &TCPServe{},
|
|
|
|
"tls": &TLSServe{},
|
2018-08-26 23:46:59 +02:00
|
|
|
"stdinserver": &StdinserverServer{},
|
2018-09-24 14:43:53 +02:00
|
|
|
"local" : &LocalServe{},
|
2018-08-26 15:17:15 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *PruningEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
|
|
|
"not_replicated": &PruneKeepNotReplicated{},
|
2018-08-26 16:44:34 +02:00
|
|
|
"last_n": &PruneKeepLastN{},
|
|
|
|
"grid": &PruneGrid{},
|
2018-10-11 13:07:52 +02:00
|
|
|
"regex": &PruneKeepRegex{},
|
2018-08-26 15:17:15 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:22:52 +02:00
|
|
|
func (t *SnapshottingEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
|
|
|
"periodic": &SnapshottingPeriodic{},
|
|
|
|
"manual": &SnapshottingManual{},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
func (t *LoggingOutletEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
|
|
|
"stdout": &StdoutLoggingOutlet{},
|
|
|
|
"syslog": &SyslogLoggingOutlet{},
|
2018-08-26 16:44:34 +02:00
|
|
|
"tcp": &TCPLoggingOutlet{},
|
2018-08-26 15:17:15 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-26 20:25:06 +02:00
|
|
|
func (t *MonitoringEnum) UnmarshalYAML(u func(interface{}, bool) error) (err error) {
|
|
|
|
t.Ret, err = enumUnmarshal(u, map[string]interface{}{
|
|
|
|
"prometheus": &PrometheusMonitoring{},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:15 +02:00
|
|
|
var ConfigFileDefaultLocations = []string{
|
|
|
|
"/etc/zrepl/zrepl.yml",
|
|
|
|
"/usr/local/etc/zrepl/zrepl.yml",
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func ParseConfig(path string) (i *Config, err error) {
|
2018-08-26 15:17:15 +02:00
|
|
|
|
|
|
|
if path == "" {
|
|
|
|
// Try default locations
|
|
|
|
for _, l := range ConfigFileDefaultLocations {
|
|
|
|
stat, statErr := os.Stat(l)
|
|
|
|
if statErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !stat.Mode().IsRegular() {
|
|
|
|
err = errors.Errorf("file at default location is not a regular file: %s", l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path = l
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytes []byte
|
|
|
|
|
|
|
|
if bytes, err = ioutil.ReadFile(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
return ParseConfigBytes(bytes)
|
|
|
|
}
|
2018-08-26 15:17:15 +02:00
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func ParseConfigBytes(bytes []byte) (*Config, error) {
|
|
|
|
var c *Config
|
|
|
|
if err := yaml.UnmarshalStrict(bytes, &c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
return nil, fmt.Errorf("config is empty or only consists of comments")
|
|
|
|
}
|
|
|
|
return c, nil
|
2018-08-26 15:17:15 +02:00
|
|
|
}
|
2018-08-26 19:20:08 +02:00
|
|
|
|
|
|
|
var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`)
|
|
|
|
|
|
|
|
func parsePostitiveDuration(e string) (d time.Duration, err error) {
|
|
|
|
comps := durationStringRegex.FindStringSubmatch(e)
|
|
|
|
if len(comps) != 3 {
|
|
|
|
err = fmt.Errorf("does not match regex: %s %#v", e, comps)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
durationFactor, err := strconv.ParseInt(comps[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if durationFactor <= 0 {
|
|
|
|
return 0, errors.New("duration must be positive integer")
|
|
|
|
}
|
|
|
|
|
|
|
|
var durationUnit time.Duration
|
|
|
|
switch comps[2] {
|
|
|
|
case "s":
|
|
|
|
durationUnit = time.Second
|
|
|
|
case "m":
|
|
|
|
durationUnit = time.Minute
|
|
|
|
case "h":
|
|
|
|
durationUnit = time.Hour
|
|
|
|
case "d":
|
|
|
|
durationUnit = 24 * time.Hour
|
|
|
|
case "w":
|
|
|
|
durationUnit = 24 * 7 * time.Hour
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("contains unknown time unit '%s'", comps[2])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d = time.Duration(durationFactor) * durationUnit
|
|
|
|
return
|
|
|
|
}
|