bootstrap.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. loadImages,
  57. bootstrapServices)
  58. return err
  59. }
  60. func runCloudInitServices(cfg *config.CloudConfig) error {
  61. c, err := startDocker(cfg)
  62. if err != nil {
  63. return err
  64. }
  65. defer stopDocker(c)
  66. _, err = config.ChainCfgFuncs(cfg,
  67. loadImages,
  68. runCloudInitServiceSet)
  69. return err
  70. }