-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileBackend.go
More file actions
145 lines (120 loc) · 3.35 KB
/
fileBackend.go
File metadata and controls
145 lines (120 loc) · 3.35 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
)
type fileBackend interface {
GetNfsRoot() string
GetNfsAddress() string
GetBootRoot() string
PutFileInNfsFolder(filePath string, content []byte) error
GetFileFromNfsFolder(filePath string) ([]byte, error)
CreateNfsFolder(string) (string, error)
DeleteNfsFolder(string) error
GetNfsFolders(string) []string
CopyNfsFolder(string, string) (string, error)
CopyBootFolder(string, string) (string, error)
}
type FileBackend struct {
nfsRoot string
nfsAddress string
bootRoot string
nfsExportMutex *sync.Mutex
}
func newFileBackend(nfsAddress, nfsRoot, bootRoot string) (fileBackend, error) {
if nfsAddress == "" || nfsRoot == "" {
return &FileBackend{}, fmt.Errorf("fileBackend: nfsAddress or nfsRoot not configured")
}
return &FileBackend{
nfsRoot: nfsRoot,
nfsAddress: nfsAddress,
bootRoot: bootRoot,
nfsExportMutex: &sync.Mutex{},
}, nil
}
func (nfs *FileBackend) GetNfsRoot() string {
return nfs.nfsRoot
}
func (nfs *FileBackend) GetNfsAddress() string {
return nfs.nfsAddress
}
func (nfs *FileBackend) GetBootRoot() string {
return nfs.bootRoot
}
func (f *FileBackend) copyFolder(s, d string) error {
_, err := exec.Command("rsync", "-xa", s, d).CombinedOutput()
if err != nil && err.Error() != "exit status 23" { //avoid bug in ubuntu 14.04
return err
}
return nil
}
func (f *FileBackend) PutFileInNfsFolder(filePath string, content []byte) error {
fullFilePath := path.Join(f.nfsRoot, filePath)
dir, _ := path.Split(fullFilePath)
err := os.MkdirAll(dir, 0666)
if err != nil {
return err
}
return ioutil.WriteFile(fullFilePath, content, 0666)
}
func (f *FileBackend) GetFileFromNfsFolder(filePath string) ([]byte, error) {
fullFilePath := path.Join(f.nfsRoot, filePath)
return ioutil.ReadFile(fullFilePath)
}
func (f *FileBackend) CopyBootFolder(s, dest string) (string, error) {
d := path.Join(f.bootRoot, dest)
//d = strings.Replace(d, "//", "/", -1)
return d, f.copyFolder(s, d)
}
func (f *FileBackend) CopyNfsFolder(s, dest string) (string, error) {
d := path.Join(f.nfsRoot, dest)
//d = strings.Replace(d, "//", "/", -1)
err := f.copyFolder(s, d)
if err != nil {
return "", err
}
return d, f.regenNfsExports()
}
func (f *FileBackend) CreateNfsFolder(d string) (string, error) {
location := path.Join(f.nfsRoot, d)
err := os.Mkdir(location, 0644)
if err != nil {
return "", err
}
err = f.regenNfsExports()
return location, err
}
func (f *FileBackend) DeleteNfsFolder(d string) error {
location := path.Join(f.nfsRoot, d)
err := os.RemoveAll(location)
if err != nil {
return err
}
return f.regenNfsExports()
}
func (f *FileBackend) GetNfsFolders(pattern string) []string {
files, _ := filepath.Glob(path.Join(f.nfsRoot, pattern))
return files
}
func (f *FileBackend) regenNfsExports() error {
f.nfsExportMutex.Lock()
defer f.nfsExportMutex.Unlock()
folderList := f.GetNfsFolders("*")
exportsContent := ""
for _, folder := range folderList {
exportsContent = exportsContent + fmt.Sprintf("%v *(rw,sync,no_subtree_check,no_root_squash)\n", folder)
}
log.Println("Generated new exports file:\n" + exportsContent)
err := ioutil.WriteFile("/etc/exports", []byte(exportsContent), 0644)
if err != nil {
return err
}
_, err = exec.Command("exportfs", "-a").CombinedOutput()
return err
}