Example #1
0
func parseServiceBackend(rawCfg map[string]interface{}) (discovery.ServiceBackend, error) {
	var discoveryService discovery.ServiceBackend
	var err error
	discoveryCount := 0
	for _, key := range discovery.GetBackends() {
		handler := discovery.GetConfigHook(key)
		if handler != nil {
			if rawCfg, ok := rawCfg[key]; ok {
				discoveryService, err = handler(rawCfg)
				if err != nil {
					return nil, err
				}
				log.Debugf("parsed service discovery backend: %s", key)
				discoveryCount++
			}
			delete(rawCfg, key)
		}
	}
	if discoveryCount == 0 {
		return nil, errors.New("No discovery backend defined")
	} else if discoveryCount > 1 {
		return nil, errors.New("More than one discovery backend defined")
	}
	return discoveryService, nil
}
Example #2
0
// ParseConfig parses a raw config flag
func ParseConfig(configFlag string) (*Config, error) {
	if configFlag == "" {
		return nil, errors.New("-config flag is required")
	}

	var data []byte
	if strings.HasPrefix(configFlag, "file://") {
		var err error
		fName := strings.SplitAfter(configFlag, "file://")[1]
		if data, err = ioutil.ReadFile(fName); err != nil {
			return nil, fmt.Errorf("Could not read config file: %s", err)
		}
	} else {
		data = []byte(configFlag)
	}

	template, err := ApplyTemplate(data)
	if err != nil {
		return nil, fmt.Errorf(
			"Could not apply template to config: %v", err)
	}
	configMap, err := unmarshalConfig(template)
	if err != nil {
		return nil, err
	}
	discoveryService, err := parseServiceBackend(configMap)
	if err != nil {
		return nil, err
	}
	// Delete discovery backend keys
	for _, backend := range discovery.GetBackends() {
		delete(configMap, backend)
	}
	raw := &rawConfig{}
	if err = decodeConfig(configMap, raw); err != nil {
		return nil, err
	}
	cfg := &Config{}
	cfg.ServiceBackend = discoveryService
	cfg.LogConfig = raw.logConfig

	preStartCmd, err := raw.parsePreStart()
	if err != nil {
		return nil, err
	}
	cfg.PreStart = preStartCmd

	preStopCmd, err := raw.parsePreStop()
	if err != nil {
		return nil, err
	}
	cfg.PreStop = preStopCmd

	postStopCmd, err := raw.parsePostStop()
	if err != nil {
		return nil, err
	}
	cfg.PostStop = postStopCmd

	stopTimeout, err := raw.parseStopTimeout()
	if err != nil {
		return nil, err
	}
	cfg.StopTimeout = stopTimeout

	services, err := raw.parseServices(discoveryService)
	if err != nil {
		return nil, fmt.Errorf("Unable to parse services: %v", err)
	}
	cfg.Services = services

	backends, err := raw.parseBackends(discoveryService)
	if err != nil {
		return nil, fmt.Errorf("Unable to parse backends: %v", err)
	}
	cfg.Backends = backends

	telemetry, err := raw.parseTelemetry()
	if err != nil {
		return nil, err
	}

	if telemetry != nil {
		telemetryService, err2 := createTelemetryService(telemetry, discoveryService)
		if err2 != nil {
			return nil, err2
		}
		cfg.Telemetry = telemetry
		cfg.Services = append(cfg.Services, telemetryService)
	}

	tasks, err := raw.parseTasks()
	if err != nil {
		return nil, err
	}
	cfg.Tasks = tasks

	coprocesses, err := raw.parseCoprocesses()
	if err != nil {
		return nil, err
	}
	cfg.Coprocesses = coprocesses

	return cfg, nil
}