From bebdd2bf7a446f4bead0b3f962c5204774a5c443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 15 Dec 2017 20:55:31 +0100 Subject: [PATCH] config-patch: backup config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/commands/config.go | 5 +++++ repo/fsrepo/fsrepo.go | 26 ++++++++++++++++++++++++++ repo/mock.go | 4 ++++ repo/repo.go | 1 + test/sharness/t0021-config.sh | 12 ++++++++++++ 5 files changed, 48 insertions(+) diff --git a/core/commands/config.go b/core/commands/config.go index 8736dd77f67b..24875c3ea1c8 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error { return err } + _, err = r.BackupConfig("profile-") + if err != nil { + return err + } + return r.SetConfig(cfg) } diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 757c36438aeb..51823042a77e 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager { return r.filemgr } +func (r *FSRepo) BackupConfig(prefix string) (string, error) { + temp, err := ioutil.TempFile(r.path, "config-"+prefix) + if err != nil { + return "", err + } + defer temp.Close() + + configFilename, err := config.Filename(r.path) + if err != nil { + return "", err + } + + orig, err := os.OpenFile(configFilename, os.O_RDONLY, 0600) + if err != nil { + return "", err + } + defer orig.Close() + + _, err = io.Copy(temp, orig) + if err != nil { + return "", err + } + + return orig.Name(), nil +} + // setConfigUnsynced is for private use. func (r *FSRepo) setConfigUnsynced(updated *config.Config) error { configFilename, err := config.Filename(r.path) diff --git a/repo/mock.go b/repo/mock.go index 030c2ff28d1a..3d05b8b7649f 100644 --- a/repo/mock.go +++ b/repo/mock.go @@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error { return nil } +func (m *Mock) BackupConfig(prefix string) (string, error) { + return "config-" + prefix + "-backup", nil +} + func (m *Mock) SetConfigKey(key string, value interface{}) error { return errTODO } diff --git a/repo/repo.go b/repo/repo.go index 0cbf8f536690..3403482c9ea8 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -18,6 +18,7 @@ var ( type Repo interface { Config() (*config.Config, error) + BackupConfig(prefix string) (string, error) SetConfig(*config.Config) error SetConfigKey(key string, value interface{}) error diff --git a/test/sharness/t0021-config.sh b/test/sharness/t0021-config.sh index a7ff59bcfcbb..9a086b1d8dd6 100755 --- a/test/sharness/t0021-config.sh +++ b/test/sharness/t0021-config.sh @@ -183,10 +183,18 @@ test_config_cmd() { test $(cat actual_config | wc -l) = 1 ' + test_expect_success "copy ipfs config" ' + cp "$IPFS_PATH/config" before_patch + ' + test_expect_success "'ipfs config profile apply server' works" ' ipfs config profile apply server ' + test_expect_success "backup was created and looks good" ' + test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch + ' + test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" ' ipfs config Swarm.AddrFilters > actual_config && test $(cat actual_config | wc -l) = 17 @@ -209,6 +217,10 @@ test_config_cmd() { # won't work as it changes datastore definition, which makes ipfs not launch # without converting first # test_profile_apply_revert badgerds + + test_expect_success "cleanup config backups" ' + find "$IPFS_PATH" -name "config-profile*" -exec rm {} \; + ' } test_init_ipfs