Esempio n. 1
0
func (n *Node) createSerfAgent(iface string) {

	serfConfig := serf.DefaultConfig()

	serfConfig.Tags = n.tags

	serfConfig.MemberlistConfig.BindAddr = iface
	serfConfig.MemberlistConfig.BindPort = getRandomPort(iface)

	serfConfig.NodeName = fmt.Sprintf("%s@%s", n.UUID, iface)
	serfConfig.Tags = n.tags
	serfConfig.LogOutput = ioutil.Discard
	//	serfConfig.MemberlistConfig.GossipInterval = 5 * time.Millisecond
	//	serfConfig.MemberlistConfig.ProbeInterval = 50 * time.Millisecond
	//	serfConfig.MemberlistConfig.ProbeTimeout = 25 * time.Millisecond
	//	serfConfig.MemberlistConfig.SuspicionMult = 1
	serfConfig.Init()
	agentConfig := agent.DefaultConfig()
	agentConfig.Tags = n.tags
	agentConfig.LogLevel = "INFO"
	agt, err := agent.Create(agentConfig, serfConfig, ioutil.Discard)
	//	agt, err := agent.Create(agentConfig, serfConfig, n.cfg.Logger())

	if n.handleErr(err) {
		eventHandler := newEventHandler()
		n.join.Join(eventHandler.join.WhereNot(n.isSelf))
		n.leave.Join(eventHandler.leave.WhereNot(n.isSelf).Transform(toLeaveEvent()))
		n.query.Join(eventHandler.query.Transform(toQueryEvent(iface)))
		agt.RegisterEventHandler(eventHandler)
		n.agents[iface] = agt
		n.logger.Println("Agent Created On Port", agt.SerfConfig().MemberlistConfig.BindPort)
	} else {
		n.logger.Println("Failed to create Agent")
	}
}
Esempio n. 2
0
// InitSerfAndServe initializes a serf client for agent running at specified
// IP address and enters the event/query serving loop.
// If an empty IP address the the client tries to reach the agent at
// it's default address (usually 127.0.0.1:7373)
func (r *Router) InitSerfAndServe(addr string) error {
	var (
		c   *client.RPCClient
		err error
	)

	if addr == "" {
		addr = agent.DefaultConfig().RPCAddr
	}

	if c, err = client.NewRPCClient(addr); err != nil {
		return err
	}
	return r.serve(c)
}
Esempio n. 3
0
func testKeysCommandAgent(t *testing.T) *agent.Agent {
	key1, err := base64.StdEncoding.DecodeString("SNCg1bQSoCdGVlEx+TgfBw==")
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	key2, err := base64.StdEncoding.DecodeString("vbitCcJNwNP4aEWHgofjMg==")
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	keyring, err := memberlist.NewKeyring([][]byte{key1, key2}, key1)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	agentConf := agent.DefaultConfig()
	serfConf := serf.DefaultConfig()
	serfConf.MemberlistConfig.Keyring = keyring

	a1 := testAgentWithConfig(t, agentConf, serfConf)
	return a1
}
Esempio n. 4
0
func testAgent(t *testing.T) *agent.Agent {
	agentConfig := agent.DefaultConfig()
	serfConfig := serf.DefaultConfig()
	serfConfig.MemberlistConfig.BindAddr = testutil.GetBindAddr().String()
	serfConfig.MemberlistConfig.ProbeInterval = 50 * time.Millisecond
	serfConfig.MemberlistConfig.ProbeTimeout = 25 * time.Millisecond
	serfConfig.MemberlistConfig.SuspicionMult = 1
	serfConfig.NodeName = serfConfig.MemberlistConfig.BindAddr
	serfConfig.Tags = map[string]string{"role": "test", "tag1": "foo", "tag2": "bar"}

	agent, err := agent.Create(agentConfig, serfConfig, nil)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	if err := agent.Start(); err != nil {
		t.Fatalf("err: %s", err)
	}

	return agent
}
Esempio n. 5
0
func StartMaster(c *cli.Context) {
	rpcAddress := c.GlobalString("rpc")
	rpcAuthKey := c.GlobalString("rpc-auth")
	masterAddress := c.String("master")
	listenAddress := c.String("listen")

	fields := strings.Split(listenAddress, ":")
	if len(fields) != 2 {
		log.Fatalf("listen requires host:port! %s is not valid", listenAddress)
	}
	bindAddr := fields[0]
	bindPort, err := strconv.Atoi(fields[1])
	if err != nil {
		log.Fatalf("Unable to parse %s into port", fields[1])
	}
	serfConfig := serf.DefaultConfig()
	serfConfig.MemberlistConfig.BindAddr = bindAddr
	serfConfig.MemberlistConfig.BindPort = bindPort
	if c.IsSet("name") {
		serfConfig.NodeName = c.String("name")
	}
	serfConfig.Tags = map[string]string{"master": "true"}

	log.Printf("Starting master on %s", listenAddress)
	log.Printf("Starting master RPC listener on %s", rpcAddress)
	logOutput := os.Stderr
	logWriter := agent.NewLogWriter(123)
	a, err := agent.Create(agent.DefaultConfig(), serfConfig, logWriter)
	if err != nil {
		log.Fatalf("Unable to create agent: %s", err)
	}

	//register event handlers
	meh := MasterEventHandler{}
	a.RegisterEventHandler(&meh)

	if err := a.Start(); err != nil {
		log.Fatalf("Unable to start agent: %s", err)
	}

	log.Printf("Joining cluster by way of %s", masterAddress)
	n, err := a.Join([]string{masterAddress}, true)
	if n > 0 {
		log.Printf("Cluster joined; %d nodes participating", n)
	}
	if err != nil {
		log.Fatalf("unable to join cluster: %s", err)
	}

	members := a.Serf().Members()
	log.Printf("%d nodes currently in cluster:", len(members))
	for _, m := range members {
		log.Printf("  %s %s:%d %v %s", m.Name, m.Addr, m.Port, m.Tags, m.Status)
	}

	rpcListener, err := net.Listen("tcp", rpcAddress)
	if err != nil {
		log.Fatalf("Error starting RPC listener: %s", err)
	}
	//TODO should we listen for shutdown signals and close the agent properly?
	agent.NewAgentIPC(a, rpcAuthKey, rpcListener, logOutput, logWriter)
	select {}

}
func testAgent(t *testing.T) *agent.Agent {
	agentConfig := agent.DefaultConfig()
	serfConfig := serf.DefaultConfig()
	return testAgentWithConfig(t, agentConfig, serfConfig)
}