configfiles.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package configfiles
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/pkg/log"
  9. "github.com/rancher/os/pkg/util"
  10. )
  11. var (
  12. ConfigFiles = map[string][]byte{}
  13. )
  14. func ReadConfigFiles(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  15. filesToCopy := []string{
  16. config.CloudConfigInitFile,
  17. config.CloudConfigScriptFile,
  18. config.CloudConfigBootFile,
  19. config.CloudConfigNetworkFile,
  20. config.MetaDataFile,
  21. config.EtcResolvConfFile,
  22. }
  23. // And all the files in /var/log/boot/
  24. // TODO: I wonder if we can put this code into the log module, and have things write to the buffer until we FsReady()
  25. bootLog := "/var/log/"
  26. if files, err := ioutil.ReadDir(bootLog); err == nil {
  27. for _, file := range files {
  28. if !file.IsDir() {
  29. filePath := filepath.Join(bootLog, file.Name())
  30. filesToCopy = append(filesToCopy, filePath)
  31. log.Debugf("Swizzle: Found %s to save", filePath)
  32. }
  33. }
  34. }
  35. bootLog = "/var/log/boot/"
  36. if files, err := ioutil.ReadDir(bootLog); err == nil {
  37. for _, file := range files {
  38. filePath := filepath.Join(bootLog, file.Name())
  39. filesToCopy = append(filesToCopy, filePath)
  40. log.Debugf("Swizzle: Found %s to save", filePath)
  41. }
  42. }
  43. for _, name := range filesToCopy {
  44. if _, err := os.Lstat(name); !os.IsNotExist(err) {
  45. content, err := ioutil.ReadFile(name)
  46. if err != nil {
  47. log.Errorf("read cfg file (%s) %s", name, err)
  48. continue
  49. }
  50. log.Debugf("Swizzle: Saved %s to memory", name)
  51. ConfigFiles[name] = content
  52. }
  53. }
  54. return cfg, nil
  55. }
  56. func WriteConfigFiles(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  57. for name, content := range ConfigFiles {
  58. dirMode := os.ModeDir | 0755
  59. fileMode := os.FileMode(0444)
  60. if strings.HasPrefix(name, "/var/lib/rancher/conf/") {
  61. // only make the conf files harder to get to
  62. dirMode = os.ModeDir | 0700
  63. if name == config.CloudConfigScriptFile {
  64. fileMode = os.FileMode(0755)
  65. } else {
  66. fileMode = os.FileMode(0400)
  67. }
  68. }
  69. if err := os.MkdirAll(filepath.Dir(name), dirMode); err != nil {
  70. log.Error(err)
  71. }
  72. if err := util.WriteFileAtomic(name, content, fileMode); err != nil {
  73. log.Error(err)
  74. }
  75. log.Infof("Swizzle: Wrote file to %s", name)
  76. }
  77. if err := os.MkdirAll(config.VarRancherDir, os.ModeDir|0755); err != nil {
  78. log.Error(err)
  79. }
  80. if err := os.Chmod(config.VarRancherDir, os.ModeDir|0755); err != nil {
  81. log.Error(err)
  82. }
  83. log.FsReady()
  84. log.Debugf("WARNING: switchroot and mount OEM2 and BOOT phases not written to log file")
  85. return cfg, nil
  86. }