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 (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 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 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) }
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) } }