reload.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package compose
  2. import (
  3. "fmt"
  4. "github.com/rancher/os/config"
  5. "github.com/rancher/os/pkg/docker"
  6. "github.com/rancher/os/pkg/log"
  7. "github.com/rancher/os/pkg/util/network"
  8. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  9. composeConfig "github.com/docker/libcompose/config"
  10. "github.com/docker/libcompose/project"
  11. )
  12. func LoadService(p *project.Project, cfg *config.CloudConfig, useNetwork bool, service string) error {
  13. // First check the multi engine service file.
  14. // If the name has been found in multi enging service file and matches, will not execute network.LoadServiceResource
  15. // Otherwise will execute network.LoadServiceResource
  16. bytes, err := network.LoadMultiEngineResource(service)
  17. if err != nil || bytes == nil {
  18. bytes, err = network.LoadServiceResource(service, useNetwork, cfg)
  19. if err != nil {
  20. log.Error(err)
  21. return err
  22. }
  23. }
  24. m := map[interface{}]interface{}{}
  25. if err = yaml.Unmarshal(bytes, &m); err != nil {
  26. e := fmt.Errorf("Failed to parse YAML configuration for %s: %v", service, err)
  27. log.Error(e)
  28. return e
  29. }
  30. m = adjustContainerNames(m)
  31. bytes, err = yaml.Marshal(m)
  32. if err != nil {
  33. e := fmt.Errorf("Failed to marshal YAML configuration for %s: %v", service, err)
  34. log.Error(e)
  35. return e
  36. }
  37. if err = p.Load(bytes); err != nil {
  38. e := fmt.Errorf("Failed to load %s: %v", service, err)
  39. log.Error(e)
  40. return e
  41. }
  42. return nil
  43. }
  44. func LoadSpecialService(p *project.Project, cfg *config.CloudConfig, serviceName, serviceValue string) error {
  45. // Save config in case load fails
  46. previousConfig, ok := p.ServiceConfigs.Get(serviceName)
  47. p.ServiceConfigs.Add(serviceName, &composeConfig.ServiceConfig{})
  48. if err := LoadService(p, cfg, true, serviceValue); err != nil {
  49. // Rollback to previous config
  50. if ok {
  51. p.ServiceConfigs.Add(serviceName, previousConfig)
  52. }
  53. return err
  54. }
  55. return nil
  56. }
  57. func loadConsoleService(cfg *config.CloudConfig, p *project.Project) error {
  58. if cfg.Rancher.Console == "" || cfg.Rancher.Console == "default" {
  59. return nil
  60. }
  61. return LoadSpecialService(p, cfg, "console", cfg.Rancher.Console)
  62. }
  63. func loadEngineService(cfg *config.CloudConfig, p *project.Project) error {
  64. if cfg.Rancher.Docker.Engine == "" || cfg.Rancher.Docker.Engine == cfg.Rancher.Defaults.Docker.Engine {
  65. return nil
  66. }
  67. return LoadSpecialService(p, cfg, "docker", cfg.Rancher.Docker.Engine)
  68. }
  69. func projectReload(p *project.Project, useNetwork *bool, loadConsole bool, environmentLookup *docker.ConfigEnvironment, authLookup *docker.ConfigAuthLookup) func() error {
  70. enabled := map[interface{}]interface{}{}
  71. return func() error {
  72. cfg := config.LoadConfig()
  73. environmentLookup.SetConfig(cfg)
  74. authLookup.SetConfig(cfg)
  75. enabled = addServices(p, enabled, cfg.Rancher.Services)
  76. for service, serviceEnabled := range cfg.Rancher.ServicesInclude {
  77. if _, ok := enabled[service]; ok || !serviceEnabled {
  78. continue
  79. }
  80. if err := LoadService(p, cfg, *useNetwork, service); err != nil {
  81. if err != network.ErrNoNetwork {
  82. log.Errorf("Failed to load service(%s): %v", service, err)
  83. }
  84. continue
  85. }
  86. enabled[service] = service
  87. }
  88. if !*useNetwork {
  89. return nil
  90. }
  91. if loadConsole {
  92. if err := loadConsoleService(cfg, p); err != nil {
  93. log.Errorf("Failed to load rancher.console=(%s): %v", cfg.Rancher.Console, err)
  94. }
  95. }
  96. if err := loadEngineService(cfg, p); err != nil {
  97. log.Errorf("Failed to load rancher.docker.engine=(%s): %v", cfg.Rancher.Docker.Engine, err)
  98. }
  99. return nil
  100. }
  101. }