本文整理汇总了Golang中github.com/docker/docker/pkg/stdcopy.NewStdWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewStdWriter函数的具体用法?Golang NewStdWriter怎么用?Golang NewStdWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewStdWriter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: postContainerExecStart
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
version := httputils.VersionFromContext(ctx)
if versions.GreaterThan(version, "1.21") {
if err := httputils.CheckForJSON(r); err != nil {
return err
}
}
var (
execName = vars["name"]
stdin, inStream io.ReadCloser
stdout, stderr, outStream io.Writer
)
execStartCheck := &types.ExecStartCheck{}
if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
return err
}
if exists, err := s.backend.ExecExists(execName); !exists {
return err
}
if !execStartCheck.Detach {
var err error
// Setting up the streaming http interface.
inStream, outStream, err = httputils.HijackConnection(w)
if err != nil {
return err
}
defer httputils.CloseStreams(inStream, outStream)
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
stdin = inStream
stdout = outStream
if !execStartCheck.Tty {
stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
}
}
// Now run the user process in container.
if err := s.backend.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
if execStartCheck.Detach {
return err
}
stdout.Write([]byte(err.Error() + "\r\n"))
logrus.Errorf("Error running exec in container: %v", err)
}
return nil
}
示例2: handleContainerLogs
func handleContainerLogs(w http.ResponseWriter, r *http.Request) {
var outStream, errStream io.Writer
outStream = ioutils.NewWriteFlusher(w)
// not sure how to test follow
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), 500)
}
stdout, stderr := getBoolValue(r.Form.Get("stdout")), getBoolValue(r.Form.Get("stderr"))
if stderr {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
}
if stdout {
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
}
var i int
if tail, err := strconv.Atoi(r.Form.Get("tail")); err == nil && tail > 0 {
i = 50 - tail
if i < 0 {
i = 0
}
}
for ; i < 50; i++ {
line := fmt.Sprintf("line %d", i)
if getBoolValue(r.Form.Get("timestamps")) {
l := &jsonlog.JSONLog{Log: line, Created: time.Now()}
line = fmt.Sprintf("%s %s", l.Created.Format(timeutils.RFC3339NanoFixed), line)
}
if i%2 == 0 && stderr {
fmt.Fprintln(errStream, line)
} else if i%2 == 1 && stdout {
fmt.Fprintln(outStream, line)
}
}
}
示例3: ContainerAttachWithLogs
func (daemon *Daemon) ContainerAttachWithLogs(name string, c *ContainerAttachWithLogsConfig) error {
container, err := daemon.Get(name)
if err != nil {
return err
}
var errStream io.Writer
if !container.Config.Tty && c.Multiplex {
errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr)
c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout)
} else {
errStream = c.OutStream
}
var stdin io.ReadCloser
var stdout, stderr io.Writer
if c.UseStdin {
stdin = c.InStream
}
if c.UseStdout {
stdout = c.OutStream
}
if c.UseStderr {
stderr = errStream
}
return container.AttachWithLogs(stdin, stdout, stderr, c.Logs, c.Stream)
}
示例4: ContainerAttachWithLogs
func (daemon *Daemon) ContainerAttachWithLogs(container *Container, c *ContainerAttachWithLogsConfig) error {
var errStream io.Writer
if !container.Config.Tty {
errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr)
c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout)
} else {
errStream = c.OutStream
}
var stdin io.ReadCloser
var stdout, stderr io.Writer
if c.UseStdin {
stdin = c.InStream
}
if c.UseStdout {
stdout = c.OutStream
}
if c.UseStderr {
stderr = errStream
}
return container.AttachWithLogs(stdin, stdout, stderr, c.Logs, c.Stream)
}
示例5: attach
func (c *Container) attach(stdin io.ReadCloser, stdout io.WriteCloser, winsize *hypervisor.WindowSize, rsp chan<- error) error {
if c.p.sandbox == nil || c.descript == nil {
err := fmt.Errorf("container not ready for attach")
c.Log(ERROR, err)
return err
}
tty := &hypervisor.TtyIO{
Stdin: stdin,
Stdout: stdout,
Callback: make(chan *runvtypes.VmResponse, 1),
}
if stdout != nil {
if !c.hasTty() {
tty.Stderr = stdcopy.NewStdWriter(stdout, stdcopy.Stderr)
tty.Stdout = stdcopy.NewStdWriter(stdout, stdcopy.Stdout)
}
}
if rsp != nil {
go func() {
rsp <- tty.WaitForFinish()
}()
}
return c.p.sandbox.Attach(tty, c.Id(), winsize)
}
示例6: postContainerExecStart
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
func postContainerExecStart(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return nil
}
var (
name = vars["name"]
job = eng.Job("execStart", name)
errOut io.Writer = os.Stderr
)
if err := job.DecodeEnv(r.Body); err != nil {
return err
}
if !job.GetenvBool("Detach") {
// Setting up the streaming http interface.
inStream, outStream, err := hijackServer(w)
if err != nil {
return err
}
defer func() {
if tcpc, ok := inStream.(*net.TCPConn); ok {
tcpc.CloseWrite()
} else {
inStream.Close()
}
}()
defer func() {
if tcpc, ok := outStream.(*net.TCPConn); ok {
tcpc.CloseWrite()
} else if closer, ok := outStream.(io.Closer); ok {
closer.Close()
}
}()
var errStream io.Writer
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
if !job.GetenvBool("Tty") && version.GreaterThanOrEqualTo("1.6") {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else {
errStream = outStream
}
job.Stdin.Add(inStream)
job.Stdout.Add(outStream)
job.Stderr.Set(errStream)
errOut = outStream
}
// Now run the user process in container.
job.SetCloseIO(false)
if err := job.Run(); err != nil {
fmt.Fprintf(errOut, "Error starting exec command in container %s: %s\n", name, err)
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
示例7: StartExec
func (p *XPod) StartExec(stdin io.ReadCloser, stdout io.WriteCloser, containerId, execId string) error {
p.statusLock.RLock()
es, ok := p.execs[execId]
p.statusLock.RUnlock()
if !ok {
err := fmt.Errorf("no exec %s exists for container %s", execId, containerId)
p.Log(ERROR, err)
return err
}
tty := &hypervisor.TtyIO{
Stdin: stdin,
Stdout: stdout,
Callback: make(chan *types.VmResponse, 1),
}
if !es.Terminal && stdout != nil {
tty.Stderr = stdcopy.NewStdWriter(stdout, stdcopy.Stderr)
tty.Stdout = stdcopy.NewStdWriter(stdout, stdcopy.Stdout)
}
var fin = true
for fin {
select {
case fin = <-es.finChan:
es.Log(DEBUG, "try to drain the sync chan")
default:
fin = false
es.Log(DEBUG, "the sync chan is empty")
}
}
go func(es *Exec) {
result := p.sandbox.WaitProcess(false, []string{execId}, -1)
if result == nil {
es.Log(ERROR, "can not wait exec")
return
}
r, ok := <-result
if !ok {
es.Log(ERROR, "waiting exec interrupted")
return
}
es.Log(DEBUG, "exec terminated at %v with code %d", r.FinishedAt, r.Code)
es.ExitCode = uint8(r.Code)
select {
case es.finChan <- true:
es.Log(DEBUG, "wake exec stopped chan")
default:
es.Log(WARNING, "exec already set as stopped")
}
}(es)
return p.sandbox.Exec(es.Container, es.Id, es.Cmds, es.Terminal, tty)
}
示例8: postContainersAttach
func postContainersAttach(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
if vars == nil {
return fmt.Errorf("Missing parameter")
}
d := getDaemon(eng)
cont, err := d.Get(vars["name"])
if err != nil {
return err
}
inStream, outStream, err := hijackServer(w)
if err != nil {
return err
}
defer closeStreams(inStream, outStream)
var errStream io.Writer
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
if !cont.Config.Tty && version.GreaterThanOrEqualTo("1.6") {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else {
errStream = outStream
}
logs := toBool(r.Form.Get("logs"))
stream := toBool(r.Form.Get("stream"))
var stdin io.ReadCloser
var stdout, stderr io.Writer
if toBool(r.Form.Get("stdin")) {
stdin = inStream
}
if toBool(r.Form.Get("stdout")) {
stdout = outStream
}
if toBool(r.Form.Get("stderr")) {
stderr = errStream
}
if err := cont.AttachWithLogs(stdin, stdout, stderr, logs, stream); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
}
示例9: ContainerAttachWithLogs
// ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
if c.Hijacker == nil {
return derr.ErrorCodeNoHijackConnection.WithArgs(prefixOrName)
}
container, err := daemon.GetContainer(prefixOrName)
if err != nil {
return derr.ErrorCodeNoSuchContainer.WithArgs(prefixOrName)
}
if container.IsPaused() {
return derr.ErrorCodePausedContainer.WithArgs(prefixOrName)
}
conn, _, err := c.Hijacker.Hijack()
if err != nil {
return err
}
defer conn.Close()
// Flush the options to make sure the client sets the raw mode
conn.Write([]byte{})
inStream := conn.(io.ReadCloser)
outStream := conn.(io.Writer)
if c.Upgrade {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
var errStream io.Writer
if !container.Config.Tty {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else {
errStream = outStream
}
var stdin io.ReadCloser
var stdout, stderr io.Writer
if c.UseStdin {
stdin = inStream
}
if c.UseStdout {
stdout = outStream
}
if c.UseStderr {
stderr = errStream
}
if err := daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream, c.DetachKeys); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
}
示例10: postContainersAttach
func postContainersAttach(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
if vars == nil {
return fmt.Errorf("Missing parameter")
}
var (
job = eng.Job("container_inspect", vars["name"])
c, err = job.Stdout.AddEnv()
)
if err != nil {
return err
}
if err = job.Run(); err != nil {
return err
}
inStream, outStream, err := hijackServer(w)
if err != nil {
return err
}
defer closeStreams(inStream, outStream)
var errStream io.Writer
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
if c.GetSubEnv("Config") != nil && !c.GetSubEnv("Config").GetBool("Tty") && version.GreaterThanOrEqualTo("1.6") {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else {
errStream = outStream
}
job = eng.Job("attach", vars["name"])
job.Setenv("logs", r.Form.Get("logs"))
job.Setenv("stream", r.Form.Get("stream"))
job.Setenv("stdin", r.Form.Get("stdin"))
job.Setenv("stdout", r.Form.Get("stdout"))
job.Setenv("stderr", r.Form.Get("stderr"))
job.Stdin.Add(inStream)
job.Stdout.Add(outStream)
job.Stderr.Set(errStream)
if err := job.Run(); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
}
示例11: postContainerExecStart
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
func (s *Server) postContainerExecStart(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return nil
}
var (
execName = vars["name"]
stdin io.ReadCloser
stdout io.Writer
stderr io.Writer
)
execStartCheck := &types.ExecStartCheck{}
if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
return err
}
if !execStartCheck.Detach {
// Setting up the streaming http interface.
inStream, outStream, err := hijackServer(w)
if err != nil {
return err
}
defer closeStreams(inStream, outStream)
var errStream io.Writer
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
if !execStartCheck.Tty && version.GreaterThanOrEqualTo("1.6") {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else {
errStream = outStream
}
stdin = inStream
stdout = outStream
stderr = errStream
}
// Now run the user process in container.
if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
logrus.Errorf("Error starting exec command in container %s: %s", execName, err)
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
示例12: StartExec
func (daemon *Daemon) StartExec(stdin io.ReadCloser, stdout io.WriteCloser, containerId, execId string) error {
tty := &hypervisor.TtyIO{
Stdin: stdin,
Stdout: stdout,
Callback: make(chan *types.VmResponse, 1),
}
glog.V(1).Infof("Get container id is %s", containerId)
pod, _, err := daemon.GetPodByContainerIdOrName(containerId)
if err != nil {
return err
}
status := pod.Status()
if status == nil || status.Status != types.S_POD_RUNNING {
return fmt.Errorf("container %s is not running", containerId)
}
es := status.GetExec(execId)
if es == nil {
return fmt.Errorf("Can not find exec %s", execId)
}
vmId, err := daemon.GetVmByPodId(pod.Id)
if err != nil {
return err
}
vm, ok := daemon.VmList.Get(vmId)
if !ok {
err = fmt.Errorf("Can not find VM whose Id is %s!", vmId)
return err
}
if !es.Terminal {
tty.Stderr = stdcopy.NewStdWriter(stdout, stdcopy.Stderr)
tty.Stdout = stdcopy.NewStdWriter(stdout, stdcopy.Stdout)
tty.OutCloser = stdout
}
if err := vm.Exec(es.Container, es.Id, es.Cmds, es.Terminal, tty); err != nil {
return err
}
defer func() {
glog.V(2).Info("Defer function for exec!")
}()
return nil
}
示例13: postContainerExecStart
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
func (s *Server) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}
if err := checkForJSON(r); err != nil {
return err
}
var (
execName = vars["name"]
stdin, inStream io.ReadCloser
stdout, stderr, outStream io.Writer
)
execStartCheck := &types.ExecStartCheck{}
if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
return err
}
if !execStartCheck.Detach {
var err error
// Setting up the streaming http interface.
inStream, outStream, err = hijackServer(w)
if err != nil {
return err
}
defer closeStreams(inStream, outStream)
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
stdin = inStream
stdout = outStream
if !execStartCheck.Tty {
stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
}
} else {
outStream = w
}
// Now run the user process in container.
if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
fmt.Fprintf(outStream, "Error running exec in container: %v\n", err)
}
return nil
}
示例14: ContainerAttach
// ContainerAttach attaches to logs according to the config passed in. See ContainerAttachConfig.
func (daemon *Daemon) ContainerAttach(prefixOrName string, c *backend.ContainerAttachConfig) error {
keys := []byte{}
var err error
if c.DetachKeys != "" {
keys, err = term.ToBytes(c.DetachKeys)
if err != nil {
return fmt.Errorf("Invalid escape keys (%s) provided", c.DetachKeys)
}
}
container, err := daemon.GetContainer(prefixOrName)
if err != nil {
return err
}
if container.IsPaused() {
err := fmt.Errorf("Container %s is paused. Unpause the container before attach", prefixOrName)
return errors.NewRequestConflictError(err)
}
inStream, outStream, errStream, err := c.GetStreams()
if err != nil {
return err
}
defer inStream.Close()
if !container.Config.Tty && c.MuxStreams {
errStream = stdcopy.NewStdWriter(errStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
}
var stdin io.ReadCloser
var stdout, stderr io.Writer
if c.UseStdin {
stdin = inStream
}
if c.UseStdout {
stdout = outStream
}
if c.UseStderr {
stderr = errStream
}
if err := daemon.containerAttach(container, stdin, stdout, stderr, c.Logs, c.Stream, keys); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
}
示例15: ContainerLogs
// ContainerLogs hooks up a container's stdout and stderr streams
// configured with the given struct.
func (c *Container) ContainerLogs(name string, config *backend.ContainerLogsConfig, started chan struct{}) error {
defer trace.End(trace.Begin(""))
// Look up the container name in the metadata cache to get long ID
vc := cache.ContainerCache().GetContainer(name)
if vc == nil {
return NotFoundError(name)
}
name = vc.ContainerID
tailLines, since, err := c.validateContainerLogsConfig(vc, config)
if err != nil {
return err
}
// Outstream modification (from Docker's code) so the stream is streamed with the
// necessary headers that the CLI expects. This is Docker's scheme.
wf := ioutils.NewWriteFlusher(config.OutStream)
defer wf.Close()
wf.Flush()
outStream := io.Writer(wf)
if !vc.Config.Tty {
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
}
// Make a call to our proxy to handle the remoting
err = c.containerProxy.StreamContainerLogs(name, outStream, started, config.Timestamps, config.Follow, since, tailLines)
return err
}