Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring all replica defaults #553

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 76 additions & 23 deletions cmd/litestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ The commands are:

// Config represents a configuration file for the litestream daemon.
type Config struct {
// Global replica settings
ReplicaSettings

// Bind address for serving metrics.
Addr string `yaml:"addr"`

Expand All @@ -166,10 +169,6 @@ type Config struct {
// Litestream will shutdown when subcommand exits.
Exec string `yaml:"exec"`

// Global S3 settings
AccessKeyID string `yaml:"access-key-id"`
SecretAccessKey string `yaml:"secret-access-key"`

// Logging
Logging LoggingConfig `yaml:"logging"`
}
Expand All @@ -181,16 +180,11 @@ type LoggingConfig struct {
Stderr bool `yaml:"stderr"`
}

// propagateGlobalSettings copies global S3 settings to replica configs.
// propagateGlobalSettings copies global replica settings to replica configs.
func (c *Config) propagateGlobalSettings() {
for _, dbc := range c.DBs {
for _, rc := range dbc.Replicas {
if rc.AccessKeyID == "" {
rc.AccessKeyID = c.AccessKeyID
}
if rc.SecretAccessKey == "" {
rc.SecretAccessKey = c.SecretAccessKey
}
rc.ReplicaSettings.SetDefaults(&c.ReplicaSettings)
}
}
}
Expand Down Expand Up @@ -332,18 +326,8 @@ func NewDBFromConfig(dbc *DBConfig) (*litestream.DB, error) {
return db, nil
}

// ReplicaConfig represents the configuration for a single replica in a database.
type ReplicaConfig struct {
Type string `yaml:"type"` // "file", "s3"
Name string `yaml:"name"` // name of replica, optional.
Path string `yaml:"path"`
URL string `yaml:"url"`
Retention *time.Duration `yaml:"retention"`
RetentionCheckInterval *time.Duration `yaml:"retention-check-interval"`
SyncInterval *time.Duration `yaml:"sync-interval"`
SnapshotInterval *time.Duration `yaml:"snapshot-interval"`
ValidationInterval *time.Duration `yaml:"validation-interval"`

// ReplicaSettings stores replica access credentials
type ReplicaSettings struct {
// S3 settings
AccessKeyID string `yaml:"access-key-id"`
SecretAccessKey string `yaml:"secret-access-key"`
Expand All @@ -370,6 +354,75 @@ type ReplicaConfig struct {
} `yaml:"age"`
}

func (rs *ReplicaSettings) SetDefaults(src *ReplicaSettings) {
// S3 settings
if rs.AccessKeyID == "" {
rs.AccessKeyID = src.AccessKeyID
}
if rs.SecretAccessKey == "" {
rs.SecretAccessKey = src.SecretAccessKey
}
if rs.Region == "" {
rs.Region = src.Region
}
if rs.Bucket == "" {
rs.Bucket = src.Bucket
}
if rs.Endpoint == "" {
rs.Endpoint = src.Endpoint
}
if rs.ForcePathStyle == nil {
rs.ForcePathStyle = src.ForcePathStyle
}
if src.SkipVerify {
rs.SkipVerify = true
}

// ABS settings
if rs.AccountName == "" {
rs.AccountName = src.AccountName
}
if rs.AccountKey == "" {
rs.AccountKey = src.AccountKey
}

// SFTP settings
if rs.Host == "" {
rs.Host = src.Host
}
if rs.User == "" {
rs.User = src.User
}
if rs.Password == "" {
rs.Password = src.Password
}
if rs.KeyPath == "" {
rs.KeyPath = src.KeyPath
}

if len(rs.Age.Identities) == 0 {
rs.Age.Identities = src.Age.Identities
}
if len(rs.Age.Recipients) == 0 {
rs.Age.Recipients = src.Age.Recipients
}
}

// ReplicaConfig represents the configuration for a single replica in a database.
type ReplicaConfig struct {
ReplicaSettings

Type string `yaml:"type"` // "file", "s3"
Name string `yaml:"name"` // name of replica, optional.
Path string `yaml:"path"`
URL string `yaml:"url"`
Retention *time.Duration `yaml:"retention"`
RetentionCheckInterval *time.Duration `yaml:"retention-check-interval"`
SyncInterval *time.Duration `yaml:"sync-interval"`
SnapshotInterval *time.Duration `yaml:"snapshot-interval"`
ValidationInterval *time.Duration `yaml:"validation-interval"`
}

// NewReplicaFromConfig instantiates a replica for a DB based on a config.
func NewReplicaFromConfig(c *ReplicaConfig, db *litestream.DB) (_ *litestream.Replica, err error) {
// Ensure user did not specify URL in path.
Expand Down
Loading