本文整理汇总了Golang中github.com/square/p2/pkg/kp.NewConsulClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewConsulClient函数的具体用法?Golang NewConsulClient怎么用?Golang NewConsulClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewConsulClient函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
kingpin.Version(version.VERSION)
_, opts := flags.ParseWithConsulOptions()
client := kp.NewConsulClient(opts)
store := kp.NewConsulStore(client)
if *nodeName == "" {
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("Could not get the hostname to do scheduling: %s", err)
}
*nodeName = hostname
}
if len(*manifests) == 0 {
kingpin.Usage()
log.Fatalln("No manifests given")
}
for _, manifestPath := range *manifests {
manifest, err := pods.ManifestFromPath(manifestPath)
if err != nil {
log.Fatalf("Could not read manifest at %s: %s\n", manifestPath, err)
}
path := kp.IntentPath(*nodeName, manifest.ID())
if *hookGlobal {
path = kp.HookPath(manifest.ID())
}
duration, err := store.SetPod(path, manifest)
if err != nil {
log.Fatalf("Could not write manifest %s to intent store: %s\n", manifest.ID(), err)
}
log.Printf("Scheduling %s took %s\n", manifest.ID(), duration)
}
}
开发者ID:tomzhang,项目名称:p2,代码行数:35,代码来源:main.go
示例2: main
func main() {
quitCh := make(chan struct{})
_, consulOpts, labeler := flags.ParseWithConsulOptions()
client := kp.NewConsulClient(consulOpts)
logger := logging.NewLogger(logrus.Fields{})
dsStore := dsstore.NewConsul(client, 3, &logger)
kpStore := kp.NewConsulStore(client)
healthChecker := checker.NewConsulHealthChecker(client)
sessions := make(chan string)
go consulutil.SessionManager(api.SessionEntry{
Name: SessionName(),
LockDelay: 5 * time.Second,
Behavior: api.SessionBehaviorDelete,
TTL: "15s",
}, client, sessions, quitCh, logger)
dsf := ds_farm.NewFarm(kpStore, dsStore, labeler, labels.NewConsulApplicator(client, 0), sessions, logger, nil, &healthChecker, 1*time.Second, *useCachePodMatches)
go func() {
// clear lock immediately on ctrl-C
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
<-signals
close(quitCh)
}()
dsf.Start(quitCh)
}
开发者ID:petertseng,项目名称:p2,代码行数:30,代码来源:main.go
示例3: VerifyConsulUp
func VerifyConsulUp(timeout string) error {
timeoutDur, err := time.ParseDuration(timeout)
if err != nil {
return err
}
if timeoutDur == 0 {
return nil
}
store := kp.NewConsulStore(kp.NewConsulClient(kp.Options{
Token: *consulToken, // not actually necessary because this endpoint is unauthenticated
}))
consulIsUp := make(chan struct{})
go func() {
for {
time.Sleep(200 * time.Millisecond)
err := store.Ping()
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
}
}
开发者ID:tomzhang,项目名称:p2,代码行数:30,代码来源:bootstrap.go
示例4: VerifyReality
func VerifyReality(waitTime time.Duration, consulID, agentID string) error {
quit := make(chan struct{})
defer close(quit)
store := kp.NewConsulStore(kp.NewConsulClient(kp.Options{
Token: *consulToken,
}))
hostname, _ := os.Hostname()
waitChan := time.After(waitTime)
hasConsul := false
hasPreparer := false
for {
select {
case <-waitChan:
return util.Errorf(
"Consul and/or Preparer weren't in the reality store within %s (consul=%t, preparer=%t)",
waitTime, hasConsul, hasPreparer)
case <-time.After(100 * time.Millisecond):
results, _, err := store.ListPods(kp.RealityPath(hostname))
if err != nil {
log.Printf("Error looking for pods: %s\n", err)
continue
}
for _, res := range results {
if res.Manifest.ID() == consulID {
hasConsul = true
} else if res.Manifest.ID() == agentID {
hasPreparer = true
}
}
if hasConsul && hasPreparer {
return nil
}
}
}
}
开发者ID:tomzhang,项目名称:p2,代码行数:35,代码来源:bootstrap.go
示例5: GetStore
// GetStore constructs a key-value store from the given configuration.
func (c *PreparerConfig) GetStore() (kp.Store, error) {
opts, err := c.getOpts()
if err != nil {
return nil, err
}
client := kp.NewConsulClient(opts)
return kp.NewConsulStore(client), nil
}
开发者ID:drcapulet,项目名称:p2,代码行数:9,代码来源:setup.go
示例6: GetConsulClient
func (c *PreparerConfig) GetConsulClient() (consulutil.ConsulClient, error) {
c.mux.Lock()
defer c.mux.Unlock()
if c.consulClient != nil {
return c.consulClient, nil
}
opts, err := c.getOpts()
if err != nil {
return nil, err
}
client := kp.NewConsulClient(opts)
c.consulClient = client
return client, nil
}
开发者ID:petertseng,项目名称:p2,代码行数:14,代码来源:setup.go
示例7: GetStore
// GetStore constructs a key-value store from the given configuration.
func (c *PreparerConfig) GetStore() (kp.Store, error) {
c.mux.Lock()
defer c.mux.Unlock()
if c.store != nil {
return c.store, nil
}
opts, err := c.getOpts()
if err != nil {
return nil, err
}
store := kp.NewConsulStore(kp.NewConsulClient(opts))
c.store = store
return store, nil
}
开发者ID:rudle,项目名称:p2,代码行数:15,代码来源:setup.go
示例8: main
func main() {
kingpin.Version(version.VERSION)
_, opts := flags.ParseWithConsulOptions()
if *nodeName == "" {
hostname, err := os.Hostname()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting hostname. use --node to specify a node: %v\n", err)
os.Exit(1)
}
*nodeName = hostname
}
rm := NewP2RM(kp.NewConsulClient(opts), *podName, types.NodeName(*nodeName))
podIsManagedByRC, rcID, err := rm.checkForManagingReplicationController()
if err != nil {
os.Exit(2)
}
if !podIsManagedByRC {
err = rm.deletePod()
if err != nil {
os.Exit(2)
}
}
if podIsManagedByRC && !*deallocation {
fmt.Fprintf(
os.Stderr,
"error: %s is managed by replication controller: %s\n"+
"It's possible you meant you deallocate this pod on this node. If so, please confirm your intention with --deallocate\n", *nodeName, rcID)
os.Exit(2)
}
if podIsManagedByRC && *deallocation {
err = rm.decrementDesiredCount(rcID)
if err != nil {
fmt.Fprintf(os.Stderr,
"Encountered error deallocating from the RC %s. You may attempt this command again or use `p2-rctl` to cleanup manually.\n%v",
rcID,
err)
}
}
fmt.Printf("%s: successfully removed %s\n", rm.NodeName, rm.PodName)
}
开发者ID:rudle,项目名称:p2,代码行数:47,代码来源:main.go
示例9: scheduleForThisHost
func scheduleForThisHost(manifest manifest.Manifest, alsoReality bool) error {
store := kp.NewConsulStore(kp.NewConsulClient(kp.Options{
Token: *consulToken,
}))
hostname, err := os.Hostname()
if err != nil {
return err
}
_, err = store.SetPod(kp.INTENT_TREE, types.NodeName(hostname), manifest)
if err != nil {
return err
}
if alsoReality {
_, err = store.SetPod(kp.REALITY_TREE, types.NodeName(hostname), manifest)
return err
}
return nil
}
开发者ID:petertseng,项目名称:p2,代码行数:19,代码来源:bootstrap.go
示例10: ScheduleForThisHost
func ScheduleForThisHost(manifest pods.Manifest, alsoReality bool) error {
store := kp.NewConsulStore(kp.NewConsulClient(kp.Options{
Token: *consulToken,
}))
hostname, err := os.Hostname()
if err != nil {
return err
}
_, err = store.SetPod(kp.IntentPath(hostname, manifest.ID()), manifest)
if err != nil {
return err
}
if alsoReality {
_, err = store.SetPod(kp.RealityPath(hostname, manifest.ID()), manifest)
return err
}
return nil
}
开发者ID:tomzhang,项目名称:p2,代码行数:19,代码来源:bootstrap.go
示例11: TestNewConsul
func TestNewConsul(t *testing.T) {
store := NewConsul(kp.NewConsulClient(kp.Options{}), nil)
rollstore := store.(consulStore)
if rollstore.kv == nil {
t.Fatal("kv should not be nil for constructed rollstore")
}
if rollstore.rcstore == nil {
t.Fatal("rcstore should not be nil for constructed rollstore")
}
if rollstore.labeler == nil {
t.Fatal("labeler should not be nil for constructed rollstore")
}
if rollstore.store == nil {
t.Fatal("store should not be nil for constructed rollstore")
}
}
开发者ID:rudle,项目名称:p2,代码行数:19,代码来源:consul_store_test.go
示例12: waitForPodLabeledWithRC
func waitForPodLabeledWithRC(selector klabels.Selector, rcID fields.ID) error {
client := kp.NewConsulClient(kp.Options{})
applicator := labels.NewConsulApplicator(client, 1)
// we have to label this hostname as being allowed to run tests
host, err := os.Hostname()
if err != nil {
return fmt.Errorf("Could not get hostname: %s", err)
}
err = applicator.SetLabel(labels.NODE, host, "test", "yes")
if err != nil {
return fmt.Errorf("Could not set node selector label on %s: %v", host, err)
}
quitCh := make(chan struct{})
defer close(quitCh)
watchCh := applicator.WatchMatches(selector, labels.POD, quitCh)
waitTime := time.After(30 * time.Second)
for {
select {
case <-waitTime:
return fmt.Errorf("Label selector %v wasn't matched before timeout: %s", selector, targetLogs())
case res, ok := <-watchCh:
if !ok {
return fmt.Errorf("Label selector watch unexpectedly terminated")
}
if len(res) > 1 {
return fmt.Errorf("Too many results found, should only have 1: %v", res)
}
if len(res) == 1 {
_, podID, err := labels.NodeAndPodIDFromPodLabel(res[0])
if err != nil {
return err
}
if podID.String() != "hello" {
return fmt.Errorf("Should have found the hello pod, instead found %s", podID)
}
return nil
}
}
}
}
开发者ID:petertseng,项目名称:p2,代码行数:42,代码来源:check.go
示例13: makeStore
func makeStore(t *testing.T) (kp.Store, *testutil.TestServer) {
if testing.Short() {
t.Skip("skipping test dependendent on consul because of short mode")
}
// testutil.NewTestServerConfig will skip the test if "consul" isn't in the system path.
// We'd rather the test fail.
defer func() {
if t.Skipped() {
t.Fatalf("test skipped by testutil package")
}
}()
// Create server
server := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
// consul output in test output is noisy
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
// If ports are left to their defaults, this test conflicts
// with the test consul servers in pkg/kp
var offset uint64
idx := int(atomic.AddUint64(&offset, 1))
c.Ports = &testutil.TestPortConfig{
DNS: 26000 + idx,
HTTP: 27000 + idx,
RPC: 28000 + idx,
SerfLan: 29000 + idx,
SerfWan: 30000 + idx,
Server: 31000 + idx,
}
})
client := kp.NewConsulClient(kp.Options{
Address: server.HTTPAddr,
})
store := kp.NewConsulStore(client)
return store, server
}
开发者ID:tomzhang,项目名称:p2,代码行数:39,代码来源:common_test_setup.go
示例14: main
func main() {
// Parse custom flags + standard Consul routing options
_, opts, _ := flags.ParseWithConsulOptions()
client := kp.NewConsulClient(opts)
podStore := kp_podstore.NewConsul(client.KV())
podStatusStore := podstatus.NewConsul(statusstore.NewConsul(client), kp.PreparerPodStatusNamespace)
logger := log.New(os.Stderr, "", 0)
port := getPort(logger)
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
logger.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
podstore_protos.RegisterP2PodStoreServer(s, podstore.NewServer(podStore, podStatusStore))
if err := s.Serve(lis); err != nil {
logger.Fatalf("failed to serve: %v", err)
}
}
开发者ID:petertseng,项目名称:p2,代码行数:22,代码来源:server.go
示例15: main
func main() {
// CLI takes a hostname, a token and recursively deletes all pods
// in the reality tree. This is useful if any pods on a host have
// been manually altered in some way and need to be restored to
// a known state.
kingpin.Version(version.VERSION)
_, opts := flags.ParseWithConsulOptions()
client := kp.NewConsulClient(opts)
store := kp.NewConsulStore(client)
pods, _, err := store.ListPods(kp.REALITY_TREE, types.NodeName(*nodeName))
if err != nil {
log.Fatalf("Could not list pods for node %v: %v", *nodeName, err)
}
for _, pod := range pods {
log.Printf("Deleting %v from reality\n", pod.Manifest.ID())
_, err := store.DeletePod(kp.REALITY_TREE, types.NodeName(*nodeName), pod.Manifest.ID())
if err != nil {
log.Fatalf("Could not remove %v from pod reality tree: %v", err)
}
}
}
开发者ID:drcapulet,项目名称:p2,代码行数:23,代码来源:main.go
示例16: main
func main() {
// 1. Generate pod for preparer in this code version (`rake artifact:prepare`)
// 2. Locate manifests for preparer pod, premade consul pod
// 3. Execute bootstrap with premade consul pod and preparer pod
// 4. Delete all pods from the pod store (uuid pods). This allows the same vagrant VM to be used
// between tests
// 5. Deploy p2-rctl-server pod with p2-schedule
// 6. Schedule a hello pod manifest with a replication controller
// 7. Schedule a hello pod as a "uuid pod"
// 8. Verify that p2-rctl-server is running by checking health.
// 9. Verify that the RC-deployed hello is running by checking health.
// Monitor using written pod label queries.
// 10. Verify that the uuid hello pod is running by curling its HTTP port.
// Health is not checked for uuid pods so checking health cannot be used.
// list of services running on integration test host
services := []string{"p2-preparer", "hello"}
tempdir, err := ioutil.TempDir("", "single-node-check")
log.Printf("Putting test manifests in %s\n", tempdir)
if err != nil {
log.Fatalln("Could not create temp directory, bailing")
}
userHookManifest, err := userCreationHookManifest(tempdir)
if err != nil {
log.Fatalf("Couldn't schedule the user creation hook: %s", err)
}
preparerManifest, err := generatePreparerPod(tempdir, userHookManifest)
if err != nil {
log.Fatalf("Could not generate preparer pod: %s\n", err)
}
config, err := preparer.LoadConfig(preparerManifest)
if err != nil {
log.Fatalf("could not unmarshal config: %s\n", err)
}
consulManifest, err := getConsulManifest(tempdir)
if err != nil {
log.Fatalf("Could not generate consul pod: %s\n", err)
}
signedPreparerManifest, err := signManifest(preparerManifest, tempdir)
if err != nil {
log.Fatalf("Could not sign preparer manifest: %s\n", err)
}
signedConsulManifest, err := signManifest(consulManifest, tempdir)
if err != nil {
log.Fatalf("Could not sign consul manifest: %s\n", err)
}
fmt.Println("Executing bootstrap")
err = executeBootstrap(signedPreparerManifest, signedConsulManifest)
if err != nil {
log.Fatalf("Could not execute bootstrap: %s\n%s", err, targetLogs())
}
// Wait a bit for preparer's http server to be ready
err = waitForStatus(preparerStatusPort, "preparer", 10*time.Second)
if err != nil {
log.Fatalf("Couldn't check preparer status: %s", err)
}
consulClient := kp.NewConsulClient(kp.Options{})
// Get all the pod unique keys so we can unschedule them all
keys, _, err := consulClient.KV().Keys(podstore.PodTree+"/", "", nil)
if err != nil {
log.Fatalf("Could not fetch pod keys to remove from store at beginning of test: %s", err)
}
podStore := podstore.NewConsul(consulClient.KV())
for _, key := range keys {
keyParts := strings.Split(key, "/")
err = podStore.Unschedule(types.PodUniqueKey(keyParts[len(keyParts)-1]))
if err != nil {
log.Fatalf("Could not unschedule pod %s from consul: %s", keyParts[len(keyParts)-1], err)
}
}
err = scheduleRCTLServer(tempdir)
if err != nil {
log.Fatalf("Could not schedule RCTL server: %s", err)
}
// Now we're going to test some conditions that each take non-negligible amount of time to verify.
// We'll spin up a goroutine for each "test" which either closes the error channel, or passes an error.
type testCase struct {
testName string
errCh chan error
logger logging.Logger
}
var testCases []testCase
// Test that a "legacy" pod installed by an RC comes up correctly and has health reported
legacyTest := make(chan error)
verifyLegacyPodLogger := logging.DefaultLogger.SubLogger(logrus.Fields{
"test_case": "verifyLegacyPod",
})
go verifyLegacyPod(legacyTest, tempdir, config, services, verifyLegacyPodLogger)
testCases = append(testCases, testCase{
//.........这里部分代码省略.........
开发者ID:petertseng,项目名称:p2,代码行数:101,代码来源:check.go
示例17: verifyProcessExit
//.........这里部分代码省略.........
logger.Infof("Causing hello on 43772 to exit with status %d", exitCode)
// Make an http request to hello to make it exit with exitCode. We expect the http request to fail due
// to the server exiting, so don't check for http errors.
_, err = http.Get(fmt.Sprintf("http://localhost:43772/exit/%d", exitCode))
if err == nil {
// This is bad, it means the hello server didn't die and kill our request
// in the middle
errCh <- util.Errorf("Couldn't kill hello server with http request")
return
}
urlError, ok := err.(*url.Error)
if ok && urlError.Err == io.EOF {
// This is good, it means the server died
} else {
errCh <- fmt.Errorf("Couldn't tell hello to die over http: %s", err)
return
}
logger.Infoln("Checking for exit code in SQL database")
finishService, err := podprocess.NewSQLiteFinishService(sqliteFinishDatabasePath, logging.DefaultLogger)
if err != nil {
errCh <- err
return
}
var finishResult podprocess.FinishOutput
timeout = time.After(30 * time.Second)
for {
finishResult, err = finishService.LastFinishForPodUniqueKey(podUniqueKey)
if err == nil {
break
}
select {
case <-timeout:
// Try to manually run the finish script in order to make debugging the test failure easier
output, err := exec.Command("sudo", fmt.Sprintf("/var/service/hello-%s__hello__launch/finish", podUniqueKey), "1", "2").CombinedOutput()
if err != nil {
logger.WithError(err).Infoln("DEBUG: Debug attempt to run finish script failed")
}
logger.Infof("DEBUG: Output of direct execution of finish script: %s", string(output))
errCh <- fmt.Errorf("Did not find a finish row by the deadline: %s", err)
return
default:
}
}
if finishResult.PodUniqueKey != podUniqueKey {
errCh <- fmt.Errorf("Expected finish result for '%s' but it was for '%s'", podUniqueKey, finishResult.PodUniqueKey)
return
}
if finishResult.ExitCode != exitCode {
errCh <- fmt.Errorf("Exit code for '%s' in the sqlite database was expected to be %d but was %d", podUniqueKey, exitCode, finishResult.ExitCode)
return
}
logger.Infoln("Checking for exit code in consul")
timeout = time.After(30 * time.Second)
podStatusStore := podstatus.NewConsul(statusstore.NewConsul(kp.NewConsulClient(kp.Options{})), kp.PreparerPodStatusNamespace)
for {
podStatus, _, err := podStatusStore.Get(podUniqueKey)
if err != nil {
errCh <- err
return
}
found := false
for _, processStatus := range podStatus.ProcessStatuses {
if processStatus.LaunchableID == "hello" && processStatus.EntryPoint == "launch" {
found = true
if processStatus.LastExit == nil {
errCh <- fmt.Errorf("Found no last exit in consul pod status for %s", podUniqueKey)
return
}
if processStatus.LastExit.ExitCode != exitCode {
errCh <- fmt.Errorf("Exit code for '%s' in consul was expected to be %d but was %d", podUniqueKey, exitCode, finishResult.ExitCode)
return
}
}
}
if found {
logger.Infoln("Successful!")
break
}
select {
case <-timeout:
errCh <- fmt.Errorf("There was no pod process for hello/launch for %s in consul", podUniqueKey)
return
default:
}
}
}
开发者ID:petertseng,项目名称:p2,代码行数:101,代码来源:check.go
示例18: ParseWithConsulOptions
func ParseWithConsulOptions() (string, kp.Options, labels.ApplicatorWithoutWatches) {
consulURL := kingpin.Flag("consul", "The hostname and port of a consul agent in the p2 cluster. Defaults to 0.0.0.0:8500.").String()
httpApplicatorURL := kingpin.Flag("http-applicator-url", "The URL of an labels.httpApplicator target, including the protocol and port. For example, https://consul-server.io:9999").URL()
token := kingpin.Flag("token", "The consul ACL token to use. Empty by default.").String()
tokenFile := kingpin.Flag("token-file", "The file containing the Consul ACL token").ExistingFile()
headers := kingpin.Flag("header", "An HTTP header to add to requests, in KEY=VALUE form. Can be specified multiple times.").StringMap()
https := kingpin.Flag("https", "Use HTTPS").Bool()
wait := kingpin.Flag("wait", "Maximum duration for Consul watches, before resetting and starting again.").Default("30s").Duration()
caFile := kingpin.Flag("tls-ca-file", "File containing the x509 PEM-encoded CA ").ExistingFile()
keyFile := kingpin.Flag("tls-key-file", "File containing the x509 PEM-encoded private key").ExistingFile()
certFile := kingpin.Flag("tls-cert-file", "File containing the x509 PEM-encoded public key certificate").ExistingFile()
cmd := kingpin.Parse()
if *tokenFile != "" {
tokenBytes, err := ioutil.ReadFile(*tokenFile)
if err != nil {
log.Fatalln(err)
}
*token = string(tokenBytes)
}
var transport http.RoundTripper
if *caFile != "" || *keyFile != "" || *certFile != "" {
tlsConfig, err := netutil.GetTLSConfig(*certFile, *keyFile, *caFile)
if err != nil {
log.Fatalln(err)
}
transport = &http.Transport{
TLSClientConfig: tlsConfig,
// same dialer as http.DefaultTransport
Dial: (&net.Dialer{
Timeout: http.DefaultClient.Timeout,
KeepAlive: http.DefaultClient.Timeout,
}).Dial,
}
} else {
transport = http.DefaultTransport
}
httpClient := netutil.NewHeaderClient(*headers, transport)
kpOpts := kp.Options{
Address: *consulURL,
Token: *token,
Client: httpClient,
HTTPS: *https,
WaitTime: *wait,
}
var applicator labels.ApplicatorWithoutWatches
var err error
if *httpApplicatorURL != nil {
applicator, err = labels.NewHTTPApplicator(httpClient, *httpApplicatorURL)
if err != nil {
log.Fatalln(err)
}
} else {
applicator = labels.NewConsulApplicator(kp.NewConsulClient(kpOpts), 0)
}
return cmd, kpOpts, applicator
}
开发者ID:petertseng,项目名称:p2,代码行数:61,代码来源:kingpin.go
注:本文中的github.com/square/p2/pkg/kp.NewConsulClient函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论