2016-07-29 22:35:10 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-08 18:58:41 +00:00
|
|
|
"fmt"
|
2016-09-19 07:21:14 +00:00
|
|
|
"io"
|
2016-07-29 22:35:10 +00:00
|
|
|
"os/exec"
|
2016-08-08 18:58:41 +00:00
|
|
|
"strings"
|
2017-03-02 00:50:09 +00:00
|
|
|
|
|
|
|
systemdDbus "github.com/coreos/go-systemd/dbus"
|
|
|
|
"github.com/godbus/dbus"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// ExecCmd executes a command with args and returns its output as a string along
|
|
|
|
// with an error, if any
|
2016-07-29 22:35:10 +00:00
|
|
|
func ExecCmd(name string, args ...string) (string, error) {
|
|
|
|
cmd := exec.Command(name, args...)
|
2016-08-08 18:58:41 +00:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2016-07-29 22:35:10 +00:00
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2017-05-11 09:45:31 +00:00
|
|
|
return "", fmt.Errorf("`%v %v` failed: %v %v (%v)", name, strings.Join(args, " "), stderr.String(), stdout.String(), err)
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:58:41 +00:00
|
|
|
return stdout.String(), nil
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
2016-08-01 23:48:12 +00:00
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
// ExecCmdWithStdStreams execute a command with the specified standard streams.
|
2016-09-19 07:21:14 +00:00
|
|
|
func ExecCmdWithStdStreams(stdin io.Reader, stdout, stderr io.Writer, name string, args ...string) error {
|
2016-08-01 17:39:42 +00:00
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
cmd.Stdin = stdin
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("`%v %v` failed: %v", name, strings.Join(args, " "), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
// StatusToExitCode converts wait status code to an exit code
|
|
|
|
func StatusToExitCode(status int) int {
|
|
|
|
return ((status) & 0xff00) >> 8
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
2017-03-02 00:50:09 +00:00
|
|
|
|
|
|
|
// RunUnderSystemdScope adds the specified pid to a systemd scope
|
|
|
|
func RunUnderSystemdScope(pid int, slice string, unitName string) error {
|
|
|
|
var properties []systemdDbus.Property
|
|
|
|
conn, err := systemdDbus.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
properties = append(properties, systemdDbus.PropSlice(slice))
|
|
|
|
properties = append(properties, newProp("PIDs", []uint32{uint32(pid)}))
|
|
|
|
properties = append(properties, newProp("Delegate", true))
|
|
|
|
properties = append(properties, newProp("DefaultDependencies", false))
|
2017-06-20 11:05:40 +00:00
|
|
|
ch := make(chan string)
|
|
|
|
_, err = conn.StartTransientUnit(unitName, "replace", properties, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block until job is started
|
|
|
|
<-ch
|
|
|
|
|
|
|
|
return nil
|
2017-03-02 00:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProp(name string, units interface{}) systemdDbus.Property {
|
|
|
|
return systemdDbus.Property{
|
|
|
|
Name: name,
|
|
|
|
Value: dbus.MakeVariant(units),
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 02:40:52 +00:00
|
|
|
|
|
|
|
// DetachError is special error which returned in case of container detach.
|
|
|
|
type DetachError struct{}
|
|
|
|
|
|
|
|
func (DetachError) Error() string {
|
|
|
|
return "detached from container"
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyDetachable is similar to io.Copy but support a detach key sequence to break out.
|
|
|
|
func CopyDetachable(dst io.Writer, src io.Reader, keys []byte) (written int64, err error) {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
// Default keys : ctrl-p ctrl-q
|
|
|
|
keys = []byte{16, 17}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
preservBuf := []byte{}
|
|
|
|
for i, key := range keys {
|
|
|
|
preservBuf = append(preservBuf, buf[0:nr]...)
|
|
|
|
if nr != 1 || buf[0] != key {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(keys)-1 {
|
|
|
|
// src.Close()
|
|
|
|
return 0, DetachError{}
|
|
|
|
}
|
|
|
|
nr, er = src.Read(buf)
|
|
|
|
}
|
|
|
|
var nw int
|
|
|
|
var ew error
|
|
|
|
if len(preservBuf) > 0 {
|
|
|
|
nw, ew = dst.Write(preservBuf)
|
|
|
|
nr = len(preservBuf)
|
|
|
|
} else {
|
|
|
|
nw, ew = dst.Write(buf[0:nr])
|
|
|
|
}
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
if er != io.EOF {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|