Beispiel #1
0
func (s *Service) loadAttributes() {
	attr := utils.CopyMap(s.env.GetAttributes())
	files, err := utils.AttributeFiles(s.path + spec.PATH_ATTRIBUTES)
	if err != nil {
		s.log.WithError(err).WithField("path", s.path+spec.PATH_ATTRIBUTES).Panic("Cannot load Attributes files")
	}
	attr = attributes.MergeAttributesFilesForMap(attr, files)
	s.attributes = attr
	s.log.WithField("attributes", s.attributes).Debug("Attributes loaded")
}
func (s Service) GenerateUnits(sources []string) {
	s.log.Debug("Generating units")

	tmpl, err := s.loadUnitTemplate()
	if err != nil {
		s.log.WithError(err).Error("Cannot load units template")
		return
	}

	if len(s.manifest.Nodes) == 0 {
		s.log.Error("No node to process in manifest")
		return
	}

	nodes := s.manifest.Nodes
	if s.manifest.Nodes[0][spec.NODE_HOSTNAME].(string) == "*" {
		if len(s.manifest.Nodes) > 1 {
			s.log.Error("You cannot mix all nodes with single node. Yet ?")
			return
		}

		newNodes := *new([]map[string]interface{})
		machines := s.env.ListMachineNames()
		for _, machine := range machines {
			node := utils.CopyMap(s.manifest.Nodes[0])
			node[spec.NODE_HOSTNAME] = machine
			newNodes = append(newNodes, node)
		}

		nodes = newNodes
	}

	acis, err := s.prepareAciList(sources)
	if err != nil {
		s.log.WithError(err).Error("Cannot prepare aci list")
		return
	}

	for i, node := range nodes {
		s.writeUnit(i, node, tmpl, acis)
	}
}
func (s Service) writeUnit(i int, node map[string]interface{}, tmpl *Templating, acis string) {
	if node[spec.NODE_HOSTNAME].(string) == "" {
		s.log.WithField("index", i).Error("hostname is mandatory in node informations")
	}
	s.log.Debug("Processing node :" + node[spec.NODE_HOSTNAME].(string))

	unitName := s.UnitName(node[spec.NODE_HOSTNAME].(string))

	data := make(map[string]interface{})

	data["node"] = node
	data["node"].(map[string]interface{})["acis"] = acis

	data["attribute"] = utils.CopyMap(s.attributes)
	if data["node"].(map[string]interface{})["attributes"] != nil {
		source := utils.CopyMapInterface(data["node"].(map[string]interface{})["attributes"].(map[interface{}]interface{}))
		data["attribute"] = mergemap.Merge(data["attribute"].(map[string]interface{}), source.(map[string]interface{}))
	}

	out, err := json.Marshal(data["attribute"])
	if err != nil {
		s.log.WithError(err).Panic("Cannot marshall attributes")
	}
	data["attributes"] = strings.Replace(string(out), "\\\"", "\\\\\\\"", -1)

	var b bytes.Buffer
	err = tmpl.Execute(&b, data)
	if err != nil {
		s.log.Error("Failed to run templating for unit "+unitName, err)
	}
	ok, err := utils.Exists(s.path + "/units")
	if !ok || err != nil {
		os.Mkdir(s.path+"/units", 0755)
	}
	err = ioutil.WriteFile(s.path+"/units"+"/"+unitName, b.Bytes(), 0644)
	if err != nil {
		s.log.WithError(err).WithField("path", s.path+"/units"+"/"+unitName).Error("Cannot writer unit")
	}
}