datarhei-dragonfork-core/app/import/main.go

149 lines
4.2 KiB
Go
Raw Normal View History

2022-05-13 13:26:45 -04:00
package main
import (
2022-07-22 02:33:38 -04:00
"fmt"
2022-05-13 13:26:45 -04:00
"os"
cfgstore "github.com/datarhei/core/v16/config/store"
cfgvars "github.com/datarhei/core/v16/config/vars"
2023-02-01 10:09:20 -05:00
"github.com/datarhei/core/v16/io/fs"
"github.com/datarhei/core/v16/log"
"github.com/datarhei/core/v16/restream/store"
2022-05-13 13:26:45 -04:00
_ "github.com/joho/godotenv/autoload"
)
func main() {
logger := log.New("Import").WithOutput(log.NewConsoleWriter(os.Stderr, log.Linfo, true)).WithField("version", "v1")
configfile := cfgstore.Location(os.Getenv("CORE_CONFIGFILE"))
2023-02-01 10:09:20 -05:00
diskfs, err := fs.NewDiskFilesystem(fs.DiskConfig{})
if err != nil {
logger.Error().WithError(err).Log("Access disk filesystem failed")
os.Exit(1)
}
configstore, err := cfgstore.NewJSON(diskfs, configfile, nil)
2022-05-13 13:26:45 -04:00
if err != nil {
logger.Error().WithError(err).Log("Loading configuration failed")
2022-07-22 02:33:38 -04:00
os.Exit(1)
}
2023-02-01 10:09:20 -05:00
if err := doImport(logger, diskfs, configstore); err != nil {
2022-07-22 02:33:38 -04:00
os.Exit(1)
}
}
2023-02-01 10:09:20 -05:00
func doImport(logger log.Logger, fs fs.Filesystem, configstore cfgstore.Store) error {
2022-07-22 02:33:38 -04:00
if logger == nil {
logger = log.New("")
2022-05-13 13:26:45 -04:00
}
2022-07-22 02:33:38 -04:00
logger.Info().Log("Database import")
2022-05-13 13:26:45 -04:00
cfg := configstore.Get()
// Merging the persisted config with the environment variables
cfg.Merge()
cfg.Validate(false)
if cfg.HasErrors() {
logger.Error().Log("The configuration contains errors")
2022-07-22 02:33:38 -04:00
messages := []string{}
cfg.Messages(func(level string, v cfgvars.Variable, message string) {
2022-05-13 13:26:45 -04:00
if level == "error" {
logger.Error().WithFields(log.Fields{
"variable": v.Name,
"value": v.Value,
"env": v.EnvName,
"description": v.Description,
}).Log(message)
2022-07-22 02:33:38 -04:00
messages = append(messages, v.Name+": "+message)
2022-05-13 13:26:45 -04:00
}
})
2022-07-22 02:33:38 -04:00
return fmt.Errorf("the configuration contains errors: %v", messages)
2022-05-13 13:26:45 -04:00
}
logger.Info().Log("Checking for database ...")
// Check if there's a v1.json from the old Restreamer
v1filename := cfg.DB.Dir + "/v1.json"
logger = logger.WithField("database", v1filename)
2023-02-01 10:09:20 -05:00
if _, err := fs.Stat(v1filename); err != nil {
2022-05-13 13:26:45 -04:00
if os.IsNotExist(err) {
logger.Info().Log("Database doesn't exist and nothing will be imported")
2022-07-22 02:33:38 -04:00
return nil
2022-05-13 13:26:45 -04:00
}
logger.Error().WithError(err).Log("Checking for v1 database")
2022-07-22 02:33:38 -04:00
return fmt.Errorf("checking for v1 database: %w", err)
2022-05-13 13:26:45 -04:00
}
logger.Info().Log("Found database")
// Load an existing DB
2023-02-01 10:09:20 -05:00
datastore, err := store.NewJSON(store.JSONConfig{
Filesystem: fs,
Filepath: cfg.DB.Dir + "/db.json",
2022-05-13 13:26:45 -04:00
})
2023-02-01 10:09:20 -05:00
if err != nil {
logger.Error().WithError(err).Log("Creating datastore for new database failed")
return fmt.Errorf("creating datastore for new database failed: %w", err)
}
2022-05-13 13:26:45 -04:00
data, err := datastore.Load()
if err != nil {
logger.Error().WithError(err).Log("Loading new database failed")
2022-07-22 02:33:38 -04:00
return fmt.Errorf("loading new database failed: %w", err)
2022-05-13 13:26:45 -04:00
}
// Check if the existing DB has already some data in it.
// If it's not empty, we will not import any v1 DB.
if !data.IsEmpty() {
logger.Info().Log("There's already information stored in the new database and the v1 database will not be imported")
2022-07-22 02:33:38 -04:00
return nil
2022-05-13 13:26:45 -04:00
}
logger.Info().Log("Importing database ...")
// Read the Restreamer config from the environment variables
importConfig := importConfigFromEnvironment()
importConfig.binary = cfg.FFmpeg.Binary
// Rewrite the old database to the new database
2023-02-01 10:09:20 -05:00
r, err := importV1(fs, v1filename, importConfig)
2022-05-13 13:26:45 -04:00
if err != nil {
logger.Error().WithError(err).Log("Importing database failed")
2022-07-22 02:33:38 -04:00
return fmt.Errorf("importing database failed: %w", err)
2022-05-13 13:26:45 -04:00
}
// Persist the imported DB
if err := datastore.Store(r); err != nil {
logger.Error().WithError(err).Log("Storing imported data to new database failed")
2022-07-22 02:33:38 -04:00
return fmt.Errorf("storing imported data to new database failed: %w", err)
2022-05-13 13:26:45 -04:00
}
// Get the unmerged config for persisting
cfg = configstore.Get()
// Add static routes to mimic the old URLs
cfg.Router.Routes["/hls/live.stream.m3u8"] = "/memfs/" + importConfig.id + ".m3u8"
cfg.Router.Routes["/images/live.jpg"] = "/memfs/" + importConfig.id + ".jpg"
cfg.Router.Routes["/player.html"] = "/" + importConfig.id + ".html"
// Persist the modified config
if err := configstore.Set(cfg); err != nil {
logger.Error().WithError(err).Log("Storing adjusted config failed")
2022-07-22 02:33:38 -04:00
return fmt.Errorf("storing adjusted config failed: %w", err)
2022-05-13 13:26:45 -04:00
}
logger.Info().Log("Successfully imported data")
2022-07-22 02:33:38 -04:00
return nil
2022-05-13 13:26:45 -04:00
}