Migrate config

This commit is contained in:
Audrius Butkevicius 2014-09-28 13:22:39 +01:00
parent f561d3261a
commit d4dc7911eb
3 changed files with 67 additions and 18 deletions

View File

@ -30,6 +30,9 @@ type Configuration struct {
GUI GUIConfiguration `xml:"gui"`
Options OptionsConfiguration `xml:"options"`
XMLName xml.Name `xml:"configuration" json:"-"`
Deprecated_Repositories []FolderConfiguration `xml:"repository" json:"-"`
Deprecated_Nodes []DeviceConfiguration `xml:"node" json:"-"`
}
type FolderConfiguration struct {
@ -43,6 +46,9 @@ type FolderConfiguration struct {
Versioning VersioningConfiguration `xml:"versioning"`
deviceIDs []protocol.DeviceID
Depreceted_Directory string `xml:"directory,omitempty,attr" json:"-"`
Depreceted_Nodes []FolderDeviceConfiguration `xml:"node" json:"-"`
}
type VersioningConfiguration struct {
@ -352,6 +358,11 @@ func (cfg *Configuration) prepare(myID protocol.DeviceID) {
convertV3V4(cfg)
}
// Upgrade to v5 configuration if appropriate
if cfg.Version == 4 {
convertV4V5(cfg)
}
// Hash old cleartext passwords
if len(cfg.GUI.Password) > 0 && cfg.GUI.Password[0] != '$' {
hash, err := bcrypt.GenerateFromPassword([]byte(cfg.GUI.Password), 0)
@ -465,12 +476,38 @@ func ChangeRequiresRestart(from, to Configuration) bool {
return false
}
func convertV4V5(cfg *Configuration) {
// Renamed a bunch of fields in the structs.
if cfg.Deprecated_Nodes == nil {
cfg.Deprecated_Nodes = []DeviceConfiguration{}
}
if cfg.Deprecated_Repositories == nil {
cfg.Deprecated_Repositories = []FolderConfiguration{}
}
cfg.Devices = cfg.Deprecated_Nodes
cfg.Folders = cfg.Deprecated_Repositories
for i := range cfg.Folders {
cfg.Folders[i].Path = cfg.Folders[i].Depreceted_Directory
cfg.Folders[i].Depreceted_Directory = ""
cfg.Folders[i].Devices = cfg.Folders[i].Depreceted_Nodes
cfg.Folders[i].Depreceted_Nodes = nil
}
cfg.Deprecated_Nodes = nil
cfg.Deprecated_Repositories = nil
cfg.Version = 5
}
func convertV3V4(cfg *Configuration) {
// In previous versions, rescan interval was common for each folder.
// From now, it can be set independently. We have to make sure, that after upgrade
// the individual rescan interval will be defined for every existing folder.
for i := range cfg.Folders {
cfg.Folders[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
for i := range cfg.Deprecated_Repositories {
cfg.Deprecated_Repositories[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
}
cfg.Options.Deprecated_RescanIntervalS = 0
@ -478,10 +515,10 @@ func convertV3V4(cfg *Configuration) {
// In previous versions, folders held full device configurations.
// Since that's the only place where device configs were in V1, we still have
// to define the deprecated fields to be able to upgrade from V1 to V4.
for i, folder := range cfg.Folders {
for i, folder := range cfg.Deprecated_Repositories {
for j := range folder.Devices {
rncfg := cfg.Folders[i].Devices[j]
for j := range folder.Depreceted_Nodes {
rncfg := cfg.Deprecated_Repositories[i].Depreceted_Nodes[j]
rncfg.Deprecated_Name = ""
rncfg.Deprecated_Addresses = nil
}
@ -494,8 +531,8 @@ func convertV2V3(cfg *Configuration) {
// In previous versions, compression was always on. When upgrading, enable
// compression on all existing new. New devices will get compression on by
// default by the GUI.
for i := range cfg.Devices {
cfg.Devices[i].Compression = true
for i := range cfg.Deprecated_Nodes {
cfg.Deprecated_Nodes[i].Compression = true
}
// The global discovery format and port number changed in v0.9. Having the
@ -513,27 +550,27 @@ func convertV1V2(cfg *Configuration) {
// device ID. Set all folders to read only if the global read only flag is
// set.
var devices = map[string]FolderDeviceConfiguration{}
for i, folder := range cfg.Folders {
cfg.Folders[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
for j, device := range folder.Devices {
for i, folder := range cfg.Deprecated_Repositories {
cfg.Deprecated_Repositories[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
for j, device := range folder.Depreceted_Nodes {
id := device.DeviceID.String()
if _, ok := devices[id]; !ok {
devices[id] = device
}
cfg.Folders[i].Devices[j] = FolderDeviceConfiguration{DeviceID: device.DeviceID}
cfg.Deprecated_Repositories[i].Depreceted_Nodes[j] = FolderDeviceConfiguration{DeviceID: device.DeviceID}
}
}
cfg.Options.Deprecated_ReadOnly = false
// Set and sort the list of devices.
for _, device := range devices {
cfg.Devices = append(cfg.Devices, DeviceConfiguration{
cfg.Deprecated_Nodes = append(cfg.Deprecated_Nodes, DeviceConfiguration{
DeviceID: device.DeviceID,
Name: device.Deprecated_Name,
Addresses: device.Deprecated_Addresses,
})
}
sort.Sort(DeviceConfigurationList(cfg.Devices))
sort.Sort(DeviceConfigurationList(cfg.Deprecated_Nodes))
// GUI
cfg.GUI.Address = cfg.Options.Deprecated_GUIAddress

View File

@ -47,7 +47,7 @@ func TestDefaultValues(t *testing.T) {
}
func TestDeviceConfig(t *testing.T) {
for i, ver := range []string{"v1", "v2", "v3", "v4"} {
for i, ver := range []string{"v1", "v2", "v3", "v4", "v5"} {
cfg, err := Load("testdata/"+ver+".xml", device1)
if err != nil {
t.Error(err)
@ -65,21 +65,21 @@ func TestDeviceConfig(t *testing.T) {
expectedDevices := []DeviceConfiguration{
{
DeviceID: device1,
Name: "device one",
Name: "node one",
Addresses: []string{"a"},
Compression: true,
},
{
DeviceID: device4,
Name: "device two",
Name: "node two",
Addresses: []string{"b"},
Compression: true,
},
}
expectedDeviceIDs := []protocol.DeviceID{device1, device4}
if cfg.Version != 4 {
t.Errorf("%d: Incorrect version %d != 3", i, cfg.Version)
if cfg.Version != 5 {
t.Errorf("%d: Incorrect version %d != 5", i, cfg.Version)
}
if !reflect.DeepEqual(cfg.Folders, expectedFolders) {
t.Errorf("%d: Incorrect Folders\n A: %#v\n E: %#v", i, cfg.Folders, expectedFolders)

12
internal/config/testdata/v5.xml vendored Executable file
View File

@ -0,0 +1,12 @@
<configuration version="5">
<folder id="test" path="~/Sync" ro="true" ignorePerms="false" rescanIntervalS="600">
<device id="AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR"></device>
<device id="P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"></device>
</folder>
<device id="AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR" name="node one" compression="true">
<address>a</address>
</device>
<device id="P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2" name="node two" compression="true">
<address>b</address>
</device>
</configuration>