cmd/dex-overlord, db: migrations in overlord

Migrations happen only in the overlord, so there's no thundering herd,
and database initialziation can be more easily controlled.
This commit is contained in:
Bobby Rullo 2015-08-19 16:23:23 -07:00
parent 84bc8073de
commit 8b6a2699d9
2 changed files with 18 additions and 13 deletions

View file

@ -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,

View file

@ -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
}