-
Notifications
You must be signed in to change notification settings - Fork 1
/
bakeformInventory.go
186 lines (153 loc) · 3.88 KB
/
bakeformInventory.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/gorilla/mux"
)
type bakeformInventory interface {
Load() error //loads alls images from the image folder
List() BakeformList
UnmountAll() error
ListHandler(w http.ResponseWriter, r *http.Request)
UploadHandler(w http.ResponseWriter, r *http.Request)
DeleteHandler(w http.ResponseWriter, r *http.Request)
}
type BakeformInventory struct {
folder string
mountRoot string
nfs fileBackend
Content BakeformList
kpartxPath string
}
func newBakeformInventory(folder, mountRoot string, nfs fileBackend, kpartxPath string) (bakeformInventory, error) {
if mountRoot == "" || folder == "" {
return &BakeformInventory{}, fmt.Errorf("Please set IMAGE_FOLDER and IMAGE_MOUNT_ROOT en vars.")
}
newInv := &BakeformInventory{
folder: folder,
mountRoot: mountRoot,
nfs: nfs,
kpartxPath: kpartxPath,
}
err := newInv.Load()
if err != nil {
return &BakeformInventory{}, err
}
return newInv, err
}
func (i *BakeformInventory) Load() error {
imgFiles, err := filepath.Glob(i.folder + "/*.img")
if err != nil {
return err
}
list := make(BakeformList)
for _, img := range imgFiles {
nameParts := strings.Split(img, "/")
name := strings.Replace(nameParts[len(nameParts)-1], ".img", "", 1)
log.Printf("Loading image %v\n", name)
bf := &Bakeform{
Name: name,
Location: img,
mountRoot: i.mountRoot,
fb: i.nfs,
bootLocation: path.Join(i.nfs.GetBootRoot(), name),
kpartxPath: i.kpartxPath,
}
_, err := os.Stat(bf.bootLocation)
if os.IsNotExist(err) {
err := bf.mount()
if err != nil {
return err
}
_, err = i.nfs.CopyBootFolder(bf.MountedOn[0]+"/", name)
bf.unmount()
if err != nil {
return err
}
}
list[name] = bf
}
i.Content = list
return nil
}
func (i *BakeformInventory) List() BakeformList {
return i.Content
}
func (i *BakeformInventory) ListHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
jsonBytes, err := json.Marshal(i.Content)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Write(jsonBytes)
}
func (i *BakeformInventory) UploadHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
urlvars := mux.Vars(r)
name := urlvars["name"]
filepath := i.folder + "/" + name + ".img"
log.Println("Receiving upload: " + filepath)
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
log.Printf("Error creating image file: %v\n", err)
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(err.Error()))
return
}
_, err = io.Copy(file, r.Body)
if err != nil {
log.Printf("Error saving image: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = i.Load()
if err != nil {
log.Printf("Error loading images: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
jsonBytes, _ := json.Marshal(i.Content[name])
w.WriteHeader(http.StatusCreated)
w.Write(jsonBytes)
}
func (i *BakeformInventory) DeleteHandler(w http.ResponseWriter, r *http.Request) {
urlvars := mux.Vars(r)
name := urlvars["name"]
bf, exists := i.Content[name]
if !exists {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Bakeform not found"))
return
}
err := bf.Delete()
if err != nil {
log.Printf("Error deleting bakeform: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = i.Load()
if err != nil {
log.Printf("Error Reloading bakerforms: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
}
func (i *BakeformInventory) UnmountAll() error {
for _, b := range i.Content {
b.unmount()
}
return nil
}