Exemple #1
0
func linkContainers() {
	links := make([]*d.Link, 0)
	switch val := link.Options["targets"].Value.(type) {
	case string:
		link, err := linkContainer(val)
		if err != nil {
			fmt.Println("Linking error: " + err.Error())
			return
		}
		fmt.Printf("creating link " + link.Name + " [" + link.Id + "]\n")
		links = append(links, link)
	case []string:
		for _, v := range val {
			link, err := linkContainer(v)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			fmt.Printf("creating link [" + link.Id + "]\n")
			links = append(links, link)
		}
	}
	// each link has the same source
	source := links[0].Source
	fmt.Printf("starting container [" + source.Name + "]\n")
	_, err := disco.CreateDockerContainer(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	i := 0
	var exists bool
	for !exists {
		fmt.Printf("\rwaiting for discovery%s", strings.Repeat(".", i))
		time.Sleep(500 * time.Millisecond)
		exists, err = disco.ContainerExists(source.Name)
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		i++
		if i > 50 {
			fmt.Println("Error: timeout waiting for discovery")
			os.Exit(1)
		}
	}
	fmt.Println("")
	for _, link := range links {
		fmt.Println("updating", link.Source.Name)
		disco.RegisterContainer(link.Source)
		fmt.Println("updating", link.Target.Name)
		disco.RegisterContainer(link.Target)
	}
	fmt.Println("done.")
	fmt.Println("Results:")
	commando.PrintFields(true, 2, "SOURCE", "TARGET", "LINK ID")
	for _, link := range links {
		commando.PrintFields(true, 2, link.Source.Name, link.Target.Name, link.Id)
	}
}
Exemple #2
0
func listContainers() {
	cons, err := disco.GetContainers()
	if err != nil {
		fmt.Println(err)
		return
	}
	commando.PrintFields(false, 0, "NAME", "HOST NODE", "DOCKER ID", "PORTS", "LINKS")
	for _, con := range cons {
		var portMap []string
		for _, p := range con.Ports {
			portMap = append(portMap, fmt.Sprintf("%d:%d", p.Private, p.Public))
		}
		portString := strings.Join(portMap, ", ")
		commando.PrintFields(false, 0, con.Name, con.HostNode, con.Id[:12], portString, getLinkString(con))
	}
}