-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.go
175 lines (152 loc) · 3.68 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package util
import (
"bytes"
"compress/gzip"
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os/exec"
"regexp"
"strings"
"time"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)
const (
PreservedChecksumLength = 64
)
var (
cmdTimeout = time.Minute // one minute by default
)
func GenerateName(prefix string) string {
suffix := strings.Replace(NewUUID(), "-", "", -1)
return prefix + "-" + suffix[:16]
}
func NewUUID() string {
return uuid.NewV4().String()
}
func GetChecksum(data []byte) string {
checksumBytes := sha512.Sum512(data)
checksum := hex.EncodeToString(checksumBytes[:])[:PreservedChecksumLength]
return checksum
}
func CompressData(data []byte) (io.ReadSeeker, error) {
var b bytes.Buffer
w := gzip.NewWriter(&b)
if _, err := w.Write(data); err != nil {
w.Close()
return nil, err
}
w.Close()
return bytes.NewReader(b.Bytes()), nil
}
func DecompressAndVerify(src io.Reader, checksum string) (io.Reader, error) {
r, err := gzip.NewReader(src)
if err != nil {
return nil, err
}
block, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
if GetChecksum(block) != checksum {
return nil, fmt.Errorf("checksum verification failed for block")
}
return bytes.NewReader(block), nil
}
func Now() string {
return time.Now().UTC().Format(time.RFC3339)
}
func UnorderedEqual(x, y []string) bool {
if len(x) != len(y) {
return false
}
known := make(map[string]struct{})
for _, value := range x {
known[value] = struct{}{}
}
for _, value := range y {
if _, present := known[value]; !present {
return false
}
}
return true
}
func Filter(elements []string, predicate func(string) bool) []string {
var filtered []string
for _, elem := range elements {
if predicate(elem) {
filtered = append(filtered, elem)
}
}
return filtered
}
func ExtractNames(names []string, prefix, suffix string) []string {
result := []string{}
for _, f := range names {
// Remove additional slash if exists
f = strings.TrimLeft(f, "/")
// missing prefix or suffix
if !strings.HasPrefix(f, prefix) || !strings.HasSuffix(f, suffix) {
continue
}
f = strings.TrimPrefix(f, prefix)
f = strings.TrimSuffix(f, suffix)
if !ValidateName(f) {
logrus.Errorf("Invalid name %v was processed to extract name with prefix %v suffix %v",
f, prefix, suffix)
continue
}
result = append(result, f)
}
return result
}
func ValidateName(name string) bool {
validName := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`)
return validName.MatchString(name)
}
func Execute(binary string, args []string) (string, error) {
var output []byte
var err error
cmd := exec.Command(binary, args...)
done := make(chan struct{})
go func() {
output, err = cmd.CombinedOutput()
done <- struct{}{}
}()
select {
case <-done:
case <-time.After(cmdTimeout):
if cmd.Process != nil {
if err := cmd.Process.Kill(); err != nil {
logrus.Warnf("Problem killing process pid=%v: %s", cmd.Process.Pid, err)
}
}
return "", fmt.Errorf("Timeout executing: %v %v, output %v, error %v", binary, args, string(output), err)
}
if err != nil {
return "", fmt.Errorf("Failed to execute: %v %v, output %v, error %v", binary, args, string(output), err)
}
return string(output), nil
}
func UnescapeURL(url string) string {
// Deal with escape in url inputed from bash
result := strings.Replace(url, "\\u0026", "&", 1)
result = strings.Replace(result, "u0026", "&", 1)
return result
}
func IsMounted(mountPoint string) bool {
output, err := Execute("mount", []string{})
if err != nil {
return false
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, " "+mountPoint+" ") {
return true
}
}
return false
}