Example #1
0
File: main.go Project: vebin/reborn
func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn proxy v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	setStringFromOpt(&configFile, args, "-c")

	// set output log file
	if v := args["-L"]; v != nil {
		log.SetHighlighting(false)
		log.SetOutputByName(v.(string))
	}

	// set log level
	if v := args["--log-level"]; v != nil {
		log.SetLevelByString(v.(string))
	}

	// set cpu
	setIntArgFromOpt(&cpus, args, "--cpu")

	// set addr
	setStringFromOpt(&addr, args, "--addr")

	// set proto
	setStringFromOpt(&proto, args, "--proto")

	// set http addr
	setStringFromOpt(&httpAddr, args, "--http-addr")

	// set proxy id
	setStringFromOpt(&proxyID, args, "--id")
	if len(proxyID) == 0 {
		log.Fatalf("invalid empty proxy id")
	}

	// set log dump path
	dumppath := utils.GetExecutorPath()
	setStringFromOpt(&dumppath, args, "--dump-path")

	log.Info("dump file path:", dumppath)
	log.CrashLog(path.Join(dumppath, "reborn-proxy.dump"))

	// set pidfile
	setStringFromOpt(&pidfile, args, "--pidfile")

	// set proxy auth
	setStringFromOpt(&proxyAuth, args, "--proxy-auth")

	// set net time
	setIntArgFromOpt(&netTimeout, args, "--net-timeout")

	router.CheckUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)

	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}

	conf.Addr = addr
	conf.HTTPAddr = httpAddr
	conf.ProxyID = proxyID
	conf.PidFile = pidfile
	conf.NetTimeout = netTimeout
	conf.Proto = proto
	conf.ProxyAuth = proxyAuth

	if err := utils.CreatePidFile(conf.PidFile); err != nil {
		log.Fatal(err)
	}

	log.Info("running on ", addr)

	s := router.NewServer(conf)
	s.Run()
	log.Warning("exit")
}
Example #2
0
func runDashboard(addr string, httpLogFile string) {
	log.Info("dashboard listening on addr: ", addr)
	m := martini.Classic()
	f, err := os.OpenFile(httpLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		Fatal(err)
	}
	defer f.Close()

	m.Map(stdlog.New(f, "[martini]", stdlog.LstdFlags))

	binRoot := utils.GetExecutorPath()

	m.Use(martini.Static(filepath.Join(binRoot, "assets/statics")))
	m.Use(render.Renderer(render.Options{
		Directory:  filepath.Join(binRoot, "assets/template"),
		Extensions: []string{".tmpl", ".html"},
		Charset:    "UTF-8",
		IndentJSON: true,
	}))

	m.Use(cors.Allow(&cors.Options{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"POST", "GET", "DELETE", "PUT"},
		AllowHeaders:     []string{"Origin", "x-requested-with", "Content-Type", "Content-Range", "Content-Disposition", "Content-Description"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: false,
	}))

	m.Get("/api/server_groups", apiGetServerGroupList)
	m.Get("/api/overview", apiOverview)

	m.Get("/api/redis/:addr/stat", apiRedisStat)
	m.Get("/api/redis/:addr/:id/slotinfo", apiGetRedisSlotInfo)
	m.Get("/api/redis/group/:group_id/:slot_id/slotinfo", apiGetRedisSlotInfoFromGroupId)

	m.Put("/api/server_groups", binding.Json(models.ServerGroup{}), apiAddServerGroup)
	m.Put("/api/server_group/(?P<id>[0-9]+)/addServer", binding.Json(models.Server{}), apiAddServerToGroup)
	m.Delete("/api/server_group/(?P<id>[0-9]+)", apiRemoveServerGroup)

	m.Put("/api/server_group/(?P<id>[0-9]+)/removeServer", binding.Json(models.Server{}), apiRemoveServerFromGroup)
	m.Get("/api/server_group/(?P<id>[0-9]+)", apiGetServerGroup)
	m.Post("/api/server_group/(?P<id>[0-9]+)/promote", binding.Json(models.Server{}), apiPromoteServer)

	m.Get("/api/migrate/status", apiMigrateStatus)
	m.Get("/api/migrate/tasks", apiGetMigrateTasks)
	m.Delete("/api/migrate/pending_task/:id/remove", apiRemovePendingMigrateTask)
	m.Delete("/api/migrate/task/:id/stop", apiStopMigratingTask)
	m.Post("/api/migrate", binding.Json(MigrateTaskInfo{}), apiDoMigrate)

	m.Post("/api/rebalance", apiRebalance)
	m.Get("/api/rebalance/status", apiRebalanceStatus)

	m.Get("/api/slot/list", apiGetSlots)
	m.Get("/api/slot/:id", apiGetSingleSlot)
	m.Post("/api/slots/init", apiInitSlots)
	m.Get("/api/slots", apiGetSlots)
	m.Post("/api/slot", binding.Json(RangeSetTask{}), apiSlotRangeSet)
	m.Get("/api/proxy/list", apiGetProxyList)
	m.Get("/api/proxy/debug/vars", apiGetProxyDebugVars)
	m.Post("/api/proxy", binding.Json(models.ProxyInfo{}), apiSetProxyStatus)

	m.Get("/api/action/gc", apiActionGC)
	m.Get("/api/force_remove_locks", apiForceRemoveLocks)
	m.Get("/api/remove_fence", apiRemoveFence)

	m.Get("/slots", pageSlots)
	m.Get("/ping", func() int { return 200 })
	m.Get("/", func(r render.Render) {
		r.Redirect("/admin")
	})

	// create temp node in coordinator
	if err := createDashboardNode(globalConn); err != nil {
		Fatal(err)
	}
	defer releaseDashboardNode(globalConn)

	// create long live migrate manager
	globalMigrateManager = NewMigrateManager(globalConn, globalEnv.ProductName(), preMigrateCheck)
	// defer globalMigrateManager.removeNode()

	go func() {
		c := getProxySpeedChan()
		for {
			atomic.StoreInt64(&proxiesSpeed, <-c)
		}
	}()

	m.RunOnAddr(addr)
}