diff --git a/cmd/flamenco-manager-poc/main.go b/cmd/flamenco-manager-poc/main.go index 444d3897..ef3f6858 100644 --- a/cmd/flamenco-manager-poc/main.go +++ b/cmd/flamenco-manager-poc/main.go @@ -25,6 +25,7 @@ import ( "flag" "net" "net/http" + "runtime" "time" "github.com/benbjohnson/clock" @@ -66,6 +67,7 @@ func main() { // Load configuration. configService := config.NewService() + configService.Load() if cliArgs.initDB { log.Info().Msg("creating databases") @@ -76,27 +78,19 @@ func main() { return } - // Open the database. - dbCtx, dbCtxCancel := context.WithTimeout(context.Background(), 5*time.Second) - defer dbCtxCancel() - persist, err := persistence.OpenDB(dbCtx) - if err != nil { - log.Fatal().Err(err).Msg("error opening database") - } - - // TODO: load port number from the configuration in the database. // TODO: enable TLS via Let's Encrypt. - listen := ":8080" + listen := configService.Get().Listen _, port, _ := net.SplitHostPort(listen) log.Info().Str("port", port).Msg("listening") // Construct the services. + persist := openDB(*configService) timeService := clock.New() compiler, err := job_compilers.Load(timeService) if err != nil { log.Fatal().Err(err).Msg("error loading job compilers") } - logStorage := task_logs.NewStorage("./task-logs") // TODO: load job storage path from configuration. + logStorage := task_logs.NewStorage(configService.Get().TaskLogsPath) flamenco := api_impl.NewFlamenco(compiler, persist, logStorage, configService) e := buildWebService(flamenco, persist) @@ -165,3 +159,23 @@ func parseCliArgs() { } zerolog.SetGlobalLevel(logLevel) } + +// openDB opens the database or dies. +func openDB(configService config.Service) *persistence.DB { + dsn := configService.Get().DatabaseDSN + if dsn == "" { + log.Fatal().Msg("configure the database in flamenco-manager.yaml") + } + + dbCtx, dbCtxCancel := context.WithTimeout(context.Background(), 5*time.Second) + defer dbCtxCancel() + persist, err := persistence.OpenDB(dbCtx, dsn) + if err != nil { + log.Fatal(). + Err(err). + Str("dsn", dsn). + Msg("error opening database") + } + + return persist +} diff --git a/internal/manager/config/service.go b/internal/manager/config/service.go index 7b39c3eb..45aea88a 100644 --- a/internal/manager/config/service.go +++ b/internal/manager/config/service.go @@ -26,7 +26,9 @@ type Service struct { } func NewService() *Service { - return &Service{} + return &Service{ + config: DefaultConfig(), + } } func (s *Service) Load() error { diff --git a/internal/manager/config/settings.go b/internal/manager/config/settings.go index def1c7b9..a04a1388 100644 --- a/internal/manager/config/settings.go +++ b/internal/manager/config/settings.go @@ -297,6 +297,7 @@ func DefaultConfig() Conf { // loadConf parses the given file and returns its contents as a Conf object. func loadConf(filename string) (Conf, error) { + log.Info().Str("file", filename).Msg("loading configuration") yamlFile, err := os.ReadFile(filename) if err != nil { return DefaultConfig(), err diff --git a/internal/manager/persistence/db.go b/internal/manager/persistence/db.go index 02da5eab..62bcedf0 100644 --- a/internal/manager/persistence/db.go +++ b/internal/manager/persistence/db.go @@ -29,16 +29,13 @@ import ( "gorm.io/gorm" ) -// TODO : have this configurable from the CLI. -const dbDSN = "host=localhost user=flamenco password=flamenco dbname=flamenco TimeZone=Europe/Amsterdam" - // DB provides the database interface. type DB struct { gormDB *gorm.DB } -func OpenDB(ctx context.Context) (*DB, error) { - db, err := openDB(ctx, dbDSN) +func OpenDB(ctx context.Context, dsn string) (*DB, error) { + db, err := openDB(ctx, dsn) if err != nil { return nil, err } @@ -56,7 +53,7 @@ func openDB(ctx context.Context, uri string) (*DB, error) { gormDB, err := gorm.Open(postgres.Open(uri), &gorm.Config{}) if err != nil { - log.Panic().Err(err).Msg("failed to connect database") + return nil, err } db := DB{ diff --git a/internal/manager/task_logs/task_logs.go b/internal/manager/task_logs/task_logs.go index 4916a9fd..ac26d51e 100644 --- a/internal/manager/task_logs/task_logs.go +++ b/internal/manager/task_logs/task_logs.go @@ -24,8 +24,10 @@ import ( "fmt" "os" "path" + "path/filepath" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) // Storage can write data to task logs, rotate logs, etc. @@ -35,6 +37,18 @@ type Storage struct { // NewStorage creates a new log storage rooted at `basePath`. func NewStorage(basePath string) *Storage { + if !filepath.IsAbs(basePath) { + absPath, err := filepath.Abs(basePath) + if err != nil { + log.Panic().Err(err).Str("path", basePath).Msg("cannot resolve relative path to task logs") + } + basePath = absPath + } + + log.Info(). + Str("path", basePath). + Msg("task logs") + return &Storage{ BasePath: basePath, }