reload.go 3.1 KB

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