Beispiel #1
0
func NewEtcd(prefixKey string, config *EtcdConfig) (*Etcd, error) {
	var (
		client *etcd.Client
		err    error
	)

	if config.CertFile != "" && config.KeyFile != "" {
		if client, err = etcd.NewTLSClient(config.Machines, config.CertFile, config.KeyFile, config.CaFile); err != nil {
			Logger.Error("Failed to connect to Etcd. Error: %+v.", err)
			return nil, err
		}
	} else {
		client = etcd.NewClient(config.Machines)
	}

	// Set the default value if not provided.
	if config.EtcdConsistency == "" {
		config.EtcdConsistency = etcd.STRONG_CONSISTENCY
	}

	if err = client.SetConsistency(config.EtcdConsistency); err != nil {
		Logger.Error("Failed to set Etcd consitency. Error: %+v.", err)
		return nil, err
	}

	return &Etcd{client: client, prefixKey: prefixKey}, nil
}
Beispiel #2
0
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
	var c *goetcd.Client
	var err error
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)

	maxConnectAttempts := 10
	for attempt := 1; attempt <= maxConnectAttempts; attempt++ {
		success := c.SetCluster(machines)
		if success {
			break
			return &Client{c}, nil
		}

		if attempt == maxConnectAttempts {
			break
			return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
		}
		log.Info(fmt.Sprintf("[Attempt: %d] Attempting access to etcd after 5 second sleep", attempt))
		time.Sleep(5 * time.Second)
	}

	return &Client{c}, nil
}
Beispiel #3
0
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
	urls := make([]string, 0)
	if uri.Host != "" {
		urls = append(urls, f.Scheme+"://"+uri.Host)
	} else {
		urls = append(urls, f.Scheme+"://127.0.0.1:4001")
	}

	if f.Scheme == "https" {
		cert := os.Getenv("ETCD_CERTFILE")
		key := os.Getenv("ETCD_KEYFILE")
		caCert := os.Getenv("ETCD_CAFILE")
		client, err := etcd2.NewTLSClient(urls, cert, key, caCert)
		if err != nil {
			log.Fatal("etcd: error creating tls client", err)
		}
		return &EtcdAdapter{client2: client, path: uri.Path}
	}

	res, err := http.Get(urls[0] + "/version")
	if err != nil {
		log.Fatal("etcd: error retrieving version", err)
	}

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	if match, _ := regexp.Match("0\\.4\\.*", body); match == true {
		log.Println("etcd: using v0 client")
		return &EtcdAdapter{client: etcd.NewClient(urls), path: uri.Path}
	}

	return &EtcdAdapter{client2: etcd2.NewClient(urls), path: uri.Path}
}
Beispiel #4
0
func newEtcdClient(c *EtcdConfig) (*etcd.Client, error) {
	if c.Keyfile != "" || c.Certfile != "" || c.CAFile != "" {
		return etcd.NewTLSClient(c.Endpoints, c.Certfile, c.Keyfile, c.CAFile)
	} else {
		return etcd.NewClient(c.Endpoints), nil
	}
}
Beispiel #5
0
func Client() *etcd.Client {
	clientSingletonO.Do(func() {
		host := "http://localhost:4001"
		if len(os.Getenv("ETCD_HOST")) != 0 {
			host = os.Getenv("ETCD_HOST")
		}

		cacert := os.Getenv("ETCD_CACERT")
		tlskey := os.Getenv("ETCD_TLS_KEY")
		tlscert := os.Getenv("ETCD_TLS_CERT")
		if len(cacert) != 0 && len(tlskey) != 0 && len(tlscert) != 0 {
			if !strings.Contains(host, "https://") {
				host = strings.Replace(host, "http", "https", 1)
			}
			c, err := etcd.NewTLSClient([]string{host}, tlscert, tlskey, cacert)
			if err != nil {
				panic(err)
			}
			clientSingleton = c
		} else {
			clientSingleton = etcd.NewClient([]string{host})
		}
	})
	return clientSingleton
}
Beispiel #6
0
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
	urls := make([]string, 0)
	if uri.Host != "" {
		urls = append(urls, f.Scheme+"://"+uri.Host)
	} else {
		urls = append(urls, f.Scheme+"://127.0.0.1:4001")
	}

	if len(uri.Path) < 2 {
		log.Fatal("skydns2: dns domain required e.g.: skydns2://<host>/<domain>")
	}

	if f.Scheme == "https" {
		cert := os.Getenv("ETCD_CERTFILE")
		key := os.Getenv("ETCD_KEYFILE")
		caCert := os.Getenv("ETCD_CAFILE")
		client, err := etcd.NewTLSClient(urls, cert, key, caCert)
		if err != nil {
			log.Fatal("etcd: error creating tls client", err)
		}
		return &Skydns2Adapter{client: client, path: domainPath(uri.Path[1:])}
	}

	return &Skydns2Adapter{client: etcd.NewClient(urls), path: domainPath(uri.Path[1:])}
}
Beispiel #7
0
func newEtcdLockClient(e *EtcdLock) (utils.Registry, error) {
	var c utils.Registry
	// if all are set, try to use tls
	if *e.CertFile != "" && *e.KeyFile != "" && *e.CAFile != "" {
		return etcd.NewTLSClient(*e.Members, *e.CertFile, *e.KeyFile, *e.CAFile)
	}
	c = etcd.NewClient(*e.Members)

	return c, nil
}
Beispiel #8
0
func NewClient(machines []string, cert, key, ca string) (client *Client, err error) {
	client = &Client{}
	if cert != "" && key != "" && ca != "" {
		if client.Client, err = etcd.NewTLSClient(machines, cert, key, ca); err != nil {
			return
		}
	} else {
		client.Client = etcd.NewClient(machines)
	}
	return
}
func NewEtcdClient() (client *etcd.Client) {
	if etcdClient != nil {
		return etcdClient
	}

	tlsEnabled := os.Getenv("ETCDCTL_TLS")
	tls := len(tlsEnabled) > 0

	certFile := os.Getenv("ETCDCTL_CERT_FILE")
	keyFile := os.Getenv("ETCDCTL_KEY_FILE")
	caFile := os.Getenv("ETCDCTL_TRUSTED_CA_FILE")

	var err error
	peersStr := os.Getenv("ETCDCTL_PEERS")
	if len(peersStr) > 0 {
		log.Println("Connecting to etcd peers : " + peersStr)
		peers := strings.Split(peersStr, ",")
		if tls {
			etcdClient, err = etcd.NewTLSClient(peers, certFile, keyFile, caFile)
			if err != nil {
				log.Fatal(err)
			}
		} else {
			etcdClient = etcd.NewClient(peers)
		}
	} else {
		if tls {
			etcdClient, err = etcd.NewTLSClient(nil, certFile, keyFile, caFile)
			if err != nil {
				log.Fatal(err)
			}
		} else {
			etcdClient = etcd.NewClient(nil)
		}
	}

	return etcdClient
}
Beispiel #10
0
//新建注册中心客户端
func (c *Conn) NewClient() (result *registry, err error) {
	var client *etcd.Client
	if c.Tls == true {
		//https conn
		client, err = etcd.NewTLSClient(c.Hosts, c.Cert, c.Key, c.CaCert)
	} else {
		//http conn
		client = etcd.NewClient(c.Hosts)
		if client == nil {
			err = fmt.Errorf("client init faild")
		}
	}
	return &registry{pool: client}, err
}
Beispiel #11
0
func newClient() (client *etcd.Client) {
	if len(machines) == 1 && machines[0] == "" {
		machines[0] = "http://127.0.0.1:4001"
	}
	if strings.HasPrefix(machines[0], "https://") {
		var err error
		if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, ""); err != nil {
			log.Fatal(err)
		}
	} else {
		client = etcd.NewClient(machines)
	}
	client.SyncCluster()
	return client
}
Beispiel #12
0
func newClient(machines []string, tlsCert, tlsKey, tlsCACert string) (client *etcd.Client) {
	// set default if not specified in env
	if len(machines) == 1 && machines[0] == "" {
		machines[0] = "http://127.0.0.1:4001"
	}
	if strings.HasPrefix(machines[0], "https://") {
		var err error
		// TODO(miek): machines is local, the rest is global, ugly.
		if client, err = etcd.NewTLSClient(machines, tlsCert, tlsKey, tlsCACert); err != nil {
			// TODO(miek): would be nice if this wasn't a fatal error
			log.Fatalf("skydns: failure to connect: %s", err)
		}
		return client
	}
	return etcd.NewClient(machines)
}
Beispiel #13
0
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
	var c *etcd.Client
	if cert != "" && key != "" {
		c, err := etcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = etcd.NewClient(machines)
	}
	success := c.SetCluster(machines)
	if !success {
		return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
	}
	return &Client{c}, nil
}
Beispiel #14
0
func (n *ng) reconnect() error {
	n.Close()
	var client *etcd.Client
	if n.options.EtcdCertFile == "" && n.options.EtcdKeyFile == "" {
		client = etcd.NewClient(n.nodes)
	} else {
		var err error
		if client, err = etcd.NewTLSClient(n.nodes, n.options.EtcdCertFile, n.options.EtcdKeyFile, n.options.EtcdCaFile); err != nil {
			return err
		}
	}
	if err := client.SetConsistency(n.options.EtcdConsistency); err != nil {
		return err
	}
	n.client = client
	return nil
}
Beispiel #15
0
// NewClient returns an *etcd.Client with a connection to the named machines.
// It will return an error if a connection to the cluster cannot be made.
// The parameter machines needs to be a full URL with schemas.
// e.g. "http://localhost:4001" will work, but "localhost:4001" will not.
func NewClient(machines []string, cert, key, caCert string) (Client, error) {
	var c *etcd.Client
	var err error

	if cert != "" && key != "" {
		c, err = etcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return nil, err
		}
	} else {
		c = etcd.NewClient(machines)
	}
	success := c.SetCluster(machines)
	if !success {
		return nil, fmt.Errorf("cannot connect to the etcd cluster: %s", strings.Join(machines, ","))
	}
	return &client{c}, nil
}
Beispiel #16
0
func NewClient(machines []string) (client *etcd.Client) {
	// set default if not specified in env
	if len(machines) == 1 && machines[0] == "" {
		machines[0] = "http://127.0.0.1:4001"
	}
	if strings.HasPrefix(machines[0], "https://") {
		var err error
		if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, cacert); err != nil {
			// TODO(miek): would be nice if this wan't a fatal error
			log.Fatalf("failure to connect: %s\n", err)
		}
		client.SyncCluster()
	} else {
		client = etcd.NewClient(machines)
		client.SyncCluster()
	}
	return client
}
Beispiel #17
0
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
	var c *etcd.Client
	if cert != "" && key != "" {
		c, err := etcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = etcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)
	success := c.SetCluster(machines)
	if !success {
		return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
	}
	return &Client{c}, nil
}
Beispiel #18
0
Datei: etcd.go Projekt: gdb/vault
// newEtcdBackend constructs a etcd backend using a given machine address.
func newEtcdBackend(conf map[string]string) (Backend, error) {
	// Get the etcd path form the configuration.
	path, ok := conf["path"]
	if !ok {
		path = "/vault"
	}

	// Ensure path is prefixed.
	if !strings.HasPrefix(path, "/") {
		path = "/" + path
	}

	// Set a default machines list and check for an overriding address value.
	machines := "http://128.0.0.1:2379"
	if address, ok := conf["address"]; ok {
		machines = address
	}
	machinesParsed := strings.Split(machines, EtcdMachineDelimiter)

	// Create a new client from the supplied address and attempt to sync with the
	// cluster.
	var client *etcd.Client
	cert, has_cert := conf["tls_cert_file"]
	key, has_key := conf["tls_key_file"]
	ca, has_ca := conf["tls_ca_file"]
	if has_cert || has_key || has_ca {
		var err error
		client, err = etcd.NewTLSClient(machinesParsed, cert, key, ca)
		if err != nil {
			return nil, err
		}
	} else {
		client = etcd.NewClient(machinesParsed)
	}
	if !client.SyncCluster() {
		return nil, EtcdSyncClusterError
	}

	// Setup the backend.
	return &EtcdBackend{
		path:   path,
		client: client,
	}, nil
}
Beispiel #19
0
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
	urls := make([]string, 0)

	if len(uri.Path) < 2 {
		log.Fatal("skydns2: dns domain required e.g.: skydns2://<host>/<domain>")
	}

	tlskey := os.Getenv("ETCD_TLSKEY")
	tlspem := os.Getenv("ETCD_TLSPEM")
	cacert := os.Getenv("ETCD_CACERT")

	var client *etcd.Client

	// Assuming https
	if cacert != "" {
		urls = append(urls, "https://"+uri.Host)

		// Assuming Client authentication if tlskey and tlspem is set
		if tlskey != "" && tlspem != "" {
			var err error
			if client, err = etcd.NewTLSClient(urls, tlspem, tlskey, cacert); err != nil {
				log.Fatalf("skydns2: failure to connect: %s", err)
			}
		} else {
			client = etcd.NewClient(urls)
			ca, err := ioutil.ReadFile(cacert)
			if err != nil {
				log.Fatal(err)
			}
			caCertPool := x509.NewCertPool()
			caCertPool.AppendCertsFromPEM(ca)
			tr := &http.Transport{
				TLSClientConfig:    &tls.Config{RootCAs: caCertPool},
				DisableCompression: true,
			}
			client.SetTransport(tr)
		}
	} else {
		urls = append(urls, "http://"+uri.Host)
		client = etcd.NewClient(urls)
	}

	return &Skydns2Adapter{client: client, path: domainPath(uri.Path[1:])}
}
Beispiel #20
0
func newClient() (client *etcd.Client) {
	if len(machines) == 1 && machines[0] == "" {
		machines[0] = "http://127.0.0.1:4001"
		// override if we have a commandline flag as well
		if machine != "" {
			strings.Split(machine, ",")
		}
	}
	if strings.HasPrefix(machines[0], "https://") {
		var err error
		if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, ""); err != nil {
			log.Fatal(err)
		}
	} else {
		client = etcd.NewClient(machines)
	}
	client.SyncCluster()
	return client
}
Beispiel #21
0
// New creates a new Etcd client given a list
// of endpoints and an optional tls config
func New(addrs []string, options *store.Config) (store.Store, error) {
	s := &Etcd{}

	var (
		entries []string
		err     error
	)

	// Create the etcd client
	if options != nil && options.ClientTLS != nil {
		entries = store.CreateEndpoints(addrs, "https")
		s.client, err = etcd.NewTLSClient(entries, options.ClientTLS.CertFile, options.ClientTLS.KeyFile, options.ClientTLS.CACertFile)
		if err != nil {
			return nil, err
		}
	} else {
		entries = store.CreateEndpoints(addrs, "http")
		s.client = etcd.NewClient(entries)
	}

	// Set options
	if options != nil {
		// Plain TLS config overrides ClientTLS if specified
		if options.TLS != nil {
			s.setTLS(options.TLS, addrs)
		}
		if options.ConnectionTimeout != 0 {
			s.setTimeout(options.ConnectionTimeout)
		}
	}

	// Periodic SyncCluster
	go func() {
		for {
			s.client.SyncCluster()
			time.Sleep(periodicSync)
		}
	}()

	return s, nil
}
Beispiel #22
0
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
//func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
func NewEtcdClient(machines []string, cert, key string, caCert string, basicAuth bool, username string, password string) (*Client, error) {
	var c *goetcd.Client
	var err error
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	if basicAuth {
		c.SetCredentials(username, password)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)
	success := c.SetCluster(machines)
	if !success {
		return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
	}
	return &Client{c}, nil
}
Beispiel #23
0
func initializeEtcdStoreClient(logger lager.Logger, etcdOptions *etcddb.ETCDOptions) etcddb.StoreClient {
	var etcdClient *etcdclient.Client
	var tr *http.Transport

	if etcdOptions.IsSSL {
		if etcdOptions.CertFile == "" || etcdOptions.KeyFile == "" {
			logger.Fatal("failed-to-construct-etcd-tls-client", errors.New("Require both cert and key path"))
		}

		var err error
		etcdClient, err = etcdclient.NewTLSClient(etcdOptions.ClusterUrls, etcdOptions.CertFile, etcdOptions.KeyFile, etcdOptions.CAFile)
		if err != nil {
			logger.Fatal("failed-to-construct-etcd-tls-client", err)
		}

		tlsCert, err := tls.LoadX509KeyPair(etcdOptions.CertFile, etcdOptions.KeyFile)
		if err != nil {
			logger.Fatal("failed-to-construct-etcd-tls-client", err)
		}

		tlsConfig := &tls.Config{
			Certificates:       []tls.Certificate{tlsCert},
			InsecureSkipVerify: true,
			ClientSessionCache: tls.NewLRUClientSessionCache(etcdOptions.ClientSessionCacheSize),
		}
		tr = &http.Transport{
			TLSClientConfig:     tlsConfig,
			Dial:                etcdClient.DefaultDial,
			MaxIdleConnsPerHost: etcdOptions.MaxIdleConnsPerHost,
		}
		etcdClient.SetTransport(tr)
		etcdClient.AddRootCA(etcdOptions.CAFile)
	} else {
		etcdClient = etcdclient.NewClient(etcdOptions.ClusterUrls)
	}
	etcdClient.SetConsistency(etcdclient.STRONG_CONSISTENCY)

	return etcddb.NewStoreClient(etcdClient)
}
Beispiel #24
0
func initializeEtcdClient(etcdOptions *ETCDOptions) *etcd.Client {
	var etcdClient *etcd.Client
	var tr *http.Transport

	if etcdOptions.IsSSL {
		if etcdOptions.CertFile == "" || etcdOptions.KeyFile == "" {
			panic(errors.New("Require both cert and key path"))
		}

		var err error
		etcdClient, err = etcd.NewTLSClient(etcdOptions.ClusterUrls, etcdOptions.CertFile, etcdOptions.KeyFile, etcdOptions.CAFile)
		if err != nil {
			panic(err)
		}

		tlsCert, err := tls.LoadX509KeyPair(etcdOptions.CertFile, etcdOptions.KeyFile)
		if err != nil {
			panic(err)
		}

		tlsConfig := &tls.Config{
			Certificates:       []tls.Certificate{tlsCert},
			InsecureSkipVerify: true,
			ClientSessionCache: tls.NewLRUClientSessionCache(etcdOptions.ClientSessionCacheSize),
		}
		tr = &http.Transport{
			TLSClientConfig:     tlsConfig,
			Dial:                etcdClient.DefaultDial,
			MaxIdleConnsPerHost: etcdOptions.MaxIdleConnsPerHost,
		}
		etcdClient.SetTransport(tr)
	} else {
		etcdClient = etcd.NewClient(etcdOptions.ClusterUrls)
	}
	etcdClient.SetConsistency(etcd.STRONG_CONSISTENCY)

	return etcdClient
}
Beispiel #25
0
// New creates a new Etcd client given a list
// of endpoints and an optional tls config
func New(addrs []string, options *store.Config) (store.Store, error) {
	s := &Etcd{}

	var entries []string
	if options != nil && options.ClientTLS != nil {
		entries = store.CreateEndpoints(addrs, "https")
		var err error
		s.client, err = etcd.NewTLSClient(entries, options.ClientTLS.CertFile, options.ClientTLS.KeyFile, options.ClientTLS.CACertFile)
		if err != nil {
			return s, err
		}
	} else {
		entries = store.CreateEndpoints(addrs, "http")
		s.client = etcd.NewClient(entries)
	}
	// Set options
	if options != nil {
		if options.TLS != nil && options.ClientTLS == nil {
			// NewTLSClient already does this.
			s.setTLS(options.TLS)
		}
		if options.ConnectionTimeout != 0 {
			s.setTimeout(options.ConnectionTimeout)
		}
	}

	// Periodic SyncCluster
	go func() {
		for {
			s.client.SyncCluster()
			time.Sleep(periodicSync)
		}
	}()

	return s, nil
}
Beispiel #26
0
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string, noDiscover bool) (*Client, error) {
	var c *goetcd.Client
	var err error
	machines = prependSchemeToMachines(machines)
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)

	// If noDiscover is not set, we should locate the whole etcd cluster.
	if !noDiscover {
		success := c.SetCluster(machines)
		if !success {
			return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
		}
	}
	return &Client{c}, nil
}
Beispiel #27
0
func (f *FlagSet) ParseEtcd(addressFlag *Flag, caFileFlag *Flag, certFileFlag *Flag, keyFileFlag *Flag, pathFlag *Flag) error {
	// Prepare some local variables
	var (
		addresses     []string
		addressString string
		caFile        string
		certFile      string
		keyFile       string
		path          string
	)

	// Parse the flags
	if addressFlag != nil {
		addressString = addressFlag.Value.String()
		addresses = strings.Split(addressString, ",")
	}

	if caFileFlag != nil {
		caFile = caFileFlag.Value.String()
	}

	if certFileFlag != nil {
		certFile = certFileFlag.Value.String()
	}

	if keyFileFlag != nil {
		keyFile = keyFileFlag.Value.String()
	}

	if pathFlag != nil {
		path = pathFlag.Value.String()
	}

	// Connect to the etcd server
	var client *etcd.Client

	if caFile != "" && certFile != "" && keyFile != "" {
		var err error
		client, err = etcd.NewTLSClient(addresses, certFile, keyFile, caFile)
		if err != nil {
			return f.failf("Unable to create a new TLS etcd client: %s", err.Error())
		}
	} else {
		client = etcd.NewClient(addresses)
	}

	// Perform the checks
	m := f.formal

	for _, flag := range m {
		name := flag.Name

		_, set := f.actual[name]
		if set {
			continue
		}

		flag, alreadythere := m[name]
		if !alreadythere {
			if name == "help" || name == "h" { // special case for nice help message.
				f.usage()
				return ErrHelp
			}
			return f.failf("etcd setting provided but not defined: %s", name)
		}

		key := strings.ToLower(flag.Name)
		key = strings.Replace(key, "-", "_", -1)

		resp, err := client.Get(path+key, false, false)
		if err != nil {
			etcdErr, ok := err.(*etcd.EtcdError)
			if ok {
				if etcdErr.ErrorCode != 100 {
					return f.failf("etcd error: %s", err.Error())
				}
			} else {
				return f.failf("not-related etcd error: %s", err.Error())
			}
			continue
		}

		value := resp.Node.Value

		has_value := false
		if len(value) > 0 {
			has_value = true
		}

		if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
			if has_value {
				if err := fv.Set(value); err != nil {
					return f.failf("invalid boolean value %q for etcd value %s: %v", value, name, err)
				}
			} else {
				// flag without value is regarded a bool
				fv.Set("true")
			}
		} else {
			if !has_value {
				return f.failf("environment variable needs an value: %s", name)
			}

			if err := flag.Value.Set(value); err != nil {
				return f.failf("invalid value %q for etcd value %s: %v", value, name, err)
			}
		}

		// update f.actual
		if f.actual == nil {
			f.actual = make(map[string]*Flag)
		}
		f.actual[name] = flag
	}

	return nil
}
Beispiel #28
0
func main() {

	_, err := flags.ParseArgs(&Options, os.Args[1:])
	if err != nil {
		os.Exit(1)
	}

	if Options.Debug {
		log_debug = true
		debugMsg("Debug mode enabled")
	}

	// Create an ETCD client
	var e *etcd.Client
	if Options.Cert != "" && Options.Key != "" {
		debugMsg("Connecting using TLS: ", Options.EtcdHosts, Options.Cert, Options.Key, Options.CaCert)
		e, err = etcd.NewTLSClient(Options.EtcdHosts, Options.Cert, Options.Key, Options.CaCert)
		if err != nil {
			logger.Fatalf("Failed to connect to Etcd server using TLS: ", err)
		}
	} else {
		e = etcd.NewClient(Options.EtcdHosts)
	}
	if !e.SyncCluster() {
		logger.Printf("[WARNING] Failed to connect to etcd cluster at launch time")
	}

	// Register the metrics writer
	if len(Options.GraphiteServer) > 0 {
		addr, err := net.ResolveTCPAddr("tcp", Options.GraphiteServer)
		if err != nil {
			logger.Fatalf("Failed to parse graphite server: ", err)
		}

		prefix := "discodns"
		hostname, err := os.Hostname()
		if err != nil {
			logger.Fatalf("Unable to get hostname: ", err)
		}

		prefix = prefix + "." + strings.Replace(hostname, ".", "_", -1)

		go metrics.Graphite(metrics.DefaultRegistry, time.Duration(Options.GraphiteDuration)*time.Second, prefix, addr)
	} else if Options.MetricsDuration > 0 {
		go metrics.Log(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration)*time.Second, logger)

		// Register a bunch of debug metrics
		metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
		metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
		go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
		go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
	} else {
		logger.Printf("Metric logging disabled")
	}

	// Start up the DNS resolver server
	server := &Server{
		addr:       Options.ListenAddress,
		port:       Options.ListenPort,
		etcd:       e,
		prefix:     Options.Prefix,
		rTimeout:   time.Duration(5) * time.Second,
		wTimeout:   time.Duration(5) * time.Second,
		defaultTtl: Options.DefaultTtl,
		queryFilterer: &QueryFilterer{acceptFilters: parseFilters(Options.Accept),
			rejectFilters: parseFilters(Options.Reject)}}

	server.Run()

	logger.Printf("Listening on %s:%d\n", Options.ListenAddress, Options.ListenPort)

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt)

forever:
	for {
		select {
		case <-sig:
			logger.Printf("Bye bye :(\n")
			break forever
		}
	}
}
Beispiel #29
0
Datei: main.go Projekt: Gerg/bbs
func main() {
	cf_debug_server.AddFlags(flag.CommandLine)
	cf_lager.AddFlags(flag.CommandLine)
	etcdFlags := AddETCDFlags(flag.CommandLine)
	flag.Parse()

	cf_http.Initialize(*communicationTimeout)

	logger, reconfigurableSink := cf_lager.New("bbs")
	logger.Info("starting")

	initializeDropsonde(logger)

	etcdOptions, err := etcdFlags.Validate()
	if err != nil {
		logger.Fatal("etcd-validation-failed", err)
	}

	var etcdClient *etcdclient.Client
	if etcdOptions.IsSSL {
		etcdClient, err = etcdclient.NewTLSClient(etcdOptions.ClusterUrls, etcdOptions.CertFile, etcdOptions.KeyFile, etcdOptions.CAFile)
		if err != nil {
			logger.Fatal("failed-to-construct-etcd-tls-client", err)
		}
	} else {
		etcdClient = etcdclient.NewClient(etcdOptions.ClusterUrls)
	}
	etcdClient.SetConsistency(etcdclient.STRONG_CONSISTENCY)

	err = validateAuctioneerFlag()
	if err != nil {
		logger.Fatal("auctioneer-address-validation-failed", err)
	}
	auctioneerClient := auctionhandlers.NewClient(*auctioneerAddress)
	consulSession := initializeConsul(logger)
	consulDB := consuldb.NewConsul(consulSession)
	cellClient := cellhandlers.NewClient()
	db := etcddb.NewETCD(etcdClient, auctioneerClient, cellClient, consulDB, clock.NewClock())
	hub := events.NewHub()
	watcher := watcher.NewWatcher(
		logger,
		db,
		hub,
		clock.NewClock(),
		bbsWatchRetryWaitDuration,
	)

	handler := handlers.New(logger, db, hub)

	members := grouper.Members{
		{"watcher", watcher},
		{"server", http_server.New(*serverAddress, handler)},
		{"hub-closer", closeHub(logger.Session("hub-closer"), hub)},
	}

	if dbgAddr := cf_debug_server.DebugAddress(flag.CommandLine); dbgAddr != "" {
		members = append(grouper.Members{
			{"debug-server", cf_debug_server.Runner(dbgAddr, reconfigurableSink)},
		}, members...)
	}

	group := grouper.NewOrdered(os.Interrupt, members)

	monitor := ifrit.Invoke(sigmon.New(group))

	logger.Info("started")

	err = <-monitor.Wait()
	if err != nil {
		logger.Error("exited-with-failure", err)
		os.Exit(1)
	}

	logger.Info("exited")
}
func main() {
	flag.Parse()

	if *fhelp {
		flag.Usage()
		return
	}
	if *fcfg_file != "null" {
		conf_file = *fcfg_file
	} else {
		flag.Usage()
		fmt.Println("Please input the cfg file")
		return
	}

	readConfig()

	handleFlags()

	// Fetch the memory information before load test
	if mem_flag {
		memHandler(false)
	}

	// Creating a new client for handling requests .
	// If etcd instance in secure, certificates are used to create
	// client
	if *fsecure {
		ca := fmt.Sprintf(cpath + "/" + ca_cert)
		cert := fmt.Sprintf(cpath + "/" + client_cert)
		key := fmt.Sprintf(cpath + "/" + client_key)
		var machines = []string{"https://" + etcdhost + ":" + etcdport}
		client, _ = etcd.NewTLSClient(machines, cert, key, ca)

	} else {
		var machines = []string{"http://" + etcdhost + ":" + etcdport}
		client = etcd.NewClient(machines)
	}

	// Log file is opened or created for storing log entries.
	f, err = os.OpenFile(log_file, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	// Log file set
	log.SetOutput(f)
	log.Println("Starting #####")
	log.Println("Keycount, operation_count =", keycount, operation_count)

	// n result channels are made to pass time information during execution.
	// This part is useful for generating the commandline report.
	n := operation_count
	results = make(chan *result, n)

	// This is necessary for the goroutines.
	wg.Add(len(pct))

	// This part is where requests are handled.
	start := time.Now()
	switch {
	case operation == "create":
		log.Println("Operation : create")
		var values [2]int
		base := 0
		for i := 0; i < len(pct); i++ {
			values[0] = value_range[i]
			values[1] = value_range[i+1]
			go create_keys(base, pct_count[i], values)
			base = base + pct_count[i]*(keycount/operation_count)
		}
		wg.Wait()
		printReport(n, results, time.Now().Sub(start))
	case operation == "get":
		log.Println("Operation : get")
		handler(get_values)
		wg.Wait()
		printReport(n, results, time.Now().Sub(start))
	case operation == "update":
		log.Println("Operation : update")
		handler(update_values)
		wg.Wait()
		printReport(n, results, time.Now().Sub(start))
	case operation == "delete":
		log.Println("Operation : delete")
		handler(delete_values)
		wg.Wait()
		printReport(n, results, time.Now().Sub(start))
	}

	// Fetch and print memory information after load test
	if mem_flag {
		memHandler(true)
	}

	defer f.Close()
}