1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 10:49:24 +08:00

config-patch: backup config

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2017-12-15 20:55:31 +01:00
parent 2514c74750
commit 0ff9b24a32
5 changed files with 48 additions and 0 deletions

View File

@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error {
return err return err
} }
_, err = r.BackupConfig("profile-")
if err != nil {
return err
}
return r.SetConfig(cfg) return r.SetConfig(cfg)
} }

View File

@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager {
return r.filemgr 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. // setConfigUnsynced is for private use.
func (r *FSRepo) setConfigUnsynced(updated *config.Config) error { func (r *FSRepo) setConfigUnsynced(updated *config.Config) error {
configFilename, err := config.Filename(r.path) configFilename, err := config.Filename(r.path)

View File

@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error {
return nil return nil
} }
func (m *Mock) BackupConfig(prefix string) (string, error) {
return "", errTODO
}
func (m *Mock) SetConfigKey(key string, value interface{}) error { func (m *Mock) SetConfigKey(key string, value interface{}) error {
return errTODO return errTODO
} }

View File

@ -18,6 +18,7 @@ var (
type Repo interface { type Repo interface {
Config() (*config.Config, error) Config() (*config.Config, error)
BackupConfig(prefix string) (string, error)
SetConfig(*config.Config) error SetConfig(*config.Config) error
SetConfigKey(key string, value interface{}) error SetConfigKey(key string, value interface{}) error

View File

@ -183,10 +183,18 @@ test_config_cmd() {
test $(cat actual_config | wc -l) = 1 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" ' test_expect_success "'ipfs config profile apply server' works" '
ipfs config profile apply server 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" ' test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
ipfs config Swarm.AddrFilters > actual_config && ipfs config Swarm.AddrFilters > actual_config &&
test $(cat actual_config | wc -l) = 17 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 # won't work as it changes datastore definition, which makes ipfs not launch
# without converting first # without converting first
# test_profile_apply_revert badgerds # test_profile_apply_revert badgerds
test_expect_success "cleanup config backups" '
find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
'
} }
test_init_ipfs test_init_ipfs