1
0
mirror of https://github.com/rancher/os.git synced 2025-05-02 13:23:23 +00:00
os/config/config.go

98 lines
2.4 KiB
Go
Raw Permalink Normal View History

2015-02-09 04:38:37 +00:00
package config
import (
"io/ioutil"
"strings"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/rancher/os/config/cmdline"
2018-09-16 04:55:26 +00:00
"github.com/rancher/os/pkg/util"
2015-02-09 04:38:37 +00:00
)
const Banner = `
, , ______ _ _____ _____TM
,------------|'------'| | ___ \\ | | / _ / ___|
/ . '-' |- | |_/ /__ _ _ __ ___| |__ ___ _ __ | | | \\ '--.
\\/| | | | // _' | '_ \\ / __| '_ \\ / _ \\ '__' | | | |'--. \\
| .________.'----' | |\\ \\ (_| | | | | (__| | | | __/ | | \\_/ /\\__/ /
| | | | \\_| \\_\\__,_|_| |_|\\___|_| |_|\\___|_| \\___/\\____/
\\___/ \\___/ \s \r
RancherOS \v \n \l
`
2016-05-31 21:34:04 +00:00
func Merge(bytes []byte) error {
2016-06-02 01:41:55 +00:00
data, err := readConfigs(bytes, false, true)
2016-05-31 21:34:04 +00:00
if err != nil {
return err
2015-02-09 04:38:37 +00:00
}
2016-06-02 01:41:55 +00:00
existing, err := readConfigs(nil, false, true, CloudConfigFile)
2016-05-31 21:34:04 +00:00
if err != nil {
return err
2015-12-17 15:34:26 +00:00
}
2016-05-31 21:34:04 +00:00
return WriteToFile(util.Merge(existing, data), CloudConfigFile)
2015-02-09 04:38:37 +00:00
}
2016-05-31 21:34:04 +00:00
func Export(private, full bool) (string, error) {
rawCfg := loadRawConfig("", full)
rawCfg = filterAdditional(rawCfg)
if !private {
2016-05-31 21:34:04 +00:00
rawCfg = filterPrivateKeys(rawCfg)
2015-03-15 04:27:04 +00:00
}
2015-02-09 04:38:37 +00:00
2016-05-31 21:34:04 +00:00
bytes, err := yaml.Marshal(rawCfg)
2015-03-15 04:27:04 +00:00
return string(bytes), err
2015-02-09 04:38:37 +00:00
}
func filterPrivateKeys(data map[interface{}]interface{}) map[interface{}]interface{} {
for _, privateKey := range PrivateKeys {
_, data = filterKey(data, strings.Split(privateKey, "."))
}
return data
}
2015-02-09 04:38:37 +00:00
func filterAdditional(data map[interface{}]interface{}) map[interface{}]interface{} {
for _, additional := range Additional {
_, data = filterKey(data, strings.Split(additional, "."))
}
return data
}
2016-05-31 21:34:04 +00:00
func Get(key string) (interface{}, error) {
2016-06-02 01:41:55 +00:00
cfg := LoadConfig()
2016-04-01 04:31:46 +00:00
data := map[interface{}]interface{}{}
2016-05-31 21:34:04 +00:00
if err := util.ConvertIgnoreOmitEmpty(cfg, &data); err != nil {
2016-04-01 04:31:46 +00:00
return nil, err
}
v, _ := cmdline.GetOrSetVal(key, data, nil)
2016-04-01 04:31:46 +00:00
return v, nil
}
2016-05-31 21:34:04 +00:00
func Set(key string, value interface{}) error {
2016-06-02 01:41:55 +00:00
existing, err := readConfigs(nil, false, true, CloudConfigFile)
2015-03-15 04:27:04 +00:00
if err != nil {
return err
}
_, modified := cmdline.GetOrSetVal(key, existing, value)
2016-06-16 17:14:52 +00:00
c := &CloudConfig{}
if err = util.Convert(modified, c); err != nil {
return err
}
2016-06-02 01:41:55 +00:00
return WriteToFile(modified, CloudConfigFile)
2015-04-16 05:57:59 +00:00
}
func GetKernelVersion() string {
b, err := ioutil.ReadFile("/proc/version")
if err != nil {
return ""
}
elem := strings.Split(string(b), " ")
return elem[2]
}