config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package config
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  6. "github.com/rancher/os/config/cmdline"
  7. "github.com/rancher/os/util"
  8. )
  9. const Banner = `
  10. , , ______ _ _____ _____TM
  11. ,------------|'------'| | ___ \\ | | / _ / ___|
  12. / . '-' |- | |_/ /__ _ _ __ ___| |__ ___ _ __ | | | \\ '--.
  13. \\/| | | | // _' | '_ \\ / __| '_ \\ / _ \\ '__' | | | |'--. \\
  14. | .________.'----' | |\\ \\ (_| | | | | (__| | | | __/ | | \\_/ /\\__/ /
  15. | | | | \\_| \\_\\__,_|_| |_|\\___|_| |_|\\___|_| \\___/\\____/
  16. \\___/ \\___/ \s \r
  17. RancherOS \v \n \l
  18. `
  19. func Merge(bytes []byte) error {
  20. data, err := readConfigs(bytes, false, true)
  21. if err != nil {
  22. return err
  23. }
  24. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  25. if err != nil {
  26. return err
  27. }
  28. return WriteToFile(util.Merge(existing, data), CloudConfigFile)
  29. }
  30. func Export(private, full bool) (string, error) {
  31. rawCfg := loadRawConfig("", full)
  32. if !private {
  33. rawCfg = filterPrivateKeys(rawCfg)
  34. }
  35. bytes, err := yaml.Marshal(rawCfg)
  36. return string(bytes), err
  37. }
  38. func filterPrivateKeys(data map[interface{}]interface{}) map[interface{}]interface{} {
  39. for _, privateKey := range PrivateKeys {
  40. _, data = filterKey(data, strings.Split(privateKey, "."))
  41. }
  42. return data
  43. }
  44. func Get(key string) (interface{}, error) {
  45. cfg := LoadConfig()
  46. data := map[interface{}]interface{}{}
  47. if err := util.ConvertIgnoreOmitEmpty(cfg, &data); err != nil {
  48. return nil, err
  49. }
  50. v, _ := cmdline.GetOrSetVal(key, data, nil)
  51. return v, nil
  52. }
  53. func Set(key string, value interface{}) error {
  54. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  55. if err != nil {
  56. return err
  57. }
  58. _, modified := cmdline.GetOrSetVal(key, existing, value)
  59. c := &CloudConfig{}
  60. if err = util.Convert(modified, c); err != nil {
  61. return err
  62. }
  63. return WriteToFile(modified, CloudConfigFile)
  64. }
  65. func GetKernelVersion() string {
  66. b, err := ioutil.ReadFile("/proc/version")
  67. if err != nil {
  68. return ""
  69. }
  70. elem := strings.Split(string(b), " ")
  71. return elem[2]
  72. }