mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-06 16:44:04 +00:00
Refactor config commit stuff to support restartless updates better
Includes restartless updates of the GUI settings (listening port etc) as a proof of concept.
This commit is contained in:
parent
ef6f52f688
commit
76ad925842
@ -353,3 +353,24 @@ func (s *connectionSvc) shouldLimit(addr net.Addr) bool {
|
|||||||
}
|
}
|
||||||
return !tcpaddr.IP.IsLoopback()
|
return !tcpaddr.IP.IsLoopback()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *connectionSvc) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *connectionSvc) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
|
// We require a restart if a device as been removed.
|
||||||
|
|
||||||
|
newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
|
||||||
|
for _, dev := range to.Devices {
|
||||||
|
newDevices[dev.DeviceID] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dev := range from.Devices {
|
||||||
|
if !newDevices[dev.DeviceID] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
debugNet = strings.Contains(os.Getenv("STTRACE"), "net") || os.Getenv("STTRACE") == "all"
|
debugNet = strings.Contains(os.Getenv("STTRACE"), "net") || os.Getenv("STTRACE") == "all"
|
||||||
debugHTTP = strings.Contains(os.Getenv("STTRACE"), "http") || os.Getenv("STTRACE") == "all"
|
debugHTTP = strings.Contains(os.Getenv("STTRACE"), "http") || os.Getenv("STTRACE") == "all"
|
||||||
|
debugSuture = strings.Contains(os.Getenv("STTRACE"), "suture") || os.Getenv("STTRACE") == "all"
|
||||||
)
|
)
|
||||||
|
@ -53,27 +53,29 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type apiSvc struct {
|
type apiSvc struct {
|
||||||
cfg config.GUIConfiguration
|
cfg config.GUIConfiguration
|
||||||
assetDir string
|
assetDir string
|
||||||
model *model.Model
|
model *model.Model
|
||||||
fss *folderSummarySvc
|
listener net.Listener
|
||||||
listener net.Listener
|
fss *folderSummarySvc
|
||||||
|
stop chan struct{}
|
||||||
|
systemConfigMut sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAPISvc(cfg config.GUIConfiguration, assetDir string, m *model.Model) (*apiSvc, error) {
|
func newAPISvc(cfg config.GUIConfiguration, assetDir string, m *model.Model) (*apiSvc, error) {
|
||||||
svc := &apiSvc{
|
svc := &apiSvc{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
assetDir: assetDir,
|
assetDir: assetDir,
|
||||||
model: m,
|
model: m,
|
||||||
fss: newFolderSummarySvc(m),
|
systemConfigMut: sync.NewMutex(),
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
svc.listener, err = svc.getListener()
|
svc.listener, err = svc.getListener(cfg)
|
||||||
return svc, err
|
return svc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiSvc) getListener() (net.Listener, error) {
|
func (s *apiSvc) getListener(cfg config.GUIConfiguration) (net.Listener, error) {
|
||||||
cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile])
|
cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Infoln("Loading HTTPS certificate:", err)
|
l.Infoln("Loading HTTPS certificate:", err)
|
||||||
@ -110,7 +112,7 @@ func (s *apiSvc) getListener() (net.Listener, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
rawListener, err := net.Listen("tcp", s.cfg.Address)
|
rawListener, err := net.Listen("tcp", cfg.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -120,6 +122,8 @@ func (s *apiSvc) getListener() (net.Listener, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiSvc) Serve() {
|
func (s *apiSvc) Serve() {
|
||||||
|
s.stop = make(chan struct{})
|
||||||
|
|
||||||
l.AddHandler(logger.LevelWarn, s.showGuiError)
|
l.AddHandler(logger.LevelWarn, s.showGuiError)
|
||||||
sub := events.Default.Subscribe(events.AllEvents)
|
sub := events.Default.Subscribe(events.AllEvents)
|
||||||
eventSub = events.NewBufferedSubscription(sub, 1000)
|
eventSub = events.NewBufferedSubscription(sub, 1000)
|
||||||
@ -210,15 +214,63 @@ func (s *apiSvc) Serve() {
|
|||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.fss = newFolderSummarySvc(s.model)
|
||||||
|
defer s.fss.Stop()
|
||||||
s.fss.ServeBackground()
|
s.fss.ServeBackground()
|
||||||
|
|
||||||
|
l.Infoln("API listening on", s.listener.Addr())
|
||||||
err := srv.Serve(s.listener)
|
err := srv.Serve(s.listener)
|
||||||
l.Warnln("API:", err)
|
|
||||||
|
// The return could be due to an intentional close. Wait for the stop
|
||||||
|
// signal before returning. IF there is no stop signal within a second, we
|
||||||
|
// assume it was unintentional and log the error before retrying.
|
||||||
|
select {
|
||||||
|
case <-s.stop:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
l.Warnln("API:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiSvc) Stop() {
|
func (s *apiSvc) Stop() {
|
||||||
|
close(s.stop)
|
||||||
s.listener.Close()
|
s.listener.Close()
|
||||||
s.fss.Stop()
|
}
|
||||||
|
|
||||||
|
func (s *apiSvc) String() string {
|
||||||
|
return fmt.Sprintf("apiSvc@%p", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *apiSvc) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *apiSvc) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
|
if to.GUI == from.GUI {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order here is important. We must close the listener to stop Serve(). We
|
||||||
|
// must create a new listener before Serve() starts again. We can't create
|
||||||
|
// a new listener on the same port before the previous listener is closed.
|
||||||
|
// To assist in this little dance the Serve() method will wait for a
|
||||||
|
// signal on the stop channel after the listener has closed.
|
||||||
|
|
||||||
|
s.listener.Close()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
s.listener, err = s.getListener(to.GUI)
|
||||||
|
if err != nil {
|
||||||
|
// Ideally this should be a verification error, but we check it by
|
||||||
|
// creating a new listener which requires shutting down the previous
|
||||||
|
// one first, which is too destructive for the VerifyConfiguration
|
||||||
|
// method.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s.cfg = to.GUI
|
||||||
|
|
||||||
|
close(s.stop)
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPostHandler(get, post http.Handler) http.Handler {
|
func getPostHandler(get, post http.Handler) http.Handler {
|
||||||
@ -464,43 +516,46 @@ func (s *apiSvc) getSystemConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiSvc) postSystemConfig(w http.ResponseWriter, r *http.Request) {
|
func (s *apiSvc) postSystemConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
var newCfg config.Configuration
|
s.systemConfigMut.Lock()
|
||||||
err := json.NewDecoder(r.Body).Decode(&newCfg)
|
defer s.systemConfigMut.Unlock()
|
||||||
|
|
||||||
|
var to config.Configuration
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warnln("decoding posted config:", err)
|
l.Warnln("decoding posted config:", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if newCfg.GUI.Password != cfg.GUI().Password {
|
if to.GUI.Password != cfg.GUI().Password {
|
||||||
if newCfg.GUI.Password != "" {
|
if to.GUI.Password != "" {
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0)
|
hash, err := bcrypt.GenerateFromPassword([]byte(to.GUI.Password), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warnln("bcrypting password:", err)
|
l.Warnln("bcrypting password:", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newCfg.GUI.Password = string(hash)
|
to.GUI.Password = string(hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixup usage reporting settings
|
// Fixup usage reporting settings
|
||||||
|
|
||||||
if curAcc := cfg.Options().URAccepted; newCfg.Options.URAccepted > curAcc {
|
if curAcc := cfg.Options().URAccepted; to.Options.URAccepted > curAcc {
|
||||||
// UR was enabled
|
// UR was enabled
|
||||||
newCfg.Options.URAccepted = usageReportVersion
|
to.Options.URAccepted = usageReportVersion
|
||||||
newCfg.Options.URUniqueID = randomString(8)
|
to.Options.URUniqueID = randomString(8)
|
||||||
} else if newCfg.Options.URAccepted < curAcc {
|
} else if to.Options.URAccepted < curAcc {
|
||||||
// UR was disabled
|
// UR was disabled
|
||||||
newCfg.Options.URAccepted = -1
|
to.Options.URAccepted = -1
|
||||||
newCfg.Options.URUniqueID = ""
|
to.Options.URUniqueID = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Activate and save
|
// Activate and save
|
||||||
|
|
||||||
configInSync = !config.ChangeRequiresRestart(cfg.Raw(), newCfg)
|
resp := cfg.Replace(to)
|
||||||
cfg.Replace(newCfg)
|
configInSync = !resp.RequiresRestart
|
||||||
cfg.Save()
|
cfg.Save()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,6 +155,7 @@ are mostly useful for developers. Use with care.
|
|||||||
- "model" (the model package)
|
- "model" (the model package)
|
||||||
- "scanner" (the scanner package)
|
- "scanner" (the scanner package)
|
||||||
- "stats" (the stats package)
|
- "stats" (the stats package)
|
||||||
|
- "suture" (the suture package; service management)
|
||||||
- "upnp" (the upnp package)
|
- "upnp" (the upnp package)
|
||||||
- "xdr" (the xdr package)
|
- "xdr" (the xdr package)
|
||||||
- "all" (all of the above)
|
- "all" (all of the above)
|
||||||
@ -420,11 +421,12 @@ func upgradeViaRest() error {
|
|||||||
|
|
||||||
func syncthingMain() {
|
func syncthingMain() {
|
||||||
// Create a main service manager. We'll add things to this as we go along.
|
// Create a main service manager. We'll add things to this as we go along.
|
||||||
// We want any logging it does to go through our log system, with INFO
|
// We want any logging it does to go through our log system.
|
||||||
// severity.
|
|
||||||
mainSvc := suture.New("main", suture.Spec{
|
mainSvc := suture.New("main", suture.Spec{
|
||||||
Log: func(line string) {
|
Log: func(line string) {
|
||||||
l.Infoln(line)
|
if debugSuture {
|
||||||
|
l.Debugln(line)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
mainSvc.ServeBackground()
|
mainSvc.ServeBackground()
|
||||||
@ -586,6 +588,7 @@ func syncthingMain() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m := model.NewModel(cfg, myID, myName, "syncthing", Version, ldb)
|
m := model.NewModel(cfg, myID, myName, "syncthing", Version, ldb)
|
||||||
|
cfg.Subscribe(m)
|
||||||
|
|
||||||
if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 {
|
if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 {
|
||||||
it, err := strconv.Atoi(t)
|
it, err := strconv.Atoi(t)
|
||||||
@ -643,6 +646,7 @@ func syncthingMain() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg)
|
connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg)
|
||||||
|
cfg.Subscribe(connectionSvc)
|
||||||
mainSvc.Add(connectionSvc)
|
mainSvc.Add(connectionSvc)
|
||||||
|
|
||||||
if cpuProfile {
|
if cpuProfile {
|
||||||
@ -792,6 +796,7 @@ func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatalln("Cannot start GUI:", err)
|
l.Fatalln("Cannot start GUI:", err)
|
||||||
}
|
}
|
||||||
|
cfg.Subscribe(api)
|
||||||
mainSvc.Add(api)
|
mainSvc.Add(api)
|
||||||
|
|
||||||
if opts.StartBrowser && !noBrowser && !stRestarting {
|
if opts.StartBrowser && !noBrowser && !stRestarting {
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -37,7 +38,7 @@ func newUsageReportingManager(m *model.Model, cfg *config.Wrapper) *usageReporti
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start UR if it's enabled.
|
// Start UR if it's enabled.
|
||||||
mgr.Changed(cfg.Raw())
|
mgr.CommitConfiguration(config.Configuration{}, cfg.Raw())
|
||||||
|
|
||||||
// Listen to future config changes so that we can start and stop as
|
// Listen to future config changes so that we can start and stop as
|
||||||
// appropriate.
|
// appropriate.
|
||||||
@ -46,8 +47,12 @@ func newUsageReportingManager(m *model.Model, cfg *config.Wrapper) *usageReporti
|
|||||||
return mgr
|
return mgr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *usageReportingManager) Changed(cfg config.Configuration) error {
|
func (m *usageReportingManager) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
if cfg.Options.URAccepted >= usageReportVersion && m.sup == nil {
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *usageReportingManager) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
|
if to.Options.URAccepted >= usageReportVersion && m.sup == nil {
|
||||||
// Usage reporting was turned on; lets start it.
|
// Usage reporting was turned on; lets start it.
|
||||||
svc := &usageReportingService{
|
svc := &usageReportingService{
|
||||||
model: m.model,
|
model: m.model,
|
||||||
@ -55,12 +60,17 @@ func (m *usageReportingManager) Changed(cfg config.Configuration) error {
|
|||||||
m.sup = suture.NewSimple("usageReporting")
|
m.sup = suture.NewSimple("usageReporting")
|
||||||
m.sup.Add(svc)
|
m.sup.Add(svc)
|
||||||
m.sup.ServeBackground()
|
m.sup.ServeBackground()
|
||||||
} else if cfg.Options.URAccepted < usageReportVersion && m.sup != nil {
|
} else if to.Options.URAccepted < usageReportVersion && m.sup != nil {
|
||||||
// Usage reporting was turned off
|
// Usage reporting was turned off
|
||||||
m.sup.Stop()
|
m.sup.Stop()
|
||||||
m.sup = nil
|
m.sup = nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *usageReportingManager) String() string {
|
||||||
|
return fmt.Sprintf("usageReportingManager@%p", m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reportData returns the data to be sent in a usage report. It's used in
|
// reportData returns the data to be sent in a usage report. It's used in
|
||||||
|
83
internal/config/commit_test.go
Normal file
83
internal/config/commit_test.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type requiresRestart struct{}
|
||||||
|
|
||||||
|
func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (requiresRestart) CommitConfiguration(_, _ Configuration) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func (requiresRestart) String() string {
|
||||||
|
return "requiresRestart"
|
||||||
|
}
|
||||||
|
|
||||||
|
type validationError struct{}
|
||||||
|
|
||||||
|
func (validationError) VerifyConfiguration(_, _ Configuration) error {
|
||||||
|
return errors.New("some error")
|
||||||
|
}
|
||||||
|
func (validationError) CommitConfiguration(_, _ Configuration) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (validationError) String() string {
|
||||||
|
return "validationError"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceCommit(t *testing.T) {
|
||||||
|
w := Wrap("/dev/null", Configuration{Version: 0})
|
||||||
|
if w.Raw().Version != 0 {
|
||||||
|
t.Fatal("Config incorrect")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace config. We should get back a clean response and the config
|
||||||
|
// should change.
|
||||||
|
|
||||||
|
resp := w.Replace(Configuration{Version: 1})
|
||||||
|
if resp.ValidationError != nil {
|
||||||
|
t.Fatal("Should not have a validation error")
|
||||||
|
}
|
||||||
|
if resp.RequiresRestart {
|
||||||
|
t.Fatal("Should not require restart")
|
||||||
|
}
|
||||||
|
if w.Raw().Version != 1 {
|
||||||
|
t.Fatal("Config should have changed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now with a subscriber requiring restart. We should get a clean response
|
||||||
|
// but with the restart flag set, and the config should change.
|
||||||
|
|
||||||
|
w.Subscribe(requiresRestart{})
|
||||||
|
|
||||||
|
resp = w.Replace(Configuration{Version: 2})
|
||||||
|
if resp.ValidationError != nil {
|
||||||
|
t.Fatal("Should not have a validation error")
|
||||||
|
}
|
||||||
|
if !resp.RequiresRestart {
|
||||||
|
t.Fatal("Should require restart")
|
||||||
|
}
|
||||||
|
if w.Raw().Version != 2 {
|
||||||
|
t.Fatal("Config should have changed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now with a subscriber that throws a validation error. The config should
|
||||||
|
// not change.
|
||||||
|
|
||||||
|
w.Subscribe(validationError{})
|
||||||
|
|
||||||
|
resp = w.Replace(Configuration{Version: 3})
|
||||||
|
if resp.ValidationError == nil {
|
||||||
|
t.Fatal("Should have a validation error")
|
||||||
|
}
|
||||||
|
if resp.RequiresRestart {
|
||||||
|
t.Fatal("Should not require restart")
|
||||||
|
}
|
||||||
|
if w.Raw().Version != 2 {
|
||||||
|
t.Fatal("Config should not have changed")
|
||||||
|
}
|
||||||
|
}
|
@ -20,14 +20,11 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/calmh/logger"
|
|
||||||
"github.com/syncthing/protocol"
|
"github.com/syncthing/protocol"
|
||||||
"github.com/syncthing/syncthing/internal/osutil"
|
"github.com/syncthing/syncthing/internal/osutil"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var l = logger.DefaultLogger
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OldestHandledVersion = 5
|
OldestHandledVersion = 5
|
||||||
CurrentVersion = 10
|
CurrentVersion = 10
|
||||||
|
19
internal/config/debug.go
Normal file
19
internal/config/debug.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (C) 2015 The Syncthing Authors.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/calmh/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
debug = strings.Contains(os.Getenv("STTRACE"), "config") || os.Getenv("STTRACE") == "all"
|
||||||
|
l = logger.DefaultLogger
|
||||||
|
)
|
@ -17,17 +17,39 @@ import (
|
|||||||
"github.com/syncthing/syncthing/internal/sync"
|
"github.com/syncthing/syncthing/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// An interface to handle configuration changes, and a wrapper type á la
|
// The Committer interface is implemented by objects that need to know about
|
||||||
// http.Handler
|
// or have a say in configuration changes.
|
||||||
|
//
|
||||||
type Handler interface {
|
// When the configuration is about to be changed, VerifyConfiguration() is
|
||||||
Changed(Configuration) error
|
// called for each subscribing object, with the old and new configuration. A
|
||||||
|
// nil error is returned if the new configuration is acceptable (i.e. does not
|
||||||
|
// contain any errors that would prevent it from being a valid config).
|
||||||
|
// Otherwise an error describing the problem is returned.
|
||||||
|
//
|
||||||
|
// If any subscriber returns an error from VerifyConfiguration(), the
|
||||||
|
// configuration change is not committed and an error is returned to whoever
|
||||||
|
// tried to commit the broken config.
|
||||||
|
//
|
||||||
|
// If all verification calls returns nil, CommitConfiguration() is called for
|
||||||
|
// each subscribing object. The callee returns true if the new configuration
|
||||||
|
// has been successfully applied, otherwise false. Any Commit() call returning
|
||||||
|
// false will result in a "restart needed" respone to the API/user. Note that
|
||||||
|
// the new configuration will still have been applied by those who were
|
||||||
|
// capable of doing so.
|
||||||
|
type Committer interface {
|
||||||
|
VerifyConfiguration(from, to Configuration) error
|
||||||
|
CommitConfiguration(from, to Configuration) (handled bool)
|
||||||
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandlerFunc func(Configuration) error
|
type CommitResponse struct {
|
||||||
|
ValidationError error
|
||||||
|
RequiresRestart bool
|
||||||
|
}
|
||||||
|
|
||||||
func (fn HandlerFunc) Changed(cfg Configuration) error {
|
var ResponseNoRestart = CommitResponse{
|
||||||
return fn(cfg)
|
ValidationError: nil,
|
||||||
|
RequiresRestart: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// A wrapper around a Configuration that manages loads, saves and published
|
// A wrapper around a Configuration that manages loads, saves and published
|
||||||
@ -42,7 +64,7 @@ type Wrapper struct {
|
|||||||
replaces chan Configuration
|
replaces chan Configuration
|
||||||
mut sync.Mutex
|
mut sync.Mutex
|
||||||
|
|
||||||
subs []Handler
|
subs []Committer
|
||||||
sMut sync.Mutex
|
sMut sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +78,6 @@ func Wrap(path string, cfg Configuration) *Wrapper {
|
|||||||
sMut: sync.NewMutex(),
|
sMut: sync.NewMutex(),
|
||||||
}
|
}
|
||||||
w.replaces = make(chan Configuration)
|
w.replaces = make(chan Configuration)
|
||||||
go w.Serve()
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,21 +98,6 @@ func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
|
|||||||
return Wrap(path, cfg), nil
|
return Wrap(path, cfg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve handles configuration replace events and calls any interested
|
|
||||||
// handlers. It is started automatically by Wrap() and Load() and should not
|
|
||||||
// be run manually.
|
|
||||||
func (w *Wrapper) Serve() {
|
|
||||||
for cfg := range w.replaces {
|
|
||||||
w.sMut.Lock()
|
|
||||||
subs := w.subs
|
|
||||||
w.sMut.Unlock()
|
|
||||||
|
|
||||||
for _, h := range subs {
|
|
||||||
h.Changed(cfg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop stops the Serve() loop. Set and Replace operations will panic after a
|
// Stop stops the Serve() loop. Set and Replace operations will panic after a
|
||||||
// Stop.
|
// Stop.
|
||||||
func (w *Wrapper) Stop() {
|
func (w *Wrapper) Stop() {
|
||||||
@ -100,9 +106,9 @@ func (w *Wrapper) Stop() {
|
|||||||
|
|
||||||
// Subscribe registers the given handler to be called on any future
|
// Subscribe registers the given handler to be called on any future
|
||||||
// configuration changes.
|
// configuration changes.
|
||||||
func (w *Wrapper) Subscribe(h Handler) {
|
func (w *Wrapper) Subscribe(c Committer) {
|
||||||
w.sMut.Lock()
|
w.sMut.Lock()
|
||||||
w.subs = append(w.subs, h)
|
w.subs = append(w.subs, c)
|
||||||
w.sMut.Unlock()
|
w.sMut.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,14 +118,50 @@ func (w *Wrapper) Raw() Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Replace swaps the current configuration object for the given one.
|
// Replace swaps the current configuration object for the given one.
|
||||||
func (w *Wrapper) Replace(cfg Configuration) {
|
func (w *Wrapper) Replace(cfg Configuration) CommitResponse {
|
||||||
w.mut.Lock()
|
w.mut.Lock()
|
||||||
defer w.mut.Unlock()
|
defer w.mut.Unlock()
|
||||||
|
return w.replaceLocked(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
w.cfg = cfg
|
func (w *Wrapper) replaceLocked(to Configuration) CommitResponse {
|
||||||
|
from := w.cfg
|
||||||
|
|
||||||
|
for _, sub := range w.subs {
|
||||||
|
if debug {
|
||||||
|
l.Debugln(sub, "verifying configuration")
|
||||||
|
}
|
||||||
|
if err := sub.VerifyConfiguration(from, to); err != nil {
|
||||||
|
if debug {
|
||||||
|
l.Debugln(sub, "rejected config:", err)
|
||||||
|
}
|
||||||
|
return CommitResponse{
|
||||||
|
ValidationError: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allOk := true
|
||||||
|
for _, sub := range w.subs {
|
||||||
|
if debug {
|
||||||
|
l.Debugln(sub, "committing configuration")
|
||||||
|
}
|
||||||
|
ok := sub.CommitConfiguration(from, to)
|
||||||
|
if !ok {
|
||||||
|
if debug {
|
||||||
|
l.Debugln(sub, "requires restart")
|
||||||
|
}
|
||||||
|
allOk = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.cfg = to
|
||||||
w.deviceMap = nil
|
w.deviceMap = nil
|
||||||
w.folderMap = nil
|
w.folderMap = nil
|
||||||
w.replaces <- cfg.Copy()
|
|
||||||
|
return CommitResponse{
|
||||||
|
RequiresRestart: !allOk,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Devices returns a map of devices. Device structures should not be changed,
|
// Devices returns a map of devices. Device structures should not be changed,
|
||||||
@ -138,22 +180,24 @@ func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
|
|||||||
|
|
||||||
// SetDevice adds a new device to the configuration, or overwrites an existing
|
// SetDevice adds a new device to the configuration, or overwrites an existing
|
||||||
// device with the same ID.
|
// device with the same ID.
|
||||||
func (w *Wrapper) SetDevice(dev DeviceConfiguration) {
|
func (w *Wrapper) SetDevice(dev DeviceConfiguration) CommitResponse {
|
||||||
w.mut.Lock()
|
w.mut.Lock()
|
||||||
defer w.mut.Unlock()
|
defer w.mut.Unlock()
|
||||||
|
|
||||||
w.deviceMap = nil
|
newCfg := w.cfg.Copy()
|
||||||
|
replaced := false
|
||||||
for i := range w.cfg.Devices {
|
for i := range newCfg.Devices {
|
||||||
if w.cfg.Devices[i].DeviceID == dev.DeviceID {
|
if newCfg.Devices[i].DeviceID == dev.DeviceID {
|
||||||
w.cfg.Devices[i] = dev
|
newCfg.Devices[i] = dev
|
||||||
w.replaces <- w.cfg.Copy()
|
replaced = true
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !replaced {
|
||||||
|
newCfg.Devices = append(w.cfg.Devices, dev)
|
||||||
|
}
|
||||||
|
|
||||||
w.cfg.Devices = append(w.cfg.Devices, dev)
|
return w.replaceLocked(newCfg)
|
||||||
w.replaces <- w.cfg.Copy()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Folders returns a map of folders. Folder structures should not be changed,
|
// Folders returns a map of folders. Folder structures should not be changed,
|
||||||
@ -172,22 +216,24 @@ func (w *Wrapper) Folders() map[string]FolderConfiguration {
|
|||||||
|
|
||||||
// SetFolder adds a new folder to the configuration, or overwrites an existing
|
// SetFolder adds a new folder to the configuration, or overwrites an existing
|
||||||
// folder with the same ID.
|
// folder with the same ID.
|
||||||
func (w *Wrapper) SetFolder(fld FolderConfiguration) {
|
func (w *Wrapper) SetFolder(fld FolderConfiguration) CommitResponse {
|
||||||
w.mut.Lock()
|
w.mut.Lock()
|
||||||
defer w.mut.Unlock()
|
defer w.mut.Unlock()
|
||||||
|
|
||||||
w.folderMap = nil
|
newCfg := w.cfg.Copy()
|
||||||
|
replaced := false
|
||||||
for i := range w.cfg.Folders {
|
for i := range newCfg.Folders {
|
||||||
if w.cfg.Folders[i].ID == fld.ID {
|
if newCfg.Folders[i].ID == fld.ID {
|
||||||
w.cfg.Folders[i] = fld
|
newCfg.Folders[i] = fld
|
||||||
w.replaces <- w.cfg.Copy()
|
replaced = true
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !replaced {
|
||||||
|
newCfg.Folders = append(w.cfg.Folders, fld)
|
||||||
|
}
|
||||||
|
|
||||||
w.cfg.Folders = append(w.cfg.Folders, fld)
|
return w.replaceLocked(newCfg)
|
||||||
w.replaces <- w.cfg.Copy()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options returns the current options configuration object.
|
// Options returns the current options configuration object.
|
||||||
@ -198,11 +244,12 @@ func (w *Wrapper) Options() OptionsConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetOptions replaces the current options configuration object.
|
// SetOptions replaces the current options configuration object.
|
||||||
func (w *Wrapper) SetOptions(opts OptionsConfiguration) {
|
func (w *Wrapper) SetOptions(opts OptionsConfiguration) CommitResponse {
|
||||||
w.mut.Lock()
|
w.mut.Lock()
|
||||||
defer w.mut.Unlock()
|
defer w.mut.Unlock()
|
||||||
w.cfg.Options = opts
|
newCfg := w.cfg.Copy()
|
||||||
w.replaces <- w.cfg.Copy()
|
newCfg.Options = opts
|
||||||
|
return w.replaceLocked(newCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GUI returns the current GUI configuration object.
|
// GUI returns the current GUI configuration object.
|
||||||
@ -213,11 +260,12 @@ func (w *Wrapper) GUI() GUIConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetGUI replaces the current GUI configuration object.
|
// SetGUI replaces the current GUI configuration object.
|
||||||
func (w *Wrapper) SetGUI(gui GUIConfiguration) {
|
func (w *Wrapper) SetGUI(gui GUIConfiguration) CommitResponse {
|
||||||
w.mut.Lock()
|
w.mut.Lock()
|
||||||
defer w.mut.Unlock()
|
defer w.mut.Unlock()
|
||||||
w.cfg.GUI = gui
|
newCfg := w.cfg.Copy()
|
||||||
w.replaces <- w.cfg.Copy()
|
newCfg.GUI = gui
|
||||||
|
return w.replaceLocked(newCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IgnoredDevice returns whether or not connection attempts from the given
|
// IgnoredDevice returns whether or not connection attempts from the given
|
||||||
|
@ -15,6 +15,7 @@ package db
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/syncthing/protocol"
|
"github.com/syncthing/protocol"
|
||||||
@ -125,15 +126,22 @@ func NewBlockFinder(db *leveldb.DB, cfg *config.Wrapper) *BlockFinder {
|
|||||||
db: db,
|
db: db,
|
||||||
mut: sync.NewRWMutex(),
|
mut: sync.NewRWMutex(),
|
||||||
}
|
}
|
||||||
f.Changed(cfg.Raw())
|
|
||||||
|
f.CommitConfiguration(config.Configuration{}, cfg.Raw())
|
||||||
cfg.Subscribe(f)
|
cfg.Subscribe(f)
|
||||||
|
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// Changed implements config.Handler interface
|
// VerifyConfiguration implementes the config.Committer interface
|
||||||
func (f *BlockFinder) Changed(cfg config.Configuration) error {
|
func (f *BlockFinder) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
folders := make([]string, len(cfg.Folders))
|
return nil
|
||||||
for i, folder := range cfg.Folders {
|
}
|
||||||
|
|
||||||
|
// CommitConfiguration implementes the config.Committer interface
|
||||||
|
func (f *BlockFinder) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
|
folders := make([]string, len(to.Folders))
|
||||||
|
for i, folder := range to.Folders {
|
||||||
folders[i] = folder.ID
|
folders[i] = folder.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +151,11 @@ func (f *BlockFinder) Changed(cfg config.Configuration) error {
|
|||||||
f.folders = folders
|
f.folders = folders
|
||||||
f.mut.Unlock()
|
f.mut.Unlock()
|
||||||
|
|
||||||
return nil
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *BlockFinder) String() string {
|
||||||
|
return fmt.Sprintf("BlockFinder@%p", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate takes an iterator function which iterates over all matching blocks
|
// Iterate takes an iterator function which iterates over all matching blocks
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
stdsync "sync"
|
stdsync "sync"
|
||||||
@ -1718,6 +1719,37 @@ func (m *Model) String() string {
|
|||||||
return fmt.Sprintf("model@%p", m)
|
return fmt.Sprintf("model@%p", m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Model) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
|
// TODO: This should not use reflect, and should take more care to try to handle stuff without restart.
|
||||||
|
|
||||||
|
// Adding, removing or changing folders requires restart
|
||||||
|
if !reflect.DeepEqual(from.Folders, to.Folders) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removing a device requres restart
|
||||||
|
toDevs := make(map[protocol.DeviceID]bool, len(from.Devices))
|
||||||
|
for _, dev := range to.Devices {
|
||||||
|
toDevs[dev.DeviceID] = true
|
||||||
|
}
|
||||||
|
for _, dev := range from.Devices {
|
||||||
|
if _, ok := toDevs[dev.DeviceID]; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All of the generic options require restart
|
||||||
|
if !reflect.DeepEqual(from.Options, to.Options) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func symlinkInvalid(isLink bool) bool {
|
func symlinkInvalid(isLink bool) bool {
|
||||||
if !symlinks.Supported && isLink {
|
if !symlinks.Supported && isLink {
|
||||||
SymlinkWarning.Do(func() {
|
SymlinkWarning.Do(func() {
|
||||||
|
@ -317,21 +317,22 @@ func TestDeviceRename(t *testing.T) {
|
|||||||
|
|
||||||
defer os.Remove("tmpconfig.xml")
|
defer os.Remove("tmpconfig.xml")
|
||||||
|
|
||||||
cfg := config.New(device1)
|
rawCfg := config.New(device1)
|
||||||
cfg.Devices = []config.DeviceConfiguration{
|
rawCfg.Devices = []config.DeviceConfiguration{
|
||||||
{
|
{
|
||||||
DeviceID: device1,
|
DeviceID: device1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cfg := config.Wrap("tmpconfig.xml", rawCfg)
|
||||||
|
|
||||||
db, _ := leveldb.Open(storage.NewMemStorage(), nil)
|
db, _ := leveldb.Open(storage.NewMemStorage(), nil)
|
||||||
m := NewModel(config.Wrap("tmpconfig.xml", cfg), protocol.LocalDeviceID, "device", "syncthing", "dev", db)
|
m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", db)
|
||||||
if cfg.Devices[0].Name != "" {
|
if cfg.Devices()[device1].Name != "" {
|
||||||
t.Errorf("Device already has a name")
|
t.Errorf("Device already has a name")
|
||||||
}
|
}
|
||||||
|
|
||||||
m.ClusterConfig(device1, ccm)
|
m.ClusterConfig(device1, ccm)
|
||||||
if cfg.Devices[0].Name != "" {
|
if cfg.Devices()[device1].Name != "" {
|
||||||
t.Errorf("Device already has a name")
|
t.Errorf("Device already has a name")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,13 +343,13 @@ func TestDeviceRename(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.ClusterConfig(device1, ccm)
|
m.ClusterConfig(device1, ccm)
|
||||||
if cfg.Devices[0].Name != "tester" {
|
if cfg.Devices()[device1].Name != "tester" {
|
||||||
t.Errorf("Device did not get a name")
|
t.Errorf("Device did not get a name")
|
||||||
}
|
}
|
||||||
|
|
||||||
ccm.Options[0].Value = "tester2"
|
ccm.Options[0].Value = "tester2"
|
||||||
m.ClusterConfig(device1, ccm)
|
m.ClusterConfig(device1, ccm)
|
||||||
if cfg.Devices[0].Name != "tester" {
|
if cfg.Devices()[device1].Name != "tester" {
|
||||||
t.Errorf("Device name got overwritten")
|
t.Errorf("Device name got overwritten")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
@ -37,8 +38,10 @@ func NewProgressEmitter(cfg *config.Wrapper) *ProgressEmitter {
|
|||||||
timer: time.NewTimer(time.Millisecond),
|
timer: time.NewTimer(time.Millisecond),
|
||||||
mut: sync.NewMutex(),
|
mut: sync.NewMutex(),
|
||||||
}
|
}
|
||||||
t.Changed(cfg.Raw())
|
|
||||||
|
t.CommitConfiguration(config.Configuration{}, cfg.Raw())
|
||||||
cfg.Subscribe(t)
|
cfg.Subscribe(t)
|
||||||
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,17 +84,22 @@ func (t *ProgressEmitter) Serve() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Changed implements the config.Handler Interface to handle configuration
|
// VerifyConfiguration implements the config.Committer interface
|
||||||
// changes
|
func (t *ProgressEmitter) VerifyConfiguration(from, to config.Configuration) error {
|
||||||
func (t *ProgressEmitter) Changed(cfg config.Configuration) error {
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommitConfiguration implements the config.Committer interface
|
||||||
|
func (t *ProgressEmitter) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
t.mut.Lock()
|
t.mut.Lock()
|
||||||
defer t.mut.Unlock()
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
t.interval = time.Duration(cfg.Options.ProgressUpdateIntervalS) * time.Second
|
t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugln("progress emitter: updated interval", t.interval)
|
l.Debugln("progress emitter: updated interval", t.interval)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops the emitter.
|
// Stop stops the emitter.
|
||||||
@ -138,3 +146,7 @@ func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *ProgressEmitter) String() string {
|
||||||
|
return fmt.Sprintf("ProgressEmitter@%p", t)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user