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
	}
}
Пример #2
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
}
Пример #3
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
}
Пример #4
0
func (state *StateAPI) Get(w http.ResponseWriter, r *http.Request) {
	templateData, _ := haproxy.GetTemplateData(state.Config, state.Zookeeper)
	payload, _ := json.Marshal(templateData)
	io.WriteString(w, string(payload))
}