Compare commits

...

6 Commits

Author SHA1 Message Date
Marc Brugger
f8578e85b2 add API server to trigger sync remotely 2021-03-29 08:43:18 +02:00
bakito
5dafdf5472 change output paths 2021-03-28 20:28:58 +02:00
bakito
23d5f30eb8 add systemd script 2021-03-28 20:25:25 +02:00
bakito
44609a93e3 correct logger name 2021-03-28 20:00:22 +02:00
bakito
70e60bb7d0 use external semver 2021-03-28 19:31:11 +02:00
bakito
aacffb1518 document config file 2021-03-28 17:04:41 +02:00
14 changed files with 273 additions and 79 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
.idea
coverage.out
dist
adguardhome-sync
main
.adguardhome-sync.yaml

View File

@@ -20,10 +20,15 @@ test: tidy fmt vet
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
release:
@version=$$(go run version/semver/main.go); \
release: semver
@version=$$(semver); \
git tag -s $$version -m"Release $$version"
goreleaser --rm-dist
test-release:
goreleaser --skip-publish --snapshot --rm-dist
goreleaser --skip-publish --snapshot --rm-dist
semver:
ifeq (, $(shell which semver))
$(shell go get -u github.com/bakito/semver)
endif

View File

@@ -33,4 +33,47 @@ adguardhome-sync run
# run as daemon
adguardhome-sync run --cron "*/10 * * * *"
```
```
### Config file
location: $HOME/.adguardhome-sync.yaml
```yaml
# cron expression to run in daemon mode. (default; "" = runs only once)
cron: "*/10 * * * *"
origin:
# url of the origin instance
url: https://192.168.1.2:3000
# apiPath: define an api path if other than "/control"
# insecureSkipVerify: true # disable tls check
username: username
password: password
# replica instance (optional, if only one)
replica:
# url of the replica instance
url: http://192.168.1.3
username: username
password: password
# replicas instances (optional, if more than one)
replicas:
# url of the replica instance
- url: http://192.168.1.3
username: username
password: password
- url: http://192.168.1.4
username: username
password: password
# Configure the sync API server, disabled if api port is 0
api:
# Port, default 8080
port: 8080
# if username and password are defined, basic auth is applied to the sync API
username: username
password: password
```

View File

@@ -16,6 +16,10 @@ import (
const (
configCron = "cron"
configAPIPort = "api.port"
configAPIUsername = "api.username"
configAPIPassword = "api.password"
configOriginURL = "origin.url"
configOriginAPIPath = "origin.apiPath"
configOriginUsername = "origin.username"

View File

@@ -15,8 +15,7 @@ var doCmd = &cobra.Command{
Short: "Start a synchronisation from origin to replica",
Long: `Synchronizes the configuration form an origin instance to a replica`,
Run: func(cmd *cobra.Command, args []string) {
logger = log.GetLogger("root")
logger = log.GetLogger("run")
cfg := &types.Config{}
if err := viper.Unmarshal(cfg); err != nil {
logger.Error(err)
@@ -31,6 +30,12 @@ func init() {
rootCmd.AddCommand(doCmd)
doCmd.PersistentFlags().String("cron", "", "The cron expression to run in daemon mode")
_ = viper.BindPFlag(configCron, doCmd.PersistentFlags().Lookup("cron"))
doCmd.PersistentFlags().Int("api-port", 8080, "Sync API Port, the API endpoint will be started to enable remote triggering; if 0 port API is disabled.")
_ = viper.BindPFlag(configAPIPort, doCmd.PersistentFlags().Lookup("api-port"))
doCmd.PersistentFlags().String("api-username", "", "Sync API username")
_ = viper.BindPFlag(configAPIUsername, doCmd.PersistentFlags().Lookup("api-username"))
doCmd.PersistentFlags().String("api-password", "", "Sync API password")
_ = viper.BindPFlag(configAPIPassword, doCmd.PersistentFlags().Lookup("api-password"))
doCmd.PersistentFlags().String("origin-url", "", "Origin instance url")
_ = viper.BindPFlag(configOriginURL, doCmd.PersistentFlags().Lookup("origin-url"))

1
go.mod
View File

@@ -3,7 +3,6 @@ module github.com/bakito/adguardhome-sync
go 1.16
require (
github.com/coreos/go-semver v0.3.0
github.com/go-resty/resty/v2 v2.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/robfig/cron/v3 v3.0.1

1
go.sum
View File

@@ -28,7 +28,6 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=

View File

@@ -19,7 +19,7 @@ func init() {
Development: false,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}

116
pkg/sync/http.go Normal file
View File

@@ -0,0 +1,116 @@
package sync
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func (w *worker) handleSync(rw http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
l.With("remote-addr", req.RemoteAddr).Info("Starting sync from API")
w.sync()
default:
http.Error(rw, "only POST allowed", http.StatusBadRequest)
}
}
func (w *worker) handleRoot(rw http.ResponseWriter, _ *http.Request) {
_, _ = rw.Write([]byte("adguardhome-sync"))
}
func (w *worker) basicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
username, password, authOK := r.BasicAuth()
if !authOK {
http.Error(rw, "Not authorized", 401)
return
}
if username != w.cfg.API.Username || password != w.cfg.API.Password {
http.Error(rw, "Not authorized", 401)
return
}
h.ServeHTTP(rw, r)
}
}
func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)
}
return h
}
func (w *worker) listenAndServe() {
l.With("port", w.cfg.API.Port).Info("Starting API server")
ctx, cancel := context.WithCancel(context.Background())
mux := http.NewServeMux()
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", w.cfg.API.Port),
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
if w.cfg.API.Username != "" && w.cfg.API.Password != "" {
mux.HandleFunc("/api/v1/sync", use(w.handleSync, w.basicAuth))
mux.HandleFunc("/", use(w.handleRoot, w.basicAuth))
} else {
mux.HandleFunc("/api/v1/sync", w.handleSync)
mux.HandleFunc("/", w.handleRoot)
}
go func() {
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
l.With("error", err).Fatalf("HTTP server ListenAndServe")
}
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
l.Info("os.Interrupt - shutting down...")
go func() {
<-signalChan
l.Fatal("os.Kill - terminating...")
}()
gracefullCtx, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelShutdown()
if w.cron != nil {
l.Info("Stopping cron")
w.cron.Stop()
}
if err := httpServer.Shutdown(gracefullCtx); err != nil {
l.With("error", err).Error("Shutdown error")
defer os.Exit(1)
} else {
l.Info("API server stopped")
}
// manually cancel context if not using httpServer.RegisterOnShutdown(cancel)
cancel()
defer os.Exit(0)
}

View File

@@ -14,27 +14,50 @@ var (
// Sync config from origin to replica
func Sync(cfg *types.Config) {
w := &worker{
cfg: cfg,
}
if cfg.Cron != "" {
c := cron.New()
w.cron = cron.New()
cl := l.With("cron", cfg.Cron)
_, err := c.AddFunc(cfg.Cron, func() {
sync(cfg)
_, err := w.cron.AddFunc(cfg.Cron, func() {
w.sync()
})
if err != nil {
cl.With("error", err).Error("Error creating cron job")
cl.With("error", err).Error("Error during cron job setup")
return
}
cl.Info("Starting cronjob")
c.Run()
cl.Info("Setup cronjob")
if cfg.API.Port != 0 {
w.cron.Start()
} else {
w.cron.Run()
}
} else {
sync(cfg)
w.sync()
}
if cfg.API.Port != 0 {
w.listenAndServe()
}
}
func sync(cfg *types.Config) {
oc, err := client.New(cfg.Origin)
type worker struct {
cfg *types.Config
running bool
cron *cron.Cron
}
func (w *worker) sync() {
if w.running {
l.Info("Sync already running")
return
}
w.running = true
defer func() { w.running = false }()
oc, err := client.New(w.cfg.Origin)
if err != nil {
l.With("error", err, "url", cfg.Origin.URL).Error("Error creating origin client")
l.With("error", err, "url", w.cfg.Origin.URL).Error("Error creating origin client")
return
}
@@ -71,13 +94,13 @@ func sync(cfg *types.Config) {
return
}
replicas := cfg.UniqueReplicas()
replicas := w.cfg.UniqueReplicas()
for _, replica := range replicas {
syncTo(sl, o, replica)
w.syncTo(sl, o, replica)
}
}
func syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardInstance) {
func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardInstance) {
rc, err := client.New(replica)
if err != nil {
@@ -97,24 +120,24 @@ func syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardInstance) {
l.With("originVersion", o.status.Version, "replicaVersion", rs.Version).Warn("Versions do not match")
}
err = syncRewrites(o.rewrites, rc)
err = w.syncRewrites(o.rewrites, rc)
if err != nil {
l.With("error", err).Error("Error syncing rewrites")
return
}
err = syncFilters(o.filters, rc)
err = w.syncFilters(o.filters, rc)
if err != nil {
l.With("error", err).Error("Error syncing filters")
return
}
err = syncServices(o.services, rc)
err = w.syncServices(o.services, rc)
if err != nil {
l.With("error", err).Error("Error syncing services")
return
}
if err = syncClients(o.clients, rc); err != nil {
if err = w.syncClients(o.clients, rc); err != nil {
l.With("error", err).Error("Error syncing clients")
return
}
@@ -122,7 +145,7 @@ func syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardInstance) {
rl.Info("Sync done")
}
func syncServices(os *types.Services, replica client.Client) error {
func (w *worker) syncServices(os *types.Services, replica client.Client) error {
rs, err := replica.Services()
if err != nil {
return err
@@ -136,7 +159,7 @@ func syncServices(os *types.Services, replica client.Client) error {
return nil
}
func syncFilters(of *types.FilteringStatus, replica client.Client) error {
func (w *worker) syncFilters(of *types.FilteringStatus, replica client.Client) error {
rf, err := replica.Filtering()
if err != nil {
return err
@@ -185,7 +208,7 @@ func syncFilters(of *types.FilteringStatus, replica client.Client) error {
return nil
}
func syncRewrites(or *types.RewriteEntries, replica client.Client) error {
func (w *worker) syncRewrites(or *types.RewriteEntries, replica client.Client) error {
replicaRewrites, err := replica.RewriteList()
if err != nil {
@@ -203,7 +226,7 @@ func syncRewrites(or *types.RewriteEntries, replica client.Client) error {
return nil
}
func syncClients(oc *types.Clients, replica client.Client) error {
func (w *worker) syncClients(oc *types.Clients, replica client.Client) error {
rc, err := replica.Clients()
if err != nil {
return err

View File

@@ -13,6 +13,13 @@ type Config struct {
Replica *AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"`
Replicas []AdGuardInstance `json:"replicas,omitempty" yaml:"replicas,omitempty"`
Cron string `json:"cron,omitempty" yaml:"cron,omitempty"`
API API `json:"api,omitempty" yaml:"api,omitempty"`
}
type API struct {
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
}
func (cfg *Config) UniqueReplicas() []AdGuardInstance {

View File

@@ -0,0 +1,28 @@
[Unit]
Description=AdGuard Home Sync service
ConditionFileIsExecutable=/opt/AdGuardHomeSync/adguardhome-sync
Requires=network.target
After=network-online.target syslog.target
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/opt/AdGuardHomeSync/adguardhome-sync "run" "--config" "/opt/AdGuardHomeSync/adguardhome-sync.yaml"
WorkingDirectory=/opt/AdGuardHome
Restart=on-success
SuccessExitStatus=1 2 8 SIGKILL
RestartSec=120
EnvironmentFile=-/etc/sysconfig/GoServiceExampleLogging
StandardOutput=file:/var/log/AdGuardHomeSync.out
StandardError=file:/var/log/AdGuardHomeSync.err
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

11
systemd/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Install
```bash
mkdir -p /opt/AdGuardHomeSync/
sudo cp adguardhome-sync /opt/AdGuardHomeSync/adguardhome-sync
sudo cp adguardhome-sync.yaml /opt/AdGuardHomeSync/adguardhome-sync.yaml
sudo cp AdGuardHomeSync.service /etc/systemd/system/AdGuardHomeSync.service
sudo systemctl enable AdGuardHomeSync
```

View File

@@ -1,49 +0,0 @@
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"github.com/coreos/go-semver/semver"
)
func main() {
out, err := exec.Command("git", "branch", "--show-current").Output()
if err != nil {
panic(err)
}
branch := strings.TrimSpace(string(out))
if branch != "main" {
panic(fmt.Errorf(`error: must be in "master" branch, current branch: %q`, branch))
}
out, err = exec.Command("git", "describe").Output()
if err != nil {
panic(err)
}
version := strings.TrimPrefix(strings.TrimSpace(string(out)), "v")
v := semver.New(version)
v.BumpPatch()
reader := bufio.NewReader(os.Stdin)
if _, err = fmt.Fprintf(os.Stderr, "Enter Release Version: [v%v] ", v); err != nil {
panic(err)
}
text, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if strings.HasPrefix(text, "v") {
text = text[1:]
v = semver.New(strings.TrimSpace(text))
}
if _, err = fmt.Fprintf(os.Stderr, "Using Version: v%v\n", v); err != nil {
panic(err)
}
fmt.Printf("v%v", v)
}