本文整理汇总了Golang中github.com/lxc/lxd/shared.WebsocketSendStream函数的典型用法代码示例。如果您正苦于以下问题:Golang WebsocketSendStream函数的具体用法?Golang WebsocketSendStream怎么用?Golang WebsocketSendStream使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WebsocketSendStream函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: send
func (s *btrfsMigrationSourceDriver) send(conn *websocket.Conn, btrfsPath string, btrfsParent string) error {
args := []string{"send", btrfsPath}
if btrfsParent != "" {
args = append(args, "-p", btrfsParent)
}
cmd := exec.Command("btrfs", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
<-shared.WebsocketSendStream(conn, stdout, 4*1024*1024)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.LogError("problem reading btrfs send stderr", log.Ctx{"err": err})
}
err = cmd.Wait()
if err != nil {
shared.LogError("problem with btrfs send", log.Ctx{"output": string(output)})
}
return err
}
开发者ID:vahe,项目名称:lxd,代码行数:35,代码来源:storage_btrfs.go
示例2: Send
func (s btrfsMigrationSource) Send(conn *websocket.Conn) error {
args := []string{"send", s.btrfsPath}
if s.btrfsParent != "" {
args = append(args, "-p", s.btrfsParent)
}
cmd := exec.Command("btrfs", args...)
deleteAfterSending := func(path string) {
s.btrfs.subvolsDelete(path)
os.Remove(filepath.Dir(path))
}
stdout, err := cmd.StdoutPipe()
if err != nil {
if s.deleteAfterSending {
deleteAfterSending(s.btrfsPath)
}
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
if s.deleteAfterSending {
deleteAfterSending(s.btrfsPath)
}
return err
}
if err := cmd.Start(); err != nil {
if s.deleteAfterSending {
deleteAfterSending(s.btrfsPath)
}
return err
}
<-shared.WebsocketSendStream(conn, stdout)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.Log.Error("problem reading btrfs send stderr", "err", err)
}
err = cmd.Wait()
if err != nil {
shared.Log.Error("problem with btrfs send", "output", string(output))
}
if s.deleteAfterSending {
deleteAfterSending(s.btrfsPath)
}
return err
}
开发者ID:hex2a,项目名称:lxd,代码行数:52,代码来源:storage_btrfs.go
示例3: send
func (s *zfsMigrationSourceDriver) send(conn *websocket.Conn, zfsName string, zfsParent string, readWrapper func(io.ReadCloser) io.ReadCloser) error {
fields := strings.SplitN(s.container.Name(), shared.SnapshotDelimiter, 2)
args := []string{"send", fmt.Sprintf("%s/containers/%[email protected]%s", s.zfs.zfsPool, fields[0], zfsName)}
if zfsParent != "" {
args = append(args, "-i", fmt.Sprintf("%s/containers/%[email protected]%s", s.zfs.zfsPool, s.container.Name(), zfsParent))
}
cmd := exec.Command("zfs", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
readPipe := io.ReadCloser(stdout)
if readWrapper != nil {
readPipe = readWrapper(stdout)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
<-shared.WebsocketSendStream(conn, readPipe, 4*1024*1024)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.LogError("problem reading zfs send stderr", log.Ctx{"err": err})
}
err = cmd.Wait()
if err != nil {
shared.LogError("problem with zfs send", log.Ctx{"output": string(output)})
}
return err
}
开发者ID:akshaykarle,项目名称:lxd,代码行数:42,代码来源:storage_zfs.go
示例4: Exec
// Exec runs a command inside the LXD container. For "interactive" use such as
// `lxc exec ...`, one should pass a controlHandler that talks over the control
// socket and handles things like SIGWINCH. If running non-interactive, passing
// a nil controlHandler will cause Exec to return when all of the command
// output is sent to the output buffers.
func (c *Client) Exec(name string, cmd []string, env map[string]string,
stdin io.ReadCloser, stdout io.WriteCloser,
stderr io.WriteCloser, controlHandler func(*Client, *websocket.Conn)) (int, error) {
body := shared.Jmap{
"command": cmd,
"wait-for-websocket": true,
"interactive": controlHandler != nil,
"environment": env,
}
resp, err := c.post(fmt.Sprintf("containers/%s/exec", name), body, Async)
if err != nil {
return -1, err
}
var fds shared.Jmap
op, err := resp.MetadataAsOperation()
if err == nil && op.Metadata != nil {
fds, err = op.Metadata.GetMap("fds")
if err != nil {
return -1, err
}
} else {
// FIXME: This is a backward compatibility codepath
md := execMd{}
if err := json.Unmarshal(resp.Metadata, &md); err != nil {
return -1, err
}
fds, err = shared.ParseMetadata(md.FDs)
if err != nil {
return -1, err
}
}
if controlHandler != nil {
var control *websocket.Conn
if wsControl, ok := fds["control"]; ok {
control, err = c.websocket(resp.Operation, wsControl.(string))
if err != nil {
return -1, err
}
defer control.Close()
go controlHandler(c, control)
}
conn, err := c.websocket(resp.Operation, fds["0"].(string))
if err != nil {
return -1, err
}
shared.WebsocketSendStream(conn, stdin)
<-shared.WebsocketRecvStream(stdout, conn)
conn.Close()
} else {
conns := make([]*websocket.Conn, 3)
dones := make([]chan bool, 3)
conns[0], err = c.websocket(resp.Operation, fds[strconv.Itoa(0)].(string))
if err != nil {
return -1, err
}
defer conns[0].Close()
dones[0] = shared.WebsocketSendStream(conns[0], stdin)
outputs := []io.WriteCloser{stdout, stderr}
for i := 1; i < 3; i++ {
conns[i], err = c.websocket(resp.Operation, fds[strconv.Itoa(i)].(string))
if err != nil {
return -1, err
}
defer conns[i].Close()
dones[i] = shared.WebsocketRecvStream(outputs[i-1], conns[i])
}
/*
* We'll get a read signal from each of stdout, stderr when they've
* both died. We need to wait for these in addition to the operation,
* because the server may indicate that the operation is done before we
* can actually read the last bits of data off these sockets and print
* it to the screen.
*
* We don't wait for stdin here, because if we're interactive, the user
* may not have closed it (e.g. if the command exits but the user
* didn't ^D).
*/
for i := 1; i < 3; i++ {
<-dones[i]
}
//.........这里部分代码省略.........
开发者ID:djibi2,项目名称:lxd,代码行数:101,代码来源:client.go
示例5: Do
//.........这里部分代码省略.........
case <-controlExit:
return
}
for {
mt, r, err := s.conns[-1].NextReader()
if mt == websocket.CloseMessage {
break
}
if err != nil {
shared.Debugf("Got error getting next reader %s", err)
break
}
buf, err := ioutil.ReadAll(r)
if err != nil {
shared.Debugf("Failed to read message %s", err)
break
}
command := shared.ContainerExecControl{}
if err := json.Unmarshal(buf, &command); err != nil {
shared.Debugf("Failed to unmarshal control socket command: %s", err)
continue
}
if command.Command == "window-resize" {
winchWidth, err := strconv.Atoi(command.Args["width"])
if err != nil {
shared.Debugf("Unable to extract window width: %s", err)
continue
}
winchHeight, err := strconv.Atoi(command.Args["height"])
if err != nil {
shared.Debugf("Unable to extract window height: %s", err)
continue
}
err = shared.SetSize(int(ptys[0].Fd()), winchWidth, winchHeight)
if err != nil {
shared.Debugf("Failed to set window size to: %dx%d", winchWidth, winchHeight)
continue
}
}
if err != nil {
shared.Debugf("Got error writing to writer %s", err)
break
}
}
}()
go func() {
<-shared.WebsocketMirror(s.conns[0], ptys[0], ptys[0])
wgEOF.Done()
}()
} else {
wgEOF.Add(len(ttys) - 1)
for i := 0; i < len(ttys); i++ {
go func(i int) {
if i == 0 {
<-shared.WebsocketRecvStream(ttys[i], s.conns[i])
ttys[i].Close()
} else {
<-shared.WebsocketSendStream(s.conns[i], ptys[i])
ptys[i].Close()
wgEOF.Done()
}
}(i)
}
}
result := runCommand(
s.container,
s.command,
s.options,
)
for _, tty := range ttys {
tty.Close()
}
if s.conns[-1] == nil {
if s.interactive {
controlExit <- true
}
} else {
s.conns[-1].Close()
}
wgEOF.Wait()
for _, pty := range ptys {
pty.Close()
}
return result
}
开发者ID:pombredanne,项目名称:lxd,代码行数:101,代码来源:container_exec.go
示例6: Send
func (s zfsMigrationSource) Send(conn *websocket.Conn) error {
args := []string{"send", fmt.Sprintf("%s/%s", s.zfs.zfsPool, s.zfsName)}
if s.zfsParent != "" {
args = append(args, "-i", fmt.Sprintf("%s/%s", s.zfs.zfsPool, s.zfsParent))
}
cmd := exec.Command("zfs", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
/* If this is not a lxd snapshot, that means it is the root container.
* The way we zfs send a root container is by taking a temporary zfs
* snapshot and sending that, then deleting that snapshot. Here's where
* we delete it.
*
* Note that we can't use a defer here, because zfsDestroy
* takes some time, and defer doesn't block the current
* goroutine. Due to our retry mechanism for network failures
* (and because zfsDestroy takes a while), we might retry
* moving (and thus creating a temporary snapshot) before the
* last one is deleted, resulting in either a snapshot name
* collision if it was fast enough, or an extra snapshot with
* an odd name on the destination side. Instead, we don't use
* defer so we always block until the snapshot is dead.
*/
if s.deleteAfterSending {
s.zfs.zfsDestroy(s.zfsName)
}
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
if s.deleteAfterSending {
s.zfs.zfsDestroy(s.zfsName)
}
return err
}
if err := cmd.Start(); err != nil {
if s.deleteAfterSending {
s.zfs.zfsDestroy(s.zfsName)
}
return err
}
<-shared.WebsocketSendStream(conn, stdout)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.Log.Error("problem reading zfs send stderr", "err", err)
}
err = cmd.Wait()
if err != nil {
shared.Log.Error("problem with zfs send", "output", string(output))
}
if s.deleteAfterSending {
s.zfs.zfsDestroy(s.zfsName)
}
return err
}
开发者ID:mickydelfavero,项目名称:lxd,代码行数:62,代码来源:storage_zfs.go
示例7: Exec
func (c *Client) Exec(name string, cmd []string, env map[string]string, stdin *os.File, stdout *os.File, stderr *os.File) (int, error) {
interactive := terminal.IsTerminal(int(stdin.Fd()))
body := shared.Jmap{"command": cmd, "wait-for-websocket": true, "interactive": interactive, "environment": env}
resp, err := c.post(fmt.Sprintf("containers/%s/exec", name), body, Async)
if err != nil {
return -1, err
}
md := execMd{}
if err := json.Unmarshal(resp.Metadata, &md); err != nil {
return -1, err
}
if interactive {
if wsControl, ok := md.FDs["control"]; ok {
go func() {
control, err := c.websocket(resp.Operation, wsControl)
if err != nil {
return
}
for {
width, height, err := terminal.GetSize(syscall.Stdout)
if err != nil {
continue
}
shared.Debugf("Window size is now: %dx%d", width, height)
w, err := control.NextWriter(websocket.TextMessage)
if err != nil {
shared.Debugf("Got error getting next writer %s", err)
break
}
msg := shared.ContainerExecControl{}
msg.Command = "window-resize"
msg.Args = make(map[string]string)
msg.Args["width"] = strconv.Itoa(width)
msg.Args["height"] = strconv.Itoa(height)
buf, err := json.Marshal(msg)
if err != nil {
shared.Debugf("Failed to convert to json %s", err)
break
}
_, err = w.Write(buf)
w.Close()
if err != nil {
shared.Debugf("Got err writing %s", err)
break
}
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGWINCH)
sig := <-ch
shared.Debugf("Received '%s signal', updating window geometry.", sig)
}
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
control.WriteMessage(websocket.CloseMessage, closeMsg)
}()
}
conn, err := c.websocket(resp.Operation, md.FDs["0"])
if err != nil {
return -1, err
}
shared.WebsocketSendStream(conn, stdin)
<-shared.WebsocketRecvStream(stdout, conn)
} else {
sources := []*os.File{stdin, stdout, stderr}
conns := make([]*websocket.Conn, 3)
dones := make([]chan bool, 3)
for i := 0; i < 3; i++ {
conns[i], err = c.websocket(resp.Operation, md.FDs[strconv.Itoa(i)])
if err != nil {
return -1, err
}
if i == 0 {
dones[i] = shared.WebsocketSendStream(conns[i], sources[i])
} else {
dones[i] = shared.WebsocketRecvStream(sources[i], conns[i])
}
}
/*
* We'll get a read signal from each of stdout, stderr when they've
* both died. We need to wait for these in addition to the operation,
* because the server may indicate that the operation is done before we
* can actually read the last bits of data off these sockets and print
* it to the screen.
*
* We don't wait for stdin here, because if we're interactive, the user
* may not have closed it (e.g. if the command exits but the user
//.........这里部分代码省略.........
开发者ID:achanda,项目名称:lxd,代码行数:101,代码来源:client.go
示例8: Do
//.........这里部分代码省略.........
case <-controlExit:
return
}
for {
mt, r, err := s.conns[-1].NextReader()
if mt == websocket.CloseMessage {
break
}
if err != nil {
shared.Debugf("Got error getting next reader %s", err)
break
}
buf, err := ioutil.ReadAll(r)
if err != nil {
shared.Debugf("Failed to read message %s", err)
break
}
command := shared.ContainerExecControl{}
if err := json.Unmarshal(buf, &command); err != nil {
shared.Debugf("Failed to unmarshal control socket command: %s", err)
continue
}
if command.Command == "window-resize" {
winchWidth, err := strconv.Atoi(command.Args["width"])
if err != nil {
shared.Debugf("Unable to extract window width: %s", err)
continue
}
winchHeight, err := strconv.Atoi(command.Args["height"])
if err != nil {
shared.Debugf("Unable to extract window height: %s", err)
continue
}
err = shared.SetSize(int(ptys[0].Fd()), winchWidth, winchHeight)
if err != nil {
shared.Debugf("Failed to set window size to: %dx%d", winchWidth, winchHeight)
continue
}
}
}
}()
go func() {
readDone, writeDone := shared.WebsocketMirror(s.conns[0], ptys[0], ptys[0])
<-readDone
<-writeDone
s.conns[0].Close()
wgEOF.Done()
}()
} else {
wgEOF.Add(len(ttys) - 1)
for i := 0; i < len(ttys); i++ {
go func(i int) {
if i == 0 {
<-shared.WebsocketRecvStream(ttys[i], s.conns[i])
ttys[i].Close()
} else {
<-shared.WebsocketSendStream(s.conns[i], ptys[i])
ptys[i].Close()
wgEOF.Done()
}
}(i)
}
}
cmdResult, cmdErr := s.container.Exec(s.command, s.env, stdin, stdout, stderr)
for _, tty := range ttys {
tty.Close()
}
if s.conns[-1] == nil {
if s.interactive {
controlExit <- true
}
} else {
s.conns[-1].Close()
}
wgEOF.Wait()
for _, pty := range ptys {
pty.Close()
}
metadata := shared.Jmap{"return": cmdResult}
err = op.UpdateMetadata(metadata)
if err != nil {
return err
}
return cmdErr
}
开发者ID:jsavikko,项目名称:lxd,代码行数:101,代码来源:container_exec.go
示例9: Do
//.........这里部分代码省略.........
shared.LogDebugf("Unable to extract window height: %s", err)
continue
}
err = shared.SetSize(int(ptys[0].Fd()), winchWidth, winchHeight)
if err != nil {
shared.LogDebugf("Failed to set window size to: %dx%d", winchWidth, winchHeight)
continue
}
} else if command.Command == "signal" {
if err := syscall.Kill(receivedPid, command.Signal); err != nil {
shared.LogDebugf("Failed forwarding signal '%s' to PID %d.", command.Signal, receivedPid)
continue
}
shared.LogDebugf("Forwarded signal '%s' to PID %d.", command.Signal, receivedPid)
}
}
}()
go func() {
readDone, writeDone := shared.WebsocketMirror(s.conns[0], ptys[0], ptys[0])
<-readDone
<-writeDone
s.conns[0].Close()
wgEOF.Done()
}()
} else {
wgEOF.Add(len(ttys) - 1)
for i := 0; i < len(ttys); i++ {
go func(i int) {
if i == 0 {
<-shared.WebsocketRecvStream(ttys[i], s.conns[i])
ttys[i].Close()
} else {
<-shared.WebsocketSendStream(s.conns[i], ptys[i], -1)
ptys[i].Close()
wgEOF.Done()
}
}(i)
}
}
finisher := func(cmdResult int, cmdErr error) error {
for _, tty := range ttys {
tty.Close()
}
if s.conns[-1] == nil {
if s.interactive {
controlExit <- true
}
} else {
s.conns[-1].Close()
}
wgEOF.Wait()
for _, pty := range ptys {
pty.Close()
}
metadata := shared.Jmap{"return": cmdResult}
err = op.UpdateMetadata(metadata)
if err != nil {
return err
}
开发者ID:vahe,项目名称:lxd,代码行数:66,代码来源:container_exec.go
示例10: Do
//.........这里部分代码省略.........
shared.LogDebugf("Unable to extract window height: %s", err)
continue
}
err = shared.SetSize(int(ptys[0].Fd()), winchWidth, winchHeight)
if err != nil {
shared.LogDebugf("Failed to set window size to: %dx%d", winchWidth, winchHeight)
continue
}
} else if command.Command == "signal" {
if err := syscall.Kill(receivedPid, command.Signal); err != nil {
shared.LogDebugf("Failed forwarding signal '%s' to PID %d.", command.Signal, receivedPid)
continue
}
shared.LogDebugf("Forwarded signal '%s' to PID %d.", command.Signal, receivedPid)
}
}
}()
go func() {
readDone, writeDone := shared.WebsocketMirror(s.conns[0], ptys[0], ptys[0])
<-readDone
<-writeDone
s.conns[0].Close()
wgEOF.Done()
}()
} else {
wgEOF.Add(len(ttys) - 1)
for i := 0; i < len(ttys); i++ {
go func(i int) {
if i == 0 {
<-shared.WebsocketRecvStream(ttys[i], s.conns[i])
ttys[i].Close()
} else {
<-shared.WebsocketSendStream(s.conns[i], ptys[i], -1)
ptys[i].Close()
wgEOF.Done()
}
}(i)
}
}
finisher := func(cmdResult int, cmdErr error) error {
for _, tty := range ttys {
tty.Close()
}
if s.conns[-1] == nil {
if s.interactive {
controlExit <- true
}
} else {
s.conns[-1].Close()
}
wgEOF.Wait()
for _, pty := range ptys {
pty.Close()
}
metadata := shared.Jmap{"return": cmdResult}
err = op.UpdateMetadata(metadata)
if err != nil {
return err
}
开发者ID:akshaykarle,项目名称:lxd,代码行数:66,代码来源:container_exec.go
注:本文中的github.com/lxc/lxd/shared.WebsocketSendStream函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论