// ListContainerOption returns docker.ListContainersOptions according to the flag
// @param flag int (0: all, 1: created, 2: restarting, 3: running, 4: paused, 5&6: exited)
func ListContainerOption(flag int) docker.ListContainersOptions {
	options := docker.ListContainersOptions{Limit: 100, Filters: map[string][]string{}}
	switch flag {
	case 0:
		options.All = true
		break
	case 1:
		options.All = false
		options.Filters["status"] = []string{"created"}
		break
	case 2:
		options.All = false
		options.Filters["status"] = []string{"restarting"}
		break
	case 3:
		options.All = false
		options.Filters["status"] = []string{"running"}
		break
	case 4:
		options.All = false
		options.Filters["status"] = []string{"paused"}
		break
	case 5:
		options.All = false
		options.Filters["status"] = []string{"exited"}
		break
	case 6:
		options.All = false
		options.Filters["exited"] = []string{"0"}
		break
	}
	return options
}
Exemple #2
0
func GetContainers() ([]Container, error) {
	listOpts := docker.ListContainersOptions{}
	listOpts.All = true
	listOpts.Limit = 1000
	listOpts.Size = true

	apiContainers, err := client.ListContainers(listOpts)

	if err != nil {
		return nil, err
	}

	containers := make([]Container, 0, len(apiContainers))
	for _, c := range apiContainers {
		ports := make([]Port, 0, 5)
		for _, p := range c.Ports {
			port := Port{
				Private: uint64(p.PrivatePort),
				Public:  uint64(p.PublicPort),
				Type:    p.Type,
				IP:      p.IP,
			}
			ports = append(ports, port)
		}

		labels := make([]Label, 0, 5)
		for k, v := range c.Labels {
			labels = append(labels, Label{k, v})
		}

		c := Container{
			Command:    c.Command,
			Id:         c.ID,
			Created:    c.Created,
			Image:      c.Image,
			Name:       c.Names[0][1:],
			Ports:      ports,
			Labels:     labels,
			RootFsSize: c.SizeRootFs,
			RwSize:     c.SizeRw,
			Status:     c.Status,
		}

		containers = append(containers, c)
	}

	return containers, nil
}
func DockerPs(serverid string) (DockerPsResponse, error) {
	response := DockerPsResponse{}
	var err error

	//logit.Info.Println("DockerPs called on " + serverid)

	docker, err := dockerapi.NewClient("tcp://" + serverid)
	if err != nil {
		logit.Error.Println(err.Error())
		return response, err
	}

	var info []dockerapi.APIContainers
	options := dockerapi.ListContainersOptions{}
	options.All = true

	info, err = docker.ListContainers(options)
	if err != nil {
		logit.Error.Println(err.Error())
		return response, err
	}

	response.Output = make([]DockerPsInfo, 0)

	var apicontainer dockerapi.APIContainers
	for x := range info {
		apicontainer = info[x]
		//logit.Info.Printf("x=%d\n", x)
		if strings.Index(apicontainer.Image, "cpm-node") > 0 ||
			strings.Index(apicontainer.Image, "cpm-pgpool") > 0 {
			cinfo := DockerPsInfo{}
			if len(apicontainer.Names) > 0 {
				cinfo.Name = strings.Trim(apicontainer.Names[0], "/")
				//logit.Info.Println("name=" + cinfo.Name + " status=" + apicontainer.Status)
			}
			cinfo.Status = apicontainer.Status
			cinfo.Image = apicontainer.Image
			response.Output = append(response.Output, cinfo)
		}
	}

	//logit.Info.Println("dockerps returning results")
	return response, nil
}
Exemple #4
0
func listContainers(client *docker.Client, all bool) (map[string]*docker.Container, error) {
	var opts docker.ListContainersOptions
	if all {
		opts.All = true
	} else {
		opts.Filters = map[string][]string{"status": []string{"running"}}
	}
	l, err := client.ListContainers(opts)
	if err != nil {
		return nil, err
	}

	m := make(map[string]*docker.Container, 0)
	for _, c := range l {
		info, err := client.InspectContainer(c.ID)
		if err != nil {
			DEBUG("failed to inspect container: %s", err)
		}
		m[info.Name] = info
	}

	return m, nil
}