bootstrap.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package control
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "time"
  8. "github.com/codegangsta/cli"
  9. "github.com/rancher/os/config"
  10. "github.com/rancher/os/log"
  11. "github.com/rancher/os/util"
  12. )
  13. func bootstrapAction(c *cli.Context) error {
  14. log.Debugf("bootstrapAction")
  15. if err := UdevSettle(); err != nil {
  16. log.Errorf("Failed to run udev settle: %v", err)
  17. }
  18. log.Debugf("bootstrapAction: loadingConfig")
  19. cfg := config.LoadConfig()
  20. log.Debugf("bootstrapAction: MdadmScan(%v)", cfg.Rancher.State.MdadmScan)
  21. if cfg.Rancher.State.MdadmScan {
  22. if err := mdadmScan(); err != nil {
  23. log.Errorf("Failed to run mdadm scan: %v", err)
  24. }
  25. }
  26. stateScript := cfg.Rancher.State.Script
  27. log.Debugf("bootstrapAction: stateScript(%v)", stateScript)
  28. if stateScript != "" {
  29. if err := runStateScript(stateScript); err != nil {
  30. log.Errorf("Failed to run state script: %v", err)
  31. }
  32. }
  33. log.Debugf("bootstrapAction: RunCommandSequence(%v)", cfg.Bootcmd)
  34. util.RunCommandSequence(cfg.Bootcmd)
  35. if cfg.Rancher.State.Dev != "" && cfg.Rancher.State.Wait {
  36. waitForRoot(cfg)
  37. }
  38. autoformatDevices := cfg.Rancher.State.Autoformat
  39. log.Debugf("bootstrapAction: Autoformat(%v)", cfg.Rancher.State.Autoformat)
  40. if len(autoformatDevices) > 0 {
  41. if err := autoformat(autoformatDevices); err != nil {
  42. log.Errorf("Failed to run autoformat: %v", err)
  43. }
  44. }
  45. log.Debugf("bootstrapAction: udev settle2")
  46. if err := UdevSettle(); err != nil {
  47. log.Errorf("Failed to run udev settle: %v", err)
  48. }
  49. return nil
  50. }
  51. func mdadmScan() error {
  52. cmd := exec.Command("mdadm", "--assemble", "--scan")
  53. cmd.Stdout = os.Stdout
  54. cmd.Stderr = os.Stderr
  55. return cmd.Run()
  56. }
  57. func runStateScript(script string) error {
  58. f, err := ioutil.TempFile("", "")
  59. if err != nil {
  60. return err
  61. }
  62. if _, err := f.WriteString(script); err != nil {
  63. return err
  64. }
  65. if err := f.Chmod(os.ModePerm); err != nil {
  66. return err
  67. }
  68. if err := f.Close(); err != nil {
  69. return err
  70. }
  71. return util.RunScript(f.Name())
  72. }
  73. func waitForRoot(cfg *config.CloudConfig) {
  74. var dev string
  75. for i := 0; i < 30; i++ {
  76. dev = util.ResolveDevice(cfg.Rancher.State.Dev)
  77. if dev != "" {
  78. break
  79. }
  80. time.Sleep(time.Millisecond * 1000)
  81. }
  82. if dev == "" {
  83. return
  84. }
  85. for i := 0; i < 30; i++ {
  86. if _, err := os.Stat(dev); err == nil {
  87. break
  88. }
  89. time.Sleep(time.Millisecond * 1000)
  90. }
  91. }
  92. func autoformat(autoformatDevices []string) error {
  93. cmd := exec.Command("/usr/sbin/auto-format.sh")
  94. cmd.Stdout = os.Stdout
  95. cmd.Stderr = os.Stderr
  96. cmd.Env = []string{
  97. "AUTOFORMAT=" + strings.Join(autoformatDevices, " "),
  98. }
  99. return cmd.Run()
  100. }