1
0
mirror of https://github.com/rancher/os.git synced 2025-06-24 22:11:33 +00:00
os/config/config.go

213 lines
4.4 KiB
Go
Raw Normal View History

2015-02-09 04:38:37 +00:00
package config
import (
"io/ioutil"
"strings"
log "github.com/Sirupsen/logrus"
2015-08-04 21:45:38 +00:00
"github.com/docker/libcompose/project"
2015-02-14 16:29:07 +00:00
"github.com/rancherio/os/util"
2015-02-17 21:31:20 +00:00
"gopkg.in/yaml.v2"
2015-02-09 04:38:37 +00:00
)
func (c *CloudConfig) Import(bytes []byte) error {
2015-03-15 04:27:04 +00:00
data, err := readConfig(bytes, PrivateConfigFile)
if err != nil {
return err
}
if err := saveToDisk(data); err != nil {
2015-03-15 04:27:04 +00:00
return err
}
return c.Reload()
2015-02-17 21:31:20 +00:00
}
2015-03-18 12:38:36 +00:00
// This function only sets "non-empty" values
func (c *CloudConfig) SetConfig(newConfig *CloudConfig) error {
2015-03-18 12:38:36 +00:00
bytes, err := yaml.Marshal(newConfig)
if err != nil {
return err
}
return c.Merge(bytes)
}
func (c *CloudConfig) Merge(bytes []byte) error {
data, err := readConfig(bytes, LocalConfigFile, PrivateConfigFile)
2015-02-17 21:31:20 +00:00
if err != nil {
2015-03-15 04:27:04 +00:00
return err
2015-02-14 16:29:07 +00:00
}
2015-02-09 04:38:37 +00:00
if err := saveToDisk(data); err != nil {
2015-02-18 00:00:30 +00:00
return err
}
2015-03-15 04:27:04 +00:00
return c.Reload()
2015-02-18 00:00:30 +00:00
}
func LoadConfig() (*CloudConfig, error) {
2015-02-09 04:38:37 +00:00
cfg := NewConfig()
if err := cfg.Reload(); err != nil {
2015-08-20 13:06:48 +00:00
log.WithFields(log.Fields{"cfg": cfg, "err": err}).Error("Failed to reload config")
2015-02-09 04:38:37 +00:00
return nil, err
}
if cfg.Rancher.Debug {
2015-02-09 04:38:37 +00:00
log.SetLevel(log.DebugLevel)
if !util.Contains(cfg.Rancher.UserDocker.Args, "-D") {
cfg.Rancher.UserDocker.Args = append(cfg.Rancher.UserDocker.Args, "-D")
}
if !util.Contains(cfg.Rancher.SystemDocker.Args, "-D") {
cfg.Rancher.SystemDocker.Args = append(cfg.Rancher.SystemDocker.Args, "-D")
}
2015-02-09 04:38:37 +00:00
}
return cfg, nil
}
func (c *CloudConfig) merge(values map[interface{}]interface{}) error {
2015-08-20 14:20:51 +00:00
t := &CloudConfig{}
if err := util.Convert(values, t); err != nil {
return err
}
2015-03-15 04:27:04 +00:00
return util.Convert(values, c)
2015-02-14 16:29:07 +00:00
}
func (c *CloudConfig) readFiles() error {
data, err := readConfig(nil, CloudConfigFile, LocalConfigFile, PrivateConfigFile)
2015-02-18 00:00:30 +00:00
if err != nil {
2015-08-20 13:06:48 +00:00
log.WithFields(log.Fields{"err": err}).Error("Error reading config files")
2015-02-18 00:00:30 +00:00
return err
}
if err := c.merge(data); err != nil {
2015-08-20 13:06:48 +00:00
log.WithFields(log.Fields{"cfg": c, "data": data, "err": err}).Error("Error merging config data")
return err
}
return nil
2015-02-18 00:00:30 +00:00
}
func (c *CloudConfig) readCmdline() error {
2015-02-14 16:29:07 +00:00
log.Debug("Reading config cmdline")
cmdLine, err := ioutil.ReadFile("/proc/cmdline")
2015-02-09 04:38:37 +00:00
if err != nil {
2015-08-20 13:06:48 +00:00
log.WithFields(log.Fields{"err": err}).Error("Failed to read kernel params")
2015-02-09 04:38:37 +00:00
return err
}
2015-02-14 16:29:07 +00:00
if len(cmdLine) == 0 {
return nil
}
log.Debugf("Config cmdline %s", cmdLine)
cmdLineObj := parseCmdline(strings.TrimSpace(string(cmdLine)))
if err := c.merge(cmdLineObj); err != nil {
2015-08-20 13:06:48 +00:00
log.WithFields(log.Fields{"cfg": c, "cmdLine": cmdLine, "data": cmdLineObj, "err": err}).Warn("Error adding kernel params to config")
}
return nil
2015-02-09 04:38:37 +00:00
}
2015-03-15 04:27:04 +00:00
func Dump(private, full bool) (string, error) {
files := []string{CloudConfigFile, LocalConfigFile}
2015-03-15 04:27:04 +00:00
if private {
files = append(files, PrivateConfigFile)
2015-02-09 04:38:37 +00:00
}
c := &CloudConfig{}
2015-02-09 04:38:37 +00:00
2015-03-15 04:27:04 +00:00
if full {
2015-03-18 12:38:36 +00:00
c = NewConfig()
2015-03-15 04:27:04 +00:00
}
2015-02-09 04:38:37 +00:00
2015-03-15 04:27:04 +00:00
data, err := readConfig(nil, files...)
if err != nil {
return "", err
}
2015-02-09 04:38:37 +00:00
if err := c.merge(data); err != nil {
2015-03-15 04:27:04 +00:00
return "", err
}
2015-02-09 04:38:37 +00:00
2015-08-20 13:06:48 +00:00
if err := c.readCmdline(); err != nil {
2015-03-15 04:27:04 +00:00
return "", err
2015-02-09 04:38:37 +00:00
}
c.amendNils()
2015-03-15 04:27:04 +00:00
bytes, err := yaml.Marshal(c)
return string(bytes), err
2015-02-09 04:38:37 +00:00
}
func (c *CloudConfig) amendNils() error {
if c.Rancher.Environment == nil {
c.Rancher.Environment = map[string]string{}
}
if c.Rancher.Autoformat == nil {
c.Rancher.Autoformat = map[string]*project.ServiceConfig{}
}
if c.Rancher.BootstrapContainers == nil {
c.Rancher.BootstrapContainers = map[string]*project.ServiceConfig{}
}
if c.Rancher.Services == nil {
c.Rancher.Services = map[string]*project.ServiceConfig{}
}
if c.Rancher.ServicesInclude == nil {
c.Rancher.ServicesInclude = map[string]bool{}
}
return nil
}
func (c *CloudConfig) Reload() error {
2015-03-15 04:27:04 +00:00
return util.ShortCircuit(
c.readFiles,
2015-08-20 13:06:48 +00:00
c.readCmdline,
c.amendNils,
2015-03-15 04:27:04 +00:00
)
}
func (c *CloudConfig) Get(key string) (interface{}, error) {
2015-03-15 04:27:04 +00:00
data := make(map[interface{}]interface{})
err := util.Convert(c, &data)
if err != nil {
return nil, err
2015-02-09 04:38:37 +00:00
}
2015-03-15 04:27:04 +00:00
return getOrSetVal(key, data, nil), nil
}
func (c *CloudConfig) Set(key string, value interface{}) error {
data, err := readConfig(nil, LocalConfigFile, PrivateConfigFile)
2015-03-15 04:27:04 +00:00
if err != nil {
return err
}
getOrSetVal(key, data, value)
cfg := NewConfig()
if err := util.Convert(data, cfg); err != nil {
return err
}
if err := saveToDisk(data); err != nil {
2015-03-15 04:27:04 +00:00
return err
}
return c.Reload()
2015-02-09 04:38:37 +00:00
}
2015-04-03 21:59:24 +00:00
2015-04-16 05:57:59 +00:00
func (r Repositories) ToArray() []string {
result := make([]string, 0, len(r))
for _, repo := range r {
if repo.Url != "" {
result = append(result, repo.Url)
}
}
return result
}