AdGuardHome/internal/confmigrate/migrator.go
Eugene Burkov 1e45178980 Pull request 1991: AG-25392 confmigrate vol.2
Merge in DNS/adguard-home from AG-25392-confmigrate-vol.2 to master

Squashed commit of the following:

commit 7bcdf443135523022a7e5ed6b2da05fcd784a896
Merge: 4c25331b5 f84ff2bd0
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 20:33:08 2023 +0300

    Merge branch 'master' into AG-25392-confmigrate-vol.2

commit 4c25331b507078670e7f3505384b53179a210292
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 20:14:23 2023 +0300

    confmigrate: fix fmt

commit d7dbfc46a7f825d248cb028d50b60eede941bf67
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 20:07:25 2023 +0300

    confmigrate: imp code

commit cd29729a23d1782a0b5fbd1e9153fe61ff3dbc71
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 18:21:14 2023 +0300

    confmigrate: imp code, split files

commit f075a14c251c68a13152709e14a326ad17c2f889
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 17:19:47 2023 +0300

    confmigrate: imp code

commit 37335597e7870184528c1d868e2d5bd5525a74bb
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 16:54:13 2023 +0300

    confmigrate: imp code, docs

commit 1df0ddcad99e5056b4989d17490ca4b44ae17838
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Sep 4 14:41:52 2023 +0300

    all: add testdata

commit e62690dbe8e458811bbd6b115b4449affa729560
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 31 16:13:14 2023 +0300

    all: imp code

commit 0934d5974b31a58b31b12412c3cd1a50810ffc4f
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 30 15:50:52 2023 +0300

    home: don't reread config

commit 7566ab6f330460bd608aae6dc0428a631e2fa4e3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 30 15:43:57 2023 +0300

    all: imp code

commit 6f6aae6290f8d3101f3678ecc705eee7030ad8d5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 30 15:25:46 2023 +0300

    all: introduce confmigrate
2023-09-04 20:45:50 +03:00

139 lines
3.8 KiB
Go

// Package confmigrate provides a way to upgrade the YAML configuration file.
package confmigrate
import (
"bytes"
"fmt"
"github.com/AdguardTeam/golibs/log"
yaml "gopkg.in/yaml.v3"
)
// LastSchemaVersion is the most recent schema version.
const LastSchemaVersion uint = 26
// Config is a the configuration for initializing a [Migrator].
type Config struct {
// WorkingDir is an absolute path to the working directory of AdGuardHome.
WorkingDir string
}
// Migrator performs the YAML configuration file migrations.
type Migrator struct {
// workingDir is an absolute path to the working directory of AdGuardHome.
workingDir string
}
// New creates a new Migrator.
func New(cfg *Config) (m *Migrator) {
return &Migrator{
workingDir: cfg.WorkingDir,
}
}
// Migrate preforms necessary upgrade operations to upgrade file to target
// schema version, if needed. It returns the body of the upgraded config file,
// whether the file was upgraded, and an error, if any. If upgraded is false,
// the body is the same as the input.
func (m *Migrator) Migrate(body []byte, target uint) (newBody []byte, upgraded bool, err error) {
diskConf := yobj{}
err = yaml.Unmarshal(body, &diskConf)
if err != nil {
return body, false, fmt.Errorf("parsing config file for upgrade: %w", err)
}
currentInt, _, err := fieldVal[int](diskConf, "schema_version")
if err != nil {
// Don't wrap the error, since it's informative enough as is.
return body, false, err
}
current := uint(currentInt)
log.Debug("got schema version %v", current)
if err = validateVersion(current, target); err != nil {
// Don't wrap the error, since it's informative enough as is.
return body, false, err
} else if current == target {
return body, false, nil
}
if err = m.upgradeConfigSchema(current, target, diskConf); err != nil {
// Don't wrap the error, since it's informative enough as is.
return body, false, err
}
buf := bytes.NewBuffer(newBody)
enc := yaml.NewEncoder(buf)
enc.SetIndent(2)
if err = enc.Encode(diskConf); err != nil {
return body, false, fmt.Errorf("generating new config: %w", err)
}
return buf.Bytes(), true, nil
}
// validateVersion validates the current and desired schema versions.
func validateVersion(current, target uint) (err error) {
switch {
case current > target:
return fmt.Errorf("unknown current schema version %d", current)
case target > LastSchemaVersion:
return fmt.Errorf("unknown target schema version %d", target)
case target < current:
return fmt.Errorf("target schema version %d lower than current %d", target, current)
default:
return nil
}
}
// migrateFunc is a function that upgrades a config and returns an error.
type migrateFunc = func(diskConf yobj) (err error)
// upgradeConfigSchema upgrades the configuration schema in diskConf from
// current to target version. current must be less than target, and both must
// be non-negative and less or equal to [LastSchemaVersion].
func (m *Migrator) upgradeConfigSchema(current, target uint, diskConf yobj) (err error) {
upgrades := [LastSchemaVersion]migrateFunc{
0: m.migrateTo1,
1: m.migrateTo2,
2: migrateTo3,
3: migrateTo4,
4: migrateTo5,
5: migrateTo6,
6: migrateTo7,
7: migrateTo8,
8: migrateTo9,
9: migrateTo10,
10: migrateTo11,
11: migrateTo12,
12: migrateTo13,
13: migrateTo14,
14: migrateTo15,
15: migrateTo16,
16: migrateTo17,
17: migrateTo18,
18: migrateTo19,
19: migrateTo20,
20: migrateTo21,
21: migrateTo22,
22: migrateTo23,
23: migrateTo24,
24: migrateTo25,
25: migrateTo26,
}
for i, migrate := range upgrades[current:target] {
cur := current + uint(i)
next := current + uint(i) + 1
log.Printf("Upgrade yaml: %d to %d", cur, next)
if err = migrate(diskConf); err != nil {
return fmt.Errorf("migrating schema %d to %d: %w", cur, next, err)
}
}
return nil
}