Beispiel #1
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n",
			path.Base(os.Args[0]), cbgt.VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	cmd.MainCommon(cbgt.VERSION, flagAliases)

	cfg, err := cmd.MainCfgClient(path.Base(os.Args[0]), flags.CfgConnect)
	if err != nil {
		log.Fatalf("%v", err)
		return
	}

	if flags.IndexTypes != "" {
		cmd.RegisterIndexTypes(strings.Split(flags.IndexTypes, ","))
	}

	nodesToRemove := []string(nil)
	if len(flags.RemoveNodes) > 0 {
		nodesToRemove = strings.Split(flags.RemoveNodes, ",")
	}

	var steps map[string]bool
	if flags.Steps != "" {
		steps = cbgt.StringsToMap(strings.Split(flags.Steps, ","))
	}

	// ------------------------------------------------

	if steps == nil || steps["rebalance"] {
		log.Printf("main: step rebalance")

		err := runRebalance(cfg, flags.Server, nodesToRemove,
			flags.FavorMinNodes, flags.DryRun, flags.Verbose)
		if err != nil {
			log.Fatalf("%v", err)
			return
		}
	}

	// ------------------------------------------------

	err = cmd.PlannerSteps(steps, cfg, cbgt.VERSION,
		flags.Server, nodesToRemove, flags.DryRun)
	if err != nil {
		log.Fatalf("%v", err)
		return
	}

	log.Printf("main: done")
}
Beispiel #2
0
func main() {
	xsrc := flag.String("src", "./content", "Source directory to read markdown from")
	xdst := flag.String("dst", "", "Destination to write translated content")
	flag.Parse()

	srcD, srcE := os.Stat(*xsrc)
	if os.IsNotExist(srcE) || !srcD.IsDir() {
		clog.Fatalf("Source directory does not exist: %s", *xsrc)
	}

	if len(*xdst) > 0 {
		dstD, dstE := os.Stat(*xdst)
		if os.IsNotExist(dstE) || !dstD.IsDir() {
			clog.Fatalf("Target directory does not exist: %s", *xdst)
		}
	}

	tld := strings.Trim(*xsrc, "/")
	dirpos := strings.LastIndex(tld, "/")
	if dirpos < 0 {
		tld = tld[dirpos+1:]
	}
	if len(tld) < 1 {
		clog.Fatalf("Source directory path must be at least one level deep, example: ./content")
	}

	if len(*xdst) > 0 {
		translate(*xsrc, *xdst, tld)
	} else {
		serve(*xsrc, tld)
	}
}
Beispiel #3
0
func translate(src, dst, tld string) {

	if !isEmpty(dst) {
		clog.Fatalf("Target directory %s is not empty", dst)
	}

	var idx Index = make(map[string]int)
	walker := func(fsrc string, f os.FileInfo, err error) error {
		return setup(dst, fsrc, f, idx, err)
	}
	if err := filepath.Walk(src, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}

	json, err := json.Marshal(idx)
	if err != nil {
		clog.Fatalf("During Index JSON Marshal %v", err)
	}

	jfile := strings.TrimRight(dst, "/")
	jfile += "/" + tld + "/index.json"
	err = ioutil.WriteFile(jfile, json, 0666)
	if err != nil {
		clog.Fatalf("Error writing json file: %s", jfile)
	}
}
Beispiel #4
0
func serve(cdir string, tld string) {
	tempDir, _ := ioutil.TempDir("", "tut")
	tempDir += string(os.PathSeparator)
	defer os.RemoveAll(tempDir)
	clog.Log("Workdir %s", tempDir)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		<-c
		os.RemoveAll(tempDir)
		clog.Fatal("Stopped")
	}()

	var idx Index
	idx = make(map[string]int)

	walker := func(src string, f os.FileInfo, err error) error {
		return setup(tempDir, src, f, idx, err)
	}

	if err := filepath.Walk(cdir, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}

	getindex := func(w http.ResponseWriter, r *http.Request) {
		json, err := json.Marshal(idx)
		if err != nil {
			clog.Fatalf("During Index JSON Marshal %v", err)
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(json)
	}
	http.HandleFunc("/tutorial/index.json", getindex)

	url, _ := url.Parse("http://localhost:8093")
	rp := httputil.NewSingleHostReverseProxy(url)
	http.Handle("/query", rp)

	fs := http.FileServer(http.Dir(tempDir + "/" + tld + "/"))
	http.Handle("/tutorial/", http.StripPrefix("/tutorial/", fs))

	http.Handle("/", http.RedirectHandler("/tutorial/index.html#1", 302))

	clog.Log("Running at http://localhost:8000/")
	go func() {
		for {
			filepath.Walk(cdir, walker)
			time.Sleep(2 * time.Second)
		}
	}()

	// last step
	if err := http.ListenAndServe(":8000", nil); err != nil {
		clog.Fatalf("ListenAndServe %v", err)
	}
}
Beispiel #5
0
func MainTool(cfg cbgt.Cfg, uuid string, tags []string, flags Flags,
	options map[string]string) int {
	tools, exists := options["tool"]
	if !exists {
		return -1 // Negative means caller should keep going with main server.
	}

	for _, tool := range strings.Split(tools, "|") {
		if tool == "" || tool == "h" || tool == "?" || tool == "usage" {
			tool = "help"
		}

		toolDef, exists := ToolDefs[tool]
		if !exists {
			log.Fatalf("tool: unknown tool: %s", tool)
			return 1
		}

		rv := toolDef.Handler(cfg, uuid, tags, flags, options)
		if rv >= 0 {
			return rv
		}
	}

	return -1
}
Beispiel #6
0
func isEmpty(dir string) bool {
	empty := true
	walker := func(dir string, f os.FileInfo, err error) error {
		if f.IsDir() {
			return nil
		}
		empty = false
		return nil
	}
	if err := filepath.Walk(dir, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}
	return empty
}
Beispiel #7
0
// The proto may be "http" or "https".
func MainServeHttp(proto, bindHttp string, anyHostPorts map[string]bool,
	certFile, keyFile string) {
	if bindHttp[0] == ':' {
		bindHttp = "localhost" + bindHttp
	}

	bar := "------------------------------------------------------------"

	if anyHostPorts != nil {
		// If we've already bound to 0.0.0.0 on the same port, then
		// skip this hostPort.
		hostPort := strings.Split(bindHttp, ":")
		if len(hostPort) >= 2 {
			anyHostPort := "0.0.0.0:" + hostPort[1]
			if anyHostPorts[anyHostPort] {
				if anyHostPort != bindHttp {
					log.Printf(bar)
					log.Printf("web UI / REST API is available"+
						" (via 0.0.0.0): %s://%s", proto, bindHttp)
					log.Printf(bar)
				}
				return
			}
		}
	}

	log.Printf(bar)
	log.Printf("web UI / REST API is available: %s://%s", proto, bindHttp)
	log.Printf(bar)

	var err error
	if proto == "http" {
		err = http.ListenAndServe(bindHttp, nil) // Blocks on success.
	} else {
		err = http.ListenAndServeTLS(bindHttp, certFile, keyFile, nil)
	}
	if err != nil {
		log.Fatalf("main: listen, err: %v\n"+
			"  Please check that your -bindHttp(s) parameter (%q)\n"+
			"  is correct and available.", err, bindHttp)
	}
}
Beispiel #8
0
func MainServeHttp(bindHttp string, anyHostPorts map[string]bool) {
	if bindHttp[0] == ':' {
		bindHttp = "localhost" + bindHttp
	}

	bar := "------------------------------------------------------------"

	if anyHostPorts != nil {
		// If we've already bound to 0.0.0.0 on the same port, then
		// skip this hostPort.
		hostPort := strings.Split(bindHttp, ":")
		if len(hostPort) >= 2 {
			anyHostPort := "0.0.0.0:" + hostPort[1]
			if anyHostPorts[anyHostPort] {
				if anyHostPort != bindHttp {
					log.Printf(bar)
					log.Printf("web UI / REST API is available"+
						" (via 0.0.0.0): http://%s", bindHttp)
					log.Printf(bar)
				}
				return
			}
		}
	}

	log.Printf(bar)
	log.Printf("web UI / REST API is available: http://%s", bindHttp)
	log.Printf(bar)

	err := http.ListenAndServe(bindHttp, nil) // Blocks.
	if err != nil {
		log.Fatalf("main: listen, err: %v\n"+
			"  Please check that your -bindHttp parameter (%q)\n"+
			"  is correct and available.", err, bindHttp)
	}
}
Beispiel #9
0
func runCtlPrompt(ctlInst *ctl.Ctl) {
	reader := bufio.NewReader(os.Stdin)

	i := 0
	for {
		fmt.Printf("ctl [%d]> ", i)

		line, err := reader.ReadString('\n')
		if err != nil {
			log.Fatalf("exiting, err: %v", err)
		}

		line = strings.TrimSpace(line)
		if len(line) > 0 {
			lineParts := strings.Split(line, " ")
			if len(lineParts) > 0 {
				op := lineParts[0]
				if op == "?" || op == "h" || op == "help" {
					log.Printf("available commands:\n" +
						" getTopology\n" +
						" gt (alias for getTopology)\n" +
						" changeTopology $rev $mode $memberNodeUUIDsCSV\n" +
						" ct (alias for changeTopology)\n" +
						" stopChangeTopology $rev\n" +
						" sct (alias for stopChangeTopology)\n" +
						" indexDefsChanged\n" +
						" idc (alias for indexDefsChanged)\n" +
						" exit, quit, q")
				} else if op == "getTopology" || op == "gt" {
					topology := ctlInst.GetTopology()
					b, _ := json.Marshal(topology)
					log.Printf("topology: %s", string(b))
				} else if op == "changeTopology" || op == "ct" {
					if len(lineParts) != 4 {
						log.Printf("expected 3 arguments")
					} else {
						rev := lineParts[1]
						mode := lineParts[2]
						memberNodeUUIDs := strings.Split(lineParts[3], ",")

						log.Printf("changeTopology,"+
							" rev: %s, mode: %s, memberNodeUUIDs: %#v",
							rev, mode, memberNodeUUIDs)
						topology, err :=
							ctlInst.ChangeTopology(&ctl.CtlChangeTopology{
								Rev:             rev,
								Mode:            mode,
								MemberNodeUUIDs: memberNodeUUIDs,
							}, nil)

						b, _ := json.Marshal(topology)
						log.Printf("topology: %s", string(b))

						log.Printf("err: %v", err)
					}
				} else if op == "stopChangeTopology" || op == "sct" {
					if len(lineParts) != 2 {
						log.Printf("expected 1 arguments")
					} else {
						rev := lineParts[1]

						log.Printf("stopChangeTopology, rev: %s", rev)

						ctlInst.StopChangeTopology(rev)
					}
				} else if op == "indexDefsChanged" || op == "idc" {
					err = ctlInst.IndexDefsChanged()

					log.Printf("err: %v", err)
				} else if op == "quit" || op == "q" || op == "exit" {
					log.Printf("bye")
					os.Exit(0)
				} else {
					log.Printf("unknown op: %s", op)
				}
			}
		}

		i++
	}
}
Beispiel #10
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	go cmd.DumpOnSignalForPlatform()

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfg(cmdName, flags.CfgConnect,
		flags.BindHttp, flags.Register, flags.DataDir)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf(fmt.Sprintf("%v", err))
			return
		}
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	expvars.Set("indexes", bleveHttp.IndexStats())

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extra,
		flags.BindHttp, flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, flags.Options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	log.Printf("main: listening on: %s", flags.BindHttp)
	u := flags.BindHttp
	if u[0] == ':' {
		u = "localhost" + u
	}
	if strings.HasPrefix(u, "0.0.0.0:") {
		u = "localhost" + u[len("0.0.0.0"):]
	}
	log.Printf("------------------------------------------------------------")
	log.Printf("web UI / REST API is available: http://%s", u)
	log.Printf("------------------------------------------------------------")
	err = http.ListenAndServe(flags.BindHttp, nil)
	if err != nil {
		log.Fatalf("main: listen, err: %v\n"+
			"  Please check that your -bindHttp parameter (%q)\n"+
			"  is correct and available.", err, flags.BindHttp)
	}
}
Beispiel #11
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n",
			path.Base(os.Args[0]), cbgt.VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	cmd.MainCommon(cbgt.VERSION, flagAliases)

	cfg, err := cmd.MainCfgClient(path.Base(os.Args[0]), flags.CfgConnect)
	if err != nil {
		log.Fatalf("%v", err)
		return
	}

	if flags.IndexTypes != "" {
		cmd.RegisterIndexTypes(strings.Split(flags.IndexTypes, ","))
	}

	nodesToRemove := []string(nil)
	if len(flags.RemoveNodes) > 0 {
		nodesToRemove = strings.Split(flags.RemoveNodes, ",")
	}

	steps := map[string]bool{}
	if flags.Steps != "" {
		steps = cbgt.StringsToMap(strings.Split(flags.Steps, ","))
	}

	// ------------------------------------------------

	if steps != nil && steps["rebalance"] {
		steps["rebalance_"] = true
		steps["unregister"] = true
		steps["planner"] = true
	}

	// ------------------------------------------------

	if steps != nil && steps["rebalance_"] {
		log.Printf("main: step rebalance_")

		err := rebalance.RunRebalance(cfg, flags.Server, nodesToRemove,
			flags.FavorMinNodes, flags.DryRun, flags.Verbose, nil)
		if err != nil {
			log.Fatalf("main: RunRebalance, err: %v", err)
			return
		}
	}

	// ------------------------------------------------

	err = cmd.PlannerSteps(steps, cfg, cbgt.VERSION,
		flags.Server, nodesToRemove, flags.DryRun, nil)
	if err != nil {
		log.Fatalf("main: PlannerSteps, err: %v", err)
		return
	}

	// ------------------------------------------------

	var c *ctl.Ctl

	if steps != nil && (steps["service"] || steps["rest"] || steps["prompt"]) {
		c, err = ctl.StartCtl(cfg, flags.Server, ctl.CtlOptions{
			DryRun:             flags.DryRun,
			Verbose:            flags.Verbose,
			FavorMinNodes:      flags.FavorMinNodes,
			WaitForMemberNodes: flags.WaitForMemberNodes,
		})
		if err != nil {
			log.Fatalf("main: StartCtl, err: %v", err)
			return
		}

		if steps["service"] {
			// TODO.
		}

		if steps["rest"] {
			bindHttp := flags.BindHttp
			if bindHttp[0] == ':' {
				bindHttp = "localhost" + bindHttp
			}
			if strings.HasPrefix(bindHttp, "0.0.0.0:") {
				bindHttp = "localhost" + bindHttp[len("0.0.0.0"):]
			}

			http.Handle("/", newRestRouter(c))

			go func() {
				log.Printf("------------------------------------------------------------")
				log.Printf("REST API is available: http://%s", bindHttp)
				log.Printf("------------------------------------------------------------")

				err := http.ListenAndServe(bindHttp, nil) // Blocks.
				if err != nil {
					log.Fatalf("main: listen, err: %v\n"+
						"  Please check that your -bindHttp parameter (%q)\n"+
						"  is correct and available.", err, bindHttp)
				}
			}()
		}

		if steps["prompt"] {
			go runCtlPrompt(c)
		}

		<-make(chan struct{})
	}

	// ------------------------------------------------

	log.Printf("main: done")
}
Beispiel #12
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)
	log.SetLoggerCallback(LoggerFunc)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	go cmd.DumpOnSignalForPlatform()

	bleve.StoreDynamic = false
	bleve.MappingJSONStrict = true

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	cbft.SetAuthType(flags.AuthType)
	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	// User may supply a comma-separated list of HOST:PORT values for
	// http addresss/port listening, but only the first entry is used
	// for cbgt node and Cfg registration.
	bindHttps := strings.Split(flags.BindHttp, ",")

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfg(cmdName, flags.CfgConnect,
		bindHttps[0], flags.Register, flags.DataDir)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf(fmt.Sprintf("%v", err))
			return
		}
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extras,
		bindHttps[0], flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, flags.Options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	anyHostPorts := map[string]bool{}

	// Bind to 0.0.0.0's first.
	for _, bindHttp := range bindHttps {
		if strings.HasPrefix(bindHttp, "0.0.0.0:") {
			go MainServeHttp(bindHttp, nil)

			anyHostPorts[bindHttp] = true
		}
	}

	for i := len(bindHttps) - 1; i >= 1; i-- {
		go MainServeHttp(bindHttps[i], anyHostPorts)
	}

	MainServeHttp(bindHttps[0], anyHostPorts)

	<-(make(chan struct{})) // Block forever.
}
Beispiel #13
0
func InitMossOptions(options map[string]string) (err error) {
	if options == nil {
		return nil
	}

	if bpamv, exists := options["blevePIndexAllowMoss"]; exists {
		bpam, err := strconv.ParseBool(bpamv)
		if err != nil {
			return fmt.Errorf("init_moss:"+
				" parsing blevePIndexAllowMoss: %q, err: %v", bpamv, err)
		}

		cbft.BlevePIndexAllowMoss = bpam
	}

	if !cbft.BlevePIndexAllowMoss {
		return nil
	}

	var memQuota uint64
	v, exists := options["ftsMossMemoryQuota"] // In bytes.
	if exists {
		fmmq, err := strconv.Atoi(v)
		if err != nil {
			return fmt.Errorf("init_moss:"+
				" parsing ftsMossMemoryQuota: %q, err: %v", v, err)
		}
		memQuota = uint64(fmmq)
	} else {
		frac, err := ParseFTSMemoryQuotaMossFraction(options)
		if err != nil {
			return err
		}

		v, exists = options["ftsMemoryQuota"] // In bytes.
		if exists {
			fmq, err := strconv.Atoi(v)
			if err != nil {
				return fmt.Errorf("init_moss:"+
					" parsing ftsMemoryQuota: %q, err: %v", v, err)
			}
			memQuota = uint64(float64(fmq) * frac)
		}
	}

	var mossDebug int
	mossDebugV, exists := options["ftsMossDebug"] // Higher means more debug info.
	if exists {
		mossDebug, err = strconv.Atoi(mossDebugV)
		if err != nil {
			return fmt.Errorf("init_moss:"+
				" parsing ftsMossDebug: %q, err: %v", mossDebugV, err)
		}
	}

	bleveMoss.RegistryCollectionOptions["fts"] = moss.CollectionOptions{
		Debug: mossDebug,
		Log:   log.Printf,
		OnError: func(err error) {
			log.Fatalf("moss OnError, treating this as fatal, err: %v", err)
		},
		OnEvent: NewMossHerderOnEvent(memQuota),
	}

	return nil
}
Beispiel #14
-1
func main() {
	platform.HideConsole(true)
	defer platform.HideConsole(false)

	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)
	log.SetLoggerCallback(LoggerFunc)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	// disabling as speculative change for
	// https://issues.couchbase.com/browse/MB-20002
	// theory being that invoking signal.Notify()
	// interferes with forestdb signal handler
	// go cmd.DumpOnSignalForPlatform()

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf("%v", err)
			return
		}
	}

	options := cmd.ParseOptions(flags.Options, "CBFT_ENV_OPTIONS",
		map[string]string{
			"managerLoadDataDir": "async",
			"authType":           flags.AuthType,
		})

	err = InitHttpOptions(options)
	if err != nil {
		log.Fatalf("main: InitHttpOptions, err: %v", err)
		return
	}

	// User may supply a comma-separated list of HOST:PORT values for
	// http addresss/port listening, but only the first http entry
	// is used for cbgt node and Cfg registration.
	bindHttpList := strings.Split(flags.BindHttp, ",")

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfgEx(cmdName, flags.CfgConnect,
		bindHttpList[0], flags.Register, flags.DataDir, uuid, options)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extras,
		bindHttpList[0], flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	anyHostPorts := map[string]bool{}

	// Bind to 0.0.0.0's first for http listening.
	for _, bindHttp := range bindHttpList {
		if strings.HasPrefix(bindHttp, "0.0.0.0:") {
			go MainServeHttp("http", bindHttp, nil, "", "")

			anyHostPorts[bindHttp] = true
		}
	}

	for i := len(bindHttpList) - 1; i >= 1; i-- {
		go MainServeHttp("http", bindHttpList[i], anyHostPorts, "", "")
	}

	if flags.BindHttps != "" {
		bindHttpsList := strings.Split(flags.BindHttps, ",")

		// Bind to 0.0.0.0's first for https listening.
		for _, bindHttps := range bindHttpsList {
			if strings.HasPrefix(bindHttps, "0.0.0.0:") {
				go MainServeHttp("https", bindHttps, nil,
					flags.TlsCertFile, flags.TlsKeyFile)

				anyHostPorts[bindHttps] = true
			}
		}

		for _, bindHttps := range bindHttpsList {
			go MainServeHttp("https", bindHttps, anyHostPorts,
				flags.TlsCertFile, flags.TlsKeyFile)
		}
	}

	MainServeHttp("http", bindHttpList[0], anyHostPorts, "", "")

	<-(make(chan struct{})) // Block forever.
}