service.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package docker
  2. import (
  3. "fmt"
  4. dockerclient "github.com/docker/engine-api/client"
  5. "github.com/docker/engine-api/types"
  6. composeConfig "github.com/docker/libcompose/config"
  7. "github.com/docker/libcompose/docker"
  8. "github.com/docker/libcompose/project"
  9. "github.com/docker/libcompose/project/options"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/log"
  12. "golang.org/x/net/context"
  13. )
  14. type Service struct {
  15. *docker.Service
  16. deps map[string][]string
  17. context *docker.Context
  18. project *project.Project
  19. }
  20. func NewService(factory *ServiceFactory, name string, serviceConfig *composeConfig.ServiceConfig, context *docker.Context, project *project.Project) *Service {
  21. return &Service{
  22. Service: docker.NewService(name, serviceConfig, context),
  23. deps: factory.Deps,
  24. context: context,
  25. project: project,
  26. }
  27. }
  28. func (s *Service) DependentServices() []project.ServiceRelationship {
  29. rels := s.Service.DependentServices()
  30. for _, dep := range s.deps[s.Name()] {
  31. rels = appendLink(rels, dep, true, s.project)
  32. }
  33. if s.requiresSyslog() {
  34. rels = appendLink(rels, "syslog", false, s.project)
  35. }
  36. if s.requiresUserDocker() {
  37. rels = appendLink(rels, "docker", false, s.project)
  38. } else if s.missingImage() {
  39. rels = appendLink(rels, "network", false, s.project)
  40. }
  41. return rels
  42. }
  43. func (s *Service) missingImage() bool {
  44. image := s.Config().Image
  45. if image == "" {
  46. return false
  47. }
  48. client := s.context.ClientFactory.Create(s)
  49. _, _, err := client.ImageInspectWithRaw(context.Background(), image, false)
  50. return err != nil
  51. }
  52. func (s *Service) requiresSyslog() bool {
  53. return s.Config().Logging.Driver == "syslog"
  54. }
  55. func (s *Service) requiresUserDocker() bool {
  56. return s.Config().Labels[config.ScopeLabel] != config.System
  57. }
  58. func appendLink(deps []project.ServiceRelationship, name string, optional bool, p *project.Project) []project.ServiceRelationship {
  59. if _, ok := p.ServiceConfigs.Get(name); !ok {
  60. return deps
  61. }
  62. rel := project.NewServiceRelationship(name, project.RelTypeLink)
  63. rel.Optional = optional
  64. return append(deps, rel)
  65. }
  66. func (s *Service) shouldRebuild(ctx context.Context) (bool, error) {
  67. containers, err := s.Containers(ctx)
  68. if err != nil {
  69. return false, err
  70. }
  71. cfg := config.LoadConfig()
  72. for _, c := range containers {
  73. outOfSync, err := c.(*docker.Container).OutOfSync(ctx, s.Service.Config().Image)
  74. if err != nil {
  75. return false, err
  76. }
  77. _, containerInfo, err := s.getContainer(ctx)
  78. if err != nil {
  79. return false, err
  80. }
  81. name := containerInfo.Name[1:]
  82. origRebuildLabel := containerInfo.Config.Labels[config.RebuildLabel]
  83. newRebuildLabel := s.Config().Labels[config.RebuildLabel]
  84. rebuildLabelChanged := newRebuildLabel != origRebuildLabel
  85. log.WithFields(log.Fields{
  86. "origRebuildLabel": origRebuildLabel,
  87. "newRebuildLabel": newRebuildLabel,
  88. "rebuildLabelChanged": rebuildLabelChanged,
  89. "outOfSync": outOfSync}).Debug("Rebuild values")
  90. if newRebuildLabel == "always" {
  91. return true, nil
  92. }
  93. if s.Name() == "console" && cfg.Rancher.ForceConsoleRebuild {
  94. if err := config.Set("rancher.force_console_rebuild", false); err != nil {
  95. return false, err
  96. }
  97. return true, nil
  98. }
  99. if outOfSync {
  100. if s.Name() == "console" {
  101. origConsoleLabel := containerInfo.Config.Labels[config.ConsoleLabel]
  102. newConsoleLabel := s.Config().Labels[config.ConsoleLabel]
  103. if newConsoleLabel != origConsoleLabel {
  104. return true, nil
  105. }
  106. } else if rebuildLabelChanged || origRebuildLabel != "false" {
  107. return true, nil
  108. } else {
  109. log.Warnf("%s needs rebuilding", name)
  110. }
  111. }
  112. }
  113. return false, nil
  114. }
  115. func (s *Service) Up(ctx context.Context, options options.Up) error {
  116. labels := s.Config().Labels
  117. if err := s.Service.Create(ctx, options.Create); err != nil {
  118. return err
  119. }
  120. shouldRebuild, err := s.shouldRebuild(ctx)
  121. if err != nil {
  122. return err
  123. }
  124. if shouldRebuild {
  125. log.Infof("Rebuilding %s", s.Name())
  126. cs, err := s.Service.Containers(ctx)
  127. if err != nil {
  128. return err
  129. }
  130. for _, c := range cs {
  131. if _, err := c.(*docker.Container).Recreate(ctx, s.Config().Image); err != nil {
  132. return err
  133. }
  134. }
  135. if err = s.rename(ctx); err != nil {
  136. return err
  137. }
  138. }
  139. if labels[config.CreateOnlyLabel] == "true" {
  140. return s.checkReload(labels)
  141. }
  142. if err := s.Service.Up(ctx, options); err != nil {
  143. return err
  144. }
  145. if labels[config.DetachLabel] == "false" {
  146. if err := s.wait(ctx); err != nil {
  147. return err
  148. }
  149. }
  150. return s.checkReload(labels)
  151. }
  152. func (s *Service) checkReload(labels map[string]string) error {
  153. if labels[config.ReloadConfigLabel] == "true" {
  154. return project.ErrRestart
  155. }
  156. return nil
  157. }
  158. func (s *Service) Create(ctx context.Context, options options.Create) error {
  159. return s.Service.Create(ctx, options)
  160. }
  161. func (s *Service) getContainer(ctx context.Context) (dockerclient.APIClient, types.ContainerJSON, error) {
  162. containers, err := s.Service.Containers(ctx)
  163. if err != nil {
  164. return nil, types.ContainerJSON{}, err
  165. }
  166. if len(containers) == 0 {
  167. return nil, types.ContainerJSON{}, fmt.Errorf("No containers found for %s", s.Name())
  168. }
  169. id, err := containers[0].ID()
  170. if err != nil {
  171. return nil, types.ContainerJSON{}, err
  172. }
  173. client := s.context.ClientFactory.Create(s)
  174. info, err := client.ContainerInspect(context.Background(), id)
  175. return client, info, err
  176. }
  177. func (s *Service) wait(ctx context.Context) error {
  178. client, info, err := s.getContainer(ctx)
  179. if err != nil {
  180. return err
  181. }
  182. if _, err := client.ContainerWait(context.Background(), info.ID); err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. func (s *Service) rename(ctx context.Context) error {
  188. client, info, err := s.getContainer(ctx)
  189. if err != nil {
  190. return err
  191. }
  192. if len(info.Name) > 0 && info.Name[1:] != s.Name() {
  193. log.Debugf("Renaming container %s => %s", info.Name[1:], s.Name())
  194. return client.ContainerRename(context.Background(), info.ID, s.Name())
  195. }
  196. return nil
  197. }