factory.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package docker
  2. import (
  3. log "github.com/Sirupsen/logrus"
  4. "github.com/rancherio/os/config"
  5. "github.com/rancherio/os/util"
  6. "github.com/rancherio/rancher-compose/librcompose/project"
  7. )
  8. type ContainerFactory struct {
  9. cfg *config.Config
  10. }
  11. type containerBasedService struct {
  12. project.EmptyService
  13. name string
  14. project *project.Project
  15. container *Container
  16. serviceConfig *project.ServiceConfig
  17. cfg *config.Config
  18. }
  19. func NewContainerFactory(cfg *config.Config) *ContainerFactory {
  20. return &ContainerFactory{
  21. cfg: cfg,
  22. }
  23. }
  24. func (c *containerBasedService) Up() error {
  25. container := c.container
  26. containerCfg := c.container.ContainerCfg
  27. fakeCreate := false
  28. create := containerCfg.CreateOnly
  29. if util.Contains(c.cfg.Disable, c.name) {
  30. fakeCreate = true
  31. }
  32. var event project.Event
  33. c.project.Notify(project.CONTAINER_STARTING, c.name, map[string]string{})
  34. if fakeCreate {
  35. event = project.CONTAINER_CREATED
  36. } else if create {
  37. container.Create()
  38. event = project.CONTAINER_CREATED
  39. } else {
  40. container.StartAndWait()
  41. event = project.CONTAINER_STARTED
  42. }
  43. if container.Err != nil {
  44. log.Errorf("Failed to run %v: %v", containerCfg.Id, container.Err)
  45. }
  46. if container.Err == nil && containerCfg.ReloadConfig {
  47. return project.ErrRestart
  48. }
  49. if container.Container != nil {
  50. c.project.Notify(event, c.name, map[string]string{
  51. project.CONTAINER_ID: container.Container.ID,
  52. })
  53. }
  54. return container.Err
  55. }
  56. func (c *containerBasedService) Config() *project.ServiceConfig {
  57. return c.serviceConfig
  58. }
  59. func (c *containerBasedService) Name() string {
  60. return c.name
  61. }
  62. func isSystemService(serviceConfig *project.ServiceConfig) bool {
  63. return serviceConfig.Labels.MapParts()[config.SCOPE] == config.SYSTEM
  64. }
  65. func (c *ContainerFactory) Create(project *project.Project, name string, serviceConfig *project.ServiceConfig) (project.Service, error) {
  66. host := config.DOCKER_HOST
  67. if isSystemService(serviceConfig) {
  68. host = config.DOCKER_SYSTEM_HOST
  69. }
  70. container := NewContainerFromService(host, name, serviceConfig)
  71. if container.Err != nil {
  72. return nil, container.Err
  73. }
  74. return &containerBasedService{
  75. name: name,
  76. project: project,
  77. container: container,
  78. serviceConfig: serviceConfig,
  79. cfg: c.cfg,
  80. }, nil
  81. }