Sybren A. Stüvel e70a44a146 Manager: switch from PostgreSQL to SQLite
This includes a modified copy of the Gorm SQLite backend, adjusted to use
https://modernc.org/sqlite instead.
2022-03-01 18:50:31 +01:00

173 lines
5.0 KiB
Go

package main
/* ***** BEGIN GPL LICENSE BLOCK *****
*
* Original Code Copyright (C) 2022 Blender Foundation.
*
* This file is part of Flamenco.
*
* Flamenco is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Flamenco is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Flamenco. If not, see <https://www.gnu.org/licenses/>.
*
* ***** END GPL LICENSE BLOCK ***** */
import (
"context"
"flag"
"net"
"net/http"
"runtime"
"time"
"github.com/benbjohnson/clock"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/mattn/go-colorable"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/ziflex/lecho/v3"
"gitlab.com/blender/flamenco-ng-poc/internal/appinfo"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/api_impl"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/config"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/job_compilers"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/persistence"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/swagger_ui"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/task_logs"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/task_state_machine"
"gitlab.com/blender/flamenco-ng-poc/pkg/api"
)
var cliArgs struct {
version bool
}
func main() {
output := zerolog.ConsoleWriter{Out: colorable.NewColorableStdout(), TimeFormat: time.RFC3339}
log.Logger = log.Output(output)
log.Info().
Str("version", appinfo.ApplicationVersion).
Str("os", runtime.GOOS).
Str("arch", runtime.GOARCH).
Msgf("starting %v", appinfo.ApplicationName)
parseCliArgs()
if cliArgs.version {
return
}
// Load configuration.
configService := config.NewService()
configService.Load()
// TODO: enable TLS via Let's Encrypt.
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(configService.Get().TaskLogsPath)
taskStateMachine := task_state_machine.NewStateMachine(persist)
flamenco := api_impl.NewFlamenco(compiler, persist, logStorage, configService, taskStateMachine)
e := buildWebService(flamenco, persist)
// Start the web server.
finalErr := e.Start(listen)
log.Warn().Err(finalErr).Msg("shutting down")
}
func buildWebService(flamenco api.ServerInterface, persist api_impl.PersistenceService) *echo.Echo {
e := echo.New()
e.HideBanner = true
// Hook Zerolog onto Echo:
e.Use(lecho.Middleware(lecho.Config{
Logger: lecho.From(log.Logger),
}))
// Ensure panics when serving a web request won't bring down the server.
e.Use(middleware.Recover())
// Load the API definition and enable validation & authentication checks.
swagger, err := api.GetSwagger()
if err != nil {
log.Fatal().Err(err).Msg("unable to get swagger")
}
validator := api_impl.SwaggerValidator(swagger, persist)
e.Use(validator)
// Register routes.
api.RegisterHandlers(e, flamenco)
swagger_ui.RegisterSwaggerUIStaticFiles(e)
e.GET("/api/openapi3.json", func(c echo.Context) error {
return c.JSON(http.StatusOK, swagger)
})
// Log available routes
routeLogger := log.Level(zerolog.DebugLevel)
routeLogger.Debug().Msg("available routes:")
for _, route := range e.Routes() {
routeLogger.Debug().Msgf("%7s %s", route.Method, route.Path)
}
return e
}
func parseCliArgs() {
var quiet, debug, trace bool
flag.BoolVar(&cliArgs.version, "version", false, "Shows the application version, then exits.")
flag.BoolVar(&quiet, "quiet", false, "Only log warning-level and worse.")
flag.BoolVar(&debug, "debug", false, "Enable debug-level logging.")
flag.BoolVar(&trace, "trace", false, "Enable trace-level logging.")
flag.Parse()
var logLevel zerolog.Level
switch {
case trace:
logLevel = zerolog.TraceLevel
case debug:
logLevel = zerolog.DebugLevel
case quiet:
logLevel = zerolog.WarnLevel
default:
logLevel = zerolog.InfoLevel
}
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
}