bootstrap.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package init
  2. import (
  3. "os"
  4. "os/exec"
  5. "syscall"
  6. "fmt"
  7. log "github.com/Sirupsen/logrus"
  8. "github.com/rancherio/os/config"
  9. "github.com/rancherio/os/docker"
  10. "github.com/rancherio/os/util"
  11. "github.com/rancherio/rancher-compose/librcompose/project"
  12. "strings"
  13. )
  14. func autoformat(cfg *config.Config) error {
  15. if len(cfg.State.Autoformat) == 0 || util.ResolveDevice(cfg.State.Dev) != "" {
  16. return nil
  17. }
  18. AUTOFORMAT := "AUTOFORMAT=" + strings.Join(cfg.State.Autoformat, " ")
  19. FORMATZERO := "FORMATZERO=" + fmt.Sprint(cfg.State.FormatZero)
  20. cfg.Autoformat["autoformat"].Environment = project.NewMaporEqualSlice([]string{AUTOFORMAT, FORMATZERO})
  21. log.Info("Running Autoformat services")
  22. err := docker.RunServices("autoformat", cfg, cfg.Autoformat)
  23. return err
  24. }
  25. func runBootstrapContainers(cfg *config.Config) error {
  26. log.Info("Running Bootstrap services")
  27. return docker.RunServices("bootstrap", cfg, cfg.BootstrapContainers)
  28. }
  29. func startDocker(cfg *config.Config) (chan interface{}, error) {
  30. for _, d := range []string{config.DOCKER_SYSTEM_HOST, "/var/run"} {
  31. err := os.MkdirAll(d, 0700)
  32. if err != nil {
  33. return nil, err
  34. }
  35. }
  36. cmd := exec.Command(cfg.BootstrapDocker.Args[0], cfg.BootstrapDocker.Args[1:]...)
  37. if cfg.Debug {
  38. cmd.Stdout = os.Stdout
  39. cmd.Stderr = os.Stderr
  40. }
  41. err := cmd.Start()
  42. if err != nil {
  43. return nil, err
  44. }
  45. c := make(chan interface{})
  46. go func() {
  47. <-c
  48. cmd.Process.Signal(syscall.SIGTERM)
  49. cmd.Wait()
  50. c <- struct{}{}
  51. }()
  52. return c, nil
  53. }
  54. func stopDocker(c chan interface{}) error {
  55. c <- struct{}{}
  56. <-c
  57. return os.RemoveAll(config.DOCKER_SYSTEM_HOME)
  58. }
  59. func bootstrap(cfg *config.Config) error {
  60. log.Info("Launching Bootstrap Docker")
  61. c, err := startDocker(cfg)
  62. if err != nil {
  63. return err
  64. }
  65. initFuncs := []config.InitFunc{
  66. loadImages,
  67. runBootstrapContainers,
  68. autoformat,
  69. }
  70. defer stopDocker(c)
  71. return config.RunInitFuncs(cfg, initFuncs)
  72. }