config.go 2.0 KB

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