Exemple #1
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("config file is missing.")
		os.Exit(1)
	}

	var err error
	config, err = conf.ReadConfigFile(args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "read config file failed:%s", err)
		os.Exit(1)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	go func() {
		s := <-c
		log.Printf("catch signal %v, program will exit", s)
		quit = true
	}()

	code := run()
	os.Exit(code)
}
Exemple #2
0
func initConfig() bool {
	c, err := conf.ReadConfigFile(*configFile)
	if err != nil {
		fmt.Printf("Could not load config file: %v\n\r", err)
		return false
	}

	g_config = c

	return true
}
Exemple #3
0
func startWatcher() {
	// Start the event loops. Use inotify to watch the configfile and each
	// repository path.
	Config, e := conf.ReadConfigFile(ConfigName)
	if e != nil {
		log.Fatal(e)
	}
	watcher, e := inotify.NewWatcher()
	if e != nil {
		log.Fatal(e)
	}
	log.Println("watching configfile")
	e = watcher.Watch(ConfigName)
	if e != nil {
		log.Fatal(e)
	}
	Locks = make([]int64, RemoteCount)
	Paths = make([]string, RemoteCount)
	for i := 1; i <= RemoteCount; i++ {
		section := strings.Join([]string{"remote-", strconv.Itoa(i)}, "")
		if !Config.HasOption(section, "path") {
			log.Println("doesnt have path")
			continue
		}
		path, e := Config.GetRawString(section, "path")
		if e != nil {
			log.Fatal(e)
		}
		log.Println("watching path", path)
		e = watcher.Watch(path)
		if e != nil {
			log.Fatal(e)
		}
		Locks[i-1] = 0
		Paths[i-1] = path
	}
	for {
		select {
		case ev := <-watcher.Event:
			go handleEvent(ev)
		case err := <-watcher.Error:
			log.Println("error:", err)
		}
	}
}
Exemple #4
0
func initConfig() {
	// Check that the configfile exists.
	Home = os.Getenv("HOME")
	//ConfigName = strings.Join([]string{Home, ".eggconfig"}, "/")
	ConfigName = "/etc/eggconfig"
	fd, e := os.OpenFile(ConfigName, os.O_RDWR|os.O_CREATE, 0644)
	if e != nil {
		log.Fatal(e)
	}
	e = fd.Close()
	if e != nil {
		log.Fatal(e)
	}

	// Make sure the configfile is set up correctly.
	Config, e := conf.ReadConfigFile(ConfigName)
	if e != nil {
		log.Fatal(e)
	}
	Config.AddSection("global")
	if !Config.HasOption("global", "count") {
		Config.AddOption("global", "count", "0")
	}

	// Modify the configfile through cmdline args.
	if len(os.Args) > 2 && os.Args[1] == "add" {
		log.Println("adding", os.Args[2], "to eggd path")
		count, _ := Config.GetInt("global", "count")
		count = count + 1
		Config.AddOption("global", "count", strconv.Itoa(count))
		section := strings.Join([]string{"remote-", strconv.Itoa(count)}, "")
		Config.AddOption(section, "path", os.Args[2])
		Config.WriteConfigFile(ConfigName, 0644, "")
		os.Exit(0)
	}

	RemoteCount, e = Config.GetInt("global", "count")
	if e != nil {
		log.Fatal(e)
	}
	Config.WriteConfigFile(ConfigName, 0644, "")
}
Exemple #5
0
func init() {
	c, err := conf.ReadConfigFile("data/server.conf")
	CheckErr(err)
	Config = c
}
Exemple #6
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("config file is missing.")
		os.Exit(1)
	}

	config, err := conf.ReadConfigFile(args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "read config file failed:%s", err)
		os.Exit(1)
	}

	logfile, _ := config.GetString("log", "file")
	fp, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Fprintf(os.Stderr, "open log file failed:%s", err)
		os.Exit(1)
	}
	defer fp.Close()
	log.SetOutput(io.MultiWriter(fp, os.Stderr))

	host, _ := config.GetString("redis", "host")
	password, _ := config.GetString("redis", "password")
	db, _ := config.GetInt("redis", "db")
	events, _ := config.GetString("redis", "events")
	channel, _ := config.GetString("redis", "channel")

	queue := make(chan string, 1024)

	cli1 := redis.NewRedis(host, password, db)
	m := NewMonitor(cli1, events, channel)

	dbname, err := config.GetString("leveldb", "dbname")
	if err != nil {
		log.Fatalf("get leveldb config failed:%v", err)
	}

	database := NewLeveldb()
	err = database.Open(dbname)
	if err != nil {
		log.Panicf("open db failed, err:%v", err)
	} else {
		log.Printf("open db succeed, dbname:%v", dbname)
	}

	defer database.Close()

	cli2 := redis.NewRedis(host, password, db)
	s := NewStorer(cli2, database)

	addr, _ := config.GetString("manager", "addr")
	c := NewCmdService(addr)

	cli3 := redis.NewRedis(host, password, db)
	context := &Context{database, cli3, m, s, c, make(chan os.Signal)}
	context.Register(c)

	signal.Notify(context.quit_chan, syscall.SIGINT, syscall.SIGTERM)

	go m.Start(queue)
	go s.Start(queue)
	go c.Start()

	log.Println("start succeed")
	log.Printf("catch signal %v, program will exit", <-context.quit_chan)
}