bootstrap.go 1.7 KB

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