Ejemplo n.º 1
0
func (n *Node) GetContainers(do *dockerclient.DockerClient) ([]dockerclient.Container, error) {
	containers, err := do.ListContainers(false, false, "")
	if err != nil {
		log.Fatal(err)
	}
	return containers, err
}
Ejemplo n.º 2
0
func GetInjectorsForPolicy(client *docker.DockerClient, name string) ([]injector, error) {
	is := []injector{}
	cs, err := client.ListContainers(true, false, fmt.Sprintf(`{"label":["%s=%s"]}`, AGENT_LABEL, name))
	if err != nil {
		return is, err
	}
	for _, c := range cs {
		is = append(is, InjectorFromContainer(c))
	}
	return is, nil
}
Ejemplo n.º 3
0
func runGC(dockerClient *dockerclient.DockerClient, filters ...string) (bool, error) {
	done := true

	images, err := dockerClient.ListImages(true)
	if err != nil {
		return true, err
	}

	imagesToSave := make(map[string]bool)

	for _, image := range images {
		for _, repoTag := range image.RepoTags {
			for _, regexFilter := range filters {
				if match, _ := regexp.MatchString(regexFilter, repoTag); match {
					log.Printf("Image %v matches regexp /%s/ to keep\n", image.Id, regexFilter)
					imagesToSave[image.Id] = true
				}
			}
		}
	}

	for _, i := range images {
		if i.ParentId != "" {
			log.Printf("Image %s has children\n", i.ParentId)
			imagesToSave[i.ParentId] = true
		}
	}

	containers, err := dockerClient.ListContainers(true, false, "")
	if err != nil {
		return true, err
	}

	for _, c := range containers {
		info, _ := dockerClient.InspectContainer(c.Id)
		log.Printf("Image %s in use by container %v\n", info.Image, c.Id)
		imagesToSave[info.Image] = true
	}

	for _, image := range images {
		if !imagesToSave[image.Id] {
			log.Printf("Deleting image with image id %s %v\n", image.Id, image.RepoTags)
			done = false
			_, err = dockerClient.RemoveImage(image.Id)
			if err != nil {
				log.Println("Failed to delete image: ", err)
			}
		}
	}

	log.Println("Done with images GC")

	return done, nil
}
Ejemplo n.º 4
0
func ListContainersDetailed(dockerClient *dockerclient.DockerClient) ([]*dockerclient.ContainerInfo, error) {
	containers, err := dockerClient.ListContainers(true, false, "")
	if err != nil {
		return nil, err
	}
	var result = make([]*dockerclient.ContainerInfo, len(containers))
	for i, container := range containers {
		containerInfo, err := dockerClient.InspectContainer(container.Id)
		if err != nil {
			return nil, err
		}
		result[i] = containerInfo
	}
	return result, nil
}
Ejemplo n.º 5
0
func list_containers(docker *dockerclient.DockerClient) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		// Get only running containers
		containers, err := docker.ListContainers(false, false, "")
		if err != nil {
			log.Fatal(err)
		}

		dat, err := json.MarshalIndent(&containers, "", "  ")
		if err != nil {
			log.Fatal(err)
		}
		fmt.Fprintf(w, string(dat))
	}
}
Ejemplo n.º 6
0
func listDevices(docker *dc.DockerClient) []string {
	containers, err := docker.ListContainers(false)
	if err != nil {
		panic(err)
	}

	res := make([]string, 0, 10)

	for _, c := range containers {
		name := arrayToStr(c.Names)
		if strings.HasPrefix(name, "/device") {
			// found a device container
			res = append(res, name[len("/device"):])
		}
	}

	return res
}
func supprDocker(docker *dockerclient.DockerClient, dockerName string) {

	var searchDockerName string = "{\"name\":[\"" + dockerName + "\"]}"
	//l4g.Info("%v\n", searchDockerName)
	// Get only running containers
	containers, err := docker.ListContainers(true, true, searchDockerName)
	if err != nil {
		log.Fatal(err)
	}
	for _, c := range containers {
		l4g.Info(c.Id, c.Names)

		if err := docker.RemoveContainer(c.Id, true, false); err != nil {
			//l4g.Info("cannot stop container: %s", err)
			var RS string = fmt.Sprintf("cannot stop container: %s", err)
			l4g.Info(RS)
			writeSDLstr(RS)

		}
	}
}
Ejemplo n.º 8
0
func GetAcceptanceTestContainerInfo(docker *dockerclient.DockerClient, containerType string) *dockerclient.ContainerInfo {

	// Get only running containers
	containers, err := docker.ListContainers(false, false, "")
	if err != nil {
		log.Fatal(err)
	}

	//Loop through them until we find a match
	for _, c := range containers {
		xtContainerType, ok := c.Labels["xt-container-type"]
		if ok && xtContainerType == containerType {
			//Grab the information for the container
			info, err := docker.InspectContainer(c.Id)
			if err != nil {
				log.Fatal(err)
			}

			return info
		}
	}

	return nil
}
Ejemplo n.º 9
0
func getExpired(client *dockerclient.DockerClient) (expiredContainers []*dockerclient.ContainerInfo, expiredImages []*dockerclient.Image, err error) {
	now := time.Now()
	// Containers
	containers, err := client.ListContainers(true, false, "") // true = all containers
	if err != nil {
		return nil, nil, err
	}

	usedVolumeContainers := map[string]int{}
	usedImages := map[string]int{}
	oldContainers := []*dockerclient.ContainerInfo{}
	for _, c := range containers {
		debug("< container: %s", c.Id)
		container, err := client.InspectContainer(c.Id)
		if err != nil {
			debug("  + Couldn't inspect container %s, skipping: %s", c.Id, err)
			continue
		}

		// Increment reference counter refering to how many containers use volume container and image
		if len(container.HostConfig.VolumesFrom) > 0 {
			for _, vc := range container.HostConfig.VolumesFrom {
				usedVolumeContainers[vc]++
			}
		}
		debug("  + Container %s uses image %s", c.Id, container.Image)
		usedImages[container.Image]++

		if container.State.Running {
			debug("  + Container is still running")
			continue
		}
		debug("  + not running")

		if container.State.FinishedAt == time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) && !*unsafe {
			debug("  + Container %s has empty FinishedAt field, skipping", c.Id)
			continue
		}
		created, err := time.Parse(time.RFC3339, container.Created)
		if err != nil {
			return nil, nil, err
		}
		debug("  + Container and image threshold %s", now.Add(-*ageContainer))
		if created.After(now.Add(-*ageContainer)) {
			debug("  + Creation time is not old enough: %s", created)
			continue
		}
		debug("  + Creation time is older than %s", *ageContainer)
		if container.State.FinishedAt.After(now.Add(-*ageContainer)) {
			debug("  + Exit time is not old enough: %s", container.State.FinishedAt)
			continue
		}
		debug("  + Exit time is older than %s", *ageContainer)

		// Decrement reference counter for old containers
		if len(container.HostConfig.VolumesFrom) > 0 {
			for _, vc := range container.HostConfig.VolumesFrom {
				usedVolumeContainers[vc]--
			}
		}
		usedImages[container.Image]--

		oldContainers = append(oldContainers, container)
	}

	for _, c := range oldContainers {
		name := c.Name[1:] // Remove leading /
		if usedVolumeContainers[name] > 0 {
			continue
		}
		expiredContainers = append(expiredContainers, c)
	}
	log.Printf("Found %d expired containers", len(expiredContainers))

	// Images
	images, err := client.ListImages(true)
	if err != nil {
		return nil, nil, err
	}

	for _, image := range images {
		debug("< image id: %s", image.Id)
		ctime := time.Unix(image.Created, 0)
		if ctime.After(now.Add(-*ageImage)) {
			continue
		}
		debug("  + older than %s", *ageImage)
		if usedImages[image.Id] > 0 {
			debug("  + in use, skipping")
			continue
		}
		expiredImages = append(expiredImages, image)
	}
	log.Printf("Found %d expired images", len(expiredImages))
	return expiredContainers, expiredImages, nil
}