Exemplo n.º 1
0
func StageServices(cfg *config.CloudConfig, services ...string) error {
	p, err := newProject("stage-services", cfg, nil)
	if err != nil {
		return err
	}

	for _, service := range services {
		bytes, err := LoadServiceResource(service, true, cfg)
		if err != nil {
			return fmt.Errorf("Failed to load %s : %v", service, err)
		}

		m := map[interface{}]interface{}{}
		if err := yaml.Unmarshal(bytes, &m); err != nil {
			return fmt.Errorf("Failed to parse YAML configuration: %s : %v", service, err)
		}

		bytes, err = yaml.Marshal(config.StringifyValues(m))
		if err != nil {
			fmt.Errorf("Failed to marshal YAML configuration: %s : %v", service, err)
		}

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

	// Reduce service configurations to just image and labels
	for serviceName, serviceConfig := range p.Configs {
		p.Configs[serviceName] = &project.ServiceConfig{
			Image:  serviceConfig.Image,
			Labels: serviceConfig.Labels,
		}

	}

	return p.Pull()
}
Exemplo n.º 2
0
func newCoreServiceProject(cfg *config.CloudConfig, network bool) (*project.Project, error) {
	projectEvents := make(chan project.Event)
	enabled := map[interface{}]interface{}{}

	p, err := newProject("os", cfg)
	if err != nil {
		return nil, err
	}

	p.AddListener(project.NewDefaultListener(p))
	p.AddListener(projectEvents)

	p.ReloadCallback = func() error {
		var err error
		cfg, err = config.LoadConfig()
		if err != nil {
			return err
		}

		enabled = addServices(p, enabled, cfg.Rancher.Services)

		for service, serviceEnabled := range cfg.Rancher.ServicesInclude {
			if _, ok := enabled[service]; ok || !serviceEnabled {
				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
			}

			m := map[interface{}]interface{}{}
			if err := yaml.Unmarshal(bytes, &m); err != nil {
				log.Errorf("Failed to parse YAML configuration: %s : %v", service, err)
				continue
			}
			bytes, err = yaml.Marshal(adjustContainerNames(config.StringifyValues(m)))
			if err != nil {
				log.Errorf("Failed to marshal YAML configuration: %s : %v", service, err)
				continue
			}
			err = p.Load(bytes)
			if err != nil {
				log.Errorf("Failed to load %s : %v", service, err)
				continue
			}

			enabled[service] = service
		}

		return nil
	}

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

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

	return p, nil
}