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