Example #1
0
// Takes the ReloadValidateCommand and returns nil if the command succeeded
func validateConfig(validateTemplate string, newContent string) (err error) {
	if validateTemplate == "" {
		return nil
	}

	tmpFile, err := ioutil.TempFile("/tmp", "bamboo")
	if err != nil {
		return
	}
	defer os.Remove(tmpFile.Name())
	defer tmpFile.Close()

	log.Println("Generating validation command")
	_, err = tmpFile.WriteString(newContent)
	if err != nil {
		return
	}

	validateCommand, err := template.RenderTemplate(
		"validate",
		validateTemplate,
		tmpFile.Name())
	if err != nil {
		return
	}

	log.Println("Validating config")
	err = execCommand(validateCommand)

	return
}
func handleHAPUpdate(conf *configuration.Configuration, conn *zk.Conn) bool {
	currentContent, _ := ioutil.ReadFile(conf.HAProxy.OutputPath)

	templateContent, err := ioutil.ReadFile(conf.HAProxy.TemplatePath)
	if err != nil {
		log.Panicf("Cannot read template file: %s", err)
	}

	templateData := haproxy.GetTemplateData(conf, conn)

	newContent, err := template.RenderTemplate(conf.HAProxy.TemplatePath, string(templateContent), templateData)

	if err != nil {
		log.Fatalf("Template syntax error: \n %s", err)
	}

	if currentContent == nil || string(currentContent) != newContent {
		err := ioutil.WriteFile(conf.HAProxy.OutputPath, []byte(newContent), 0666)
		if err != nil {
			log.Fatalf("Failed to write template on path: %s", err)
		}

		err = execCommand(conf.HAProxy.ReloadCommand)
		if err != nil {
			log.Fatalf("HAProxy: update failed\n")
		} else {
			conf.StatsD.Increment(1.0, "reload.marathon", 1)
			log.Println("HAProxy: Configuration updated")
		}
		return true
	} else {
		log.Println("HAProxy: Same content, no need to reload")
		return false
	}
}
Example #3
0
// Generates the new config to be written
func generateConfig(templatePath string, conf *configuration.Configuration, conn *zk.Conn) (config string, err error) {
	templateContent, err := ioutil.ReadFile(templatePath)
	if err != nil {
		log.Println("Failed to read template contents")
		return
	}

	templateData, err := haproxy.GetTemplateData(conf, conn)
	if err != nil {
		log.Println("Failed to retrieve template data")
		return
	}

	config, err = template.RenderTemplate(templatePath, string(templateContent), templateData)
	if err != nil {
		log.Println("Template syntax error")
		return
	}

	return
}
Example #4
0
// Generates the new config to be written
func generateConfig(h *Handlers) (config string, err error) {
	conf := h.Conf
	templateContent, err := ioutil.ReadFile(conf.HAProxy.TemplatePath)
	if err != nil {
		log.Println("Failed to read template contents")
		return
	}

	templateData, err := haproxy.GetTemplateData(conf, h.Storage, h.AppStorage)
	if err != nil {
		log.Println("Failed to retrieve template data")
		TemplateInvalid = true
		return
	}

	config, err = template.RenderTemplate(conf.HAProxy.TemplatePath, string(templateContent), templateData)
	if err != nil {
		log.Println("Template syntax error")
		TemplateInvalid = true
		return
	}
	TemplateInvalid = false
	return
}