Skip to content

Commit

Permalink
fix: various improvements to setup command (#20155)
Browse files Browse the repository at this point in the history
When checking for existing configs, check the length of the array after
parsing. There could be an empty config file present.

If --name was specified then use that as the configuration name, regardless of
whether or not there are existing configs.

Allow a 0 (infinite) retention to be specified with --retention when in
interactive mode.

Co-authored-by: Dan Moran <dmoran@influxdata.com>
  • Loading branch information
adrian-thurston and danxmoran committed Dec 3, 2020
1 parent e974b0b commit c020bb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ want to use the default.
1. [20166](https://github.com/influxdata/influxdb/pull/20166): Enforce max value of 2147483647 on query queue size to avoid startup panic.
1. [20182](https://github.com/influxdata/influxdb/pull/20182): Auto-migrate existing DBRP mappings from old schema to avoid panic.
1. [20202](https://github.com/influxdata/influxdb/pull/20202): Optimize shard lookup in groups containing only one shard. Thanks @StoneYunZhao!
1. [20155](https://github.com/influxdata/influxdb/pull/20155): Respect the `--name` option in `influx setup` whether configs already exist or not.
1. [20155](https://github.com/influxdata/influxdb/pull/20155): Allow for 0 (infinite) values for `--retention` in `influx setup`.

## v2.0.2 [2020-11-18]
----------------------
Expand Down
53 changes: 32 additions & 21 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,13 @@ func setupF(cmd *cobra.Command, args []string) error {
return fmt.Errorf("instance at %q has already been setup", activeConfig.Host)
}

existingConfigs := make(config.Configs)
if _, err := os.Stat(dPath); err == nil {
existingConfigs, _ = localConfigSVC.ListConfigs()
// ignore the error if found nothing
if setupFlags.name == "" {
return errors.New("flag name is required if you already have existing configs")
}
if _, ok := existingConfigs[setupFlags.name]; ok {
return &influxdb.Error{
Code: influxdb.EConflict,
Msg: fmt.Sprintf("config name %q already existed", setupFlags.name),
}
}
if err := validateNoNameCollision(localConfigSVC, setupFlags.name); err != nil {
return err
}

req, err := onboardingRequest()
if err != nil {
return fmt.Errorf("failed to retrieve data to setup instance: %v", err)
return fmt.Errorf("failed to setup instance: %v", err)
}

result, err := s.OnboardInitialUser(context.Background(), req)
Expand All @@ -169,7 +158,7 @@ func setupF(cmd *cobra.Command, args []string) error {
p := config.DefaultConfig
p.Token = result.Auth.Token
p.Org = result.Org.Name
if len(existingConfigs) > 0 {
if setupFlags.name != "" {
p.Name = setupFlags.name
}
if activeConfig.Host != "" {
Expand Down Expand Up @@ -206,6 +195,29 @@ func setupF(cmd *cobra.Command, args []string) error {
return nil
}

// validateNoNameCollision asserts that there isn't already a local config with a given name.
func validateNoNameCollision(localConfigSvc config.Service, configName string) error {
existingConfigs, err := localConfigSvc.ListConfigs()
if err != nil {
return fmt.Errorf("error checking existing configs: %v", err)
}
if len(existingConfigs) == 0 {
return nil
}

// If there are existing configs then require that a name be
// specified in order to distinguish this new config from what's
// there already.
if configName == "" {
return errors.New("flag name is required if you already have existing configs")
}
if _, ok := existingConfigs[configName]; ok {
return fmt.Errorf("config name %q already exists", configName)
}

return nil
}

func isInteractive() bool {
return !setupFlags.force ||
setupFlags.username == "" ||
Expand Down Expand Up @@ -277,12 +289,11 @@ func interactive() (req *influxdb.OnboardingRequest, err error) {
req.Bucket = internal2.GetInput(ui, "Please type your primary bucket name", "")
}

dur, err := internal2.RawDurationToTimeDuration(setupFlags.retention)
if err != nil {
return nil, err
}

if dur > 0 {
if setupFlags.retention != "" {
dur, err := internal2.RawDurationToTimeDuration(setupFlags.retention)
if err != nil {
return nil, err
}
req.RetentionPeriod = dur
} else {
for {
Expand Down

0 comments on commit c020bb9

Please sign in to comment.