Beispiel #1
0
func newClient(c *cli.Context) (client.Client, error) {
	eps, err := getEndpoints(c)
	if err != nil {
		return nil, err
	}

	tr, err := getTransport(c)
	if err != nil {
		return nil, err
	}

	cfg := client.Config{
		Transport:               tr,
		Endpoints:               eps,
		HeaderTimeoutPerRequest: c.GlobalDuration("timeout"),
	}

	uFlag := c.GlobalString("username")

	if uFlag == "" {
		uFlag = os.Getenv("ETCDCTL_USERNAME")
	}

	if uFlag != "" {
		username, password, err := getUsernamePasswordFromFlag(uFlag)
		if err != nil {
			return nil, err
		}
		cfg.Username = username
		cfg.Password = password
	}

	return client.New(cfg)
}
Beispiel #2
0
func processGlobalFlags(c *cli.Context) time.Duration {
	var network string
	if c.GlobalBool("tcp") {
		network = "tcp"
	} else {
		network = "udp"
	}
	client, err := raidman.Dial(network, fmt.Sprintf("%s:%d", c.GlobalString("host"), c.GlobalInt("port")))
	if c.GlobalString("event-host") == "nil" {
		log.Panic("Failed to automatically get the hostname. Please specify it with --host")
	}
	if err != nil {
		log.Panicf("Failed to connect to the riemann host because %s", err)
	}
	attribute, err := processAttributes(c.GlobalStringSlice("attribute"))
	if err != nil {
		log.Panic(err)
	}
	eventTemplate := raidman.Event{
		Ttl:        float32(c.GlobalDuration("ttl").Seconds()),
		Tags:       c.GlobalStringSlice("tags"),
		Host:       c.GlobalString("event-host"),
		Attributes: attribute,
	}
	riemannSend = func(url, method string, duration float64) {
		event := eventTemplate
		event.Service = fmt.Sprintf("%s %s", url, method)
		event.Time = time.Now().Unix()
		event.Metric = duration
		client.Send(&event)
	}

	return c.GlobalDuration("interval")
}
Beispiel #3
0
func getTransport(c *cli.Context) (*http.Transport, error) {
	cafile := c.GlobalString("ca-file")
	certfile := c.GlobalString("cert-file")
	keyfile := c.GlobalString("key-file")

	// Use an environment variable if nothing was supplied on the
	// command line
	if cafile == "" {
		cafile = os.Getenv("ETCDCTL_CA_FILE")
	}
	if certfile == "" {
		certfile = os.Getenv("ETCDCTL_CERT_FILE")
	}
	if keyfile == "" {
		keyfile = os.Getenv("ETCDCTL_KEY_FILE")
	}

	tls := transport.TLSInfo{
		CAFile:   cafile,
		CertFile: certfile,
		KeyFile:  keyfile,
	}

	dialTimeout := defaultDialTimeout
	totalTimeout := c.GlobalDuration("total-timeout")
	if totalTimeout != 0 && totalTimeout < dialTimeout {
		dialTimeout = totalTimeout
	}
	return transport.NewTransport(tls, dialTimeout)
}
Beispiel #4
0
// TODO: parse flags and pass opts
func getClient(ctx *cli.Context) types.APIClient {
	// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
	grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
	dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(ctx.GlobalDuration("conn-timeout"))}
	dialOpts = append(dialOpts,
		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
			return net.DialTimeout("unix", addr, timeout)
		},
		))
	conn, err := grpc.Dial(ctx.GlobalString("address"), dialOpts...)
	if err != nil {
		fatal(err.Error(), 1)
	}
	return types.NewAPIClient(conn)
}
Beispiel #5
0
func actionMain(c *cli.Context) {
	if !c.GlobalIsSet("url") || !c.GlobalIsSet("ref") {
		log.Fatalln("Error: --url and --ref required")
	}

	where, err := git.PrepBuildDirectory(
		c.GlobalString("destination"),
		c.GlobalString("url"),
		c.GlobalString("ref"),
		c.GlobalDuration("timeout"),
		os.Stderr)
	if err != nil {
		log.Fatalln("Error:", err)
	}
	log.Printf("Checked out %v at %v", where.Name, where.Dir)
	fmt.Println(where.Dir)
}
Beispiel #6
0
func etcdConfigFromFlags(c *cli.Context) etcdclient.Config {
	eFlag := c.GlobalString("endpoint")
	uFlag := c.GlobalString("username")
	pFlag := c.GlobalString("password")
	tFlag := c.GlobalDuration("timeout")

	cfg := etcdclient.Config{
		Endpoints:               strings.Split(eFlag, ","),
		Transport:               etcdclient.DefaultTransport,
		HeaderTimeoutPerRequest: tFlag,
	}

	if uFlag != "" && pFlag != "" {
		cfg.Username = uFlag
		cfg.Password = pFlag
	}

	return cfg
}
Beispiel #7
0
// TODO: parse flags and pass opts
func getClient(ctx *cli.Context) types.APIClient {
	// Parse proto://address form addresses.
	bindSpec := ctx.GlobalString("address")
	bindParts := strings.SplitN(bindSpec, "://", 2)
	if len(bindParts) != 2 {
		fatal(fmt.Sprintf("bad bind address format %s, expected proto://address", bindSpec), 1)
	}

	// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
	grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
	dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(ctx.GlobalDuration("conn-timeout"))}
	dialOpts = append(dialOpts,
		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
			return net.DialTimeout(bindParts[0], bindParts[1], timeout)
		},
		))
	conn, err := grpc.Dial(bindSpec, dialOpts...)
	if err != nil {
		fatal(err.Error(), 1)
	}
	return types.NewAPIClient(conn)
}
Beispiel #8
0
func loadConfig(c *cli.Context) Etcdtool {
	// Enable debug
	if c.GlobalBool("debug") {
		debug = true
	}

	// Default path for config file.
	u, _ := user.Current()
	cfgs := []string{
		"/etcd/etcdtool.json",
		"/etcd/etcdtool.yaml",
		"/etcd/etcdtool.toml",
		u.HomeDir + "/.etcdtool.json",
		u.HomeDir + "/.etcdtool.yaml",
		u.HomeDir + "/.etcdtool.toml",
	}

	// Check if we have an arg. for config file and that it exist's.
	if c.GlobalString("config") != "" {
		if _, err := os.Stat(c.GlobalString("config")); os.IsNotExist(err) {
			fatalf("Config file doesn't exist: %s", c.GlobalString("config"))
		}
		cfgs = append([]string{c.GlobalString("config")}, cfgs...)
	}

	// Check if config file exists and load it.
	e := Etcdtool{}
	for _, fn := range cfgs {
		if _, err := os.Stat(fn); os.IsNotExist(err) {
			continue
		}
		infof("Using config file: %s", fn)
		f, err := iodatafmt.FileFormat(fn)
		if err != nil {
			fatal(err.Error())
		}
		if err := iodatafmt.LoadPtr(&e, fn, f); err != nil {
			fatal(err.Error())
		}
	}

	// Override with arguments or env. variables.
	if c.GlobalString("peers") != "" {
		e.Peers = c.GlobalString("peers")
	}

	if c.GlobalString("cert") != "" {
		e.Cert = c.GlobalString("cert")
	}

	if c.GlobalString("key") != "" {
		e.Key = c.GlobalString("key")
	}

	if c.GlobalString("ca") != "" {
		e.CA = c.GlobalString("ca")
	}

	if c.GlobalString("user") != "" {
		e.User = c.GlobalString("user")
	}

	if c.GlobalDuration("timeout") != 0 {
		e.Timeout = c.GlobalDuration("timeout")
	}

	if c.GlobalDuration("command-timeout") != 0 {
		e.CommandTimeout = c.GlobalDuration("command-timeout")
	}

	return e
}
Beispiel #9
0
func (cfg *Config) Load(c *cli.Context) {
	// Enable debug.
	if c.GlobalBool("debug") {
		log.SetDebug()
	}

	// Default path for config file.
	u, _ := user.Current()
	cfgs := []string{
		u.HomeDir + "/.etcdrest.json",
		u.HomeDir + "/.etcdrest.yaml",
		u.HomeDir + "/.etcdrest.yml",
		u.HomeDir + "/.etcdrest.toml",
		u.HomeDir + "/.etcdrest.tml",
		"/etc/etcdrest.json",
		"/etc/etcdrest.yaml",
		"/etc/etcdrest.yml",
		"/etc/etcdrest.toml",
		"/etc/etcdrest.tml",
		"/app/etc/etcdrest.json",
		"/app/etc/etcdrest.yaml",
		"/app/etc/etcdrest.yml",
		"/app/etc/etcdrest.toml",
		"/app/etc/etcdrest.tml",
	}

	// Check if we have an arg. for config file and that it exist's.
	if c.GlobalString("config") != "" {
		if _, err := os.Stat(c.GlobalString("config")); os.IsNotExist(err) {
			log.Fatalf("Config file doesn't exist: %s", c.GlobalString("config"))
		}
		cfgs = append([]string{c.GlobalString("config")}, cfgs...)
	}

	for _, fn := range cfgs {
		if _, err := os.Stat(fn); os.IsNotExist(err) {
			continue
		}

		log.Infof("Using config file: %s", fn)

		// Load config file.
		b, err := ioutil.ReadFile(fn)
		if err != nil {
			log.Fatal(err.Error())
		}

		switch filepath.Ext(fn) {
		case ".json":
			if err := json.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		case ".yaml", ".yml":
			if err := yaml.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		case ".toml", ".tml":
			if err := toml.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		default:
			log.Fatal("unsupported data format")
		}

		// Validate config using JSON schema.

		break
	}

	// Override configuration.
	if c.GlobalString("templ-dir") != "" {
		cfg.TemplDir = c.GlobalString("templ-dir")
	}

	if c.GlobalString("schema-uri") != "" {
		cfg.SchemaURI = c.GlobalString("schema-uri")
	}

	if c.GlobalString("bind") != "" {
		cfg.Bind = c.GlobalString("bind")
	}

	if c.GlobalString("server-uri") != "" {
		cfg.ServerURI = c.GlobalString("server-uri")
	}

	if c.GlobalBool("envelope") {
		cfg.Envelope = true
	}

	if c.GlobalBool("no-indent") {
		cfg.Indent = true
	}

	// Override etcd configuration.
	if c.GlobalString("peers") != "" {
		cfg.Etcd.Peers = c.GlobalString("peers")
	}

	if c.GlobalString("cert") != "" {
		cfg.Etcd.Cert = c.GlobalString("cert")
	}

	if c.GlobalString("key") != "" {
		cfg.Etcd.Key = c.GlobalString("key")
	}

	if c.GlobalString("ca") != "" {
		cfg.Etcd.CA = c.GlobalString("ca")
	}

	if c.GlobalString("user") != "" {
		cfg.Etcd.User = c.GlobalString("user")
	}

	if c.GlobalDuration("timeout") != 0 {
		cfg.Etcd.Timeout = c.GlobalDuration("timeout")
	}

	if c.GlobalDuration("command-timeout") != 0 {
		cfg.Etcd.CmdTimeout = c.GlobalDuration("command-timeout")
	}
}
Beispiel #10
0
func contextWithTotalTimeout(c *cli.Context) (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout"))
}
Beispiel #11
0
func contextWithCommandTimeout(c *cli.Context) (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), c.GlobalDuration("command-timeout"))
}