當前位置: 首頁>>代碼示例>>Golang>>正文


Golang KeysAPI.Get方法代碼示例

本文整理匯總了Golang中github.com/coreos/etcd/client.KeysAPI.Get方法的典型用法代碼示例。如果您正苦於以下問題:Golang KeysAPI.Get方法的具體用法?Golang KeysAPI.Get怎麽用?Golang KeysAPI.Get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/coreos/etcd/client.KeysAPI的用法示例。


在下文中一共展示了KeysAPI.Get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Store

func Store(api etcd.KeysAPI, path string, ttl time.Duration) frameworkid.Storage {
	// TODO(jdef) validate Config
	return &storage{
		LookupFunc: func(ctx context.Context) (string, error) {
			if response, err := api.Get(ctx, path, nil); err != nil {
				if !etcdutil.IsEtcdNotFound(err) {
					return "", fmt.Errorf("unexpected failure attempting to load framework ID from etcd: %v", err)
				}
			} else {
				return response.Node.Value, nil
			}
			return "", nil
		},
		RemoveFunc: func(ctx context.Context) (err error) {
			if _, err = api.Delete(ctx, path, &etcd.DeleteOptions{Recursive: true}); err != nil {
				if !etcdutil.IsEtcdNotFound(err) {
					return fmt.Errorf("failed to delete framework ID from etcd: %v", err)
				}
			}
			return
		},
		StoreFunc: func(ctx context.Context, id string) (err error) {
			_, err = api.Set(ctx, path, id, &etcd.SetOptions{TTL: ttl})
			return
		},
	}
}
開發者ID:CodeJuan,項目名稱:kubernetes,代碼行數:27,代碼來源:etcd.go

示例2: mustGetServices

func mustGetServices(ctx context.Context, client etcd.KeysAPI, basepath *string) services {
	resp, err := client.Get(ctx, *basepath, &etcd.GetOptions{Recursive: true})
	if err != nil {
		log.WithFields(log.Fields{
			"error":    err,
			"basepath": *basepath,
		}).Fatal("unable to get service definitions from etcd")
	}

	var svcs services
	for _, node := range resp.Node.Nodes {
		s, err := newService(node.Key, []byte(node.Value))
		if err != nil {
			log.WithFields(log.Fields{
				"error":    err,
				"basepath": *basepath,
				"key":      node.Key,
			}).Warn("invalid service definition. skipping.")
		} else {
			svcs = append(svcs, s)
		}
	}

	return svcs
}
開發者ID:christian-blades-cb,項目名稱:desoto,代碼行數:25,代碼來源:main.go

示例3: keysToLocal

// keysToLocal copies SSH host keys from etcd to the local file system.
//
// This only fails if the main key, sshHostKey cannot be stored or retrieved.
func keysToLocal(c cookoo.Context, k client.KeysAPI, ciphers []string, etcdPath string) error {
	lpath := "/etc/ssh/ssh_host_%s_key"
	privkey := "%s/sshHost%sKey"
	for _, cipher := range ciphers {
		path := fmt.Sprintf(lpath, cipher)
		key := fmt.Sprintf(privkey, etcdPath, cipher)
		res, err := k.Get(dctx(), key, &client.GetOptions{})
		if err != nil || res.Node == nil {
			continue
		}

		content := res.Node.Value
		if err := ioutil.WriteFile(path, []byte(content), 0600); err != nil {
			log.Errf(c, "Error writing ssh host key file: %s", err)
		}
	}

	// Now get generic key.
	res, err := k.Get(dctx(), "sshHostKey", &client.GetOptions{})
	if err != nil || res.Node == nil {
		return fmt.Errorf("Failed to get sshHostKey from etcd. %v", err)
	}

	content := res.Node.Value
	if err := ioutil.WriteFile("/etc/ssh/ssh_host_key", []byte(content), 0600); err != nil {
		log.Errf(c, "Error writing ssh host key file: %s", err)
		return err
	}
	return nil
}
開發者ID:helgi,項目名稱:pkg,代碼行數:33,代碼來源:etcd.go

示例4: cleanBackends

func cleanBackends(kapi client.KeysAPI) {

	resp, err := kapi.Get(context.Background(), "/vulcand/backends/", &client.GetOptions{Recursive: true})
	if err != nil {
		if e, _ := err.(client.Error); e.Code == etcderr.EcodeKeyNotFound {
			return
		}
		panic(err)
	}
	if !resp.Node.Dir {
		log.Println("/vulcand/backends is not a directory.")
		return
	}
	for _, be := range resp.Node.Nodes {
		beHasContent := false
		if be.Dir {
			for _, child := range be.Nodes {
				// anything apart from an empty "servers" dir means this is needed.
				if filepath.Base(child.Key) != "servers" || len(child.Nodes) > 0 {
					beHasContent = true
					break
				}
			}
		}
		if !beHasContent {
			_, err := kapi.Delete(context.Background(), be.Key, &client.DeleteOptions{Recursive: true})
			if err != nil {
				log.Printf("failed to remove unwanted backend %v\n", be.Key)
			}
		}
	}

}
開發者ID:Financial-Times,項目名稱:vulcan-config-builder,代碼行數:33,代碼來源:main.go

示例5: NewPubSubTopicByKey

func NewPubSubTopicByKey(ctx context.Context, keyid string, ttl time.Duration, kapi etcdc.KeysAPI) (*EtcdPubSubTopic, error) {
	_, err := kapi.Get(ctx, keyid, nil)
	if IsKeyNotFound(err) {
		opt := &etcdc.SetOptions{PrevExist: etcdc.PrevNoExist, TTL: ttl, Dir: true}
		_, err = kapi.Set(ctx, keyid, "", opt)
		if err != nil && !IsCompareAndSwapFailure(err) && !IsNodeExists(err) {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	}

	keepalive, err := NewNodeKeepAlive(keyid, ttl, kapi)
	if err != nil {
		return nil, err
	}

	dlog("signal: new signal(pub/sub) for %v, ttl:%v", keyid, ttl)

	return &EtcdPubSubTopic{
		ctx:       ctx,
		keyid:     keyid,
		kapi:      kapi,
		ttl:       ttl,
		keepalive: keepalive,
		stop:      make(chan bool),
	}, nil
}
開發者ID:lytics,項目名稱:sereno,代碼行數:28,代碼來源:signal.go

示例6: generateConfig

func generateConfig(kapi client.KeysAPI) (*client.Response, error) {
	resp, err := kapi.Get(context.Background(), *etcdPrefix, &client.GetOptions{Recursive: true})
	if err != nil {
		return resp, err
	}
	traverseConfigDirectory(resp.Node)
	return resp, nil
}
開發者ID:themecloud,項目名稱:etcd2envfile,代碼行數:8,代碼來源:etcd2envfile.go

示例7: ClientGet

// ClientGet gets data from etcd sending in an url and receiving a etcd.Response object
func ClientGet(kapi client.KeysAPI, url string) *client.Response {
	resp, err := kapi.Get(context.Background(), url, &clientGetOpts)
	if err != nil {
		logr.LogLine(logr.Lfatal, ltagsrc, err.Error())
		os.Exit(2)
	}
	return resp
}
開發者ID:tcotav,項目名稱:etcdhooks,代碼行數:9,代碼來源:etcdWatcher.go

示例8: doDumpQueue

func doDumpQueue(kapi client.KeysAPI) {
	var key = queuebase + *dumpqueuename

	resp, err := kapi.Get(context.TODO(), key, &client.GetOptions{Sort: true})

	if err != nil {
		log.Fatal(err)
	}
	for _, v := range resp.Node.Nodes {
		fmt.Println(v.Key + " set to " + v.Value)
	}
}
開發者ID:compose-ex,項目名稱:examplco3,代碼行數:12,代碼來源:doDumpQueue.go

示例9: dirExists

func dirExists(dir string, c *cli.Context, ki client.KeysAPI) (bool, error) {
	ctx, cancel := contextWithCommandTimeout(c)
	_, err := ki.Get(ctx, dir, &client.GetOptions{})
	cancel()
	if err != nil {
		if cerr, ok := err.(client.Error); ok && cerr.Code == 100 {
			return false, nil
		}
		return false, err
	}
	return true, nil
}
開發者ID:mickep76,項目名稱:etcdtool,代碼行數:12,代碼來源:import_command.go

示例10: isDir

func isDir(dir string, c *cli.Context, ki client.KeysAPI) (bool, error) {
	ctx, cancel := contextWithCommandTimeout(c)
	resp, err := ki.Get(ctx, dir, &client.GetOptions{})
	cancel()
	if err != nil {
		return false, err
	}
	if resp.Node.Dir {
		return false, nil
	}
	return true, nil
}
開發者ID:mickep76,項目名稱:etcdtool,代碼行數:12,代碼來源:import_command.go

示例11: readServices

func readServices(kapi client.KeysAPI) []Service {
	resp, err := kapi.Get(context.Background(), "/ft/services/", &client.GetOptions{Recursive: true})
	if err != nil {
		log.Println("error reading etcd keys")
		if e, _ := err.(client.Error); e.Code == etcderr.EcodeKeyNotFound {
			log.Println("core key not found")
			return []Service{}
		}
		log.Panicf("failed to read from etcd: %v\n", err.Error())
	}
	if !resp.Node.Dir {
		log.Panicf("%v is not a directory", resp.Node.Key)
	}

	var services []Service
	for _, node := range resp.Node.Nodes {
		if !node.Dir {
			log.Printf("skipping non-directory %v\n", node.Key)
			continue
		}
		service := Service{
			Name:         filepath.Base(node.Key),
			Addresses:    make(map[string]string),
			PathPrefixes: make(map[string]string),
			PathHosts:    make(map[string]string),
		}
		for _, child := range node.Nodes {
			switch filepath.Base(child.Key) {
			case "healthcheck":
				service.HasHealthCheck = child.Value == "true"
			case "servers":
				for _, server := range child.Nodes {
					service.Addresses[filepath.Base(server.Key)] = server.Value
				}
			case "path-regex":
				for _, path := range child.Nodes {
					service.PathPrefixes[filepath.Base(path.Key)] = path.Value
				}
			case "path-host":
				for _, path := range child.Nodes {
					service.PathHosts[filepath.Base(path.Key)] = path.Value
				}
			case "failover-predicate":
				service.FailoverPredicate = child.Value
			default:
				fmt.Printf("skipped key %v for node %v\n", child.Key, child)
			}
		}
		services = append(services, service)
	}
	return services
}
開發者ID:Financial-Times,項目名稱:vulcan-config-builder,代碼行數:52,代碼來源:main.go

示例12: loadConfig

func loadConfig(client etcd.KeysAPI, config *server.Config) error {
	// Override what isn't set yet from the command line.
	configPath := "/" + msg.PathPrefix + "/config"
	resp, err := client.Get(ctx, configPath, nil)
	if err != nil {
		log.Printf("skydns: falling back to default configuration, could not read from etcd: %s", err)
		return nil
	}
	if err := json.Unmarshal([]byte(resp.Node.Value), config); err != nil {
		return fmt.Errorf("failed to unmarshal config: %s", err.Error())
	}
	return nil
}
開發者ID:unclejack,項目名稱:skydns,代碼行數:13,代碼來源:main.go

示例13: readAllKeysFromEtcd

func readAllKeysFromEtcd(kapi client.KeysAPI, root string) (map[string]string, error) {
	m := make(map[string]string)

	resp, err := kapi.Get(context.Background(), root, &client.GetOptions{Recursive: true})
	if err != nil {
		if e, _ := err.(client.Error); e.Code == etcderr.EcodeKeyNotFound {
			return m, nil
		}
		panic(err)
	}
	addAllValuesToMap(m, resp.Node)
	return m, nil
}
開發者ID:lucmichalski,項目名稱:vulcan-config-builder,代碼行數:13,代碼來源:main.go

示例14: get

func get(kapi client.KeysAPI, key string, fn callback) {
	resp, err := kapi.Get(context.TODO(), key, nil)

	if err != nil {
		log.Printf("!! ERR: %v\n", err)
		return
	}

	if err := fn(resp.Node); err != nil {
		log.Printf("!! ERR: %v\n", err)
		log.Printf("!! Calling get again")
		get(kapi, key, fn)
	}
}
開發者ID:cyx,項目名稱:hackday,代碼行數:14,代碼來源:main.go

示例15: fetchConfigSection

//Fetches a single config section
func fetchConfigSection(configStruct interface{}, location string, kapi etcd.KeysAPI) {
	cfg := reflect.ValueOf(configStruct).Elem()
	for i := 0; i < cfg.NumField(); i++ {
		key := cfg.Type().Field(i).Name
		resp, getErr := kapi.Get(context.Background(), location+key, nil)
		if getErr != nil {
			log.Printf("Error getting key %v: %v\n", key, getErr)
			continue
		}
		valErr := setConfigVal(resp.Node.Value, cfg.Field(i))
		if valErr != nil {
			log.Printf("Error setting config field %v: %v\n", key, valErr)
		}
	}
}
開發者ID:rhinoman,項目名稱:wikifeat,代碼行數:16,代碼來源:config_etcd.go


注:本文中的github.com/coreos/etcd/client.KeysAPI.Get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。