예제 #1
0
func main() {
	docker, err := dockerclient.NewDockerClient(os.Getenv("DOCKER_HOST"), nil)
	if err != nil {
		log.Fatal(err)
	}

	client = docker

	client.StartMonitorEvents(eventCallback, nil)

	waitForInterrupt()
}
예제 #2
0
func main() {
	docker, err := dockerclient.NewDockerClient(os.Getenv("DOCKER_HOST"), nil)
	if err != nil {
		log.Fatal(err)
	}

	containerConfig := &dockerclient.ContainerConfig{Image: "busybox", Cmd: []string{"sh"}}
	containerId, err := docker.CreateContainer(containerConfig, "")
	if err != nil {
		log.Fatal(err)
	}

	// Start the container
	err = docker.StartContainer(containerId, nil)
	if err != nil {
		log.Fatal(err)
	}
	docker.StartMonitorStats(containerId, statCallback, nil)

	waitForInterrupt()
}
예제 #3
0
func main() {
	dc, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		log.Fatalf("Failed to init dockerclient: %v", err)
	}
	tpIP := getTpHost(dc)
	tp := toxiproxy.NewClient("http://" + tpIP + ":8474")

	proxies, err := tp.Proxies()
	if err != nil {
		log.Fatalf("Failed to list toxiproxy proxies: %v", err)
	}
	proxyInfos := map[string]ProxyInfo{}

	firstAvailablePort := uint16(9000)
	for proxyName, proxy := range proxies {
		tpPort, err := strconv.ParseUint(strings.Split(proxy.Listen, ":")[1], 10, 16)
		if err != nil {
			log.Printf("unable to parse port from proxy.Listen=%s\n", proxy.Listen)
			continue
		} else if uint16(tpPort) >= firstAvailablePort {
			log.Printf("port %d is taken, increasing firstAvailablePort=%d->%d\n", tpPort, firstAvailablePort, tpPort+1)
			firstAvailablePort = uint16(tpPort) + 1
		} else {
			log.Printf("port %d is taken, nobody cares\n", tpPort)
		}

		// hacky extraction of data from the container name
		dstPort, err := strconv.ParseUint(strings.Split(strings.Split(proxyName, ";")[1], ":")[1], 10, 16)
		if err == nil {
			proxyInfos[proxyName] = ProxyInfo{
				strings.Split(proxyName, ";")[2],
				strings.Split(strings.Split(proxyName, ";")[1], ":")[0],
				uint16(dstPort),
				tpIP,
				uint16(tpPort),
				proxy}
		} else {
			log.Println("failed to parse toxyproxy proxy name")
		}
	}

	s := Server{
		nextTpPort:      firstAvailablePort,
		queryConns:      make(chan bool),
		queryConnsReply: make(chan []Conn),
		tp:              tp,
		tpIP:            tpIP,
		dc:              dc,
		tpProxies:       proxyInfos,
	}

	r := mux.NewRouter()
	r.HandleFunc("/api/proxies", s.addProxyHandler).Methods("POST")
	r.HandleFunc("/api/proxies", s.getProxiesHandler).Methods("GET")
	r.HandleFunc("/api/proxies", s.deleteProxyHandler).Methods("DELETE")
	r.HandleFunc("/api/proxies/{proxyName}/toxics", s.createToxicHandler).Methods("POST")
	r.HandleFunc("/api/conns", s.getConnsHandler).Methods("GET")
	fs := FileServer(http.Dir("assets"))
	r.PathPrefix("/").Handler(fs)

	// set up the channels for the gorouties
	recordConn := make(chan Conn)

	// start the poller and the state tracker
	go connPoller(recordConn)
	go connStateTracker(recordConn, s.queryConns, s.queryConnsReply)

	log.Println("Listening on 3000...")
	http.ListenAndServe(":3000", r)
}