Exemplo n.º 1
0
func WriteAgentConfig(configValues map[string]string) error {
	writeCf := goconfig.NewConfigFile()

	writeCf.AddSection(vars.ConfigAppSettings)
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigAgentidOption, configValues[vars.ConfigAgentidOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigServerhostnameOption, configValues[vars.ConfigServerhostnameOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigServeripaddressOption, configValues[vars.ConfigServeripaddressOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigServerportOption, configValues[vars.ConfigServerportOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigAgentportOption, configValues[vars.ConfigAgentportOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigStarterportOption, configValues[vars.ConfigStarterportOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigLoglevelOption, configValues[vars.ConfigLoglevelOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigNuOption, configValues[vars.ConfigNuOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigWpOption, configValues[vars.ConfigWpOption])
	writeCf.AddOption(vars.ConfigAppSettings, vars.ConfigCustomerOption, configValues[vars.ConfigCustomerOption])

	writeCf.AddSection(vars.ConfigAgentInfo)
	writeCf.AddOption(vars.ConfigAgentInfo, vars.ConfigNameOption, configValues[vars.ConfigNameOption])
	writeCf.AddOption(vars.ConfigAgentInfo, vars.ConfigVersionOption, configValues[vars.ConfigVersionOption])
	writeCf.AddOption(vars.ConfigAgentInfo, vars.ConfigDescriptionOption, configValues[vars.ConfigDescriptionOption])

	installDate := time.Now().Format(vars.InstallDateFormat)
	writeCf.AddOption(vars.ConfigAgentInfo, vars.ConfigInstalldateOption, installDate)

	if err := writeCf.WriteConfigFile(vars.AgentConfigPath, 0600, ""); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
func createDefaultConfig(fpath string) {
	c := goconfig.NewConfigFile()
	c.AddSection("math")
	c.AddOption("math", "UseRadians", "false")
	c.AddOption("math", "SignificantFigures", "8")

	c.AddSection("latex")
	c.AddOption("latex", "UseColoredOutput", "true")
	c.AddOption("latex", "OutputColor", "blue")
	c.AddOption("latex", "OrSymbol", " \\textrm{or} ")
	c.AddOption("latex", "DefinitionSymbol", ":=")

	c.WriteConfigFile(fpath, 0644, "")
}
Exemplo n.º 3
0
func main() {

	c, err := conf.ReadConfigFile("settings.cfg")
	if err != nil {
		nc := conf.NewConfigFile()
		nc.AddOption("default", "debug", "false")
		nc.AddOption("default", "listenaddress", ":9998")
		nc.AddOption("default", "maxprocesses", "0")
		nc.AddOption("default", "chatdelay", fmt.Sprintf("%d", 300*time.Millisecond))
		nc.AddOption("default", "maxthrottletime", fmt.Sprintf("%d", 5*time.Minute))

		nc.AddSection("redis")
		nc.AddOption("redis", "address", "localhost:6379")
		nc.AddOption("redis", "database", "-1")
		nc.AddOption("redis", "password", "")

		nc.AddSection("database")
		nc.AddOption("database", "type", "mysql")
		nc.AddOption("database", "dsn", "username:password@tcp(localhost:3306)/destinygg?loc=UTC&parseTime=true&strict=true&timeout=1s&time_zone=\"+00:00\"")

		if err := nc.WriteConfigFile("settings.cfg", 0644, "DestinyChatBackend"); err != nil {
			log.Fatal("Unable to create settings.cfg: ", err)
		}
		if c, err = conf.ReadConfigFile("settings.cfg"); err != nil {
			log.Fatal("Unable to read settings.cfg: ", err)
		}
	}

	debuggingenabled, _ = c.GetBool("default", "debug")
	addr, _ := c.GetString("default", "listenaddress")
	processes, _ := c.GetInt64("default", "maxprocesses")
	delay, _ := c.GetInt64("default", "chatdelay")
	maxthrottletime, _ := c.GetInt64("default", "maxthrottletime")
	DELAY = time.Duration(delay)
	MAXTHROTTLETIME = time.Duration(maxthrottletime)

	redisaddr, _ := c.GetString("redis", "address")
	redisdb, _ := c.GetInt64("redis", "database")
	redispw, _ := c.GetString("redis", "password")

	dbtype, _ := c.GetString("database", "type")
	dbdsn, _ := c.GetString("database", "dsn")

	if processes <= 0 {
		processes = int64(runtime.NumCPU()) * 2
	}
	runtime.GOMAXPROCS(int(processes))

	if debuggingenabled {
		defer profile.Start(&profile.Config{
			Quiet:          false,
			CPUProfile:     true,
			MemProfile:     true,
			BlockProfile:   true,
			ProfilePath:    "./",
			NoShutdownHook: false,
		}).Stop()
	}

	go hub.run()
	initDatabase(dbtype, dbdsn)
	initRedis(redisaddr, redisdb, redispw)

	initMutes()
	initBans()
	initUsers()
	initEventlog()

	http.HandleFunc("/ws", Handler)

	fmt.Printf("Using %v threads, and listening on: %v\n", processes, addr)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: Aaah/erosd
func loadConfig() error {
	config, err := conf.ReadConfigFile("erosd.cfg")
	if err != nil {
		config = conf.NewConfigFile()
		config.AddSection("erosd")
		config.AddOption("erosd", "listen", ":12345")
		config.AddOption("erosd", "adminlisten", "127.0.0.1:12346")
		config.AddOption("erosd", "simulator", "false")
		config.AddOption("erosd", "allowsimulations", "false")
		config.AddOption("erosd", "testmode", "false")
		config.AddOption("erosd", "logpath", "logs")
		config.AddOption("erosd", "replaypath", "replays")

		config.AddSection("ladderdivisions")
		config.AddOption("ladderdivisions", "divisions", "4")
		config.AddOption("ladderdivisions", "divisionincrements", "5")
		config.AddOption("ladderdivisions", "divisionfirst", "20")
		config.AddOption("ladderdivisions", "divisionnames", "E;D;C;B;A")

		config.AddSection("ladder")
		config.AddOption("ladder", "longprocessresponsetime", "240")
		config.AddOption("ladder", "longprocessunlocktime", "60")
		config.AddOption("ladder", "startingpoints", "0")
		config.AddOption("ladder", "winpointsbase", "100")
		config.AddOption("ladder", "winpointsincrement", "25")
		config.AddOption("ladder", "losepointsbase", "50")
		config.AddOption("ladder", "losepointsincrement", "12.5")
		config.AddOption("ladder", "maxvetos", "3")
		config.AddOption("ladder", "matchtimeout", "7200")
		config.AddOption("ladder", "matchmakingradiusmultiplier", "5.00")
		config.AddOption("ladder", "matchmakingratingscalepersecond", "0.08")

		config.AddSection("database")
		config.AddOption("database", "type", "sqlite3")
		config.AddOption("database", "connection", "erosd.sqlite3")

		config.AddSection("chat")
		config.AddOption("chat", "fixedrooms", "Starbow;Practice Partner Search (Bronze-Silver);Practice Partner Search (Gold-Platinum);Practice Partner Search")
		config.AddOption("chat", "autojoin", "autojoin;Practice Partner Search (Bronze-Silver);Practice Partner Search (Gold-Platinum);Practice Partner Search")
		config.AddOption("chat", "maxuserchats", "5")
		config.AddOption("chat", "delay", "250")
		config.AddOption("chat", "maxthrottletime", "300")
		config.AddOption("chat", "maxmessagelength", "256")
		config.AddSection("python")
		config.AddOption("python", "port", ":54321")

		err = config.WriteConfigFile("erosd.cfg", 0644, "Erosd Config")
		if err != nil {
			return err
		}
	}

	listen, err := config.GetString("erosd", "listen")
	if err == nil {
		listenAddresses = strings.Split(listen, ";")
	}
	listen, err = config.GetString("erosd", "adminlisten")
	if err == nil {
		adminListenAddresses = strings.Split(listen, ";")
	}
	simulator, _ = config.GetBool("erosd", "simulator")
	pythonPort, _ = config.GetString("python", "port")
	testMode, _ = config.GetBool("erosd", "testmode")
	allowsimulations, _ = config.GetBool("erosd", "allowsimulations")
	logPath, _ = config.GetString("erosd", "logpath")
	replayPath, _ = config.GetString("erosd", "replaypath")

	divisionCount, _ = config.GetInt64("ladderdivisions", "divisions")
	divisionIncrements, _ = config.GetFloat("ladderdivisions", "divisionincrements")
	divisionFirstRating, _ = config.GetFloat("ladderdivisions", "divisionfirst")
	dn, err := config.GetString("ladderdivisions", "divisionnames")
	if err == nil {
		divisionNames = strings.Split(dn, ";")
	}

	matchmakingMatchTimeout, _ = config.GetInt64("ladder", "matchtimeout")
	matchmakingLongProcessUnlockTime, _ = config.GetInt64("ladder", "longprocessunlocktime")
	matchmakingLongProcessResponseTime, _ = config.GetInt64("ladder", "longprocessresponsetime")
	matchmakingRadiusMultiplier, _ = config.GetFloat("ladder", "matchmakingradiusmultiplier")
	matchmakingRatingScalePerSecond, _ = config.GetFloat("ladder", "matchmakingratingscalepersecond")

	ladderStartingPoints, _ = config.GetInt64("ladder", "startingpoints")
	ladderWinPointsBase, _ = config.GetInt64("ladder", "winpointsbase")
	ladderWinPointsIncrement, _ = config.GetFloat("ladder", "winpointsincrement")
	ladderLosePointsBase, _ = config.GetInt64("ladder", "losepointsbase")
	ladderLosePointsIncrement, _ = config.GetFloat("ladder", "losepointsincrement")
	ladderMaxMapVetos, _ = config.GetInt64("ladder", "maxvetos")
	rg, err := config.GetString("ladder", "activeregions")
	if err == nil {
		regions := strings.Split(rg, ";")
		ladderActiveRegions = make([]BattleNetRegion, 0, len(regions))

		for _, region := range regions {
			regionCode := ParseBattleNetRegion(region)
			if regionCode != BATTLENET_REGION_UNKNOWN {
				ladderActiveRegions = append(ladderActiveRegions, regionCode)
			}
		}
	}

	dbType, _ = config.GetString("database", "type")
	dbConnectionString, _ = config.GetString("database", "connection")

	cn, err := config.GetString("chat", "fixedrooms")
	if err == nil {
		fixedChatRooms = strings.Split(cn, ";")
	}
	aj, err := config.GetString("chat", "autojoin")
	if err == nil {
		autoJoinChatRooms = strings.Split(aj, ";")
	}
	maxChatRooms, _ = config.GetInt64("chat", "maxuserchats")
	chatMaxMessageLength, _ = config.GetInt64("chat", "maxmessagelength")
	mtt, _ := config.GetInt64("chat", "maxthrottletime")
	chatMaxThrottleTime = time.Duration(mtt) * time.Second
	delay, _ := config.GetInt64("chat", "delay")
	chatDelay = time.Duration(delay) * time.Millisecond
	return nil
}
Exemplo n.º 5
0
func main() {
	log.SetFlags(log.Ltime | log.Lshortfile) // Ought to be controlled with a flag
	//
	// Config File
	//
	u, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	confFolder := filepath.Join(u.HomeDir, ".config", "srom")
	confFile := filepath.Join(confFolder, "srom.conf")
	_, err = os.Stat(confFile)
	if os.IsNotExist(err) {
		os.MkdirAll(confFolder, 0700)
		c := goconfig.NewConfigFile()
		c.AddSection("google")
		c.AddOption("google", "custom_search_id", "")
		c.AddOption("google", "api_key", "")
		c.AddSection("azure")
		c.AddOption("azure", "customer_id", "")
		c.AddOption("azure", "key", "")
		header := "Sucks-Rules-O-Meter Configuration"
		err = c.WriteConfigFile(confFile, 0600, header)
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Created new config file at", confFile)
		log.Println("You will need to populate it with your API credentials")
		return
	}
	engines := []srom.SearchEngine{}
	c, err := goconfig.ReadConfigFile(confFile)
	if err != nil {
		log.Fatal(err)
	}
	for _, section := range c.GetSections() {
		//
		// Google
		//
		if section == "google" {
			google := srom.GoogleSearch{
				ApiKey:         getString(c, "google", "api_key"),
				CustomSearchId: getString(c, "google", "custom_search_id"),
			}
			engines = append(engines, &google)
		}
		if section == "azure" {
			bing := srom.BingSearch{
				CustomerId: getString(c, "azure", "customer_id"),
				Key:        getString(c, "azure", "key"),
			}
			engines = append(engines, &bing)
		}
	}
	//
	// Parse Flags
	//
	flag.Parse()
	if flag.NArg() != 1 {
		log.Fatal("Must supply one and only one argument")
	}
	//
	// Run Query
	//
	sr := srom.New(engines, &srom.NilOutput{})
	sr.Run()
	term := flag.Arg(0)
	ratio, err := sr.Query(term)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("\nThe term '%v' has a sucks/rules ratio of %v\n", term, ratio)
	msg := fmt.Sprintf("The internet thinks '%v' ", term)
	switch {
	case ratio > 1:
		msg += "SUCKS"
	case ratio == 1:
		msg += "neither sucks nor rocks.  Weird, eh?"
	case ratio < 1:
		msg += "ROCKS"
	}
	if ratio > 2 || ratio < 0.5 {
		msg += " HARD"
	}
	msg += ".\n\n"
	fmt.Printf(msg)

}
Exemplo n.º 6
0
func main() {
	c, err := conf.ReadConfigFile("angel.cfg")
	if err != nil {
		nc := conf.NewConfigFile()
		nc.AddOption("default", "debug", "false")
		nc.AddOption("default", "binarypath", "./")
		nc.AddOption("default", "serverurl", "ws://localhost:9998/ws")
		nc.AddOption("default", "origin", "http://localhost")

		if err := nc.WriteConfigFile("angel.cfg", 0644, "Chat Angel, watching over the chat and restarting it as needed"); err != nil {
			log.Fatal("Unable to create angel.cfg: ", err)
		}
		if c, err = conf.ReadConfigFile("angel.cfg"); err != nil {
			log.Fatal("Unable to read angel.cfg: ", err)
		}
	}

	debuggingenabled, _ = c.GetBool("default", "debug")
	binpath, _ := c.GetString("default", "binarypath")
	serverurl, _ := c.GetString("default", "serverurl")
	origin, _ := c.GetString("default", "origin")
	initLog()

	base := path.Base(binpath)
	basedir := strings.TrimSuffix(binpath, "/"+base)
	os.Chdir(basedir) // so that the chat can still read the settings.cfg

	shouldrestart := make(chan bool)
	processexited := make(chan bool)
	t := time.NewTicker(2 * time.Second)
	sct := make(chan os.Signal, 1)
	signal.Notify(sct, syscall.SIGTERM)

	go (func() {
		for _ = range sct {
			P("CAUGHT SIGTERM, restarting the chat")
			shouldrestart <- true
		}
	})()

	var restarting bool

again:
	initLogWriter()
	cmd := exec.Command(binpath)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		F("Stdoutpipe error: ", err)
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		F("Stderrpipe error: ", err)
	}

	go accumulateLog(stdout)
	go accumulateLog(stderr)

	if err := cmd.Start(); err != nil {
		P("Error starting", binpath, err)
		return
	}

	go (func() {
		if err := cmd.Wait(); err != nil {
			P("Error while waiting for process to exit ", err)
		}
		P("Chat process exited, restarting")
		processexited <- true
	})()

	time.Sleep(2 * time.Second)
	restarting = false

	for {
		select {
		case <-t.C:
			go checkNames(serverurl, origin, shouldrestart)
			go checkPing(serverurl, origin, shouldrestart)
		case <-shouldrestart:
			if !restarting {
				cmd.Process.Signal(syscall.SIGQUIT)
			} else {
				P("Received from shouldrestart but already restarting, ignored")
			}
			// TODO move pprof files out of the dir
		case <-processexited:
			if !restarting {
				time.Sleep(200 * time.Millisecond)
				goto again
			}
		}
	}
}