Parcourir la source

Add --checkconfig parameter

Ask Bjørn Hansen il y a 12 ans
Parent
commit
9df8726a21
2 fichiers modifiés avec 34 ajouts et 15 suppressions
  1. 16 10
      config.go
  2. 18 5
      geodns.go

+ 16 - 10
config.go

@@ -27,14 +27,17 @@ func configReader(dirName string, Zones Zones) {
 	}
 }
 
-func configReadDir(dirName string, Zones Zones) {
+func configReadDir(dirName string, Zones Zones) error {
 	dir, err := ioutil.ReadDir(dirName)
 	if err != nil {
-		panic(err)
+		log.Println("Could not read", dirName, ":", err)
+		return err
 	}
 
 	seenFiles := map[string]bool{}
 
+	var parse_err error
+
 	for _, file := range dir {
 		fileName := file.Name()
 		if !strings.HasSuffix(strings.ToLower(fileName), ".json") {
@@ -44,24 +47,27 @@ func configReadDir(dirName string, Zones Zones) {
 		seenFiles[fileName] = true
 
 		if lastRead, ok := configLastRead[fileName]; !ok || file.ModTime().After(lastRead) {
-			log.Println("Updated file, going to read", fileName)
+			logPrintln("Updated file, going to read", fileName)
 			configLastRead[fileName] = file.ModTime()
 			zoneName := fileName[0:strings.LastIndex(fileName, ".")]
 			//log.Println("FILE:", i, file, zoneName)
 			runtime.GC()
 			config, err := readZoneFile(zoneName, path.Join(dirName, fileName))
 			if config == nil || err != nil {
-				log.Println("error reading file: ", err)
-			}
-			if config != nil && err == nil {
-				Zones[zoneName] = config
-				dns.HandleFunc(zoneName, setupServerFunc(config))
-				runtime.GC()
+				log.Println(err)
+				parse_err = err
+				continue
 			}
+
+			Zones[zoneName] = config
+			dns.HandleFunc(zoneName, setupServerFunc(config))
+			runtime.GC()
 		}
 
 		// TODO(ask) Disable zones not seen in two subsequent runs
 	}
+
+	return parse_err
 }
 
 func setupPgeodnsZone(Zones Zones) {
@@ -370,7 +376,7 @@ func setupSOA(Zone *Zone) {
 		" 5400 5400 2419200 " +
 		strconv.Itoa(Zone.Options.Ttl)
 
-	log.Println("SOA: ", s)
+	// log.Println("SOA: ", s)
 
 	rr, err := dns.NewRR(s)
 

+ 18 - 5
geodns.go

@@ -19,11 +19,12 @@ var timeStarted = time.Now()
 var qCounter uint64 = 0
 
 var (
-	flagconfig = flag.String("config", "./dns/", "directory of zone files")
-	flaginter  = flag.String("interface", "*", "set the listener address")
-	flagport   = flag.String("port", "53", "default port number")
-	flaghttp   = flag.String("http", ":8053", "http listen address (:8053)")
-	flaglog    = flag.Bool("log", false, "be more verbose")
+	flagconfig      = flag.String("config", "./dns/", "directory of zone files")
+	flagcheckconfig = flag.Bool("checkconfig", false, "check configuration and exit")
+	flaginter       = flag.String("interface", "*", "set the listener address")
+	flagport        = flag.String("port", "53", "default port number")
+	flaghttp        = flag.String("http", ":8053", "http listen address (:8053)")
+	flaglog         = flag.Bool("log", false, "be more verbose")
 
 	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
 	memprofile = flag.String("memprofile", "", "write memory profile to this file")
@@ -41,6 +42,18 @@ func init() {
 func main() {
 	flag.Parse()
 
+	if *flagcheckconfig {
+		dirName := *flagconfig
+		Zones := make(Zones)
+		setupPgeodnsZone(Zones)
+		err := configReadDir(dirName, Zones)
+		if err != nil {
+			log.Println("Errors reading config")
+			os.Exit(2)
+		}
+		return
+	}
+
 	log.Printf("Starting geodns %s\n", VERSION)
 
 	if *cpuprofile != "" {