bootstrap.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. c, err := startDocker(cfg)
  50. if err != nil {
  51. return err
  52. }
  53. defer stopDocker(c)
  54. _, err = config.ChainCfgFuncs(cfg,
  55. []config.CfgFuncData{
  56. config.CfgFuncData{"bootstrap loadImages", loadImages},
  57. config.CfgFuncData{"bootstrap Services", bootstrapServices},
  58. })
  59. return err
  60. }
  61. func runCloudInitServices(cfg *config.CloudConfig) error {
  62. c, err := startDocker(cfg)
  63. if err != nil {
  64. return err
  65. }
  66. defer stopDocker(c)
  67. _, err = config.ChainCfgFuncs(cfg,
  68. []config.CfgFuncData{
  69. config.CfgFuncData{"cloudinit loadImages", loadImages},
  70. config.CfgFuncData{"cloudinit Services", runCloudInitServiceSet},
  71. })
  72. return err
  73. }