-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskManager.go
225 lines (183 loc) · 4.67 KB
/
diskManager.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
type diskManager struct {
Disks map[string]*disk `json:"disks"`
fb fileBackend
}
type disk struct {
ID string `json:"id"`
Location string `json:"location"`
Size int64 `json:"size"`
NfsAddress string `json:"nfsAddress"`
}
func NewDiskManager(fb fileBackend) (*diskManager, error) {
dm := &diskManager{
Disks: make(map[string]*disk),
fb: fb,
}
diskFolders := fb.GetNfsFolders("*")
for _, diskFolder := range diskFolders {
nameParts := strings.Split(diskFolder, "/")
id := nameParts[len(nameParts)-1]
dm.Disks[id] = &disk{
ID: id,
Location: diskFolder,
Size: dm.getDiskSize(diskFolder),
NfsAddress: fb.GetNfsAddress(),
}
}
return dm, nil
}
func (dm *diskManager) getDiskSize(location string) int64 {
var size int64 = 0
f, err := os.Open(path.Join(location, "disk.img"))
if err == nil {
fs, err := f.Stat()
if err == nil {
size = fs.Size() / 1024 / 1024
}
}
return size
}
func (dm *diskManager) RegisterDisk(id, location string) *disk {
dm.Disks[id] = &disk{
ID: id,
Location: location,
Size: dm.getDiskSize(location),
NfsAddress: dm.fb.GetNfsAddress(),
}
return dm.Disks[id]
}
func (dm *diskManager) NewDisk(size int) (*disk, error) {
if size <= 0 {
return nil, fmt.Errorf("Disk size should be larger than 0")
}
id := uuid.New().String()
log.Printf("Creating new disk with id: %v", id)
location, err := dm.fb.CreateNfsFolder(id)
if err != nil {
return &disk{}, err
}
//Create disk file
sizeInBytes := int64(size * 1024 * 1024)
fd, err := os.Create(path.Join(location, "disk.img"))
defer fd.Close()
if err != nil {
return nil, fmt.Errorf("Failed to create disk")
}
fd.Seek(sizeInBytes-1, 0)
_, err = fd.Write([]byte{0})
if err != nil {
return nil, fmt.Errorf("Write failed")
}
return dm.RegisterDisk(id, location), nil
}
func (dm *diskManager) DiskFromBakeform(bf *Bakeform) (*disk, error) {
err := bf.mount()
if err != nil {
return &disk{}, err
}
id := uuid.New().String()
log.Printf("Creating new disk with id %v from bakeform %v", id, bf.Name)
location, err := dm.fb.CopyNfsFolder(bf.MountedOn[1]+"/", id)
if err != nil {
return &disk{}, err
}
return dm.RegisterDisk(id, location), nil
}
func (dm *diskManager) DestroyDisk(id string) error {
//TODO check if realy destroying a diks, not something else.... check if id == uuid fe
delete(dm.Disks, id)
return dm.fb.DeleteNfsFolder(id)
}
func (dm *diskManager) PutFileOnDisk(diskId, filePath string, content []byte) error {
disk, exists := dm.Disks[diskId]
if !exists {
return fmt.Errorf("Disk with id %v not found", diskId)
}
file := path.Join(disk.ID, filePath)
dm.fb.PutFileInNfsFolder(file, content)
return nil
}
func (dm *diskManager) GetFileFromDisk(diskId, filePath string) ([]byte, error) {
disk, exists := dm.Disks[diskId]
if !exists {
return nil, fmt.Errorf("Disk with id %v not found", diskId)
}
file := path.Join(disk.ID, filePath)
return dm.fb.GetFileFromNfsFolder(file)
}
func (dm *diskManager) createDiskHandler(w http.ResponseWriter, r *http.Request) {
var params struct {
Size int `json:"size"`
}
err := json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
disk, err := dm.NewDisk(params.Size)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(([]byte(err.Error())))
return
}
jsonBytes, err := json.Marshal(disk)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(([]byte(err.Error())))
return
}
w.WriteHeader(http.StatusCreated)
w.Write(jsonBytes)
}
func (dm *diskManager) destroyDiskHandler(w http.ResponseWriter, r *http.Request) {
diskId := mux.Vars(r)["diskId"]
if diskId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
err := dm.DestroyDisk(diskId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(([]byte(err.Error())))
return
}
w.WriteHeader(http.StatusOK)
}
func (dm *diskManager) listDisksHandler(w http.ResponseWriter, r *http.Request) {
jsonBytes, err := json.Marshal(dm)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(jsonBytes)
}
func (dm *diskManager) getDiskHandler(w http.ResponseWriter, r *http.Request) {
diskId := mux.Vars(r)["diskId"]
if diskId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
disk, exists := dm.Disks[diskId]
if !exists {
w.WriteHeader(http.StatusNotFound)
return
}
jsonBytes, err := json.Marshal(disk)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(jsonBytes)
}