bootstrap.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package init
  2. import (
  3. "syscall"
  4. "fmt"
  5. "strings"
  6. log "github.com/Sirupsen/logrus"
  7. "github.com/rancher/docker-from-scratch"
  8. "github.com/rancher/os/compose"
  9. "github.com/rancher/os/config"
  10. "github.com/rancher/os/util"
  11. )
  12. func autoformat(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  13. if len(cfg.Rancher.State.Autoformat) == 0 || util.ResolveDevice(cfg.Rancher.State.Dev) != "" {
  14. return cfg, nil
  15. }
  16. AUTOFORMAT := "AUTOFORMAT=" + strings.Join(cfg.Rancher.State.Autoformat, " ")
  17. FORMATZERO := "FORMATZERO=" + fmt.Sprint(cfg.Rancher.State.FormatZero)
  18. t := *cfg
  19. t.Rancher.Autoformat["autoformat"].Environment = []string{AUTOFORMAT, FORMATZERO}
  20. log.Info("Running Autoformat services")
  21. _, err := compose.RunServiceSet("autoformat", &t, t.Rancher.Autoformat)
  22. return &t, err
  23. }
  24. func runBootstrapContainers(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  25. log.Info("Running Bootstrap services")
  26. _, err := compose.RunServiceSet("bootstrap", cfg, cfg.Rancher.BootstrapContainers)
  27. return cfg, err
  28. }
  29. func startDocker(cfg *config.CloudConfig) (chan interface{}, error) {
  30. launchConfig, args := getLaunchConfig(cfg, &cfg.Rancher.BootstrapDocker)
  31. launchConfig.Fork = true
  32. launchConfig.LogFile = ""
  33. launchConfig.NoLog = true
  34. cmd, err := dockerlaunch.LaunchDocker(launchConfig, config.SYSTEM_DOCKER_BIN, args...)
  35. if err != nil {
  36. return nil, err
  37. }
  38. c := make(chan interface{})
  39. go func() {
  40. <-c
  41. cmd.Process.Signal(syscall.SIGTERM)
  42. cmd.Wait()
  43. c <- struct{}{}
  44. }()
  45. return c, nil
  46. }
  47. func stopDocker(c chan interface{}) error {
  48. c <- struct{}{}
  49. <-c
  50. return nil
  51. }
  52. func bootstrap(cfg *config.CloudConfig) error {
  53. log.Info("Launching Bootstrap Docker")
  54. c, err := startDocker(cfg)
  55. if err != nil {
  56. return err
  57. }
  58. defer stopDocker(c)
  59. _, err = config.ChainCfgFuncs(cfg,
  60. loadImages,
  61. runBootstrapContainers,
  62. autoformat)
  63. return err
  64. }