Exemple #1
0
func (n *Node) InspectContainer(do *dockerclient.DockerClient, id string) (*dockerclient.ContainerInfo, error) {
	containerInfo := &dockerclient.ContainerInfo{}
	var err error
	containerInfo, err = do.InspectContainer(id)
	if err != nil {
		log.Fatal(err)
	}
	return containerInfo, err
}
Exemple #2
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
}
Exemple #3
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
}
Exemple #4
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
}
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
}
func createTupperware(i int, uri *url.URL, docker *dockerclient.DockerClient) {
	defer wg.Done()

	logrus.Infof("Giving tupperware container %d some spears", i)

	// create the command flags to pass to ab
	cmd := []string{
		"ab",
		"-c",
		strconv.Itoa(concurrency),
		"-n",
		strconv.Itoa(requests),
		"-m",
		strings.ToUpper(method),
		"-s",
		strconv.Itoa(timeout),
		"-v",
		strconv.Itoa(verbosity),
		"-f",
		protocol,
	}

	if authHeader != "" {
		cmd = append(cmd, []string{"-A", authHeader}...)
	}
	if proxyAuth != "" {
		cmd = append(cmd, []string{"-P", proxyAuth}...)
	}
	if contentType != "" {
		cmd = append(cmd, []string{"-T", contentType}...)
	}
	if timelimit > 0 {
		cmd = append(cmd, []string{"-t", strconv.Itoa(timelimit)}...)
	}
	if len(headers) > 0 {
		for _, header := range headers {
			cmd = append(cmd, []string{"-H", header}...)
		}
	}
	if len(cookies) > 0 {
		for _, cookie := range cookies {
			cmd = append(cmd, []string{"-C", cookie}...)
		}
	}

	// append the uri to the cmd string
	// make sure there is a trailing slash if none given
	if uri.Path == "" {
		uri.Path = "/"
	}
	cmd = append(cmd, uri.String())

	// create the container
	containerConfig := &dockerclient.ContainerConfig{
		Image:      "jess/ab",
		Entrypoint: []string{"top"},
	}
	name := fmt.Sprintf("tws_%d", i)
	id, err := docker.CreateContainer(containerConfig, name)
	if err != nil {
		logrus.Errorf("Error while creating container (%s): %v", name, err)
		return
	}
	containers = append(containers, id)

	// start the container
	hostConfig := &dockerclient.HostConfig{}
	if err = docker.StartContainer(id, hostConfig); err != nil {
		logrus.Errorf("Error while starting container (%s): %v", name, err)
		return
	}

	// we have to start the container _before_ adding the new default gateway
	// for outbound traffic, its unfortunate but yeah we need the pid of the process
	if cidr != "" {

		// get the pid of the container
		info, err := docker.InspectContainer(id)
		if err != nil {
			logrus.Errorf("Error while inspecting container (%s): %v", name, err)
			return
		}
		pid := info.State.Pid

		nsPidPath := path.Join(netnsPath, strconv.Itoa(pid))
		// defer removal of the pid from /var/run/netns
		defer os.RemoveAll(nsPidPath)
		// create a symlink from proc to the netns pid
		procPidPath := path.Join("/proc", strconv.Itoa(pid), "ns", "net")
		if err := os.Symlink(procPidPath, nsPidPath); err != nil {
			logrus.Errorf("could not create symlink from %s to %s: %v", procPidPath, nsPidPath, err)
		}

		// create the veth pair and add to bridge
		local, guest, err := ovs.CreateVethPair(bridge)
		if err != nil {
			logrus.Error(err)
			return
		}

		// get the local link
		localLink, err := netlink.LinkByName(local)
		if err != nil {
			logrus.Errorf("getting link by name %s failed: %v", local, err)
			return
		}
		// set the local link as up
		if netlink.LinkSetUp(localLink); err != nil {
			logrus.Errorf("setting link name %s as up failed: %v", local, err)
			return
		}

		// get the guest link and setns as container pid
		guestLink, err := netlink.LinkByName(guest)
		if err != nil {
			logrus.Errorf("getting link by name %s failed: %v", guest, err)
			return
		}
		if err := netlink.LinkSetNsPid(guestLink, pid); err != nil {
			logrus.Errorf("setting link name %s to netns pid %d failed: %v", guest, pid, err)
			return
		}

		// set the interface to eth1 in the container
		ciface := "eth1"
		if _, err := ovs.NetNSExec(pid, "ip", "link", "set", guest, "name", ciface); err != nil {
			logrus.Error(err)
			return
		}

		// add the ip to the interface
		if _, err := ovs.NetNSExec(pid, "ip", "addr", "add", ip.String(), "dev", ciface); err != nil {
			logrus.Error(err)
			return
		}

		// delete the default route
		if _, err := ovs.NetNSExec(pid, "ip", "route", "delete", "default"); err != nil {
			logrus.Warn(err)
		}
		// setup the gateway
		if _, err := ovs.NetNSExec(pid, "ip", "route", "get", gateway); err != nil {
			// add it
			if _, err := ovs.NetNSExec(pid, "ip", "route", "add", fmt.Sprintf("%s/32", gateway), "dev", ciface); err != nil {
				logrus.Error(err)
				return
			}
		}
		// set gateway as default
		if _, err := ovs.NetNSExec(pid, "ip", "route", "replace", "default", "via", gateway); err != nil {
			logrus.Error(err)
			return
		}
	}

	// exec ab in the container
	args := append([]string{"exec", id}, cmd...)
	output, err := exec.Command(dockerPath, args...).CombinedOutput()
	if err != nil {
		logrus.Errorf("docker exec (%s) failed: %v: %s (%s)", id[0:7], strings.Join(args, " "), output, err)
		return
	}

	logrus.Infof("Output from container (%s)\n %s", name, output)
}