/
linux.go
203 lines (179 loc) · 4.43 KB
/
linux.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package utils
import (
"bufio"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
)
func SysGetHomeDir() (home string) {
home, err := os.UserHomeDir()
if err == nil {
return home
} else {
return ""
}
}
func SysGetUsername() string {
if user, err := user.Current(); err == nil {
return user.Username
} else {
return ""
}
}
func SysGetUserId() string {
if user, err := user.Current(); err == nil {
return user.Uid
} else {
return ""
}
}
func SysGetGroupId() string {
if user, err := user.Current(); err == nil {
return user.Gid
} else {
return ""
}
}
func ArgsGet(index int, args []string) string {
if len(args) > index {
return args[index]
} else {
return ""
}
}
/*
Linux cmd
*/
func Touch(name string) error {
file, err := os.OpenFile(name, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
log.Println(err)
return err
}
return file.Close()
// return nil
}
func Cat(filename ...string) error {
for _, finame := range filename {
f, err := os.Open(finame)
finamecp := f
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, f)
if err != nil {
return err
}
defer finamecp.Close()
}
return nil
}
func Which(filePath, PATH string) (string, bool) {
execbasename := filepath.Base(filePath)
for _, val := range strings.Split(PATH, string(os.PathListSeparator)) {
if PathIsFile(filepath.Join(val, execbasename)) {
return filepath.Join(val, execbasename), true
}
}
return "", false
}
func Cp(sourceFile, destinationFile string) bool {
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
log.Println(err)
return false
}
err = ioutil.WriteFile(destinationFile, input, 0644)
if err != nil {
log.Println("Error creating", destinationFile)
log.Println(err)
return false
}
return true
}
/*
Container
*/
func matchContainerIDWithHostname(lines string) string {
hostname := os.Getenv("HOSTNAME")
re := regexp.MustCompilePOSIX("^[[:alnum:]]{12}$")
if re.MatchString(hostname) {
regex := fmt.Sprintf("(%s[[:alnum:]]{52})", hostname)
return matchContainerID(regex, lines)
}
return ""
}
func matchContainerID(regex, lines string) string {
// Attempt to detect if we're on a line from a /proc/<pid>/mountinfo file and modify the regexp accordingly
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt section 3.5
re := regexp.MustCompilePOSIX("^[0-9]+ [0-9]+ [0-9]+:[0-9]+ /")
if re.MatchString(lines) {
regex = fmt.Sprintf("containers/%v", regex)
}
re = regexp.MustCompilePOSIX(regex)
if re.MatchString(lines) {
submatches := re.FindStringSubmatch(string(lines))
containerID := submatches[1]
return containerID
}
return ""
}
/*
GetCurrentContainerID attempts to extract the current container ID from the provided file paths. (call inside container)
If no files paths are provided, it will default to /proc/1/cpuset, /proc/self/cgroup and /proc/self/mountinfo.
It attempts to match the HOSTNAME first then use the fallback method, and returns with the first valid match.
*/
func GetCurrentContainerID(filepaths ...string) (id string) {
if len(filepaths) == 0 {
filepaths = []string{"/proc/1/cpuset", "/proc/self/cgroup", "/proc/self/mountinfo"}
}
// We try to match a 64 character hex string starting with the hostname first
for _, filepath := range filepaths {
file, err := os.Open(filepath)
if err != nil {
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
if err == nil {
strLines := string(lines)
if id = matchContainerIDWithHostname(strLines); len(id) == 64 {
return
}
}
}
}
// If we didn't get any ID that matches the hostname, fall back to matching the first 64 character hex string
for _, filepath := range filepaths {
file, err := os.Open(filepath)
if err != nil {
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
_, lines, err := bufio.ScanLines([]byte(scanner.Text()), true)
if err == nil {
strLines := string(lines)
if id = matchContainerID("([[:alnum:]]{64})", strLines); len(id) == 64 {
return
}
}
}
}
return
}
func IsContainer() bool {
return len(GetCurrentContainerID()) != 0
// return gogrep.FileIsMatchLiteralLine("/proc/self/cgroup", "docker") || gogrep.FileIsMatchLiteralLine("/proc/self/cgroup", "lxc")
}