forked from LunaNode/lobster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
236 lines (204 loc) · 5.04 KB
/
image.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
226
227
228
229
230
231
232
233
234
235
236
package lobster
import "fmt"
// database objects
type Image struct {
Id int
UserId int
Region string
Name string
Identification string
Status string
SourceVm int
Info *ImageInfo
}
// interface objects
type ImageStatus int
const (
ImagePending ImageStatus = iota
ImageActive
ImageError
)
type ImageInfo struct {
Size int64
Status ImageStatus
Details map[string]string
}
func imageListHelper(rows Rows) []*Image {
defer rows.Close()
images := make([]*Image, 0)
for rows.Next() {
image := Image{}
rows.Scan(&image.Id, &image.UserId, &image.Region, &image.Name, &image.Identification, &image.Status, &image.SourceVm)
images = append(images, &image)
}
return images
}
const IMAGE_QUERY = "SELECT id, user_id, region, name, identification, status, source_vm FROM images"
func imageListAll() []*Image {
return imageListHelper(
db.Query(
IMAGE_QUERY + " ORDER BY user_id, name",
),
)
}
func imageList(userId int) []*Image {
return imageListHelper(
db.Query(
IMAGE_QUERY+" WHERE user_id = -1 OR user_id = ? ORDER BY name",
userId,
),
)
}
func imageListRegion(userId int, region string) []*Image {
return imageListHelper(
db.Query(
IMAGE_QUERY+" WHERE (user_id = -1 OR user_id = ?) AND region = ? ORDER BY name",
userId, region,
),
)
}
func imageListVmPending(vmId int) []*Image {
return imageListHelper(
db.Query(
IMAGE_QUERY+" WHERE source_vm = ? AND status = 'pending'",
vmId,
),
)
}
func imageGet(userId int, imageId int) *Image {
images := imageListHelper(
db.Query(
IMAGE_QUERY+" WHERE id = ? AND (user_id = -1 OR user_id = ?)",
imageId, userId,
),
)
if len(images) == 1 {
return images[0]
} else {
return nil
}
}
func imageGetForce(imageId int) *Image {
images := imageListHelper(
db.Query(
"SELECT id, user_id, region, name, identification, status "+
"FROM images WHERE id = ?",
imageId,
),
)
if len(images) == 1 {
return images[0]
} else {
return nil
}
}
func imageFetch(userId int, region string, name string, url string, format string) (int, error) {
// validate credit
user := UserDetails(userId)
if user == nil {
return 0, L.Error("invalid_account")
} else if user.Credit < MINIMUM_CREDIT {
return 0, L.Error("insufficient_credit")
}
// validate region
vmi, ok := regionInterfaces[region]
if !ok {
return 0, L.Error("invalid_region")
}
vmiImage, ok := vmi.(VMIImages)
if !ok {
return 0, L.Error("operation_unsupported")
}
imageIdentification, err := vmiImage.ImageFetch(url, format)
if err != nil {
return 0, err
} else {
result := db.Exec(
"INSERT INTO images (user_id, region, name, identification, status) "+
"VALUES (?, ?, ?, ?, 'pending')",
userId, region, name, imageIdentification,
)
return result.LastInsertId(), nil
}
}
func imageAdd(name string, region string, identification string) {
db.Exec(
"INSERT INTO images (name, region, identification) VALUES (?, ?, ?)",
name, region, identification,
)
}
func imageDelete(userId int, imageId int) error {
image := imageGet(userId, imageId)
if image == nil || image.UserId != userId {
return L.Error("invalid_image")
}
vmi, ok := vmGetInterface(image.Region).(VMIImages)
if !ok {
return L.Error("operation_unsupported")
}
err := vmi.ImageDelete(image.Identification)
if err != nil {
return err
} else {
db.Exec("DELETE FROM images WHERE id = ?", image.Id)
return nil
}
}
func imageDeleteForce(imageId int) error {
image := imageGetForce(imageId)
if image == nil {
return L.Error("invalid_image")
}
vmi, ok := vmGetInterface(image.Region).(VMIImages)
if !ok {
return L.Error("operation_unsupported")
}
err := vmi.ImageDelete(image.Identification)
if err != nil {
ReportError(err, "image force deletion failed", fmt.Sprintf("image_id=%d, identification=%s", image.Id, image.Identification))
}
db.Exec("DELETE FROM images WHERE id = ?", image.Id)
return nil
}
func imageInfo(userId int, imageId int) *Image {
image := imageGet(userId, imageId)
if image == nil || image.UserId != userId {
return nil
}
vmi, ok := vmGetInterface(image.Region).(VMIImages)
if !ok {
return nil
}
var err error
image.Info, err = vmi.ImageInfo(image.Identification)
if err != nil {
ReportError(err, "imageInfo failed", fmt.Sprintf("image_id=%d, identification=%s", image.Id, image.Identification))
image.Info = new(ImageInfo)
}
return image
}
func imageAutopopulate(region string) error {
if _, ok := regionInterfaces[region]; !ok {
return fmt.Errorf("specified region %s does not exist", region)
}
vmi, ok := regionInterfaces[region].(VMIImages)
if !ok {
return L.Error("operation_unsupported")
}
images, err := vmi.ImageList()
if err != nil {
return err
}
// add images that aren't already having matching identification in database
for _, image := range images {
var count int
db.QueryRow(
"SELECT COUNT(*) FROM images WHERE region = ? AND identification = ?",
region, image.Identification,
).Scan(&count)
if count == 0 {
imageAdd(image.Name, region, image.Identification)
}
}
return nil
}