Change config format to yaml; add replicators
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pelletier/go-toml"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represents a configuration file for the litestream CLI.
|
// Config represents a configuration file for the litestream CLI.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DBs []DBConfig `toml:"db"`
|
DBs []*DBConfig `yaml:"databases"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultConfig returns a new instance of Config with defaults set.
|
// DefaultConfig returns a new instance of Config with defaults set.
|
||||||
@@ -38,12 +41,18 @@ func ReadConfigFile(filename string) (Config, error) {
|
|||||||
return config, fmt.Errorf("config file not found: %s", filename)
|
return config, fmt.Errorf("config file not found: %s", filename)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return config, err
|
return config, err
|
||||||
} else if err := toml.Unmarshal(buf, &config); err != nil {
|
} else if err := yaml.Unmarshal(buf, &config); err != nil {
|
||||||
return config, err
|
return config, err
|
||||||
}
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type DBConfig struct {
|
type DBConfig struct {
|
||||||
Path string `toml:"path"`
|
Path string `yaml:"path"`
|
||||||
|
Replicators []*ReplicatorConfig `yaml:"replicators`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReplicatorConfig struct {
|
||||||
|
Type string `yaml:"type"` // "file", "s3"
|
||||||
|
Path string `yaml:"path"` // used for file replicators
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,25 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/benbjohnson/litestream"
|
"github.com/benbjohnson/litestream"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Build information.
|
||||||
|
var (
|
||||||
|
Version = "(development build)"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Default settings.
|
||||||
|
const (
|
||||||
|
DefaultConfigPath = "~/litestream.yml"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Setup signal handler.
|
// Setup signal handler.
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
@@ -24,19 +30,26 @@ func main() {
|
|||||||
|
|
||||||
// Initialize program and read flags/config.
|
// Initialize program and read flags/config.
|
||||||
m := NewMain()
|
m := NewMain()
|
||||||
if err := m.ParseFlags(os.Args[1:]); err == flag.ErrHelp {
|
if err := m.ParseFlags(ctx, os.Args[1:]); err == flag.ErrHelp {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display version information.
|
||||||
|
fmt.Printf("Litestream %s\n", Version)
|
||||||
|
|
||||||
// Start monitoring databases.
|
// Start monitoring databases.
|
||||||
if err := m.Run(); err != nil {
|
if err := m.Run(ctx); err != nil {
|
||||||
|
m.Close()
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Notify user that initialization is done.
|
||||||
|
fmt.Printf("Initialized with %d databases; replication initialized.\n", len(m.DBs))
|
||||||
|
|
||||||
// Wait for signal to stop program.
|
// Wait for signal to stop program.
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
signal.Reset()
|
signal.Reset()
|
||||||
@@ -51,6 +64,9 @@ func main() {
|
|||||||
type Main struct {
|
type Main struct {
|
||||||
ConfigPath string
|
ConfigPath string
|
||||||
Config Config
|
Config Config
|
||||||
|
|
||||||
|
// List of managed databases specified in the config.
|
||||||
|
DBs []*litestream.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMain() *Main {
|
func NewMain() *Main {
|
||||||
@@ -60,8 +76,8 @@ func NewMain() *Main {
|
|||||||
// ParseFlags parses the flag set from args & loads the configuration.
|
// ParseFlags parses the flag set from args & loads the configuration.
|
||||||
func (m *Main) ParseFlags(ctx context.Context, args []string) (err error) {
|
func (m *Main) ParseFlags(ctx context.Context, args []string) (err error) {
|
||||||
fs := flag.NewFlagSet("litestream", flag.ContinueOnError)
|
fs := flag.NewFlagSet("litestream", flag.ContinueOnError)
|
||||||
fs.StringVar(&m.ConfigPath, "config", "", "configuration path")
|
fs.StringVar(&m.ConfigPath, "config", DefaultConfigPath, "configuration path")
|
||||||
fs.Usage = m.usage
|
fs.Usage = m.Usage
|
||||||
if err := fs.Parse(args); err != nil {
|
if err := fs.Parse(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -83,22 +99,60 @@ func (m *Main) Run(ctx context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, dbc := range m.Config.DBs {
|
for _, dbc := range m.Config.DBs {
|
||||||
db := litestream.NewDB()
|
if err := m.openDB(dbc); err != nil {
|
||||||
db.Path = dbc.Path
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// openDB instantiates and initializes a DB based on a configuration.
|
||||||
|
func (m *Main) openDB(config *DBConfig) error {
|
||||||
|
// Initialize database with given path.
|
||||||
|
db := litestream.NewDB(config.Path)
|
||||||
|
|
||||||
|
// Instantiate and attach replicators.
|
||||||
|
for _, rconfig := range config.Replicators {
|
||||||
|
r, err := m.createReplicator(db, rconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db.Replicators = append(db.Replicators, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open database & attach to program.
|
||||||
if err := db.Open(); err != nil {
|
if err := db.Open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.DBs = append(m.DBs, db)
|
m.DBs = append(m.DBs, db)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createReplicator instantiates a replicator for a DB based on a config.
|
||||||
|
func (m *Main) createReplicator(db *litestream.DB, config *ReplicatorConfig) (litestream.Replicator, error) {
|
||||||
|
switch config.Type {
|
||||||
|
case "", "file":
|
||||||
|
return m.createFileReplicator(db, config)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown replicator type in config: %q", config.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// createFileReplicator returns a new instance of FileReplicator build from config.
|
||||||
|
func (m *Main) createFileReplicator(db *litestream.DB, config *ReplicatorConfig) (*litestream.FileReplicator, error) {
|
||||||
|
if config.Path == "" {
|
||||||
|
return nil, fmt.Errorf("file replicator path require for db %q", db.Path())
|
||||||
|
}
|
||||||
|
return litestream.NewFileReplicator(db, config.Path), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Close closes all open databases.
|
// Close closes all open databases.
|
||||||
func (m *Main) Close() (err error) {
|
func (m *Main) Close() (err error) {
|
||||||
for _, db := range m.DBs {
|
for _, db := range m.DBs {
|
||||||
if e := db.Close(); e != nil {
|
if e := db.Close(); e != nil {
|
||||||
log.Printf("error closing db: path=%s err=%s", db.Path, e)
|
fmt.Printf("error closing db: path=%s err=%s\n", db.Path(), e)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = e
|
err = e
|
||||||
}
|
}
|
||||||
|
|||||||
8
db.go
8
db.go
@@ -1,16 +1,14 @@
|
|||||||
package litestream
|
package litestream
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -34,6 +32,10 @@ type DB struct {
|
|||||||
cancel func()
|
cancel func()
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
|
// List of replicators for the database.
|
||||||
|
// Must be set before calling Open().
|
||||||
|
Replicators []Replicator
|
||||||
|
|
||||||
// Frequency at which to perform db sync.
|
// Frequency at which to perform db sync.
|
||||||
MonitorInterval time.Duration
|
MonitorInterval time.Duration
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -5,4 +5,5 @@ go 1.15
|
|||||||
require (
|
require (
|
||||||
github.com/mattn/go-sqlite3 v1.14.5
|
github.com/mattn/go-sqlite3 v1.14.5
|
||||||
github.com/pelletier/go-toml v1.8.1
|
github.com/pelletier/go-toml v1.8.1
|
||||||
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -33,3 +33,6 @@ golang.org/x/tools v0.0.0-20200423201157-2723c5de0d66/go.mod h1:EkVYQZoAsY45+roY
|
|||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
|||||||
18
replicator.go
Normal file
18
replicator.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package litestream
|
||||||
|
|
||||||
|
type Replicator interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileReplicator is a replicator that replicates a DB to a local file path.
|
||||||
|
type FileReplicator struct {
|
||||||
|
db *DB // source database
|
||||||
|
dst string // destination path
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFileReplicator returns a new instance of FileReplicator.
|
||||||
|
func NewFileReplicator(db *DB, dst string) *FileReplicator {
|
||||||
|
return &FileReplicator{
|
||||||
|
db: db,
|
||||||
|
dst: dst,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user