reload.go 2.9 KB

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