Exemplo n.º 1
0
// parseConfig parses out the input config from cmdline arguments and
// environment variables.
func parseConfig(args []string) (*flag.FlagSet, error) {
	fs := flag.NewFlagSet("full flagset", flag.ContinueOnError)
	fs.Usage = func() {}
	etcdmain.NewConfig().VisitAll(func(f *flag.Flag) {
		_, isBoolFlag := f.Value.(boolFlag)
		fs.Var(&value{isBoolFlag: isBoolFlag}, f.Name, "")
	})
	if err := fs.Parse(args); err != nil && err != flag.ErrHelp {
		return nil, err
	}
	if err := flags.SetFlagsFromEnv(fs); err != nil {
		return nil, err
	}
	return fs, nil
}
Exemplo n.º 2
0
func (cfg *config) Parse(arguments []string) error {
	perr := cfg.FlagSet.Parse(arguments)
	switch perr {
	case nil:
	case flag.ErrHelp:
		os.Exit(0)
	default:
		os.Exit(2)
	}

	if cfg.printVersion {
		fmt.Println("etcd version", version.Version)
		os.Exit(0)
	}

	err := flags.SetFlagsFromEnv(cfg.FlagSet)
	if err != nil {
		log.Fatalf("etcd: %v", err)
	}

	set := make(map[string]bool)
	cfg.FlagSet.Visit(func(f *flag.Flag) {
		set[f.Name] = true
	})
	nSet := 0
	for _, v := range []bool{set["discovery"], set["initial-cluster"], set["discovery-srv"]} {
		if v {
			nSet += 1
		}
	}
	if nSet > 1 {
		return ErrConflictBootstrapFlags
	}

	flags.SetBindAddrFromAddr(cfg.FlagSet, "peer-bind-addr", "peer-addr")
	flags.SetBindAddrFromAddr(cfg.FlagSet, "bind-addr", "addr")

	cfg.lpurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-peer-urls", "peer-bind-addr", cfg.peerTLSInfo)
	if err != nil {
		return err
	}
	cfg.apurls, err = flags.URLsFromFlags(cfg.FlagSet, "initial-advertise-peer-urls", "peer-addr", cfg.peerTLSInfo)
	if err != nil {
		return err
	}
	cfg.lcurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-client-urls", "bind-addr", cfg.clientTLSInfo)
	if err != nil {
		return err
	}
	cfg.acurls, err = flags.URLsFromFlags(cfg.FlagSet, "advertise-client-urls", "addr", cfg.clientTLSInfo)
	if err != nil {
		return err
	}

	if err := cfg.resolveUrls(); err != nil {
		return errors.New("cannot resolve DNS hostnames.")
	}

	if 5*cfg.TickMs > cfg.ElectionMs {
		return fmt.Errorf("-election-timeout[%vms] should be at least as 5 times as -heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
	}

	return nil
}