diff --git a/cmd/dex-overlord/main.go b/cmd/dex-overlord/main.go index 263297b1..419b6f2c 100644 --- a/cmd/dex-overlord/main.go +++ b/cmd/dex-overlord/main.go @@ -15,6 +15,7 @@ import ( "github.com/coreos/dex/db" pflag "github.com/coreos/dex/pkg/flag" "github.com/coreos/dex/pkg/log" + ptime "github.com/coreos/dex/pkg/time" "github.com/coreos/dex/server" "github.com/coreos/dex/user" ) @@ -29,14 +30,17 @@ func main() { fs := flag.NewFlagSet("dex-overlord", flag.ExitOnError) secret := fs.String("key-secret", "", "symmetric key used to encrypt/decrypt signing key data in DB") dbURL := fs.String("db-url", "", "DSN-formatted database connection string") + + dbMigrate := fs.Bool("db-migrate", true, "perform database migrations when starting up overlord. This includes the initial DB objects creation.") + keyPeriod := fs.Duration("key-period", 24*time.Hour, "length of time for-which a given key will be valid") gcInterval := fs.Duration("gc-interval", time.Hour, "length of time between garbage collection runs") adminListen := fs.String("admin-listen", "http://0.0.0.0:5557", "scheme, host and port for listening for administrative operation requests ") + localConnectorID := fs.String("local-connector", "local", "ID of the local connector") logDebug := fs.Bool("log-debug", false, "log debug-level information") logTimestamps := fs.Bool("log-timestamps", false, "prefix log lines with timestamps") - localConnectorID := fs.String("local-connector", "local", "ID of the local connector") if err := fs.Parse(os.Args[1:]); err != nil { fmt.Fprintln(os.Stderr, err.Error()) @@ -74,6 +78,19 @@ func main() { log.Fatalf(err.Error()) } + if *dbMigrate { + var sleep time.Duration + for { + if migrations, err := db.MigrateToLatest(dbc); err == nil { + log.Infof("Performed %d db migrations", migrations) + break + } + sleep = ptime.ExpBackoff(sleep, time.Minute) + log.Errorf("Unable to migrate database, retrying in %v: %v", sleep, err) + time.Sleep(sleep) + } + } + userRepo := db.NewUserRepo(dbc) pwiRepo := db.NewPasswordInfoRepo(dbc) userManager := user.NewManager(userRepo, diff --git a/db/conn.go b/db/conn.go index c4c2a8e4..6baa5e32 100644 --- a/db/conn.go +++ b/db/conn.go @@ -5,13 +5,11 @@ import ( "errors" "fmt" "strings" - "time" "github.com/coopernurse/gorp" _ "github.com/lib/pq" "github.com/coreos/dex/pkg/log" - ptime "github.com/coreos/dex/pkg/time" "github.com/coreos/dex/repo" ) @@ -73,16 +71,6 @@ func NewConnection(cfg Config) (*gorp.DbMap, error) { } } - var sleep time.Duration - for { - if err = dbm.CreateTablesIfNotExists(); err == nil { - break - } - sleep = ptime.ExpBackoff(sleep, time.Minute) - log.Errorf("Unable to initialize database, retrying in %v: %v", sleep, err) - time.Sleep(sleep) - } - return &dbm, nil }