bootstrap.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package init
  2. import (
  3. "syscall"
  4. "github.com/rancher/os/compose"
  5. "github.com/rancher/os/config"
  6. "github.com/rancher/os/dfs"
  7. "github.com/rancher/os/log"
  8. "github.com/rancher/os/util"
  9. )
  10. func bootstrapServices(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  11. if util.ResolveDevice(cfg.Rancher.State.Dev) != "" && len(cfg.Bootcmd) == 0 {
  12. log.Info("NOT Running Bootstrap")
  13. return cfg, nil
  14. }
  15. log.Info("Running Bootstrap")
  16. _, err := compose.RunServiceSet("bootstrap", cfg, cfg.Rancher.BootstrapContainers)
  17. return cfg, err
  18. }
  19. func runCloudInitServiceSet(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  20. log.Info("Running cloud-init services")
  21. _, err := compose.RunServiceSet("cloud-init", cfg, cfg.Rancher.CloudInitServices)
  22. return cfg, err
  23. }
  24. func startDocker(cfg *config.CloudConfig) (chan interface{}, error) {
  25. launchConfig, args := getLaunchConfig(cfg, &cfg.Rancher.BootstrapDocker)
  26. launchConfig.Fork = true
  27. launchConfig.LogFile = ""
  28. launchConfig.NoLog = true
  29. cmd, err := dfs.LaunchDocker(launchConfig, config.SystemDockerBin, args...)
  30. if err != nil {
  31. return nil, err
  32. }
  33. c := make(chan interface{})
  34. go func() {
  35. <-c
  36. cmd.Process.Signal(syscall.SIGTERM)
  37. cmd.Wait()
  38. c <- struct{}{}
  39. }()
  40. return c, nil
  41. }
  42. func stopDocker(c chan interface{}) error {
  43. c <- struct{}{}
  44. <-c
  45. return nil
  46. }
  47. func bootstrap(cfg *config.CloudConfig) error {
  48. log.Info("Launching Bootstrap Docker")
  49. log.Infof("bootstrap container: Autoformat(%v)", cfg.Rancher.State.Autoformat)
  50. c, err := startDocker(cfg)
  51. if err != nil {
  52. return err
  53. }
  54. defer stopDocker(c)
  55. _, err = config.ChainCfgFuncs(cfg,
  56. []config.CfgFuncData{
  57. config.CfgFuncData{"bootstrap loadImages", loadImages},
  58. config.CfgFuncData{"bootstrap Services", bootstrapServices},
  59. })
  60. return err
  61. }
  62. func runCloudInitServices(cfg *config.CloudConfig) error {
  63. c, err := startDocker(cfg)
  64. if err != nil {
  65. return err
  66. }
  67. defer stopDocker(c)
  68. _, err = config.ChainCfgFuncs(cfg,
  69. []config.CfgFuncData{
  70. config.CfgFuncData{"cloudinit loadImages", loadImages},
  71. config.CfgFuncData{"cloudinit Services", runCloudInitServiceSet},
  72. })
  73. return err
  74. }