extract to sync package

This commit is contained in:
bakito 2021-03-28 13:13:06 +02:00
parent 785c23a51a
commit 1bb31c3651
No known key found for this signature in database
GPG key ID: FAF93C1C384DD6B4
2 changed files with 162 additions and 152 deletions

154
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"github.com/bakito/adguardhome-sync/pkg/sync"
"os"
"github.com/bakito/adguardhome-sync/pkg/log"
@ -31,158 +32,7 @@ func main() {
if err != nil {
panic(err)
}
logger := l.With("from", origin.Host(), "to", replica.Host())
logger.Info("Start sync")
defer func() { logger.Info("Sync done") }()
os, err := origin.Status()
if err != nil {
panic(err)
}
rs, err := replica.Status()
if err != nil {
panic(err)
}
if os.Version != rs.Version {
panic("Versions do not match")
}
err = syncRewrites(origin, replica)
if err != nil {
panic(err)
}
err = syncFilters(origin, replica)
if err != nil {
panic(err)
}
err = syncServices(origin, replica)
if err != nil {
panic(err)
}
err = syncClients(origin, replica)
if err != nil {
if err = sync.Sync(origin, replica); err != nil {
panic(err)
}
}
func syncServices(origin client.Client, replica client.Client) error {
os, err := origin.Services()
if err != nil {
return err
}
rs, err := replica.Services()
if err != nil {
return err
}
if !os.Equals(rs) {
if err := replica.SetServices(os); err != nil {
return err
}
}
return nil
}
func syncFilters(origin client.Client, replica client.Client) error {
of, err := origin.Filtering()
if err != nil {
return err
}
rf, err := replica.Filtering()
if err != nil {
return err
}
fa, fd := rf.Filters.Merge(of.Filters)
if err = replica.AddFilters(false, fa...); err != nil {
return err
}
if len(fa) > 0 {
if err = replica.RefreshFilters(false); err != nil {
return err
}
}
if err = replica.DeleteFilters(false, fd...); err != nil {
return err
}
fa, fd = rf.WhitelistFilters.Merge(of.WhitelistFilters)
if err = replica.AddFilters(true, fa...); err != nil {
return err
}
if len(fa) > 0 {
if err = replica.RefreshFilters(true); err != nil {
return err
}
}
if err = replica.DeleteFilters(true, fd...); err != nil {
return err
}
if of.UserRules.String() != rf.UserRules.String() {
return replica.SetCustomRules(of.UserRules)
}
if of.Enabled != rf.Enabled || of.Interval != rf.Interval {
if err = replica.ToggleFiltering(of.Enabled, of.Interval); err != nil {
return err
}
}
return nil
}
func syncRewrites(origin client.Client, replica client.Client) error {
originRewrites, err := origin.RewriteList()
if err != nil {
return err
}
replicaRewrites, err := replica.RewriteList()
if err != nil {
return err
}
a, r := replicaRewrites.Merge(originRewrites)
if err = replica.AddRewriteEntries(a...); err != nil {
return err
}
if err = replica.DeleteRewriteEntries(r...); err != nil {
return err
}
return nil
}
func syncClients(origin client.Client, replica client.Client) error {
oc, err := origin.Clients()
if err != nil {
return err
}
rc, err := replica.Clients()
if err != nil {
return err
}
a, u, r := rc.Merge(oc)
if err = replica.AddClients(a...); err != nil {
return err
}
if err = replica.UpdateClients(u...); err != nil {
return err
}
if err = replica.DeleteClients(r...); err != nil {
return err
}
return nil
}

160
pkg/sync/sync.go Normal file
View file

@ -0,0 +1,160 @@
package sync
import (
"github.com/bakito/adguardhome-sync/pkg/client"
"github.com/bakito/adguardhome-sync/pkg/log"
)
// Sync config from origin to replica
func Sync(origin client.Client, replica client.Client) error {
l := log.GetLogger("sync").With("from", origin.Host(), "to", replica.Host())
l.Info("Start sync")
defer func() { l.Info("Sync done") }()
os, err := origin.Status()
if err != nil {
return err
}
rs, err := replica.Status()
if err != nil {
return err
}
if os.Version != rs.Version {
panic("Versions do not match")
}
err = syncRewrites(origin, replica)
if err != nil {
return err
}
err = syncFilters(origin, replica)
if err != nil {
return err
}
err = syncServices(origin, replica)
if err != nil {
return err
}
return syncClients(origin, replica)
}
func syncServices(origin client.Client, replica client.Client) error {
os, err := origin.Services()
if err != nil {
return err
}
rs, err := replica.Services()
if err != nil {
return err
}
if !os.Equals(rs) {
if err := replica.SetServices(os); err != nil {
return err
}
}
return nil
}
func syncFilters(origin client.Client, replica client.Client) error {
of, err := origin.Filtering()
if err != nil {
return err
}
rf, err := replica.Filtering()
if err != nil {
return err
}
fa, fd := rf.Filters.Merge(of.Filters)
if err = replica.AddFilters(false, fa...); err != nil {
return err
}
if len(fa) > 0 {
if err = replica.RefreshFilters(false); err != nil {
return err
}
}
if err = replica.DeleteFilters(false, fd...); err != nil {
return err
}
fa, fd = rf.WhitelistFilters.Merge(of.WhitelistFilters)
if err = replica.AddFilters(true, fa...); err != nil {
return err
}
if len(fa) > 0 {
if err = replica.RefreshFilters(true); err != nil {
return err
}
}
if err = replica.DeleteFilters(true, fd...); err != nil {
return err
}
if of.UserRules.String() != rf.UserRules.String() {
return replica.SetCustomRules(of.UserRules)
}
if of.Enabled != rf.Enabled || of.Interval != rf.Interval {
if err = replica.ToggleFiltering(of.Enabled, of.Interval); err != nil {
return err
}
}
return nil
}
func syncRewrites(origin client.Client, replica client.Client) error {
originRewrites, err := origin.RewriteList()
if err != nil {
return err
}
replicaRewrites, err := replica.RewriteList()
if err != nil {
return err
}
a, r := replicaRewrites.Merge(originRewrites)
if err = replica.AddRewriteEntries(a...); err != nil {
return err
}
if err = replica.DeleteRewriteEntries(r...); err != nil {
return err
}
return nil
}
func syncClients(origin client.Client, replica client.Client) error {
oc, err := origin.Clients()
if err != nil {
return err
}
rc, err := replica.Clients()
if err != nil {
return err
}
a, u, r := rc.Merge(oc)
if err = replica.AddClients(a...); err != nil {
return err
}
if err = replica.UpdateClients(u...); err != nil {
return err
}
if err = replica.DeleteClients(r...); err != nil {
return err
}
return nil
}