func deleteImage(docker *dockerclient.DockerClient, imageIDs []string) []error { var errors []error if !noConfirm { var answer string fmt.Printf("This will delete %v images\n", len(imageIDs)) fmt.Printf("Do you want to continue? (yes/no)\n") fmt.Scanln(&answer) if answer != "yes" { fmt.Printf("%v", answer) return nil } } for _, imageID := range imageIDs { imageDelete, deleteErr := docker.RemoveImage(imageID) if deleteErr != nil { errors = append(errors, deleteErr) } for _, id := range imageDelete { if id.Deleted != "" { log.Printf("DELETED: %v", id.Deleted) } if id.Untagged != "" { log.Printf("UNTAGGED: %#v", id.Untagged) } } } if len(errors) > 0 { return errors } return nil }
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 }
func (n *Node) GetImages(do *dockerclient.DockerClient) ([]*dockerclient.Image, error) { images, err := do.ListImages(false) if err != nil { log.Fatal(err) } return images, err }
func ListImagesDetailed(dockerClient *dockerclient.DockerClient, all bool) ([]*DetailedImageInfo, error) { images, err := dockerClient.ListImages(all) if err != nil { return nil, err } var result = make([]*DetailedImageInfo, len(images)) for i, image := range images { imagesDetails, _ := InspectImage(dockerClient, image.Id) detailedImageInfo := DetailedImageInfo{ Created: image.Created, Id: image.Id, ParentId: image.ParentId, RepoTags: image.RepoTags, Size: image.Size, VirtualSize: image.VirtualSize, Architecture: imagesDetails.Architecture, Author: imagesDetails.Author, Comment: imagesDetails.Comment, Container: imagesDetails.Container, DockerVersion: imagesDetails.Container, Os: imagesDetails.Os} result[i] = &detailedImageInfo } return result, nil }
func stopCotainers(docker *dockerclient.DockerClient, imageIDs []string) []error { var errors []error if !noConfirm { var answer string fmt.Printf("This will stop %v images\n", len(imageIDs)) fmt.Printf("Do you want to continue? (yes/no)\n") fmt.Scanln(&answer) if answer != "yes" { fmt.Printf("%v", answer) return nil } } for _, imageID := range imageIDs { stopErr := docker.StopContainer(imageID, 10) if stopErr != nil { errors = append(errors, stopErr) } else { log.Printf("STOPPED: %v", imageID) } } if len(errors) > 0 { return errors } return nil }
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 }
func stopAndDeleteDevice(deviceSn string, docker *dc.DockerClient) error { container := "device" + deviceSn err := docker.KillContainer(container) if err != nil { return err } err = docker.RemoveContainer(container) return nil }
func removeImages(client *dockerclient.DockerClient, images []*dockerclient.Image) error { for _, image := range images { log.Print("rmi ", image.Id) if !*dry { if _, err := client.RemoveImage(image.Id); err != nil { debug("Couldn't remove image: %s", err) } } } return nil }
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 }
func removeContainers(client *dockerclient.DockerClient, containers []*dockerclient.ContainerInfo) error { for _, container := range containers { log.Printf("rm %s (%s)", container.Id, container.Name) if !*dry { if err := client.RemoveContainer(container.Id, false, false); err != nil { debug("Couldn't remove container: %s", err) } } } return nil }
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 }
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)) } }
func startContainer(docker *dockerclient.DockerClient, containerId string, additionalCapabilities []string, ctx *ContainerContext) error { //Build the port bindings needed when running the container dockerHostConfig := new(dockerclient.HostConfig) dockerHostConfig.PortBindings = make(map[string][]dockerclient.PortBinding) for k, v := range ctx.PortContext { pb := dockerclient.PortBinding{HostPort: v} portBindings := []dockerclient.PortBinding{pb} dockerHostConfig.PortBindings[k] = portBindings } dockerHostConfig.Links = ctx.Links dockerHostConfig.CapAdd = additionalCapabilities //Start the container return docker.StartContainer(containerId, dockerHostConfig) }
func requiredImageAvailable(docker *dockerclient.DockerClient, name string) bool { images, err := docker.ListImages(true) if err != nil { log.Fatal(err) } for _, i := range images { for _, t := range i.RepoTags { if strings.Index(t, name) == 0 { return true } } } return false }
func InspectImage(dockerClient *dockerclient.DockerClient, id string) (*dockerclient.ImageInfo, error) { cachedImageInfo, _ := imageCache.Get(id) if cachedImageInfo == nil { imageInfo, err := dockerClient.InspectImage(id) if err != nil { return nil, err } imageCache.Add(id, imageInfo) return imageInfo, nil } else { if cachedImageInfoCasted, ok := cachedImageInfo.(*dockerclient.ImageInfo); ok { return cachedImageInfoCasted, nil } else { return nil, fmt.Errorf("Cache casting error") } } }
func (job *Job) Exec(docker *dockerclient.DockerClient, w io.Writer) error { containerConfig := &dockerclient.ContainerConfig{ Image: job.Image, Cmd: []string{"/bin/bash", "-c", job.Cmd}, AttachStdout: true, AttachStderr: true, } cid, err := docker.CreateContainer(containerConfig, fmt.Sprintf("spot-trader-job-%d", job.ID)) if err != nil { return fmt.Errorf("creating container: %v", err) } defer func() { if err = docker.RemoveContainer(cid, false, false); err != nil { log.Printf("error removing container: %v", err) } }() options := &dockerclient.AttachOptions{Stdout: true, Stderr: true, Stream: true} stdout, err := docker.AttachContainer(cid, options) if err != nil { return fmt.Errorf("attaching: %v", err) } err = docker.StartContainer(cid, nil) if err != nil { return fmt.Errorf("starting container: %v", err) } done := make(chan struct{}) go func() { err := copyDockerOut(w, w, stdout) if err != nil { log.Println(err) } stdout.Close() close(done) }() <-done err = docker.StopContainer(cid, 5) if err != nil { log.Printf("error stopping container: %v", err) } return nil }
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 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 }
func loadImages(client *dockerclient.DockerClient, db *sql.DB) error { images, err := client.ListImages() if err != nil { return err } if _, err := db.Exec( "CREATE TABLE images (id, parent_id, size, virtual_size, tag, created)"); err != nil { return err } for _, i := range images { created := time.Unix(i.Created, 0) if _, err := db.Exec( "INSERT INTO images (id, parent_id, size, virtual_size, tag, created) VALUES (?, ?, ?, ?, ?, ?)", i.Id, i.ParentId, i.Size, i.VirtualSize, i.RepoTags[0], created); err != nil { return err } } return nil }
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) } } }
func createContainer(docker *dockerclient.DockerClient, env []string, ctx *ContainerContext) (string, error) { //Make a collection of exposed ports var exposedPorts map[string]struct{} exposedPorts = make(map[string]struct{}) for k, _ := range ctx.PortContext { exposedPorts[k] = struct{}{} } log.Printf("Creating container with environment %v", env) //Build the Docker container config from the configuration provided by the caller containerConfig := dockerclient.ContainerConfig{ Image: ctx.ImageName, Labels: ctx.Labels, ExposedPorts: exposedPorts, Env: env, } //Create the container return docker.CreateContainer(&containerConfig, "") }
func startDevice(devType string, sn string, server string, docker *dc.DockerClient) error { // try to start the associated docker container var cfg dc.ContainerConfig cfg.Image = "device-" + devType cfg.Env = []string{"DEVICE_SN=" + sn, "DEVICE_SERVER=" + server} cfg.Tty = true cfg.AttachStdout = true cfg.AttachStdin = false cfg.AttachStderr = true id, err := docker.CreateContainer(&cfg, "device"+sn) if err != nil { return err } var host dc.HostConfig err = docker.StartContainer(id, &host) return err }
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 startDocker(docker *dockerclient.DockerClient, s2 string, s3 string, s4 string) { var dirpath string = "/tmp/shareDocker" + s3 os.RemoveAll(dirpath) os.Mkdir(dirpath, 0777) var filepath string = dirpath + "/config.ini" //l4g.Info("startDocker -> %v", filepath) var RS string = fmt.Sprintf("startDocker -> %v", filepath) l4g.Info(RS) writeSDLstr(RS) f, _ := os.Create(filepath) f.WriteString("ip_broker=rabbitmq\n") f.Sync() f.WriteString("\n") f.Sync() f.WriteString(s2) f.Sync() f.WriteString("\n") defer f.Close() //l4g.Info("Init the client") RS = fmt.Sprintf("Init the client") l4g.Info(RS) writeSDLstr(RS) l4g.Info(os.Getenv("DOCKER_HOST")) //l4g.Info("DOCKER_HOST") RS = fmt.Sprintf("DOCKER_HOST") l4g.Info(RS) writeSDLstr(RS) var dockerName string = "docker" + s3 supprDocker(docker, dockerName) //l4g.Info(" Create a container") RS = fmt.Sprintf(" Create a container") l4g.Info(RS) writeSDLstr(RS) containerConfig := &dockerclient.ContainerConfig{ Image: "hwgrepo:hwg", User: "******", // Env: []string{"PATH=\/home\/developer\/sdl_sensor_broker\/"}, Cmd: []string{"sh"}, AttachStdin: false, AttachStdout: false, AttachStderr: false, Tty: true, OpenStdin: true, StdinOnce: false, } containerId, err := docker.CreateContainer(containerConfig, dockerName) if err != nil { log.Fatal(err) } var port_tcp string = s3 + "/tcp" portBinding := map[string][]dockerclient.PortBinding{port_tcp: {{"", s3}}} var sharedDocker string = dirpath + ":/home/developer/sdl_sensor_broker/log" l4g.Info(" Start the container") hostConfig := &dockerclient.HostConfig{ Binds: []string{sharedDocker, "/tmp/.X11-unix:/tmp/.X11-unix:rw"}, Privileged: true, PortBindings: portBinding, Links: []string{"rabbitmq:rabbitmq"}, } err = docker.StartContainer(containerId, hostConfig) if err != nil { log.Fatal(err) } execConfig := &dockerclient.ExecConfig{ AttachStdin: false, AttachStdout: false, AttachStderr: false, Tty: true, Cmd: []string{"sh", "runNoVNCplayer.sh", s4, s3}, Container: containerId, Detach: true} _, err = docker.Exec(execConfig) if err != nil { log.Fatal(err) l4g.Info(" error exec container") RS = fmt.Sprintf(" error exec container") l4g.Info(RS) writeSDLstr(RS) } }
func stopVaultOnShutdown(containerId string, docker *dockerclient.DockerClient) { log.Info("... stopping container ", containerId, "...") docker.StopContainer(containerId, 5) docker.RemoveContainer(containerId, true, false) }
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) }