func ParseCatalogConfig(contents []byte) (*model.RancherCompose, error) {
	cfg, err := config.CreateConfig(contents)
	if err != nil {
		return nil, err
	}
	var rawCatalogConfig interface{}

	if cfg.Version == "2" && cfg.Services[".catalog"] != nil {
		rawCatalogConfig = cfg.Services[".catalog"]
	}

	var data map[string]interface{}
	if err := yaml.Unmarshal(contents, &data); err != nil {
		return nil, err
	}

	if data["catalog"] != nil {
		rawCatalogConfig = data["catalog"]
	} else if data[".catalog"] != nil {
		rawCatalogConfig = data[".catalog"]
	}

	if rawCatalogConfig != nil {
		var catalogConfig model.RancherCompose
		if err := utils.Convert(rawCatalogConfig, &catalogConfig); err != nil {
			return nil, err
		}

		return &catalogConfig, nil
	}

	return &model.RancherCompose{}, nil
}
示例#2
0
func (q *QuestionLookup) parse(file string) error {
	contents, err := ioutil.ReadFile(file)
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return err
	}

	config, err := config.CreateConfig(contents)
	if err != nil {
		return err
	}
	data := config.Services

	rawQuestions := data[".catalog"]
	if rawQuestions != nil {
		var wrapper questionWrapper
		if err := utils.Convert(rawQuestions, &wrapper); err != nil {
			return err
		}

		for _, question := range wrapper.Questions {
			q.questions[question.Variable] = question
		}
	}

	return nil
}
func (c *Context) unmarshalBytes(composeBytes, bytes []byte) error {
	rawServiceMap := config.RawServiceMap{}
	if composeBytes != nil {
		config, err := config.CreateConfig(composeBytes)
		if err != nil {
			return err
		}
		rawServiceMap = config.Services
		for key := range rawServiceMap {
			delete(rawServiceMap[key], "hostname")
		}
	}
	if bytes != nil && len(bytes) > 0 {
		config, err := config.CreateConfig(bytes)
		if err != nil {
			return err
		}
		rawServiceMap = config.Services
	}
	return c.fillInRancherConfig(rawServiceMap)
}
//ExtractBindings gets bindings from created RawServiceMap
func ExtractBindings(yamlContent []byte) (BindingProperty, error) {
	var rawConfigDocker config.RawServiceMap
	var bindingsMap map[string]ServiceBinding
	var bindingPropertyMap BindingProperty
	var labels libYaml.SliceorMap

	config, err := config.CreateConfig(yamlContent)
	if err != nil {
		return nil, err
	}
	rawConfigDocker = config.Services

	preProcessServiceMap := preprocess.PreprocessServiceMap(nil)
	rawConfigDocker, err = preProcessServiceMap(rawConfigDocker)
	if err != nil {
		log.Errorf("Error during preprocess : %v\n", err)
		return nil, err
	}

	bindingsMap = make(map[string]ServiceBinding)
	bindingPropertyMap = make(map[string]interface{})

	for key := range rawConfigDocker {
		if _, serviceParsed := bindingsMap[key]; serviceParsed {
			log.Debugf("Service bindings already provided")
			continue
		}
		newServiceBinding := ServiceBinding{}

		newServiceBinding.Labels = MapLabel{}
		newServiceBinding.Ports = PortArray{}

		if rawConfigDocker[key]["labels"] != nil {
			err := utils.Convert(rawConfigDocker[key]["labels"], &labels)
			if err != nil {
				return nil, err
			}
			for k, v := range labels {
				newServiceBinding.Labels[k] = v
			}
		}
		if rawConfigDocker[key]["ports"] != nil {
			for _, port := range rawConfigDocker[key]["ports"].([]interface{}) {
				newServiceBinding.Ports = append(newServiceBinding.Ports, port)
			}
		}
		bindingsMap[key] = newServiceBinding
	}

	bindingPropertyMap["services"] = bindingsMap

	return bindingPropertyMap, nil
}