Esempio n. 1
0
func AgentPostConfig(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	mcRouterConfigFile := context.Get(r, "mcRouterConfigFile").(string)
	if mcRouterConfigFile == "" {
		err := errors.New("McRouter config file is missing")
		libhttp.HandleErrorJson(w, err)
		return
	}

	configManager, err := models.NewMcRouterConfigManager(mcRouterConfigFile)
	if err != nil {
		libhttp.HandleErrorJson(w, err)
		return
	}

	mcRouterConfigJson, err := ioutil.ReadAll(r.Body)
	if err != nil {
		libhttp.HandleErrorJson(w, err)
		return
	}

	err = configManager.UpdateConfigJson(mcRouterConfigJson)
	if err != nil {
		libhttp.HandleErrorJson(w, err)
		return
	}

	libhttp.HandleSuccessJson(w, "New config is saved successfully")
}
Esempio n. 2
0
func New() (*Application, error) {
	app := &Application{}
	app.Settings = make(map[string]string)
	app.Storage = storage.New()

	for _, e := range os.Environ() {
		pair := strings.Split(e, "=")
		app.Settings[pair[0]] = pair[1]
	}

	if app.Settings["MCRHUB_MODE"] == "" {
		app.Settings["MCRHUB_MODE"] = "agent"
	}
	if app.Settings["MCRHUB_MODE"] == "agent" && app.Settings["MCROUTER_ADDR"] == "" {
		return nil, errors.New("MCROUTER_ADDR is required")
	}
	if app.Settings["MCRHUB_MODE"] == "agent" && app.Settings["MCROUTER_CONFIG_FILE"] == "" {
		return nil, errors.New("MCROUTER_CONFIG_FILE is required")
	}
	if app.Settings["MCRHUB_MODE"] == "agent" && app.Settings["MCRHUB_READ_ONLY"] == "" {
		app.Settings["MCRHUB_READ_ONLY"] = "true"
	}
	if app.Settings["MCRHUB_MODE"] == "central" && app.Settings["MCRHUB_READ_ONLY"] == "" {
		app.Settings["MCRHUB_READ_ONLY"] = "false"
	}
	if app.Settings["MCRHUB_REPORT_INTERVAL"] == "" {
		app.Settings["MCRHUB_REPORT_INTERVAL"] = "3s"
	}
	if app.Settings["MCRHUB_ADDR"] == "" {
		if app.IsAgentMode() {
			app.Settings["MCRHUB_ADDR"] = ":5001"
		} else {
			app.Settings["MCRHUB_ADDR"] = ":5002"
		}
	}

	if app.IsAgentMode() {
		app.McRouterStatsManager = models.NewMcRouterStatsManager(app.Settings["MCROUTER_ADDR"])

		configManager, err := models.NewMcRouterConfigManager(app.Settings["MCROUTER_CONFIG_FILE"])
		if err != nil {
			return nil, err
		}
		app.McRouterConfigManager = configManager
	}

	return app, nil
}
Esempio n. 3
0
func AgentGetConfig(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	mcRouterConfigFile := context.Get(r, "mcRouterConfigFile").(string)
	if mcRouterConfigFile == "" {
		err := errors.New("McRouter config file is missing")
		libhttp.HandleErrorJson(w, err)
		return
	}

	configManager, err := models.NewMcRouterConfigManager(mcRouterConfigFile)
	if err != nil {
		libhttp.HandleErrorJson(w, err)
		return
	}

	mcRouterConfigJson, err := configManager.ConfigJson()
	if err != nil {
		libhttp.HandleErrorJson(w, err)
		return
	}

	w.Write(mcRouterConfigJson)
}