Beispiel #1
0
func RenderTemplate(templatePath string, updates *types.MasterDetailsCollection) (string, error) {

	templateContent, err := ioutil.ReadFile(templatePath)
	if err != nil {
		return "", err
	}

	tmpl, err := template.New(templatePath).Parse(string(templateContent))

	if err != nil {
		return "", err
	}

	sortedUpdates := updates.Items()
	sort.Sort(types.ByName(sortedUpdates))

	data := TemplateData{Clusters: sortedUpdates}
	strBuffer := new(bytes.Buffer)

	err = tmpl.Execute(strBuffer, data)
	if err != nil {
		return "", err
	}
	return strBuffer.String(), nil
}
func (c *ConsulFlipperClient) InitialiseRunningState(state *types.MasterDetailsCollection) {

	logger.Info.Printf("InitialiseRunningState called : %s", util.String(state.Items()))
	for _, md := range state.Items() {
		c.UpdateConsul(md.Name, md.Ip, md.Port)
	}
}
func (flipper *HAProxyFlipperClient) InitialiseRunningState(details *types.MasterDetailsCollection) {
	flipper.lock.Lock()
	defer flipper.lock.Unlock()

	if details.IsEmpty() {
		logger.Info.Print("Empty initial state : Nothing to do")
		return
	}

	configuration := flipper.configurationManager.GetCurrentConfiguration()
	_, err := flipper.renderAndReload(configuration, details)

	if err != nil {
		logger.Error.Panicf("Unable to initilise and write state : %s", err.Error())
	}
	flipper.state = details
}
func renderTemplate(details *types.MasterDetailsCollection, outputPath string, templatepath string) (bool, error) {

	logger.Info.Printf("Details %s", util.String(details.Items()))
	renderedTemplate, err := template.RenderTemplate(templatepath, details)

	if err != nil {
		logger.Error.Printf("Error rendering tempate at %s.", templatepath)
		return false, err
	}

	if util.FileExists(outputPath) {
		newFileHash := util.HashString(renderedTemplate)
		oldFileHash, err := util.HashFile(outputPath)

		if err != nil {
			logger.Error.Printf("Error hashing existing HAProxy config file at %s.", outputPath)
			return false, err
		}

		if newFileHash == oldFileHash {
			logger.NoteWorthy.Printf("Existing config file up todate. New file hash : %s == Old file hash %s. Nothing to do.", newFileHash, oldFileHash)
			return true, nil
		}

		logger.Info.Printf("Updating config file. New file hash : %s != Old file hash %s", newFileHash, oldFileHash)
	}

	err = util.WriteFile(outputPath, renderedTemplate)

	if err != nil {
		logger.Error.Printf("Error writing file to %s : %s\n", outputPath, err.Error())
		return false, err
	}

	return true, nil
}
func (*NoOpFlipperClient) InitialiseRunningState(state *types.MasterDetailsCollection) {
	logger.NoteWorthy.Printf("InitialiseRunningState called : %s", util.String(state.Items()))
}