Esempio n. 1
0
func NewClientWithAddr(addr string) (*Client, error) {
	c, err := rpcplus.DialHTTP("tcp", addr)
	if err != nil {
		return nil, err
	}
	return newClient(c, addr), nil
}
Esempio n. 2
0
func (c *Client) reconnect(disconnectedClient *rpcplus.Client) error {
	if c.addr == "" {
		c.notify(ConnStatusDisconnected)
		return errors.New("no reconnect address set")
	}
	c.clientMtx.Lock()
	defer c.clientMtx.Unlock()
	if disconnectedClient != c.client {
		// another goroutine has already reconnected
		return nil
	}
	c.setReconnecting(true)
	c.notify(ConnStatusDisconnected)
	for {
		if c.isClosed() {
			return ErrDisconnected
		}
		log.Printf("discover: reconnecting to %s", c.addr)
		client, err := rpcplus.DialHTTP("tcp", c.addr)
		if err != nil {
			c.notify(ConnStatusConnectFailed)
			log.Printf("discover: failed to reconnect to %s: %s", c.addr, err)
			time.Sleep(time.Second)
			continue
		}
		log.Printf("discover: reconnected to %s", c.addr)
		c.client = client
		c.setReconnecting(false)
		c.notify(ConnStatusConnected)
		return nil
	}
}
Esempio n. 3
0
func NewClientUsingAddress(addr string) (*Client, error) {
	client, err := rpcplus.DialHTTP("tcp", addr)
	return &Client{
		client:     client,
		heartbeats: make(map[string]bool),
	}, err
}
Esempio n. 4
0
func NewClientUsingAddress(addr string) (*Client, error) {
	client, err := rpcplus.DialHTTP("tcp", addr)
	return &Client{
		client:        client,
		heartbeats:    make(map[string]chan struct{}),
		expandedAddrs: make(map[string]string),
		names:         make(map[string]string),
	}, err
}
Esempio n. 5
0
func (c *Client) ConnectHost(id string) (*Host, error) {
	// TODO: reuse connection if leader id == id
	services := c.service.Select(map[string]string{"id": id})
	if len(services) == 0 {
		return nil, ErrNoServers
	}
	rc, err := rpcplus.DialHTTP("tcp", services[0].Addr)
	return &Host{service: c.service, c: rc}, err
}
Esempio n. 6
0
func New() (*Client, error) {
	services, err := discoverd.Services("flynn-strowger-rpc", discoverd.DefaultTimeout)
	if err != nil {
		return nil, err
	}
	if len(services) == 0 {
		return nil, errors.New("strowger: no servers found")
	}
	c, err := rpcplus.DialHTTP("tcp", services[0].Addr)
	return &Client{c}, err
}
Esempio n. 7
0
func main() {
	client, err := rpcplus.DialHTTP("tcp", "localhost:1115")
	if err != nil {
		log.Fatal(err)
	}

	err = client.Call("Router.AddFrontend", &strowger.Config{Service: "example-server", HTTPDomain: os.Args[1]}, &struct{}{})
	if err != nil {
		log.Fatal(err)
	}
}
Esempio n. 8
0
func New(id string) (*Client, error) {
	services, err := discoverd.NewServiceSet("flynn-lorne")
	services.Filter(map[string]string{"id": id})
	if err != nil {
		return nil, err
	}
	addrs := services.Addrs()
	if len(addrs) == 0 {
		return nil, ErrNoServers
	}
	c, err := rpcplus.DialHTTP("tcp", addrs[0])
	return &Client{c, services}, err
}
Esempio n. 9
0
func New() (*Client, error) {
	services, err := discoverd.NewServiceSet("flynn-sampi")
	if err != nil {
		return nil, err
	}
	defer services.Close()
	select {
	case <-services.Watch(true, true):
	case <-time.After(time.Second):
		return nil, errors.New("sampi: no servers found")
	}
	c, err := rpcplus.DialHTTP("tcp", services.Leader().Addr)
	return &Client{c}, err
}
Esempio n. 10
0
func New() (*Client, error) {
	disc, err := discover.NewClient()
	if err != nil {
		return nil, err
	}
	services, err := disc.Services("flynn-sampi")
	if err != nil {
		return nil, err
	}
	addrs := services.OnlineAddrs()
	if len(addrs) == 0 {
		return nil, errors.New("sampi: no servers found")
	}
	c, err := rpcplus.DialHTTP("tcp", addrs[0])
	return &Client{c}, err
}
Esempio n. 11
0
func (c *Client) followLeader(firstErr chan<- error) {
	for update := range c.service.Leaders() {
		c.mtx.Lock()
		if closer, ok := c.c.(io.Closer); ok {
			closer.Close()
		}
		c.err = Attempts.Run(func() (err error) {
			c.c, err = rpcplus.DialHTTP("tcp", update.Addr)
			return
		})
		c.mtx.Unlock()
		if firstErr != nil {
			firstErr <- c.err
			firstErr = nil
		}
	}
	// TODO: reconnect to discoverd here
}
Esempio n. 12
0
func NewClientWithAddr(addr string) (*Client, error) {
	c, err := rpcplus.DialHTTP("tcp", addr)
	return newClient(c), err
}
Esempio n. 13
0
func main() {
	externalAddr := flag.String("external", "", "external IP of host")
	flag.Parse()

	go attachServer()
	go rpcServer()
	go allocatePorts()

	id := randomID()
	disc, err := discover.NewClient()
	if err != nil {
		log.Fatal(err)
	}
	if err := disc.Register("flynn-lorne-rpc."+id, "1113", nil); err != nil {
		log.Fatal(err)
	}
	if err := disc.Register("flynn-lorne-attach."+id, "1114", nil); err != nil {
		log.Fatal(err)
	}

	services, err := disc.Services("flynn-sampi")
	if err != nil {
		log.Fatal(err)
	}
	schedulers := services.Online()
	if len(schedulers) == 0 {
		log.Fatal("No sampi instances found")
	}

	scheduler, err := rpcplus.DialHTTP("tcp", schedulers[0].Addr)
	if err != nil {
		log.Fatal(err)
	}
	log.Print("Connected to scheduler")

	Docker, err = docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}

	go streamEvents(Docker)
	go syncScheduler(scheduler)

	host := sampi.Host{
		ID:        id,
		Resources: map[string]sampi.ResourceValue{"memory": sampi.ResourceValue{Value: 1024}},
	}

	jobs := make(chan *sampi.Job)
	scheduler.StreamGo("Scheduler.RegisterHost", host, jobs)
	log.Print("Host registered")
	for job := range jobs {
		log.Printf("%#v", job.Config)
		var hostConfig *docker.HostConfig
		if job.TCPPorts > 0 {
			port := strconv.Itoa(<-portAllocator)
			job.Config.Env = append(job.Config.Env, "PORT="+port)
			job.Config.ExposedPorts = map[string]struct{}{port + "/tcp": struct{}{}}
			hostConfig = &docker.HostConfig{
				PortBindings: map[string][]docker.PortBinding{port + "/tcp": {{HostPort: port}}},
			}
		}
		if *externalAddr != "" {
			job.Config.Env = append(job.Config.Env, "EXTERNAL_IP="+*externalAddr, "SD_HOST="+*externalAddr, "DISCOVERD="+*externalAddr+":1111")
		}
		state.AddJob(job)
		container, err := Docker.CreateContainer(job.Config)
		if err == docker.ErrNoSuchImage {
			err = Docker.PullImage(docker.PullImageOptions{Repository: job.Config.Image}, os.Stdout)
			if err != nil {
				log.Fatal(err)
			}
			container, err = Docker.CreateContainer(job.Config)
		}
		if err != nil {
			log.Fatal(err)
		}
		state.SetContainerID(job.ID, container.ID)
		state.WaitAttach(job.ID)
		if err := Docker.StartContainer(container.ID, hostConfig); err != nil {
			log.Fatal(err)
		}
		state.SetStatusRunning(job.ID)
	}
}