Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upgrade): fallback to user's home when saving upgraded config #19773

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 12 additions & 42 deletions cmd/influxd/upgrade/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ package upgrade

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -85,15 +82,9 @@ func upgradeConfig(configFile string, targetOptions optionsV2, log *zap.Logger)
// update new config with upgrade command options
cu.updateV2Config(cTransformed, targetOptions)

// backup existing 2.x config if already exists (it should not)
configFileV2 := strings.TrimSuffix(configFile, filepath.Ext(configFile)) + ".toml"
err = cu.backupIfExists(configFileV2)
if err != nil {
return nil, err
}

// save new config
err = cu.save(cTransformed, configFileV2)
configFileV2 := strings.TrimSuffix(configFile, filepath.Ext(configFile)) + ".toml"
configFileV2, err = cu.save(cTransformed, configFileV2)
if err != nil {
return nil, err
}
Expand All @@ -111,34 +102,6 @@ type configUpgrader struct {
log *zap.Logger
}

func (cu *configUpgrader) backupIfExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}

source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()

backupFile := path + "~"
if _, err := os.Stat(backupFile); !os.IsNotExist(err) {
errMsg := fmt.Sprintf("upgrade: config file backup %s already exist", backupFile)
return errors.New(errMsg)
}

destination, err := os.Create(backupFile)
if err != nil {
return err
}
defer destination.Close()

_, err = io.Copy(destination, source)

return err
}

func (cu *configUpgrader) updateV2Config(config map[string]interface{}, targetOptions optionsV2) {
if targetOptions.enginePath != "" {
config["engine-path"] = targetOptions.enginePath
Expand All @@ -165,13 +128,20 @@ func (cu *configUpgrader) load(path string) ([]byte, error) {
return bs, err
}

func (cu *configUpgrader) save(config map[string]interface{}, path string) error {
func (cu *configUpgrader) save(config map[string]interface{}, path string) (string, error) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(&config); err != nil {
return err
return "", err
}

err := ioutil.WriteFile(path, buf.Bytes(), 0666)
if err != nil { // permission issue possible - try to save the file to home or current dir
cu.log.Warn(fmt.Sprintf("Could not save upgraded config to %s, trying to save it to user's home.", path), zap.Error(err))
path = filepath.Join(homeOrAnyDir(), filepath.Base(path))
err = ioutil.WriteFile(path, buf.Bytes(), 0666)
}

return ioutil.WriteFile(path, buf.Bytes(), 0666)
return path, err
}

// Credits: @rogpeppe (Roger Peppe)
Expand Down
73 changes: 0 additions & 73 deletions cmd/influxd/upgrade/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,79 +105,6 @@ func TestConfigUpgrade(t *testing.T) {
})
}
}

func TestConfigUpgradeWithBackup(t *testing.T) {
targetOtions := optionsV2{
boltPath: "/db/.influxdbv2/influxd.bolt",
enginePath: "/db/.influxdbv2/engine",
}

type testCase struct {
name string
config1x string
config2x string
}

var testCases = []testCase{
{
name: "default",
config1x: testConfigV1default,
config2x: testConfigV2default,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
configFile := testCreateTempFile(t, "influxdb-*.conf", tc.config1x)
configFileV2 := strings.TrimSuffix(configFile, filepath.Ext(configFile)) + ".toml"
configFileV2Backup := strings.TrimSuffix(configFile, filepath.Ext(configFile)) + ".toml~"
defer func() {
os.Remove(configFile)
os.Remove(configFileV2)
os.Remove(configFileV2Backup)
}()

// upgrade
_, err := upgradeConfig(configFile, targetOtions, zap.NewNop())
if err != nil {
t.Fatal(err)
}

// upgrade again, backup of existing newly created config should get created
_, err = upgradeConfig(configFile, targetOtions, zap.NewNop())
if err != nil {
t.Fatal(err)
}

// validate new config
var actual, expected map[string]interface{}
if _, err = toml.Decode(tc.config2x, &expected); err != nil {
t.Fatal(err)
}
if _, err = toml.DecodeFile(configFileV2, &actual); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatal(diff)
}

// validate backup of the new config, it should exists and be the same
var actualBackup, expectedBackup map[string]interface{}
if _, err = toml.Decode(tc.config2x, &expectedBackup); err != nil {
t.Fatal(err)
}
if _, err = toml.DecodeFile(configFileV2Backup, &actualBackup); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expectedBackup, actualBackup); diff != "" {
t.Fatal(diff)
}
})
}
}

func TestConfigUpgradeFileNotExists(t *testing.T) {
targetOtions := optionsV2{
}
Expand Down