2019-12-27 19:12:08 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
// Daniele Rondina <geaaru@sabayonlinux.org>
|
|
|
|
//
|
|
|
|
// This program 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 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-01-12 22:30:10 +00:00
|
|
|
"errors"
|
2019-12-27 19:12:08 +00:00
|
|
|
"fmt"
|
2020-02-12 09:20:07 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-02-12 10:21:08 +00:00
|
|
|
"os/user"
|
2020-02-12 09:20:07 +00:00
|
|
|
"path/filepath"
|
2020-02-12 10:21:08 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-12-27 19:12:08 +00:00
|
|
|
|
2020-05-01 06:18:18 +00:00
|
|
|
"github.com/mudler/luet/pkg/helpers"
|
2020-02-12 09:20:07 +00:00
|
|
|
solver "github.com/mudler/luet/pkg/solver"
|
2020-05-01 06:18:18 +00:00
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
v "github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var LuetCfg = NewLuetConfig(v.GetViper())
|
2020-02-12 10:21:08 +00:00
|
|
|
var AvailableResolvers = strings.Join([]string{solver.QLearningResolverType}, " ")
|
2019-12-27 19:12:08 +00:00
|
|
|
|
|
|
|
type LuetLoggingConfig struct {
|
2020-05-09 08:08:21 +00:00
|
|
|
// Path of the logfile
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
// Enable/Disable logging to file
|
|
|
|
EnableLogFile bool `mapstructure:"enable_logfile"`
|
|
|
|
// Enable JSON format logging in file
|
|
|
|
JsonFormat bool `mapstructure:"json_format"`
|
|
|
|
|
|
|
|
// Log level
|
|
|
|
Level string `mapstructure:"level"`
|
|
|
|
|
|
|
|
// Enable emoji
|
|
|
|
EnableEmoji bool `mapstructure:"enable_emoji"`
|
|
|
|
// Enable/Disable color in logging
|
|
|
|
Color bool `mapstructure:"color"`
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LuetGeneralConfig struct {
|
2020-02-01 16:58:23 +00:00
|
|
|
SameOwner bool `mapstructure:"same_owner"`
|
2019-12-27 19:12:08 +00:00
|
|
|
Concurrency int `mapstructure:"concurrency"`
|
|
|
|
Debug bool `mapstructure:"debug"`
|
|
|
|
ShowBuildOutput bool `mapstructure:"show_build_output"`
|
|
|
|
SpinnerMs int `mapstructure:"spinner_ms"`
|
|
|
|
SpinnerCharset int `mapstructure:"spinner_charset"`
|
2020-01-03 14:41:45 +00:00
|
|
|
FatalWarns bool `mapstructure:"fatal_warnings"`
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 10:21:08 +00:00
|
|
|
type LuetSolverOptions struct {
|
2020-02-12 11:23:37 +00:00
|
|
|
Type string `mapstructure:"type"`
|
|
|
|
LearnRate float32 `mapstructure:"rate"`
|
|
|
|
Discount float32 `mapstructure:"discount"`
|
|
|
|
MaxAttempts int `mapstructure:"max_attempts"`
|
2020-02-12 10:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts LuetSolverOptions) Resolver() solver.PackageResolver {
|
|
|
|
switch opts.Type {
|
|
|
|
case solver.QLearningResolverType:
|
|
|
|
if opts.LearnRate != 0.0 {
|
|
|
|
return solver.NewQLearningResolver(opts.LearnRate, opts.Discount, opts.MaxAttempts, 999999)
|
|
|
|
|
|
|
|
}
|
|
|
|
return solver.SimpleQLearningSolver()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &solver.DummyPackageResolver{}
|
|
|
|
}
|
|
|
|
|
2020-02-12 11:23:37 +00:00
|
|
|
func (opts *LuetSolverOptions) CompactString() string {
|
|
|
|
return fmt.Sprintf("type: %s rate: %f, discount: %f, attempts: %d, initialobserved: %d",
|
|
|
|
opts.Type, opts.LearnRate, opts.Discount, opts.MaxAttempts, 999999)
|
|
|
|
}
|
|
|
|
|
2019-12-30 20:56:13 +00:00
|
|
|
type LuetSystemConfig struct {
|
|
|
|
DatabaseEngine string `yaml:"database_engine" mapstructure:"database_engine"`
|
|
|
|
DatabasePath string `yaml:"database_path" mapstructure:"database_path"`
|
|
|
|
Rootfs string `yaml:"rootfs" mapstructure:"rootfs"`
|
2020-02-01 18:35:30 +00:00
|
|
|
PkgsCachePath string `yaml:"pkgs_cache_path" mapstructure:"pkgs_cache_path"`
|
2020-04-30 18:29:28 +00:00
|
|
|
TmpDirBase string `yaml:"tmpdir_base" mapstructure:"tmpdir_base"`
|
2019-12-30 20:56:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 09:20:07 +00:00
|
|
|
func (sc LuetSystemConfig) GetRepoDatabaseDirPath(name string) string {
|
|
|
|
dbpath := filepath.Join(sc.Rootfs, sc.DatabasePath)
|
|
|
|
dbpath = filepath.Join(dbpath, "repos/"+name)
|
|
|
|
err := os.MkdirAll(dbpath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dbpath
|
|
|
|
}
|
|
|
|
|
2020-02-12 10:21:08 +00:00
|
|
|
func (sc LuetSystemConfig) GetSystemRepoDatabaseDirPath() string {
|
2020-02-12 09:20:07 +00:00
|
|
|
dbpath := filepath.Join(sc.Rootfs,
|
|
|
|
sc.DatabasePath)
|
|
|
|
err := os.MkdirAll(dbpath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dbpath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc LuetSystemConfig) GetSystemPkgsCacheDirPath() (ans string) {
|
|
|
|
var cachepath string
|
|
|
|
if sc.PkgsCachePath != "" {
|
|
|
|
cachepath = sc.PkgsCachePath
|
|
|
|
} else {
|
|
|
|
// Create dynamic cache for test suites
|
|
|
|
cachepath, _ = ioutil.TempDir(os.TempDir(), "cachepkgs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.IsAbs(cachepath) {
|
|
|
|
ans = cachepath
|
|
|
|
} else {
|
|
|
|
ans = filepath.Join(sc.GetSystemRepoDatabaseDirPath(), cachepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
type LuetRepository struct {
|
2019-12-31 15:50:44 +00:00
|
|
|
Name string `json:"name" yaml:"name" mapstructure:"name"`
|
|
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description"`
|
|
|
|
Urls []string `json:"urls" yaml:"urls" mapstructure:"urls"`
|
|
|
|
Type string `json:"type" yaml:"type" mapstructure:"type"`
|
|
|
|
Mode string `json:"mode,omitempty" yaml:"mode,omitempty" mapstructure:"mode,omitempty"`
|
|
|
|
Priority int `json:"priority,omitempty" yaml:"priority,omitempty" mapstructure:"priority"`
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
2020-01-12 22:30:10 +00:00
|
|
|
Cached bool `json:"cached,omitempty" yaml:"cached,omitempty" mapstructure:"cached,omitempty"`
|
2019-12-31 15:50:44 +00:00
|
|
|
Authentication map[string]string `json:"auth,omitempty" yaml:"auth,omitempty" mapstructure:"auth,omitempty"`
|
2020-01-03 19:09:29 +00:00
|
|
|
TreePath string `json:"tree_path,omitempty" yaml:"tree_path,omitempty" mapstructure:"tree_path"`
|
2020-03-23 22:56:29 +00:00
|
|
|
MetaPath string `json:"meta_path,omitempty" yaml:"meta_path,omitempty" mapstructure:"meta_path"`
|
2020-01-03 19:09:29 +00:00
|
|
|
|
|
|
|
// Serialized options not used in repository configuration
|
|
|
|
|
|
|
|
// Incremented value that identify revision of the repository in a user-friendly way.
|
|
|
|
Revision int `json:"revision,omitempty" yaml:"-,omitempty" mapstructure:"-,omitempty"`
|
|
|
|
// Epoch time in seconds
|
|
|
|
LastUpdate string `json:"last_update,omitempty" yaml:"-,omitempty" mapstructure:"-,omitempty"`
|
2019-12-31 00:21:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 22:30:10 +00:00
|
|
|
func NewLuetRepository(name, t, descr string, urls []string, priority int, enable, cached bool) *LuetRepository {
|
2019-12-31 00:21:06 +00:00
|
|
|
return &LuetRepository{
|
|
|
|
Name: name,
|
|
|
|
Description: descr,
|
|
|
|
Urls: urls,
|
|
|
|
Type: t,
|
|
|
|
// Used in cached repositories
|
|
|
|
Mode: "",
|
|
|
|
Priority: priority,
|
|
|
|
Enable: enable,
|
2020-01-12 22:30:10 +00:00
|
|
|
Cached: cached,
|
2019-12-31 00:21:06 +00:00
|
|
|
Authentication: make(map[string]string, 0),
|
|
|
|
TreePath: "",
|
2020-03-23 22:56:29 +00:00
|
|
|
MetaPath: "",
|
2019-12-31 00:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 18:37:51 +00:00
|
|
|
func NewEmptyLuetRepository() *LuetRepository {
|
|
|
|
return &LuetRepository{
|
|
|
|
Name: "",
|
|
|
|
Description: "",
|
|
|
|
Urls: []string{},
|
|
|
|
Type: "",
|
|
|
|
Priority: 9999,
|
|
|
|
TreePath: "",
|
2020-03-23 22:56:29 +00:00
|
|
|
MetaPath: "",
|
2020-01-03 18:37:51 +00:00
|
|
|
Enable: false,
|
2020-01-12 22:30:10 +00:00
|
|
|
Cached: false,
|
2020-01-03 18:37:51 +00:00
|
|
|
Authentication: make(map[string]string, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 00:21:06 +00:00
|
|
|
func (r *LuetRepository) String() string {
|
2020-01-12 22:30:10 +00:00
|
|
|
return fmt.Sprintf("[%s] prio: %d, type: %s, enable: %t, cached: %t",
|
|
|
|
r.Name, r.Priority, r.Type, r.Enable, r.Cached)
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LuetConfig struct {
|
|
|
|
Viper *v.Viper
|
|
|
|
|
|
|
|
Logging LuetLoggingConfig `mapstructure:"logging"`
|
|
|
|
General LuetGeneralConfig `mapstructure:"general"`
|
2019-12-30 20:56:13 +00:00
|
|
|
System LuetSystemConfig `mapstructure:"system"`
|
2020-02-12 10:21:08 +00:00
|
|
|
Solver LuetSolverOptions `mapstructure:"solver"`
|
2019-12-27 19:12:08 +00:00
|
|
|
|
2020-06-02 07:04:40 +00:00
|
|
|
RepositoriesConfDir []string `mapstructure:"repos_confdir"`
|
|
|
|
ConfigProtectConfDir []string `mapstructure:"config_protect_confdir"`
|
|
|
|
CacheRepositories []LuetRepository `mapstructure:"repetitors"`
|
|
|
|
SystemRepositories []LuetRepository `mapstructure:"repositories"`
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLuetConfig(viper *v.Viper) *LuetConfig {
|
|
|
|
if viper == nil {
|
|
|
|
viper = v.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
GenDefault(viper)
|
|
|
|
return &LuetConfig{Viper: viper}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenDefault(viper *v.Viper) {
|
|
|
|
viper.SetDefault("logging.level", "info")
|
2020-05-07 06:14:37 +00:00
|
|
|
viper.SetDefault("logging.enable_logfile", false)
|
|
|
|
viper.SetDefault("logging.path", "/var/log/luet.log")
|
2020-01-03 14:20:42 +00:00
|
|
|
viper.SetDefault("logging.json_format", false)
|
2020-05-09 08:08:21 +00:00
|
|
|
viper.SetDefault("logging.enable_emoji", true)
|
|
|
|
viper.SetDefault("logging.color", true)
|
2019-12-27 19:12:08 +00:00
|
|
|
|
|
|
|
viper.SetDefault("general.concurrency", runtime.NumCPU())
|
|
|
|
viper.SetDefault("general.debug", false)
|
|
|
|
viper.SetDefault("general.show_build_output", false)
|
|
|
|
viper.SetDefault("general.spinner_ms", 100)
|
|
|
|
viper.SetDefault("general.spinner_charset", 22)
|
2020-01-03 14:41:45 +00:00
|
|
|
viper.SetDefault("general.fatal_warnings", false)
|
2019-12-27 19:12:08 +00:00
|
|
|
|
2020-04-11 18:05:56 +00:00
|
|
|
u, err := user.Current()
|
|
|
|
// os/user doesn't work in from scratch environments
|
2020-05-30 14:51:10 +00:00
|
|
|
if err != nil || (u != nil && u.Uid == "0") {
|
2020-02-01 16:58:23 +00:00
|
|
|
viper.SetDefault("general.same_owner", true)
|
|
|
|
} else {
|
|
|
|
viper.SetDefault("general.same_owner", false)
|
|
|
|
}
|
|
|
|
|
2019-12-30 20:56:13 +00:00
|
|
|
viper.SetDefault("system.database_engine", "boltdb")
|
|
|
|
viper.SetDefault("system.database_path", "/var/cache/luet")
|
|
|
|
viper.SetDefault("system.rootfs", "/")
|
2020-04-30 18:29:28 +00:00
|
|
|
viper.SetDefault("system.tmpdir_base", filepath.Join(os.TempDir(), "tmpluet"))
|
2020-02-01 18:35:30 +00:00
|
|
|
viper.SetDefault("system.pkgs_cache_path", "packages")
|
2019-12-30 20:56:13 +00:00
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
viper.SetDefault("repos_confdir", []string{"/etc/luet/repos.conf.d"})
|
2020-06-02 07:04:40 +00:00
|
|
|
viper.SetDefault("config_protect_confdir", []string{"/etc/luet/config.protect.d"})
|
2019-12-27 19:12:08 +00:00
|
|
|
viper.SetDefault("cache_repositories", []string{})
|
|
|
|
viper.SetDefault("system_repositories", []string{})
|
2020-02-12 10:21:08 +00:00
|
|
|
|
|
|
|
viper.SetDefault("solver.type", "")
|
|
|
|
viper.SetDefault("solver.rate", 0.7)
|
|
|
|
viper.SetDefault("solver.discount", 1.0)
|
|
|
|
viper.SetDefault("solver.max_attempts", 9000)
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetConfig) AddSystemRepository(r LuetRepository) {
|
|
|
|
c.SystemRepositories = append(c.SystemRepositories, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetConfig) GetLogging() *LuetLoggingConfig {
|
|
|
|
return &c.Logging
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetConfig) GetGeneral() *LuetGeneralConfig {
|
|
|
|
return &c.General
|
|
|
|
}
|
|
|
|
|
2019-12-30 20:56:13 +00:00
|
|
|
func (c *LuetConfig) GetSystem() *LuetSystemConfig {
|
|
|
|
return &c.System
|
|
|
|
}
|
|
|
|
|
2020-02-12 10:21:08 +00:00
|
|
|
func (c *LuetConfig) GetSolverOptions() *LuetSolverOptions {
|
|
|
|
return &c.Solver
|
|
|
|
}
|
|
|
|
|
2020-01-12 22:30:10 +00:00
|
|
|
func (c *LuetConfig) GetSystemRepository(name string) (*LuetRepository, error) {
|
|
|
|
var ans *LuetRepository = nil
|
|
|
|
|
|
|
|
for idx, repo := range c.SystemRepositories {
|
|
|
|
if repo.Name == name {
|
|
|
|
ans = &c.SystemRepositories[idx]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ans == nil {
|
|
|
|
return nil, errors.New("Repository " + name + " not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ans, nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 10:21:08 +00:00
|
|
|
func (c *LuetSolverOptions) String() string {
|
|
|
|
ans := fmt.Sprintf(`
|
|
|
|
solver:
|
|
|
|
type: %s
|
|
|
|
rate: %f
|
|
|
|
discount: %f
|
|
|
|
max_attempts: %d`, c.Type, c.LearnRate, c.Discount,
|
|
|
|
c.MaxAttempts)
|
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
func (c *LuetGeneralConfig) String() string {
|
|
|
|
ans := fmt.Sprintf(`
|
|
|
|
general:
|
|
|
|
concurrency: %d
|
2020-02-01 16:58:23 +00:00
|
|
|
same_owner: %t
|
2019-12-27 19:12:08 +00:00
|
|
|
debug: %t
|
2020-01-03 14:41:45 +00:00
|
|
|
fatal_warnings: %t
|
2019-12-27 19:12:08 +00:00
|
|
|
show_build_output: %t
|
|
|
|
spinner_ms: %d
|
2020-02-01 16:58:23 +00:00
|
|
|
spinner_charset: %d`, c.Concurrency, c.SameOwner, c.Debug,
|
|
|
|
c.FatalWarns, c.ShowBuildOutput,
|
2019-12-27 19:12:08 +00:00
|
|
|
c.SpinnerMs, c.SpinnerCharset)
|
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetGeneralConfig) GetSpinnerMs() time.Duration {
|
|
|
|
duration, err := time.ParseDuration(fmt.Sprintf("%dms", c.SpinnerMs))
|
|
|
|
if err != nil {
|
|
|
|
return 100 * time.Millisecond
|
|
|
|
}
|
|
|
|
return duration
|
|
|
|
}
|
|
|
|
|
2020-04-18 21:22:00 +00:00
|
|
|
func (c *LuetLoggingConfig) SetLogLevel(s string) {
|
|
|
|
c.Level = s
|
|
|
|
}
|
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
func (c *LuetLoggingConfig) String() string {
|
|
|
|
ans := fmt.Sprintf(`
|
|
|
|
logging:
|
2020-05-07 06:14:37 +00:00
|
|
|
enable_logfile: %t
|
2019-12-27 19:12:08 +00:00
|
|
|
path: %s
|
2020-01-03 14:20:42 +00:00
|
|
|
json_format: %t
|
2020-05-09 08:08:21 +00:00
|
|
|
color: %t
|
|
|
|
enable_emoji: %t
|
|
|
|
level: %s`, c.EnableLogFile, c.Path, c.JsonFormat,
|
|
|
|
c.Color, c.EnableEmoji, c.Level)
|
2019-12-30 20:56:13 +00:00
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetSystemConfig) String() string {
|
|
|
|
ans := fmt.Sprintf(`
|
|
|
|
system:
|
|
|
|
database_engine: %s
|
|
|
|
database_path: %s
|
2020-02-01 18:35:30 +00:00
|
|
|
pkgs_cache_path: %s
|
2020-04-30 18:29:28 +00:00
|
|
|
tmpdir_base: %s
|
2019-12-30 20:56:13 +00:00
|
|
|
rootfs: %s`,
|
2020-04-30 18:29:28 +00:00
|
|
|
c.DatabaseEngine, c.DatabasePath, c.PkgsCachePath,
|
|
|
|
c.TmpDirBase, c.Rootfs)
|
2019-12-27 19:12:08 +00:00
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|
2020-04-30 18:29:28 +00:00
|
|
|
|
|
|
|
func (c *LuetSystemConfig) InitTmpDir() error {
|
2020-05-01 06:18:18 +00:00
|
|
|
if !helpers.Exists(c.TmpDirBase) {
|
|
|
|
return os.MkdirAll(c.TmpDirBase, os.ModePerm)
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-30 18:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetSystemConfig) CleanupTmpDir() error {
|
|
|
|
return os.RemoveAll(c.TmpDirBase)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetSystemConfig) TempDir(pattern string) (string, error) {
|
2020-05-01 06:18:18 +00:00
|
|
|
err := c.InitTmpDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-04-30 18:29:28 +00:00
|
|
|
return ioutil.TempDir(c.TmpDirBase, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LuetSystemConfig) TempFile(pattern string) (*os.File, error) {
|
2020-05-01 06:18:18 +00:00
|
|
|
err := c.InitTmpDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-30 18:29:28 +00:00
|
|
|
return ioutil.TempFile(c.TmpDirBase, pattern)
|
|
|
|
}
|