Sybren A. Stüvel 11a352968a Fix T99434: Two-way Variables
Two-way variable implementation in the job submission end-point. Where
Flamenco v2 did the variable replacement in the add-on, this has now
been moved to the Manager itself. The only thing the add-on needs to
pass is its platform, so that the right values can be recognised.

This also implements two-way replacement when tasks are handed out, such
that the `{jobs}` value gets replaced to a value suitable for the
Worker's platform as well.
2022-07-22 11:58:35 +02:00

86 lines
2.2 KiB
Go

package config
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"errors"
"fmt"
"io/fs"
"github.com/rs/zerolog/log"
)
// Service provides access to Flamenco Manager configuration.
type Service struct {
config Conf
forceFirstRun bool
}
func NewService() *Service {
return &Service{
config: DefaultConfig(),
}
}
// IsFirstRun returns true if this is likely to be the first run of Flamenco.
func (s *Service) IsFirstRun() (bool, error) {
if s.forceFirstRun {
return true, nil
}
config, err := getConf()
switch {
case errors.Is(err, fs.ErrNotExist):
// No configuration means first run.
return true, nil
case err != nil:
return false, fmt.Errorf("loading %s: %w", configFilename, err)
}
// No shared storage configured means first run.
return config.SharedStoragePath == "", nil
}
func (s *Service) ForceFirstRun() {
s.forceFirstRun = true
}
func (s *Service) Load() error {
config, err := getConf()
if err != nil {
return err
}
s.config = config
return nil
}
func (s *Service) Get() *Conf {
return &s.config
}
// Save writes the in-memory configuration to the config file.
func (s *Service) Save() error {
err := s.config.Write(configFilename)
if err != nil {
return err
}
// Do the logging here, as our caller doesn't know `configFilename``.
log.Info().Str("filename", configFilename).Msg("configuration file written")
return nil
}
// Expose some functions on Conf here, for easier mocking of functionality via interfaces.
func (s *Service) ExpandVariables(inputChannel <-chan string, outputChannel chan<- string, audience VariableAudience, platform VariablePlatform) {
s.config.ExpandVariables(inputChannel, outputChannel, audience, platform)
}
func (s *Service) ConvertTwoWayVariables(inputChannel <-chan string, outputChannel chan<- string, audience VariableAudience, platform VariablePlatform) {
s.config.ConvertTwoWayVariables(inputChannel, outputChannel, audience, platform)
}
func (s *Service) ResolveVariables(audience VariableAudience, platform VariablePlatform) map[string]ResolvedVariable {
return s.config.ResolveVariables(audience, platform)
}
func (s *Service) EffectiveStoragePath() string {
return s.config.EffectiveStoragePath()
}