config.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. rawCfg = filterAdditional(rawCfg)
  33. if !private {
  34. rawCfg = filterPrivateKeys(rawCfg)
  35. }
  36. bytes, err := yaml.Marshal(rawCfg)
  37. return string(bytes), err
  38. }
  39. func filterPrivateKeys(data map[interface{}]interface{}) map[interface{}]interface{} {
  40. for _, privateKey := range PrivateKeys {
  41. _, data = filterKey(data, strings.Split(privateKey, "."))
  42. }
  43. return data
  44. }
  45. func filterAdditional(data map[interface{}]interface{}) map[interface{}]interface{} {
  46. for _, additional := range Additional {
  47. _, data = filterKey(data, strings.Split(additional, "."))
  48. }
  49. return data
  50. }
  51. func Get(key string) (interface{}, error) {
  52. cfg := LoadConfig()
  53. data := map[interface{}]interface{}{}
  54. if err := util.ConvertIgnoreOmitEmpty(cfg, &data); err != nil {
  55. return nil, err
  56. }
  57. v, _ := cmdline.GetOrSetVal(key, data, nil)
  58. return v, nil
  59. }
  60. func Set(key string, value interface{}) error {
  61. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  62. if err != nil {
  63. return err
  64. }
  65. _, modified := cmdline.GetOrSetVal(key, existing, value)
  66. c := &CloudConfig{}
  67. if err = util.Convert(modified, c); err != nil {
  68. return err
  69. }
  70. return WriteToFile(modified, CloudConfigFile)
  71. }
  72. func GetKernelVersion() string {
  73. b, err := ioutil.ReadFile("/proc/version")
  74. if err != nil {
  75. return ""
  76. }
  77. elem := strings.Split(string(b), " ")
  78. return elem[2]
  79. }