Пример #1
0
func cmdRm(c *cli.Context) {
	args := c.Args()

	if len(args) != 1 {
		cli.ShowAppHelp(c)
		log.Fatalln("missing argument: container name")
	}

	exposeClient, err := expose.NewExposeFromConf()
	if err != nil {
		log.Fatalln(err)
	}

	exposedHosts, err := exposeClient.List(true)
	if err != nil {
		log.Fatalln(err)
	}

	name := args[0]
	if _, isExposed := exposedHosts.Find(name); !isExposed {
		log.Fatalf("container %s is not exposed\n", name)
	}

	err = exposeClient.Delete(name)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("Container %s successfully removed!\n", name)
}
Пример #2
0
func cmdList(c *cli.Context) {
	exposeClient, err := expose.NewExposeFromConf()
	if err != nil {
		log.Fatalln(err)
	}

	exposedHosts, err := exposeClient.List(true)
	if err != nil {
		log.Fatalln(err)
	}

	client, err := docker.NewClientFromEnv()
	if err != nil {
		log.Fatalln(err)
	}

	containers, err := client.ListContainers(docker.ListContainersOptions{})
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("%-20s%-48s%-64s%s\n", "CONTAINER ID", "CONTAINER NAME", "URL", "STATUS")
	for _, container := range containers {
		name := bestName(container.Names)
		exposedHost, isExposed := exposedHosts.Find(name)
		url := exposeClient.UserURL(name)
		status := chalk.Blue.String() + "NOT EXPOSED" + chalk.Reset.String()
		if isExposed {
			if exposedHost.Status == 0 {
				status = chalk.Red.String() + "DOWN" + chalk.Reset.String()
			}

			if exposedHost.Status == 1 {
				status = chalk.Yellow.String() + "MAINTENANCE" + chalk.Reset.String()
			}

			if exposedHost.Status == 2 {
				status = chalk.Green.String() + "AVAILABLE" + chalk.Reset.String()
			}
		}

		fmt.Printf("%-20s%-48s%-64s%s\n", container.ID[:12], name, url, status)
	}
}
Пример #3
0
func cmdAdd(c *cli.Context) {
	args := c.Args()

	if len(args) != 1 {
		cli.ShowAppHelp(c)
		log.Fatalln("missing argument: container name")
	}

	exposeClient, err := expose.NewExposeFromConf()
	if err != nil {
		log.Fatalln(err)
	}

	client, err := docker.NewClientFromEnv()
	if err != nil {
		log.Fatalln(err)
	}

	container, err := client.InspectContainer(args[0])
	if err != nil {
		log.Fatalln(err)
	}

	port := c.Int("port")
	dockerPort := docker.Port(fmt.Sprintf("%d/tcp", port))
	portBindings := container.NetworkSettings.Ports[dockerPort]

	if len(portBindings) == 0 {
		log.Fatalf("port %d is not exposed by the container\n", port)
	}

	hostPort, _ := strconv.ParseInt(portBindings[0].HostPort, 10, 32)
	err = exposeClient.Add(container.Name[1:], int(hostPort))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("Container %s successfully exposed!\n", container.Name[1:])
	fmt.Println(exposeClient.UserURL(container.Name[1:]))
}