示例#1
0
// get a client handle for a specified address (or the local agent if "")
func getClient(address string) (*api.Client, error) {
	config := api.DefaultConfig()
	if address != "" {
		config.Address = address
	}
	return api.NewClient(config)
}
示例#2
0
func NewConsulBackend(address string) (Backend, error) {
	if address == "" {
		address = "http://127.0.0.1:8500/vault"
	}
	url, err := url.Parse(address)
	if err != nil {
		return nil, maskAny(err)
	}
	path := url.Path

	// Ensure path is suffixed but not prefixed
	if !strings.HasSuffix(path, "/") {
		path += "/"
	}
	if strings.HasPrefix(path, "/") {
		path = strings.TrimPrefix(path, "/")
	}

	consulConf := api.DefaultConfig()
	consulConf.Address = url.Host

	client, err := api.NewClient(consulConf)
	if err != nil {
		return nil, maskAny(err)
	}

	return &consulBackend{
		path:   path,
		client: client,
		kv:     client.KV(),
	}, nil
}
示例#3
0
func consulFactory(conf map[string]string) (Client, error) {
	path, ok := conf["path"]
	if !ok {
		return nil, fmt.Errorf("missing 'path' configuration")
	}

	config := consulapi.DefaultConfig()
	if token, ok := conf["access_token"]; ok && token != "" {
		config.Token = token
	}
	if addr, ok := conf["address"]; ok && addr != "" {
		config.Address = addr
	}
	if scheme, ok := conf["scheme"]; ok && scheme != "" {
		config.Scheme = scheme
	}

	client, err := consulapi.NewClient(config)
	if err != nil {
		return nil, err
	}

	return &ConsulClient{
		Client: client,
		Path:   path,
	}, nil
}
示例#4
0
/* File needs to be in the following format:
   KEY1:VALUE1
   KEY2:VALUE2
*/
func restore(ipaddress string, token string, infile string) {

	config := api.DefaultConfig()
	config.Address = ipaddress
	config.Token = token

	data, err := ioutil.ReadFile(infile)
	if err != nil {
		panic(err)
	}

	client, _ := api.NewClient(config)
	kv := client.KV()

	for _, element := range strings.Split(string(data), "\n") {
		kvp := strings.Split(element, ":")

		if len(kvp) > 1 {
			decoded_value, decode_err := base64.StdEncoding.DecodeString(kvp[1])
			if decode_err != nil {
				panic(decode_err)
			}

			p := &api.KVPair{Key: kvp[0], Value: decoded_value}
			_, err := kv.Put(p, nil)
			if err != nil {
				panic(err)
			}
		}
	}
}
示例#5
0
func TestMatchTaskRun(t *testing.T) {
	t.Log("here we are")
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	srv := NewTestServer(t)
	defer srv.Stop()
	conf := api.DefaultConfig()
	conf.Address = srv.HTTPAddr
	coord := NewConsulCoordinator(func(conf *ConsulConfig) {
		conf.Address = srv.HTTPAddr
	}).(*consulCoordinator)
	coord.Start(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}, make(chan error))
	receiver := NewMockInputManager(mockCtrl)

	// NewMatchTask(coord, receiver, StreamID("match(cpu_util{service=monitoring}, user_perc{service=monitoring} by hostname, device in alarm(avg($1, 5min) )")
	sid := StreamID("match(s1{t1=v1}, s2{t1=v1} by t2, t3 in alarm(avg(%s)<2 and avg(%s)>3))")
	_, definition, err := ParseComputationID(sid)
	assert.Nil(t, err)
	taskInfo, _ := NewMatchTask(coord, receiver, sid, definition)

	receiver.EXPECT().SubscribeTo(StreamID("alarm(avg(s1{t1=v1,t2=a,t3=1})<2 and avg(s2{t1=v1,t2=a,t3=1})>3)"), Timestamp(0), gomock.Any())
	receiver.EXPECT().SubscribeTo(StreamID("alarm(avg(s1{t1=v1,t2=b,t3=1})<2 and avg(s2{t1=v1,t2=b,t3=1})>3)"), Timestamp(0), gomock.Any())
	receiver.EXPECT().SubscribeTo(StreamID("alarm(avg(s1{t1=v1,t2=a,t3=2})<2 and avg(s2{t1=v1,t2=a,t3=2})>3)"), Timestamp(0), gomock.Any())

	go taskInfo.Task.Run(make(chan error))

	coord.RegisterAsPublisher(StreamID("s1{t1=v1,t2=a,t3=1,t4=x}"))
	coord.RegisterAsPublisher(StreamID("s2{t1=v1,t2=a,t3=1,t4=x}"))
	coord.RegisterAsPublisher(StreamID("s1{t1=v1,t2=b,t3=1,t4=y}"))
	coord.RegisterAsPublisher(StreamID("s2{t1=v1,t2=a,t3=2,t4=x}"))
	time.Sleep(2 * time.Second)
}
示例#6
0
文件: role.go 项目: edwardt/cascade
func roleList(c cli.Command) {
	client, _ := api.NewClient(api.DefaultConfig())
	agent := client.Agent()

	services, err := agent.Services()

	if err != nil {
		log.Fatalln("err: ", err)
	}

	self, err := agent.Self()

	if err != nil {
		log.Fatalln("err: ", err)
	}

	for _, service := range services {
		if service.Service == "cascade" {
			fmt.Println(self["Config"]["NodeName"], self["Config"]["AdvertiseAddr"].(string)+":")
			for _, role := range service.Tags {
				fmt.Println("  -", role)
			}
		}
	}
}
示例#7
0
func backup(ipaddress string, token string, outfile string) {

	config := api.DefaultConfig()
	config.Address = ipaddress
	config.Token = token

	client, _ := api.NewClient(config)
	kv := client.KV()

	pairs, _, err := kv.List("/", nil)
	if err != nil {
		panic(err)
	}

	sort.Sort(ByCreateIndex(pairs))

	outstring := ""
	for _, element := range pairs {
		encoded_value := base64.StdEncoding.EncodeToString(element.Value)
		outstring += fmt.Sprintf("%s:%s\n", element.Key, encoded_value)
	}

	file, err := os.Create(outfile)
	if err != nil {
		panic(err)
	}

	if _, err := file.Write([]byte(outstring)[:]); err != nil {
		panic(err)
	}
}
示例#8
0
func (s *ConsulCatalogSuite) SetUpSuite(c *check.C) {
	dockerHost := os.Getenv("DOCKER_HOST")
	if dockerHost == "" {
		// FIXME Handle windows -- see if dockerClient already handle that or not
		dockerHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket)
	}
	// Make sure we can speak to docker
	dockerClient, err := docker.NewClient(dockerHost)
	c.Assert(err, checker.IsNil, check.Commentf("Error connecting to docker daemon"))
	s.dockerClient = dockerClient

	s.createComposeProject(c, "consul_catalog")
	err = s.composeProject.Up()
	c.Assert(err, checker.IsNil, check.Commentf("Error starting project"))

	consul, err := s.GetContainer("integration-test-consul_catalog_consul_1")
	c.Assert(err, checker.IsNil, check.Commentf("Error finding consul container"))

	s.consulIP = consul.NetworkSettings.IPAddress
	config := api.DefaultConfig()
	config.Address = s.consulIP + ":8500"
	consulClient, err := api.NewClient(config)
	if err != nil {
		c.Fatalf("Error creating consul client")
	}
	s.consulClient = consulClient

	// Wait for consul to elect itself leader
	time.Sleep(2000 * time.Millisecond)
}
示例#9
0
func verifyConsulUp(timeout string) error {
	timeoutDur, err := time.ParseDuration(timeout)
	if err != nil {
		return err
	}
	if timeoutDur == 0 {
		return nil
	}

	config := api.DefaultConfig()
	config.Token = *consulToken
	client, err := api.NewClient(config)
	if err != nil {
		return util.Errorf("Could not construct consul client: '%s'", err)
	}
	consulIsUp := make(chan struct{})
	go func() {
		for {
			time.Sleep(200 * time.Millisecond)
			err := Ping(client)
			if err == nil {
				consulIsUp <- struct{}{}
				return
			}
		}
	}()
	select {
	case <-time.After(timeoutDur):
		return util.Errorf("Consul did not start or was not available after %v", timeoutDur)
	case <-consulIsUp:
		return nil
	}
}
示例#10
0
func TestTaskWatcher(t *testing.T) {
	// Create a server
	srv := NewTestServer(t)
	defer srv.Stop()

	conf := api.DefaultConfig()
	conf.Address = srv.HTTPAddr
	kv := NewSimpleKV(t, conf)
	coord := NewConsulCoordinator(func(conf *ConsulConfig) {
		conf.Address = srv.HTTPAddr
	}).(*consulCoordinator)
	newc, errc := coord.WatchTasks(nil)
	add := []StreamID{
		"task1",
		"task2",
		"task3",
	}
	remove := []StreamID{
		"task1",
		"task2",
	}
	expected := [][]StreamID{
		{},
		add[:1],
		add[:2],
		add[:3],
		add[1:],
		add[2:],
	}
	go func() {
		for _, k := range add {
			kv.Put(taskPrefix+string(k), nil)
		}
		for _, k := range remove {
			kv.Delete(taskPrefix + string(k))
		}
	}()
	var actual [][]StreamID
	timeout := time.NewTimer(2 * time.Second)

	for {
		select {
		case <-timeout.C:
			t.Fatalf("Timeout!")
		case k := <-newc:
			t.Log("new set:", k)
			ks := make([]StreamID, len(k), len(k))
			for i := range k {
				ks[i] = StreamID(k[i])
			}
			actual = append(actual, ks)
			if len(actual) == len(expected) {
				assert.Equal(t, expected, actual)
				return
			}
		case err := <-errc:
			t.Fatalf(err.Error())
		}
	}
}
示例#11
0
/* File needs to be in the following format:
   KEY1:VALUE1
   KEY2:VALUE2
*/
func restore(ipaddress string, token string, infile string) {

	config := api.DefaultConfig()
	config.Address = ipaddress
	config.Token = token

	file, err := os.Open(infile)
	if err != nil {
		panic(err)
	}

	data := make([]byte, 100)
	_, err = file.Read(data)
	if err != nil && err != io.EOF {
		panic(err)
	}

	client, _ := api.NewClient(config)
	kv := client.KV()

	for _, element := range strings.Split(string(data), "\n") {
		kvp := strings.Split(element, ":")

		if len(kvp) > 1 {
			p := &api.KVPair{Key: kvp[0], Value: []byte(kvp[1])}
			_, err := kv.Put(p, nil)
			if err != nil {
				panic(err)
			}
		}
	}
}
示例#12
0
// InitializeConsul creates a new Consul client given
// a list of endpoints and optional tls config
func InitializeConsul(endpoints []string, options *store.Config) (store.Store, error) {
	s := &Consul{}

	// Create Consul client
	config := api.DefaultConfig()
	s.config = config
	config.HttpClient = http.DefaultClient
	config.Address = endpoints[0]
	config.Scheme = "http"

	// Set options
	if options != nil {
		if options.TLS != nil {
			s.setTLS(options.TLS)
		}
		if options.ConnectionTimeout != 0 {
			s.setTimeout(options.ConnectionTimeout)
		}
		if options.EphemeralTTL != 0 {
			s.setEphemeralTTL(options.EphemeralTTL)
		}
	}

	// Creates a new client
	client, err := api.NewClient(config)
	if err != nil {
		log.Errorf("Couldn't initialize consul client..")
		return nil, err
	}
	s.client = client

	return s, nil
}
示例#13
0
/*
GetWriteMasterIP returns the write master IP for the service cluster.
*/
func (s *Service) GetWriteMasterIP() (ip string, err error) {
	log.Trace(fmt.Sprintf(`services.Service<%s>#GetWriteMaster()`, s.Name))
	client, err := consulapi.NewClient(consulapi.DefaultConfig())
	if err != nil {
		log.Error(fmt.Sprintf("services.Service<%s>#GetWriteMaster() ! %s", s.Name, err))
		return
	}
	catalog := client.Catalog()

	clusterID := os.Getenv("RDPGD_CLUSTER")
	if clusterID == "" {
		matrixName := os.Getenv(`RDPGD_MATRIX`)
		matrixNameSplit := strings.SplitAfterN(matrixName, `-`, -1)
		matrixColumn := os.Getenv(`RDPGD_MATRIX_COLUMN`)
		for i := 0; i < len(matrixNameSplit)-1; i++ {
			clusterID = clusterID + matrixNameSplit[i]
		}
		clusterID = clusterID + "c" + matrixColumn
	}
	svcs, _, err := catalog.Service(fmt.Sprintf(`%s-master`, clusterID), "", nil)

	if err != nil {
		log.Error(fmt.Sprintf(`services.Service<%s>#GetWriteMaster() ! %s`, s.Name, err))
		return
	}

	if len(svcs) == 0 {
		return "", nil
	}
	ip = svcs[0].Address
	return
}
示例#14
0
func getServiceDirectory() ServiceDirectory {
	// Lookup running services in Consul
	directory := ServiceDirectory{}
	client, _ := consulApi.NewClient(consulApi.DefaultConfig())
	agent := client.Agent()
	services, err := agent.Services()
	if err != nil {
		log.Println(err)
		panic(err)
	}

	// Find all service proxies
	for _, s := range services {
		// We assume that if there is exactly one tag, then it's the tag for our dev env
		if len(s.Tags) != 1 {
			continue
		}
		envName := s.Tags[0]

		log.Println("env:", envName)
		log.Println("service:", s.Service)
		log.Println("address:", s.Address)
		log.Println("port:", s.Port)
		log.Println()

		directory[envName] = append(directory[envName], s)
	}

	return directory
}
示例#15
0
文件: consul.go 项目: MiLk/swarm
// New creates a new Consul client given a list
// of endpoints and optional tls config
func New(endpoints []string, options *store.Config) (store.Store, error) {
	if len(endpoints) > 1 {
		return nil, ErrMultipleEndpointsUnsupported
	}

	s := &Consul{}

	// Create Consul client
	config := api.DefaultConfig()
	s.config = config
	config.HttpClient = http.DefaultClient
	config.Address = endpoints[0]
	config.Scheme = "http"

	// Set options
	if options != nil {
		if options.TLS != nil {
			s.setTLS(options.TLS)
		}
		if options.ConnectionTimeout != 0 {
			s.setTimeout(options.ConnectionTimeout)
		}
	}

	// Creates a new client
	client, err := api.NewClient(config)
	if err != nil {
		return nil, err
	}
	s.client = client

	return s, nil
}
示例#16
0
func NewSource(opts ...config.SourceOption) config.Source {
	options := config.SourceOptions{
		Name: DefaultPath,
	}

	for _, o := range opts {
		o(&options)
	}

	// use default config
	config := api.DefaultConfig()

	// check if there are any addrs
	if len(options.Hosts) > 0 {
		addr, port, err := net.SplitHostPort(options.Hosts[0])
		if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
			port = "8500"
			addr = options.Hosts[0]
			config.Address = fmt.Sprintf("%s:%s", addr, port)
		} else if err == nil {
			config.Address = fmt.Sprintf("%s:%s", addr, port)
		}
	}

	// create the client
	client, _ := api.NewClient(config)

	return &consul{
		addr:   config.Address,
		opts:   options,
		client: client,
	}
}
示例#17
0
// WorkLock - Acquire consul for cluster to aquire right to schedule tasks.
func WorkLock() (err error) {
	clusterID := os.Getenv("RDPGD_CLUSTER")
	if clusterID == "" {
		matrixName := os.Getenv(`RDPGD_MATRIX`)
		matrixNameSplit := strings.SplitAfterN(matrixName, `-`, -1)
		matrixColumn := os.Getenv(`RDPGD_MATRIX_COLUMN`)
		for i := 0; i < len(matrixNameSplit)-1; i++ {
			clusterID = clusterID + matrixNameSplit[i]
		}
		clusterID = clusterID + "c" + matrixColumn
	}

	key := fmt.Sprintf("rdpg/%s/tasks/work/lock", clusterID)
	client, _ := consulapi.NewClient(consulapi.DefaultConfig())
	workLock, err = client.LockKey(key)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.WorkLock() Error Locking Work Key %s ! %s", key, err))
		return
	}

	workLockCh, err = workLock.Lock(nil) // Acquire Consul K/V Lock
	if err != nil {
		log.Error(fmt.Sprintf("tasks.WorkLock() Error Acquiring Work Key lock %s ! %s", key, err))
		return
	}

	if workLockCh == nil {
		err = fmt.Errorf(`tasks.WorkLock() Work Lock not acquired`)
	}

	return
}
示例#18
0
func (consulSource *ConsulSource) Get() (map[string]interface{}, error) {
	config := consul.DefaultConfig()

	config.Address = consulSource.Address

	if consulSource.Scheme != "" {
		config.Scheme = consulSource.Scheme
	}

	client, err := consul.NewClient(config)
	if err != nil {
		return nil, err
	}

	pairs, _, err := client.KV().List(consulSource.Prefix, nil)
	if err != nil {
		return nil, err
	}

	result := make(map[string]interface{})
	for _, pair := range pairs {
		parts := strings.Split(pair.Key, "/")
		result[parts[len(parts)-1]] = string(pair.Value)
	}

	return result, nil
}
示例#19
0
func NewConsulClient() (ConsulClient, error) {
	client, err := consul.NewClient(consul.DefaultConfig())
	if err != nil {
		return ConsulClient{}, err
	}
	return ConsulClient{client: client}, nil
}
示例#20
0
/* File needs to be in the following format:
   KEY1:VALUE1
   KEY2:VALUE2
*/
func restoreKv(ipaddress string, token string, infile string) {

	config := api.DefaultConfig()
	config.Address = ipaddress
	config.Token = token

	data, err := ioutil.ReadFile(infile)
	if err != nil {
		panic(err)
	}

	client, _ := api.NewClient(config)
	kv := client.KV()

	for _, element := range strings.Split(string(data), "\n") {
		split := strings.Split(element, ":")
		key := strings.Join(split[:len(split)-1], ":")
		value := split[len(split)-1]

		if key != "" {
			decoded_value, decode_err := base64.StdEncoding.DecodeString(value)
			if decode_err != nil {
				panic(decode_err)
			}

			p := &api.KVPair{Key: key, Value: decoded_value}
			_, err := kv.Put(p, nil)
			if err != nil {
				panic(err)
			}
		}
	}
}
示例#21
0
func backupAcls(ipaddress string, token string, outfile string) {

	config := api.DefaultConfig()
	config.Address = ipaddress
	config.Token = token

	client, _ := api.NewClient(config)
	acl := client.ACL()

	tokens, _, err := acl.List(nil)
	if err != nil {
		panic(err)
	}
	// sort.Sort(ByCreateIndex(tokens))

	outstring := ""
	for _, element := range tokens {
		// outstring += fmt.Sprintf("%s:%s:%s:%s\n", element.ID, element.Name, element.Type, element.Rules)
		outstring += fmt.Sprintf("====\nID: %s\nName: %s\nType: %s\nRules:\n%s\n", element.ID, element.Name, element.Type, element.Rules)
	}

	file, err := os.Create(outfile)
	if err != nil {
		panic(err)
	}

	if _, err := file.Write([]byte(outstring)[:]); err != nil {
		panic(err)
	}
}
示例#22
0
文件: service.go 项目: jen20/cascade
func serviceList(c cli.Command) {
	client, _ := api.NewClient(api.DefaultConfig())
	catalog := client.Catalog()

	services, meta, err := catalog.Services(nil)

	if err != nil {
		log.Fatalln("Err:", err)
	}

	if meta.LastIndex == 0 {
		log.Fatalln("Bad: ", meta)
	}

	sorted := make([]string, 0)

	for index, _ := range services {
		sorted = append(sorted, index)
	}

	sort.Strings(sorted)

	for _, service := range sorted {
		fmt.Println("  -", service)
	}
}
示例#23
0
func (c *Consul) createAPIClient() (*api.Client, error) {
	config := api.DefaultConfig()

	if c.Address != "" {
		config.Address = c.Address
	}

	if c.Scheme != "" {
		config.Scheme = c.Scheme
	}

	if c.Datacentre != "" {
		config.Datacenter = c.Datacentre
	}

	if c.Username != "" {
		config.HttpAuth = &api.HttpBasicAuth{
			Username: c.Username,
			Password: c.Password,
		}
	}

	tlsCfg, err := internal.GetTLSConfig(
		c.SSLCert, c.SSLKey, c.SSLCA, c.InsecureSkipVerify)

	if err != nil {
		return nil, err
	}

	config.HttpClient.Transport = &http.Transport{
		TLSClientConfig: tlsCfg,
	}

	return api.NewClient(config)
}
示例#24
0
func lookupConsulServices() ([]consulapi.CatalogService, error) {
	// Get a consul client
	client, err := consulapi.NewClient(consulapi.DefaultConfig())
	if err != nil {
		return []consulapi.CatalogService{}, err
	}

	// Lookup catalog of all service names
	catalog := client.Catalog()
	services, _, err := catalog.Services(nil)
	if err != nil {
		return []consulapi.CatalogService{}, err
	}

	// Find all instances of each service
	output := []consulapi.CatalogService{}
	fmt.Println("consul services:")
	for serviceName, _ := range services {
		catalogServices, _, err := catalog.Service(serviceName, "", nil)
		if err != nil {
			return []consulapi.CatalogService{}, err
		}

		fmt.Println(serviceName)
		for _, cs := range catalogServices {
			output = append(output, *cs)
			fmt.Printf("%#v\n", cs)
		}
	}
	return output, nil
}
示例#25
0
func TestConsulBackend(t *testing.T) {
	addr := os.Getenv("CONSUL_HTTP_ADDR")
	if addr == "" {
		t.Skipf("No consul process running, skipping test")
	}

	conf := api.DefaultConfig()
	conf.Address = addr
	client, err := api.NewClient(conf)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
	defer func() {
		client.KV().DeleteTree(randPath, nil)
	}()

	logger := log.New(os.Stderr, "", log.LstdFlags)
	b, err := NewBackend("consul", logger, map[string]string{
		"address":      addr,
		"path":         randPath,
		"max_parallel": "256",
	})
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	testBackend(t, b)
	testBackend_ListPrefix(t, b)
}
示例#26
0
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
	config := consulapi.DefaultConfig()
	if uri.Scheme == "consul-unix" {
		config.Address = strings.TrimPrefix(uri.String(), "consul-")
	} else if uri.Scheme == "consul-tls" {
		tlsConfigDesc := &consulapi.TLSConfig{
			Address:            uri.Host,
			CAFile:             os.Getenv("CONSUL_CACERT"),
			CertFile:           os.Getenv("CONSUL_TLSCERT"),
			KeyFile:            os.Getenv("CONSUL_TLSKEY"),
			InsecureSkipVerify: false,
		}
		tlsConfig, err := consulapi.SetupTLSConfig(tlsConfigDesc)
		if err != nil {
			log.Fatal("Cannot set up Consul TLSConfig", err)
		}
		config.Scheme = "https"
		transport := cleanhttp.DefaultPooledTransport()
		transport.TLSClientConfig = tlsConfig
		config.HttpClient.Transport = transport
		config.Address = uri.Host
	} else if uri.Host != "" {
		config.Address = uri.Host
	}
	client, err := consulapi.NewClient(config)
	if err != nil {
		log.Fatal("consul: ", uri.Scheme)
	}
	return &ConsulAdapter{client: client}
}
示例#27
0
文件: consul.go 项目: postfix/golib-1
// Initialize is exported
func (s *Discovery) Initialize(uris string, heartbeat uint64) error {
	parts := strings.SplitN(uris, "/", 2)
	if len(parts) < 2 {
		return fmt.Errorf("invalid format %q, missing <path>", uris)
	}
	addr := parts[0]
	path := parts[1]

	config := consul.DefaultConfig()
	config.Address = addr

	client, err := consul.NewClient(config)
	if err != nil {
		return err
	}
	s.client = client
	s.heartbeat = time.Duration(heartbeat) * time.Second
	s.prefix = path + "/"
	kv := s.client.KV()
	p := &consul.KVPair{Key: s.prefix, Value: nil}
	if _, err = kv.Put(p, nil); err != nil {
		return err
	}
	_, meta, err := kv.Get(s.prefix, nil)
	if err != nil {
		return err
	}
	s.lastIndex = meta.LastIndex
	return nil
}
示例#28
0
func getMasterIP(clusterName string) (masterIp string, err error) {

	log.Trace(fmt.Sprintf("gpb#consul.getMasterIP() Calling out to Consul at address %s", mcConsulIP))

	consulConfig := consulapi.DefaultConfig()
	consulConfig.Address = mcConsulIP
	consulClient, err := consulapi.NewClient(consulConfig)
	if err != nil {
		log.Error(fmt.Sprintf(`gpb#consul.getMasterIP() Consul IP: %s ! %s`, mcConsulIP, err))
		return
	}

	masterNode, _, err := consulClient.Catalog().Service(fmt.Sprintf(`%s-master`, clusterName), "", nil)
	if err != nil {
		log.Error(fmt.Sprintf("gpb#consul.getMasterIP() Cluster Name: %s ! %s", clusterName, err))
		return
	}

	if len(masterNode) == 0 {
		masterIp = "0.0.0.0"
		return masterIp, errors.New("Could not find the consul master ip")
	}

	masterIp = masterNode[0].Address
	log.Trace(fmt.Sprintf("gpb#consul.getMasterIP() Found master ip for %s = %s", clusterName, masterIp))
	return masterIp, err

}
示例#29
0
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *ConsulCatalog) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
	config := api.DefaultConfig()
	config.Address = provider.Endpoint
	client, err := api.NewClient(config)
	if err != nil {
		return err
	}
	provider.client = client
	provider.Constraints = append(provider.Constraints, constraints...)

	pool.Go(func(stop chan bool) {
		notify := func(err error, time time.Duration) {
			log.Errorf("Consul connection error %+v, retrying in %s", err, time)
		}
		worker := func() error {
			return provider.watch(configurationChan, stop)
		}
		err := backoff.RetryNotify(worker, backoff.NewExponentialBackOff(), notify)
		if err != nil {
			log.Fatalf("Cannot connect to consul server %+v", err)
		}
	})

	return err
}
示例#30
0
文件: config.go 项目: ycaihua/gizmo
// LoadJSONFromConsulKV is a helper function to read a JSON string found
// in a path defined by configKey inside Consul's Key Value storage then
// unmarshalled into a config struct, like LoadJSONFile does.
// It assumes that the Consul agent is running with the default setup,
// where the HTTP API is found via 127.0.0.1:8500.
func LoadJSONFromConsulKV(configKeyParameter string, cfg interface{}) interface{} {
	configKeyParameterValue := strings.SplitN(configKeyParameter, ":", 2)
	if len(configKeyParameterValue) < 2 {
		log.Fatalf("Undefined Consul KV configuration path. It should be defined using the format consul:path/to/JSON/string")
	}
	configKey := configKeyParameterValue[1]
	client, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		log.Fatalf("Unable to setup Consul client: %s", err)
	}
	kv := client.KV()
	kvPair, _, err := kv.Get(configKey, nil)
	if err != nil {
		log.Fatalf("Unable to read config in key '%s' from Consul KV: %s", configKey, err)
	}
	if kvPair == nil {
		log.Fatalf("Undefined key '%s' in Consul KV", configKey)
	}
	if len(kvPair.Value) == 0 {
		log.Fatalf("Empty JSON in Consul KV for key '%s'", configKey)
	}
	if err = json.Unmarshal(kvPair.Value, &cfg); err != nil {
		log.Fatalf("Unable to parse JSON in Consul KV for key '%s': %s", configKey, err)
	}
	return cfg
}