service.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/Sirupsen/logrus"
  5. dockerclient "github.com/docker/engine-api/client"
  6. "github.com/docker/engine-api/types"
  7. composeConfig "github.com/docker/libcompose/config"
  8. "github.com/docker/libcompose/docker"
  9. "github.com/docker/libcompose/project"
  10. "github.com/docker/libcompose/project/options"
  11. "github.com/rancher/os/config"
  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. // Linking to cloud-init is a hack really. The problem is we need to link to something
  38. // that will trigger a reload
  39. rels = appendLink(rels, "cloud-init", false, s.project)
  40. } else if s.missingImage() {
  41. rels = appendLink(rels, "network", false, s.project)
  42. }
  43. return rels
  44. }
  45. func (s *Service) missingImage() bool {
  46. image := s.Config().Image
  47. if image == "" {
  48. return false
  49. }
  50. client := s.context.ClientFactory.Create(s)
  51. _, _, err := client.ImageInspectWithRaw(context.Background(), s.Config().Image, false)
  52. return err != nil
  53. }
  54. func (s *Service) requiresSyslog() bool {
  55. return s.Config().Logging.Driver == "syslog"
  56. }
  57. func (s *Service) requiresUserDocker() bool {
  58. return s.Config().Labels[config.SCOPE] != config.SYSTEM
  59. }
  60. func appendLink(deps []project.ServiceRelationship, name string, optional bool, p *project.Project) []project.ServiceRelationship {
  61. if _, ok := p.ServiceConfigs.Get(name); !ok {
  62. return deps
  63. }
  64. rel := project.NewServiceRelationship(name, project.RelTypeLink)
  65. rel.Optional = optional
  66. return append(deps, rel)
  67. }
  68. func (s *Service) shouldRebuild(ctx context.Context) (bool, error) {
  69. containers, err := s.Containers(ctx)
  70. if err != nil {
  71. return false, err
  72. }
  73. cfg := config.LoadConfig()
  74. for _, c := range containers {
  75. outOfSync, err := c.(*docker.Container).OutOfSync(ctx, s.Service.Config().Image)
  76. if err != nil {
  77. return false, err
  78. }
  79. _, containerInfo, err := s.getContainer(ctx)
  80. if err != nil {
  81. return false, err
  82. }
  83. name := containerInfo.Name[1:]
  84. origRebuildLabel := containerInfo.Config.Labels[config.REBUILD]
  85. newRebuildLabel := s.Config().Labels[config.REBUILD]
  86. rebuildLabelChanged := newRebuildLabel != origRebuildLabel
  87. logrus.WithFields(logrus.Fields{
  88. "origRebuildLabel": origRebuildLabel,
  89. "newRebuildLabel": newRebuildLabel,
  90. "rebuildLabelChanged": rebuildLabelChanged,
  91. "outOfSync": outOfSync}).Debug("Rebuild values")
  92. if newRebuildLabel == "always" {
  93. return true, nil
  94. }
  95. if s.Name() == "console" && cfg.Rancher.ForceConsoleRebuild {
  96. if err := config.Set("rancher.force_console_rebuild", false); err != nil {
  97. return false, err
  98. }
  99. return true, nil
  100. }
  101. if outOfSync {
  102. if s.Name() == "console" {
  103. origConsoleLabel := containerInfo.Config.Labels[config.CONSOLE]
  104. newConsoleLabel := s.Config().Labels[config.CONSOLE]
  105. if newConsoleLabel != origConsoleLabel {
  106. return true, nil
  107. }
  108. } else if rebuildLabelChanged || origRebuildLabel != "false" {
  109. return true, nil
  110. } else {
  111. logrus.Warnf("%s needs rebuilding", name)
  112. }
  113. }
  114. }
  115. return false, nil
  116. }
  117. func (s *Service) Up(ctx context.Context, options options.Up) error {
  118. labels := s.Config().Labels
  119. if err := s.Service.Create(ctx, options.Create); err != nil {
  120. return err
  121. }
  122. shouldRebuild, err := s.shouldRebuild(ctx)
  123. if err != nil {
  124. return err
  125. }
  126. if shouldRebuild {
  127. logrus.Infof("Rebuilding %s", s.Name())
  128. cs, err := s.Service.Containers(ctx)
  129. if err != nil {
  130. return err
  131. }
  132. for _, c := range cs {
  133. if _, err := c.(*docker.Container).Recreate(ctx, s.Config().Image); err != nil {
  134. return err
  135. }
  136. }
  137. if err = s.rename(ctx); err != nil {
  138. return err
  139. }
  140. }
  141. if labels[config.CREATE_ONLY] == "true" {
  142. return s.checkReload(labels)
  143. }
  144. if err := s.Service.Up(ctx, options); err != nil {
  145. return err
  146. }
  147. if labels[config.DETACH] == "false" {
  148. if err := s.wait(ctx); err != nil {
  149. return err
  150. }
  151. }
  152. return s.checkReload(labels)
  153. }
  154. func (s *Service) checkReload(labels map[string]string) error {
  155. if labels[config.RELOAD_CONFIG] == "true" {
  156. return project.ErrRestart
  157. }
  158. return nil
  159. }
  160. func (s *Service) Create(ctx context.Context, options options.Create) error {
  161. return s.Service.Create(ctx, options)
  162. }
  163. func (s *Service) getContainer(ctx context.Context) (dockerclient.APIClient, types.ContainerJSON, error) {
  164. containers, err := s.Service.Containers(ctx)
  165. if err != nil {
  166. return nil, types.ContainerJSON{}, err
  167. }
  168. if len(containers) == 0 {
  169. return nil, types.ContainerJSON{}, fmt.Errorf("No containers found for %s", s.Name())
  170. }
  171. id, err := containers[0].ID()
  172. if err != nil {
  173. return nil, types.ContainerJSON{}, err
  174. }
  175. client := s.context.ClientFactory.Create(s)
  176. info, err := client.ContainerInspect(context.Background(), id)
  177. return client, info, err
  178. }
  179. func (s *Service) wait(ctx context.Context) error {
  180. client, info, err := s.getContainer(ctx)
  181. if err != nil {
  182. return err
  183. }
  184. if _, err := client.ContainerWait(context.Background(), info.ID); err != nil {
  185. return err
  186. }
  187. return nil
  188. }
  189. func (s *Service) rename(ctx context.Context) error {
  190. client, info, err := s.getContainer(ctx)
  191. if err != nil {
  192. return err
  193. }
  194. if len(info.Name) > 0 && info.Name[1:] != s.Name() {
  195. logrus.Debugf("Renaming container %s => %s", info.Name[1:], s.Name())
  196. return client.ContainerRename(context.Background(), info.ID, s.Name())
  197. } else {
  198. return nil
  199. }
  200. }