// Execute command, intercepts stdout and print info func commandWrapper(c *gin.Context, command string, args []string) { old_stdout := os.Stdout // keep backup of the real stdout old_stderr := os.Stderr // keep backup of the real stdout r, w, _ := os.Pipe() os.Stdout = w os.Stderr = w err := lxclib.RunCommand(config, command, args) if err != nil { c.String(400, err.Error()) c.Error(err) } outC := make(chan string) // copy the output in a separate goroutine so printing can't block indefinitely go func() { var buf bytes.Buffer io.Copy(&buf, r) outC <- buf.String() }() // back to normal state w.Close() os.Stdout = old_stdout // restoring the real stdout os.Stderr = old_stderr // restoring the real stdout out := <-outC c.String(200, out) }
// URL: /snapshot/:remote/:container func webSnapshot(c *gin.Context) { remote := c.Param("remote") container := c.Param("container") // snapname := c.Param("snapname") args := []string{remote + ":" + container} err := lxclib.RunCommand(config, "snapshot", args) if err != nil { c.String(400, err.Error()) c.Error(err) } c.String(200, "Snapshot DONE") }
// URL: /snapshot/:remote/:container func webMove(c *gin.Context) { from := c.Param("from") to := c.Param("to") // snapname := c.Param("snapname") args := []string{from, to} err := lxclib.RunCommand(config, "move", args) if err != nil { c.String(400, err.Error()) c.Error(err) } c.String(200, "Move DONE") }
func webRemote(c *gin.Context) { remote := c.Param("remote") addr := c.Query("addr") password := c.Query("password") if password == "" { c.String(400, "Need to define a remote server password") c.Error(fmt.Errorf("Need to define a remote server password")) } if addr == "" { c.String(400, "Need to define a remote addr") c.Error(fmt.Errorf("Need to define a remote addr")) } args := []string{"add", remote, addr, "true", password, "true"} err := lxclib.RunCommand(config, "remote", args) if err != nil { c.String(400, err.Error()) c.Error(err) } c.String(200, "Remote Added") }