Skip to content

Commit

Permalink
Exit status 3 when backend already initialized (#3464)
Browse files Browse the repository at this point in the history
* Exit status 3 when backend already initialized

Signed-off-by: Eric Chlebek <eric@sensu.io>

* Fix tests

Signed-off-by: Eric Chlebek <eric@sensu.io>
  • Loading branch information
echlebek authored and amdprophet committed Dec 12, 2019
1 parent 3df40c8 commit 285e09a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/cmd/init.go
Expand Up @@ -170,7 +170,7 @@ func seedCluster(client *clientv3.Client, config seedConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := seeds.SeedCluster(ctx, store, config.SeedConfig); err != nil {
return fmt.Errorf("error initializing cluster: %s", err)
return err
}
return nil
}
14 changes: 12 additions & 2 deletions backend/seeds/seeds.go
Expand Up @@ -2,6 +2,7 @@ package seeds

import (
"context"
"errors"
"time"

corev2 "github.com/sensu/sensu-go/api/core/v2"
Expand All @@ -18,6 +19,8 @@ type Config struct {
AdminPassword string
}

var ErrAlreadyInitialized = errors.New("sensu-backend already initialized")

// SeedCluster seeds the cluster according to the provided config.
func SeedCluster(ctx context.Context, store store.Store, config Config) error {
errs := make(chan error, 1)
Expand Down Expand Up @@ -47,10 +50,17 @@ func SeedCluster(ctx context.Context, store store.Store, config Config) error {
}()

// Check that the store hasn't already been seeded
initialized, err := initializer.IsInitialized()
if err != nil || initialized {
var initialized bool
initialized, err = initializer.IsInitialized()
if err != nil {
return
}
if initialized {
logger.Info("store already initialized")
err = ErrAlreadyInitialized
return
}

logger.Info("seeding etcd store with intial data")

// Create the default namespace
Expand Down
4 changes: 3 additions & 1 deletion backend/seeds/seeds_test.go
Expand Up @@ -22,7 +22,9 @@ func TestSeedInitialData(t *testing.T) {
require.NoError(t, err, "seeding process should not raise an error")

err = SeedInitialData(st)
require.NoError(t, err, "seeding process should be able to be run more than once without error")
if err != ErrAlreadyInitialized {
require.NoError(t, err, "seeding process should be able to be run more than once without error")
}

admin, err := st.GetUser(ctx, "admin")
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/sensu-backend/main.go
Expand Up @@ -2,9 +2,11 @@ package main

import (
_ "net/http/pprof"
"os"

"github.com/sensu/sensu-go/backend"
"github.com/sensu/sensu-go/backend/cmd"
"github.com/sensu/sensu-go/backend/seeds"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -24,6 +26,9 @@ func main() {
rootCmd.AddCommand(cmd.InitCommand())

if err := rootCmd.Execute(); err != nil {
if err == seeds.ErrAlreadyInitialized {
os.Exit(3)
}
logger.WithError(err).Fatal("error executing sensu-backend")
}
}

0 comments on commit 285e09a

Please sign in to comment.