disk.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package config
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/rancherio/os/util"
  6. "gopkg.in/yaml.v2"
  7. )
  8. func writeToFile(data interface{}, filename string) error {
  9. content, err := yaml.Marshal(data)
  10. if err != nil {
  11. return err
  12. }
  13. return ioutil.WriteFile(filename, content, 400)
  14. }
  15. func saveToDisk(data map[interface{}]interface{}) error {
  16. config := make(map[interface{}]interface{})
  17. private := make(map[interface{}]interface{})
  18. for k, v := range data {
  19. if k == "ssh" {
  20. private[k] = v
  21. } else if k == "user_docker" {
  22. var userDockerConfig DockerConfig
  23. var userDockerConfigPrivate DockerConfig
  24. err := util.Convert(v, &userDockerConfig)
  25. if err != nil {
  26. return err
  27. }
  28. userDockerConfigPrivate.CAKey = userDockerConfig.CAKey
  29. userDockerConfigPrivate.CACert = userDockerConfig.CACert
  30. userDockerConfigPrivate.ServerKey = userDockerConfig.ServerKey
  31. userDockerConfigPrivate.ServerCert = userDockerConfig.ServerCert
  32. userDockerConfig.CAKey = ""
  33. userDockerConfig.CACert = ""
  34. userDockerConfig.ServerKey = ""
  35. userDockerConfig.ServerCert = ""
  36. config[k] = userDockerConfig
  37. private[k] = userDockerConfigPrivate
  38. } else {
  39. config[k] = v
  40. }
  41. }
  42. err := writeToFile(config, ConfigFile)
  43. if err != nil {
  44. return err
  45. }
  46. return writeToFile(private, PrivateConfigFile)
  47. }
  48. func readSavedConfig(bytes []byte) (map[interface{}]interface{}, error) {
  49. return readConfig(bytes, CloudConfigFile, ConfigFile, PrivateConfigFile)
  50. }
  51. func readConfig(bytes []byte, files ...string) (map[interface{}]interface{}, error) {
  52. // You can't just overlay yaml bytes on to maps, it won't merge, but instead
  53. // just override the keys and not merge the map values.
  54. left := make(map[interface{}]interface{})
  55. for _, conf := range files {
  56. content, err := readConfigFile(conf)
  57. if err != nil {
  58. return nil, err
  59. }
  60. right := make(map[interface{}]interface{})
  61. err = yaml.Unmarshal(content, &right)
  62. if err != nil {
  63. return nil, err
  64. }
  65. util.MergeMaps(left, right)
  66. }
  67. if bytes != nil && len(bytes) > 0 {
  68. right := make(map[interface{}]interface{})
  69. if err := yaml.Unmarshal(bytes, &right); err != nil {
  70. return nil, err
  71. }
  72. util.MergeMaps(left, right)
  73. }
  74. return left, nil
  75. }
  76. func readConfigFile(file string) ([]byte, error) {
  77. content, err := ioutil.ReadFile(file)
  78. if err != nil {
  79. if os.IsNotExist(err) {
  80. err = nil
  81. content = []byte{}
  82. } else {
  83. return nil, err
  84. }
  85. }
  86. return content, err
  87. }