Example #1
0
func main() {
	var err error
	if dockerClient, err = newClient(pathToSocket); err != nil {
		log.Fatal(err)
	}

	if skydns, err = client.NewClient(skydnsUrl, secret, domain, 53); err != nil {
		log.Fatal(err)
	}

	log.Println("Starting restore of running containers...")
	if err := restoreContainers(); err != nil {
		log.Fatal(err)
	}

	c, err := dockerClient.newConn()
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	req, err := http.NewRequest("GET", "/events", nil)
	if err != nil {
		log.Fatal(err)
	}

	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	log.Println("Starting run loop...")
	d := json.NewDecoder(resp.Body)
	for {
		var event *Event
		if err := d.Decode(&event); err != nil {
			if err == io.EOF {
				log.Println("Stopping cleanly via EOF")
				break
			}
			log.Printf("Error decoding json %s\n", err)
			continue
		}
		uuid := truncate(event.ContainerId)

		switch event.Status {
		case "die", "stop", "kill":
			if err := removeService(uuid); err != nil {
				log.Printf("Error deleting %s - %s\n", uuid, err)
			}
		case "start", "restart":
			if err := addService(uuid, event.Image); err != nil {
				log.Printf("Error adding %s - %s\n", uuid, err)
			}
		}
	}
}
Example #2
0
func main() {
	validateSettings()
	if err := setupLogger(); err != nil {
		fatal(err)
	}

	var (
		err   error
		group = &sync.WaitGroup{}
	)

	plugins, err = newRuntime(pluginFile)
	if err != nil {
		fatal(err)
	}

	if dockerClient, err = docker.NewClient(pathToSocket); err != nil {
		log.Logf(log.FATAL, "error connecting to docker: %s", err)
		fatal(err)
	}

	if skydnsContainerName != "" {
		container, err := dockerClient.FetchContainer(skydnsContainerName, "")
		if err != nil {
			log.Logf(log.FATAL, "error retrieving skydns container '%s': %s", skydnsContainerName, err)
			fatal(err)
		}

		skydnsUrl = "http://" + container.NetworkSettings.IpAddress + ":8080"
	}

	log.Logf(log.INFO, "skydns URL: %s", skydnsUrl)

	if skydns, err = client.NewClient(skydnsUrl, secret, domain, "172.17.42.1:53"); err != nil {
		log.Logf(log.FATAL, "error connecting to skydns: %s", err)
		fatal(err)
	}

	log.Logf(log.DEBUG, "starting restore of containers")
	if err := restoreContainers(); err != nil {
		log.Logf(log.FATAL, "error restoring containers: %s", err)
		fatal(err)
	}

	events := dockerClient.GetEvents()

	group.Add(numberOfHandlers)
	// Start event handlers
	for i := 0; i < numberOfHandlers; i++ {
		go eventHandler(events, group)
	}

	log.Logf(log.DEBUG, "starting main process")
	group.Wait()
	log.Logf(log.DEBUG, "stopping cleanly via EOF")
}
Example #3
0
func newClientFromContext(c *cli.Context) (*client.Client, error) {
	var (
		base   = c.GlobalString("host")
		dns    = c.GlobalString("dns")
		domain = c.GlobalString("domain")
		secret = c.GlobalString("secret")
	)
	s, e := client.NewClient(base, secret, domain, dns)
	if e == nil {
		s.DNS = c.Bool("d") // currently only defined when listing services
	}
	return s, e
}
Example #4
0
func main() {
	var (
		err       error
		eventChan = make(chan *Event, 100) // 100 event buffer
		group     = &sync.WaitGroup{}
	)
	if dockerClient, err = newClient(pathToSocket); err != nil {
		log.Fatal(err)
	}

	if skydns, err = client.NewClient(skydnsUrl, secret, domain, 53); err != nil {
		log.Fatal(err)
	}

	log.Println("Starting restore of running containers...")
	if err := restoreContainers(); err != nil {
		log.Fatal(err)
	}

	c, err := dockerClient.newConn()
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	// Start event handlers
	for i := 0; i < numberOfHandlers; i++ {
		go eventHandler(eventChan, group)
	}

	req, err := http.NewRequest("GET", "/events", nil)
	if err != nil {
		log.Fatal(err)
	}

	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	log.Println("Starting run loop...")
	d := json.NewDecoder(resp.Body)
	for {
		var event *Event
		if err := d.Decode(&event); err != nil {
			if err == io.EOF {
				break
			}
			log.Printf("Error decoding json %s\n", err)
			continue
		}
		eventChan <- event
	}

	// Close the event chan then wait for handlers to finish
	close(eventChan)
	group.Wait()

	log.Println("Stopping cleanly via EOF")
}