-
Notifications
You must be signed in to change notification settings - Fork 8
/
fs.go
123 lines (101 loc) · 2.48 KB
/
fs.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
package main
import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/reconquest/executil-go"
"github.com/reconquest/ser-go"
)
func getFSType(root string) (string, error) {
command := exec.Command("findmnt", "-o", "fstype", "-nfT", root)
output, _, err := executil.Run(command)
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func createBaseDirForPackages(
rootDir string,
packages []string,
storageEngine storage,
) (exists bool, dirName string, err error) {
imageName := fmt.Sprintf(
"%x",
sha256.Sum224([]byte(strings.Join(packages, ","))),
)
imageDir := getImageDir(rootDir, imageName)
if isExists(imageDir) && !isExists(imageDir, ".hastur") {
err = storageEngine.DeInitImage(imageName)
if err != nil {
return false, "", ser.Errorf(
err, "can't deinitialize image %s", imageName,
)
}
}
if !isExists(imageDir) {
err = storageEngine.InitImage(imageName)
if err != nil {
return false, "", ser.Errorf(
err, "can't initialize image %s", imageName,
)
}
return false, imageName, nil
} else {
return true, imageName, nil
}
}
func installBootstrapExecutable(root string, target string) error {
path, err := os.Readlink("/proc/self/exe")
if err != nil {
return ser.Errorf(
err, "can't read link to /proc/self/exe",
)
}
command := exec.Command("cp", path, filepath.Join(root, target))
_, _, err = executil.Run(command)
if err != nil {
return err
}
return nil
}
func listContainers(rootDir string) ([]string, error) {
containers := []string{}
filepath.Walk(
rootDir,
func(path string, info os.FileInfo, err error) error {
if path == rootDir {
return nil
}
if info.IsDir() {
containers = append(containers, filepath.Base(path))
return filepath.SkipDir
}
return nil
},
)
return containers, nil
}
func getContainerDir(rootDir string, containerName string) string {
return filepath.Join(rootDir, "containers", containerName)
}
func getImageDir(rootDir string, imageName string) string {
return filepath.Join(rootDir, "images", imageName)
}
func getBaseDirs(rootDir string) ([]string, error) {
return filepath.Glob(filepath.Join(rootDir, "base.#*"))
}
func removeContainerDir(containerDir string) error {
command := exec.Command("rm", "-rf", containerDir)
_, _, err := executil.Run(command)
if err != nil {
return err
}
return nil
}
func isExists(path ...string) bool {
_, err := os.Stat(filepath.Join(path...))
return !os.IsNotExist(err)
}