Example #1
0
func NewProject(c *Context) (*project.Project, error) {
	if err := c.open(); err != nil {
		logrus.Errorf("Failed to open project %s: %v", c.ProjectName, err)
		return nil, err
	}

	factory := &RancherServiceFactory{
		context: c,
	}

	p := project.NewProject(c.ProjectName, factory)
	p.EnvironmentLookup = c
	if c.ComposeFile == "-" {
		p.File = "."
	} else {
		p.File = c.ComposeFile
	}
	p.ConfigLookup = project.FileConfigLookup{}

	err := p.Load(c.ComposeBytes)

	c.Project = p
	c.SidekickInfo = NewSidekickInfo(p)

	return p, err
}
Example #2
0
func RunServices(name string, cfg *config.Config, configs map[string]*project.ServiceConfig) error {
	network := false
	projectEvents := make(chan project.ProjectEvent)
	p := project.NewProject(name, NewContainerFactory(cfg))
	p.EnvironmentLookup = &configEnvironment{cfg: cfg}
	p.AddListener(projectEvents)
	enabled := make(map[string]bool)

	for name, serviceConfig := range configs {
		if err := p.AddConfig(name, serviceConfig); err != nil {
			log.Infof("Failed loading service %s", name)
		}
	}

	p.ReloadCallback = func() error {
		err := cfg.Reload()
		if err != nil {
			return err
		}

		for service, serviceEnabled := range cfg.ServicesInclude {
			if !serviceEnabled {
				continue
			}

			if _, ok := enabled[service]; ok {
				continue
			}

			bytes, err := LoadServiceResource(service, network, cfg)
			if err != nil {
				if err == util.ErrNoNetwork {
					log.Debugf("Can not load %s, networking not enabled", service)
				} else {
					log.Errorf("Failed to load %s : %v", service, err)
				}
				continue
			}

			err = p.Load(bytes)
			if err != nil {
				log.Errorf("Failed to load %s : %v", service, err)
				continue
			}

			enabled[service] = true
		}

		for service, config := range cfg.Services {
			if _, ok := enabled[service]; ok {
				continue
			}

			err = p.AddConfig(service, config)
			if err != nil {
				log.Errorf("Failed to load %s : %v", service, err)
				continue
			}

			enabled[service] = true
		}

		return nil
	}

	go func() {
		for event := range projectEvents {
			if event.Event == project.CONTAINER_STARTED && event.ServiceName == "network" {
				network = true
			}
		}
	}()

	err := p.ReloadCallback()
	if err != nil {
		log.Errorf("Failed to reload %s : %v", name, err)
		return err
	}
	return p.Up()
}