Skip to content

Commit

Permalink
feat(cmd/influxd): use a better default for upgraded V2 config, and a…
Browse files Browse the repository at this point in the history
…llow users to override it (#20110)
  • Loading branch information
danxmoran authored Nov 23, 2020
1 parent ffcfe82 commit 89764ad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
## unreleased

### Breaking Changes

Previously, `influxd upgrade` would attempt to write upgraded `config.toml` files into the same directory as the source
`influxdb.conf` file. If this failed, a warning would be logged and `config.toml` would be written into the `HOME` directory.

This release breaks this behavior in two ways:
1. By default, `config.toml` is now written into the same directory as the Bolt DB and engine files (`~/.influxdbv2/`)
2. If writing upgraded config fails, the `upgrade` process exits with an error instead of falling back to the `HOME` directory

Users can use the new `--v2-config-path` option to override the output path for upgraded config if they can't or don't
want to use the default.

### Features

1. [20123](https://github.com/influxdata/influxdb/pull/20123): Allow password to be specified as a CLI option in `influx v1 auth create`.
1. [20123](https://github.com/influxdata/influxdb/pull/20123): Allow password to be specified as a CLI option in `influx v1 auth set-password`.
1. [20110](https://github.com/influxdata/influxdb/pull/20110): Allow for users to specify where V2 config should be written in `influxd upgrade`.

### Bug Fixes

1. [20110](https://github.com/influxdata/influxdb/pull/20110): Use V2 directory for default V2 config path in `influxd upgrade`.

## v2.0.2 [2020-11-19]

### Features
Expand Down
33 changes: 14 additions & 19 deletions cmd/influxd/upgrade/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package upgrade
// The strategy is to transform only those entries for which rule exists.

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -109,25 +108,22 @@ func load(path string) ([]byte, error) {
}

// upgradeConfig upgrades existing 1.x configuration file to 2.x influxdb.toml file.
func upgradeConfig(v1Config map[string]interface{}, targetOptions optionsV2, log *zap.Logger) (string, error) {
func upgradeConfig(v1Config map[string]interface{}, targetOptions optionsV2, log *zap.Logger) error {
// create and initialize helper
cu := &configUpgrader{
rules: configMapRules,
valueTransforms: configValueTransforms,
log: log,
}

// rewrite config options from V1 to V2 paths
cTransformed := cu.transform(v1Config)

// update new config with upgrade command options
cu.updateV2Config(cTransformed, targetOptions)

configFileV2, err := cu.save(cTransformed, targetOptions.configPath)
if err != nil {
return "", err
}

return configFileV2, nil
// write the ugpraded config to disk
return cu.save(cTransformed, targetOptions.configPath)
}

// configUpgrader is a helper used by `upgrade-config` command.
Expand All @@ -146,20 +142,19 @@ func (cu *configUpgrader) updateV2Config(config map[string]interface{}, targetOp
}
}

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
func (cu *configUpgrader) save(config map[string]interface{}, path string) error {
// Open the target file, creating parent directories if needed.
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
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)
outFile, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer outFile.Close()

return path, err
// Encode the config directly into the file as TOML.
return toml.NewEncoder(outFile).Encode(&config)
}

// Credits: @rogpeppe (Roger Peppe)
Expand Down
7 changes: 2 additions & 5 deletions cmd/influxd/upgrade/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ func TestConfigUpgrade(t *testing.T) {
if _, err = toml.Decode(tc.config1x, &rawV1Config); err != nil {
t.Fatal(err)
}
retval, err := upgradeConfig(rawV1Config, targetOtions, zaptest.NewLogger(t))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, retval, configFileV2)
err = upgradeConfig(rawV1Config, targetOtions, zaptest.NewLogger(t))
assert.NoError(t, err)

var actual, expected map[string]interface{}
if _, err = toml.Decode(tc.config2x, &expected); err != nil {
Expand Down
14 changes: 9 additions & 5 deletions cmd/influxd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ func NewCommand(v *viper.Viper) *cobra.Command {
Flag: "config-file",
Desc: "optional: Custom InfluxDB 1.x config file path, else the default config file",
},
{
DestP: &options.target.configPath,
Flag: "v2-config-path",
Default: filepath.Join(v2dir, "config.toml"),
Desc: "optional: Custom path where upgraded 2.x config should be written",
},
{
DestP: &options.logLevel,
Flag: "log-level",
Expand Down Expand Up @@ -355,8 +361,6 @@ func runUpgradeE(*cobra.Command, []string) error {
options.source.dataDir = v1Config.Data.Dir
options.source.walDir = v1Config.Data.WALDir
options.source.dbURL = v1Config.dbURL()

options.target.configPath = filepath.Join(filepath.Dir(options.source.configFile), "config.toml")
} else {
// Otherwise, assume a standard directory layout.
options.source.populateDirs()
Expand All @@ -371,15 +375,15 @@ func runUpgradeE(*cobra.Command, []string) error {

if genericV1ops != nil {
log.Info("Upgrading config file", zap.String("file", options.source.configFile))
v2ConfigPath, err := upgradeConfig(*genericV1ops, options.target, log)
if err != nil {
if err := upgradeConfig(*genericV1ops, options.target, log); err != nil {
return err
}
log.Info(
"Config file upgraded.",
zap.String("1.x config", options.source.configFile),
zap.String("2.x config", v2ConfigPath),
zap.String("2.x config", options.target.configPath),
)

} else {
log.Info("No InfluxDB 1.x config file specified, skipping its upgrade")
}
Expand Down

0 comments on commit 89764ad

Please sign in to comment.