Beispiel #1
0
func (c *CheckHAProxyTemplateFileExists) Check(config configuration.Configuration) (bool, error) {

	exists := util.FileExists(config.HAProxy.TemplatePath)

	if exists {
		return true, nil
	}
	return false, fmt.Errorf("HAProxy Template file at %s : does not exist.", config.HAProxy.TemplatePath)
}
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
}