2014-02-19 08:56:11 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2016-01-26 10:15:44 +08:00
|
|
|
"encoding/json"
|
2014-02-19 08:56:11 +08:00
|
|
|
"io"
|
2016-01-22 07:27:20 +08:00
|
|
|
"os"
|
2014-02-25 13:11:52 +08:00
|
|
|
"path/filepath"
|
2016-04-06 23:57:59 +08:00
|
|
|
"strings"
|
2014-06-13 21:56:58 +08:00
|
|
|
"syscall"
|
2016-09-27 18:02:22 +08:00
|
|
|
"unsafe"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2015-02-07 11:16:11 +08:00
|
|
|
const (
|
|
|
|
exitSignalOffset = 128
|
|
|
|
)
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// GenerateRandomName returns a new name joined with a prefix. This size
|
|
|
|
// specified is used to truncate the randomly generated value
|
2014-02-20 07:54:53 +08:00
|
|
|
func GenerateRandomName(prefix string, size int) (string, error) {
|
|
|
|
id := make([]byte, 32)
|
2014-02-19 08:56:11 +08:00
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-05-30 11:50:15 +08:00
|
|
|
if size > 64 {
|
|
|
|
size = 64
|
|
|
|
}
|
2014-02-20 07:54:53 +08:00
|
|
|
return prefix + hex.EncodeToString(id)[:size], nil
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-02-25 13:11:52 +08:00
|
|
|
|
|
|
|
// ResolveRootfs ensures that the current working directory is
|
|
|
|
// not a symlink and returns the absolute path to the rootfs
|
|
|
|
func ResolveRootfs(uncleanRootfs string) (string, error) {
|
|
|
|
rootfs, err := filepath.Abs(uncleanRootfs)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.EvalSymlinks(rootfs)
|
|
|
|
}
|
2014-06-13 21:56:58 +08:00
|
|
|
|
2015-02-07 11:16:11 +08:00
|
|
|
// ExitStatus returns the correct exit status for a process based on if it
|
2016-01-26 10:15:44 +08:00
|
|
|
// was signaled or exited cleanly
|
2015-02-07 11:16:11 +08:00
|
|
|
func ExitStatus(status syscall.WaitStatus) int {
|
|
|
|
if status.Signaled() {
|
|
|
|
return exitSignalOffset + int(status.Signal())
|
|
|
|
}
|
|
|
|
return status.ExitStatus()
|
|
|
|
}
|
2016-01-26 10:15:44 +08:00
|
|
|
|
|
|
|
// WriteJSON writes the provided struct v to w using standard json marshaling
|
|
|
|
func WriteJSON(w io.Writer, v interface{}) error {
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(data)
|
|
|
|
return err
|
|
|
|
}
|
2016-01-22 07:27:20 +08:00
|
|
|
|
|
|
|
// CleanPath makes a path safe for use with filepath.Join. This is done by not
|
|
|
|
// only cleaning the path, but also (if the path is relative) adding a leading
|
|
|
|
// '/' and cleaning it (then removing the leading '/'). This ensures that a
|
|
|
|
// path resulting from prepending another path will always resolve to lexically
|
|
|
|
// be a subdirectory of the prefixed path. This is all done lexically, so paths
|
|
|
|
// that include symlinks won't be safe as a result of using CleanPath.
|
|
|
|
func CleanPath(path string) string {
|
2016-02-11 18:15:50 +08:00
|
|
|
// Deal with empty strings nicely.
|
|
|
|
if path == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-01-22 07:27:20 +08:00
|
|
|
// Ensure that all paths are cleaned (especially problematic ones like
|
|
|
|
// "/../../../../../" which can cause lots of issues).
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
|
|
|
// If the path isn't absolute, we need to do more processing to fix paths
|
|
|
|
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute
|
|
|
|
// paths to relative ones.
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Clean(string(os.PathSeparator) + path)
|
|
|
|
// This can't fail, as (by definition) all paths are relative to root.
|
|
|
|
path, _ = filepath.Rel(string(os.PathSeparator), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean the path again for good measure.
|
|
|
|
return filepath.Clean(path)
|
|
|
|
}
|
2016-04-06 23:57:59 +08:00
|
|
|
|
|
|
|
// SearchLabels searches a list of key-value pairs for the provided key and
|
|
|
|
// returns the corresponding value. The pairs must be separated with '='.
|
|
|
|
func SearchLabels(labels []string, query string) string {
|
|
|
|
for _, l := range labels {
|
|
|
|
parts := strings.SplitN(l, "=", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parts[0] == query {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2016-06-03 03:44:43 +08:00
|
|
|
|
|
|
|
// Annotations returns the bundle path and user defined annotations from the
|
2016-10-11 16:32:56 +08:00
|
|
|
// libcontainer state. We need to remove the bundle because that is a label
|
2016-06-03 03:44:43 +08:00
|
|
|
// added by libcontainer.
|
|
|
|
func Annotations(labels []string) (bundle string, userAnnotations map[string]string) {
|
|
|
|
userAnnotations = make(map[string]string)
|
|
|
|
for _, l := range labels {
|
|
|
|
parts := strings.SplitN(l, "=", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parts[0] == "bundle" {
|
|
|
|
bundle = parts[1]
|
|
|
|
} else {
|
|
|
|
userAnnotations[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-09-27 18:02:22 +08:00
|
|
|
|
|
|
|
func GetIntSize() int {
|
|
|
|
return int(unsafe.Sizeof(1))
|
|
|
|
}
|