本文整理汇总了Golang中github.com/cloudfoundry-incubator/garden.Container.Attach方法的典型用法代码示例。如果您正苦于以下问题:Golang Container.Attach方法的具体用法?Golang Container.Attach怎么用?Golang Container.Attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudfoundry-incubator/garden.Container
的用法示例。
在下文中一共展示了Container.Attach方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
flag.Parse()
gclient := client.New(connection.New("tcp", "localhost:7777"))
var container garden.Container
containers, err := gclient.Containers(garden.Properties{})
must(err)
for _, c := range containers {
if c.Handle() == *containerHandle {
container = c
break
}
}
if container == nil {
panic("Container not found!")
}
process, err := container.Attach(uint32(*processId), garden.ProcessIO{})
must(err)
switch *signalType {
case "term":
fmt.Println("Signalling term")
must(process.Signal(garden.SignalTerminate))
break
case "kill":
fmt.Println("Signalling kill")
must(process.Signal(garden.SignalKill))
break
}
}
示例2:
process, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "cat",
}, garden.ProcessIO{})
Expect(err).ToNot(HaveOccurred())
processID = process.ID()
})
Measure("does not leak goroutines", func(b Benchmarker) {
for i := 1; i <= iterations; i++ {
stdoutR, stdoutW := io.Pipe()
stdinR, stdinW := io.Pipe()
_, err := container.Attach(processID, garden.ProcessIO{
Stdin: stdinR,
Stdout: stdoutW,
})
Expect(err).ToNot(HaveOccurred())
stdinData := fmt.Sprintf("hello %d", i)
_, err = stdinW.Write([]byte(stdinData + "\n"))
Expect(err).ToNot(HaveOccurred())
var line []byte
doneReading := make(chan struct{})
go func() {
line, _, err = bufio.NewReader(stdoutR).ReadLine()
close(doneReading)
}()
示例3:
_, err = fmt.Fprintf(io.Stderr, "stderr data")
Ω(err).ShouldNot(HaveOccurred())
}()
return process, nil
}
stdout := gbytes.NewBuffer()
stderr := gbytes.NewBuffer()
processIO := garden.ProcessIO{
Stdout: stdout,
Stderr: stderr,
}
process, err := container.Attach(42, processIO)
Ω(err).ShouldNot(HaveOccurred())
attachedHandle, attachedID, attachedIO := fakeConnection.AttachArgsForCall(0)
Ω(attachedHandle).Should(Equal("some-handle"))
Ω(attachedID).Should(Equal(uint32(42)))
Ω(attachedIO).Should(Equal(processIO))
Ω(process.ID()).Should(Equal(uint32(42)))
status, err := process.Wait()
Ω(err).ShouldNot(HaveOccurred())
Ω(status).Should(Equal(123))
Eventually(stdout).Should(gbytes.Say("stdout data"))
Eventually(stderr).Should(gbytes.Say("stderr data"))
示例4:
_, err = fmt.Fprintf(io.Stderr, "stderr data")
Ω(err).ShouldNot(HaveOccurred())
}()
return process, nil
}
stdout := gbytes.NewBuffer()
stderr := gbytes.NewBuffer()
processIO := garden.ProcessIO{
Stdout: stdout,
Stderr: stderr,
}
process, err := container.Attach("process-handle", processIO)
Ω(err).ShouldNot(HaveOccurred())
attachedHandle, attachedID, attachedIO := fakeConnection.AttachArgsForCall(0)
Ω(attachedHandle).Should(Equal("some-handle"))
Ω(attachedID).Should(Equal("process-handle"))
Ω(attachedIO).Should(Equal(processIO))
Ω(process.ID()).Should(Equal("process-handle"))
status, err := process.Wait()
Ω(err).ShouldNot(HaveOccurred())
Ω(status).Should(Equal(123))
Eventually(stdout).Should(gbytes.Say("stdout data"))
Eventually(stderr).Should(gbytes.Say("stderr data"))
示例5:
return process, nil
}
})
It("responds with a ProcessPayload for every chunk", func() {
stdout := gbytes.NewBuffer()
stderr := gbytes.NewBuffer()
processIO := garden.ProcessIO{
Stdin: bytes.NewBufferString("stdin data"),
Stdout: stdout,
Stderr: stderr,
}
process, err := container.Attach(42, processIO)
Ω(err).ShouldNot(HaveOccurred())
pid, _ := fakeContainer.AttachArgsForCall(0)
Ω(pid).Should(Equal(uint32(42)))
Eventually(stdout).Should(gbytes.Say("stdout data"))
Eventually(stdout).Should(gbytes.Say("mirrored stdin data"))
Eventually(stderr).Should(gbytes.Say("stderr data"))
status, err := process.Wait()
Ω(err).ShouldNot(HaveOccurred())
Ω(status).Should(Equal(123))
})
itResetsGraceTimeWhenHandling(func() {
示例6:
It("continues to stream", func() {
process, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", "while true; do echo hi; sleep 0.5; done"},
}, garden.ProcessIO{})
Expect(err).ToNot(HaveOccurred())
restartGarden(gardenArgs...)
_, err = process.Wait()
Expect(err).To(HaveOccurred())
stdout := gbytes.NewBuffer()
_, err = container.Attach(process.ID(), garden.ProcessIO{
Stdout: stdout,
})
Expect(err).ToNot(HaveOccurred())
Eventually(stdout, 30.0).Should(gbytes.Say("hi\n"))
})
It("can still accept stdin", func() {
r, w := io.Pipe()
stdout := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", "cat <&0"},