Example #1
0
func (sup *Supervisor) acceptAgent(cc net.Conn) {
	sup.mu.Lock()
	defer sup.mu.Unlock()

	raddr := addressFromNet(cc.RemoteAddr())
	ll := log.KV("raddr", raddr)

	if _, ok := sup.agents[raddr]; ok {
		ll.Info("agent already joined")
		_ = cc.Close()
		return
	}
	ll.Info("new agent joined")

	agent := &agent{
		ll:       ll,
		addr:     raddr,
		client:   rpc.RepresentAgent(cc, sup.provider),
		cc:       cc,
		setState: make(chan stack),
	}
	sup.agents[raddr] = agent

	if s, ok := sup.dfn.Machines[raddr]; !ok {
		ll.Info("no stack defined for this agent")
	} else {
		go agent.enforceState(s)
	}
}
Example #2
0
func handleAgent(ll *log.Log, cc io.ReadWriteCloser) {
	defer cc.Close()
	client := osprocess.New(nil)
	agent := rpc.RepresentAgent(cc, client)

	for i := 0; ; i++ {
		ll.Info("starting program")

		res, err := agent.StartProcess(&rpc.StartProcessReq{
			ProgramName: fmt.Sprintf("echoer v%d i was told to do this by the supervisor", i),
		})
		if err != nil {
			ll.Err(err).Error("couldn't send command to remote agent")
			return
		}
		ll.KV("process.id", res.ProcessID).Info("agent is running program")

		time.Sleep(3 * time.Second)

		ll.Info("stopping program")
		_, err = agent.StopProcess(&rpc.StopProcessReq{ProcessID: res.ProcessID, Timeout: time.Second})
		if err != nil {
			ll.Err(err).Error("couldn't send command to remote agent")
			return
		}
	}

}