-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvm.go
739 lines (647 loc) · 20.5 KB
/
vm.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package lobster
import "errors"
import "fmt"
import "log"
import "strings"
import "time"
// database objects
type VirtualMachine struct {
Id int
UserId int
Region string
Name string
Identification string
Status string
TaskPending bool
ExternalIP string
PrivateIP string
CreatedTime time.Time
Suspended string
Plan Plan
User User
Info *VmInfo
Addresses []*IpAddress
}
// interface objects
type VmInfo struct {
Ip string
PrivateIp string
Status string
Hostname string
BandwidthUsed int64 // in bytes
LoginDetails string
Details map[string]string
Actions []*VmActionDescriptor
// these fields are filled in by lobster, so VM interface should generally not set
// occassionally it may be useful for interface to override though
// these are autodetected from whether we can cast the interface, so if
// interface discovers that some capabilities aren't supported on some
// virtual machines, it may want to override that
// in that event it should set OverrideCapabilities
CanVnc bool
CanReimage bool
CanSnapshot bool
CanResize bool
CanAddresses bool
OverrideCapabilities bool
PendingSnapshots []*Image
}
type IpAddress struct {
Ip string
PrivateIp string // blank means N/A
CanRdns bool
Hostname string // current rDNS setting, always blank if CanRdns is false
}
// describes an action that we can perform on a virtual machine
type VmActionDescriptor struct {
Action string
Name string // used for button text
Options map[string]string // if non-nil, set of options to offer in modal / dropdown menu; not used for sanitization!
Description string // if non-empty, will be displayed in a modal
Dangerous bool // if true, we will have confirmation window
}
const VM_QUERY = "SELECT vms.id, vms.user_id, vms.region, vms.name, vms.identification, " +
"vms.status, vms.task_pending, vms.external_ip, vms.private_ip, " +
"vms.time_created, vms.suspended, vms.plan_id, " +
"plans.name, plans.price, plans.ram, plans.cpu, plans.storage, plans.bandwidth, " +
"users.username, users.email " +
"FROM vms, plans, users " +
"WHERE vms.plan_id = plans.id AND vms.user_id = users.id"
func vmListHelper(rows Rows) []*VirtualMachine {
vms := make([]*VirtualMachine, 0)
for rows.Next() {
var vm VirtualMachine
rows.Scan(
&vm.Id,
&vm.UserId,
&vm.Region,
&vm.Name,
&vm.Identification,
&vm.Status,
&vm.TaskPending,
&vm.ExternalIP,
&vm.PrivateIP,
&vm.CreatedTime,
&vm.Suspended,
&vm.Plan.Id,
&vm.Plan.Name,
&vm.Plan.Price,
&vm.Plan.Ram,
&vm.Plan.Cpu,
&vm.Plan.Storage,
&vm.Plan.Bandwidth,
&vm.User.Username,
&vm.User.Email,
)
vms = append(vms, &vm)
}
return vms
}
func vmList(userId int) []*VirtualMachine {
return vmListHelper(db.Query(VM_QUERY+" AND vms.user_id = ? ORDER BY id DESC", userId))
}
func vmListRegion(userId int, region string) []*VirtualMachine {
return vmListHelper(db.Query(VM_QUERY+" AND vms.user_id = ? AND region = ? ORDER BY id DESC", userId, region))
}
func vmListAll() []*VirtualMachine {
return vmListHelper(db.Query(VM_QUERY + " ORDER BY id DESC"))
}
func vmGet(vmId int) *VirtualMachine {
vms := vmListHelper(db.Query(VM_QUERY+" AND vms.id = ? ORDER BY id DESC", vmId))
if len(vms) == 1 {
return vms[0]
} else {
return nil
}
}
func vmGetUser(userId int, vmId int) *VirtualMachine {
vms := vmListHelper(db.Query(VM_QUERY+" AND vms.id = ? AND vms.user_id = ? ORDER BY id DESC", vmId, userId))
if len(vms) == 1 {
return vms[0]
} else {
return nil
}
}
func vmNameOk(name string) error {
if len(name) == 0 {
return L.Error("name_empty")
} else if len(name) > MAX_VM_NAME_LENGTH {
return L.Errorf("name_too_long", MAX_VM_NAME_LENGTH)
} else if !isPrintable(name) {
return L.Error("invalid_name_format")
} else {
return nil
}
}
type VmCreateOptions struct {
KeyID int
}
func vmCreate(userId int, name string, planId int, imageId int, options VmCreateOptions) (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 limit
var vmCount int
db.QueryRow("SELECT COUNT(*) FROM vms WHERE user_id = ?", userId).Scan(&vmCount)
if vmCount >= user.VmLimit {
return 0, L.Error("exceeded_vm_limit")
}
// validate name
err := vmNameOk(name)
if err != nil {
return 0, err
}
var vmiOptions VMIVmCreateOptions
// validate image ID
// the image also determines which region we provision in
image := imageGet(userId, imageId)
if image == nil {
return 0, L.Error("image_not_exist")
} else if image.Status != "active" {
return 0, L.Error("image_not_ready")
}
vmiOptions.ImageIdentification = image.Identification
// verify region not disabled
if !regionEnabled(image.Region) {
return 0, L.Error("region_disabled")
}
// validate plan
plan := planGetRegion(image.Region, planId)
if plan == nil {
return 0, L.Error("no_such_plan")
}
plan.LoadMetadata()
// validate key
if options.KeyID != 0 {
key := keyGet(userId, options.KeyID)
if key == nil {
return 0, L.Error("sshkey_not_exist")
}
vmiOptions.SSHKey = *key
}
// create the virtual machine asynchronously
result := db.Exec("INSERT INTO vms (user_id, region, plan_id, name, status) VALUES (?, ?, ?, ?, ?)", userId, image.Region, planId, name, "provisioning")
vmId := result.LastInsertId()
go func() {
defer errorHandler(nil, nil, true)
vm := vmGet(vmId)
vm.Plan = *plan // use plan from planGetRegion so that we have the region-specific identification
vmIdentification, err := vmGetInterface(image.Region).VmCreate(vm, &vmiOptions)
if err != nil {
ReportError(
err,
"vm creation failed",
fmt.Sprintf("hostname=%s, plan_id=%d, image_identification=%s", name, plan.Id, image.Identification),
)
db.Query("UPDATE vms SET status = 'error' WHERE id = ?", vmId)
MailWrap(userId, "vmCreateError", VmCreateErrorEmail{Id: vmId, Name: name}, true)
return
}
db.Exec("UPDATE vms SET status = 'active', identification = ? WHERE id = ?", vmIdentification, vmId)
MailWrap(userId, "vmCreate", VmCreateEmail{Id: vmId, Name: name}, true)
}()
return vmId, nil
}
func (vm *VirtualMachine) LoadInfo() {
if vm.Info != nil {
return
}
if vm.Identification == "" || vm.Status != "active" {
vm.Info = &VmInfo{Ip: L.T("pending"), PrivateIp: L.T("pending"), Status: strings.Title(vm.Status), Hostname: vm.Name}
return
}
vmi := vmGetInterface(vm.Region)
var err error
vm.Info, err = vmi.VmInfo(vm)
if err != nil {
ReportError(err, "vmInfo failed", fmt.Sprintf("vm_id=%d, identification=%s", vm.Id, vm.Identification))
vm.Info = new(VmInfo)
}
if vm.Info.Hostname == "" {
vm.Info.Hostname = vm.Name
}
if vm.Info.Ip == "" {
vm.Info.Ip = L.T("pending")
if vm.Info.PrivateIp == "" {
vm.Info.PrivateIp = L.T("pending")
}
} else {
db.Exec("UPDATE vms SET external_ip = ?, private_ip = ? WHERE id = ?", vm.Info.Ip, vm.Info.PrivateIp, vm.Id)
}
if vm.Info.Status == "" {
vm.Info.Status = L.T("unknown")
}
if !vm.Info.OverrideCapabilities {
_, vm.Info.CanVnc = vmi.(VMIVnc)
_, vm.Info.CanReimage = vmi.(VMIReimage)
_, vm.Info.CanSnapshot = vmi.(VMISnapshot)
_, vm.Info.CanResize = vmi.(VMIResize)
_, vm.Info.CanAddresses = vmi.(VMIAddresses)
}
vm.Info.PendingSnapshots = imageListVmPending(vm.Id)
}
// Attempt to apply function on the provided VM.
func (vm *VirtualMachine) doForce(f func(vm *VirtualMachine) error, ignoreSuspend bool) error {
if vm.Identification == "" || vm.Status != "active" {
return L.Error("vm_not_ready")
} else if vm.Suspended != "no" && !ignoreSuspend {
if vm.Suspended == "auto" {
return L.Error("vm_suspended_auto")
} else if vm.Suspended == "manual" {
return L.Error("vm_suspended_manual")
} else {
return L.Error("vm_suspended")
}
} else if vm.TaskPending {
return L.Error("vm_has_pending_task")
}
return f(vm)
}
func (vm *VirtualMachine) do(f func(vm *VirtualMachine) error) error {
return vm.doForce(f, false)
}
func (vm *VirtualMachine) Start() error {
log.Printf("vmStart(%d)", vm.Id)
return vm.do(vmGetInterface(vm.Region).VmStart)
}
func (vm *VirtualMachine) Stop() error {
log.Printf("vmStop(%d)", vm.Id)
return vm.doForce(vmGetInterface(vm.Region).VmStop, true)
}
func (vm *VirtualMachine) Reboot() error {
log.Printf("vmReboot(%d)", vm.Id)
return vm.do(vmGetInterface(vm.Region).VmReboot)
}
func (vm *VirtualMachine) Action(action string, value string) error {
log.Printf("vmAction(%d, %s, %s)", vm.Id, action, value)
return vm.do(func(vm *VirtualMachine) error {
return vmGetInterface(vm.Region).VmAction(vm, action, value)
})
}
func (vm *VirtualMachine) Vnc() (string, error) {
log.Printf("vmVnc(%d)", vm.Id)
var url string
err := vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMIVnc)
if !ok {
return L.Error("vm_vnc_unsupported")
}
urlTry, err := vmi.VmVnc(vm)
if err != nil {
ReportError(err, "failed to retrieve VNC URL", fmt.Sprintf("vm_id=%d, vm_identification=%s", vm.Id, vm.Identification))
return err
} else {
url = urlTry
return nil
}
})
return url, err
}
func vmReimage(userId int, vmId int, imageId int) error {
// validate image ID
image := imageGet(userId, imageId)
if image == nil {
return L.Error("image_not_exist")
} else if image.Status != "active" {
return L.Error("image_not_ready")
}
vm := vmGetUser(userId, vmId)
if vm == nil {
return L.Error("invalid_vm")
}
log.Printf("vmReimage(%d, %d, %d)", userId, vmId, imageId)
return vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMIReimage)
if ok {
return vmi.VmReimage(vm, image.Identification)
} else {
return L.Error("vm_reimage_unsupported")
}
})
}
func (vm *VirtualMachine) Snapshot(name string) (int, error) {
if name == "" {
return 0, L.Error("name_empty")
}
log.Printf("vmSnapshot(%d, %s)", vm.Id, name)
var imageId int
err := vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMISnapshot)
if ok {
imageIdentification, err := vmi.VmSnapshot(vm)
if err != nil {
return err
}
result := db.Exec(
"INSERT INTO images (user_id, region, name, identification, status, source_vm) "+
"VALUES (?, ?, ?, ?, 'pending', ?)",
vm.UserId, vm.Region, name, imageIdentification, vm.Id,
)
imageId = result.LastInsertId()
return nil
} else {
return L.Error("vm_snapshot_unsupported")
}
})
return imageId, err
}
func (vm *VirtualMachine) Resize(planId int) error {
plan := planGetRegion(vm.Region, planId)
if plan == nil {
return L.Error("no_such_plan")
}
log.Printf("vmResize(%d, %d)", vm.Id, planId)
return vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMIResize)
if ok {
err := vmi.VmResize(vm, plan)
if err != nil {
return err
}
// we need to be careful in our bandwidth accounting across resizes; the user should get both:
// a) old plan's allocation for the time provisioned so far this month
// b) new plan's allocation for the remainder of the month
// we handle this by treating it as a deletion and re-creation of the VM, i.e. call vmUpdateAdditionalBandwidth and reset VM creation time
vmUpdateAdditionalBandwidth(vm)
db.Exec("UPDATE vms SET plan_id = ?, time_created = NOW() WHERE id = ?", plan.Id, vm.Id)
return nil
} else {
return L.Error("vm_resize_unsupported")
}
})
}
func (vm *VirtualMachine) Rename(name string) error {
// validate name
err := vmNameOk(name)
if err != nil {
return err
}
log.Printf("vmRename(%d, %s)", vm.Id, name)
return vm.do(func(vm *VirtualMachine) error {
db.Exec("UPDATE vms SET name = ? WHERE id = ?", name, vm.Id)
// don't worry about back-end errors, but try to rename anyway
vmi, ok := vmGetInterface(vm.Region).(VMIRename)
if ok {
ReportError(
vmi.VmRename(vm, name),
"VM rename failed",
fmt.Sprintf("id: %d, identification: %d, name: %s", vm.Id, vm.Identification, name),
)
}
return nil
})
}
func (vm *VirtualMachine) LoadAddresses() error {
if vm.Addresses != nil {
return nil
} else if vm.Identification == "" || vm.Status != "active" {
return L.Error("vm_not_ready")
}
vmi, ok := vmGetInterface(vm.Region).(VMIAddresses)
if !ok {
return L.Error("operation_unsupported")
}
var err error
vm.Addresses, err = vmi.VmAddresses(vm)
return err
}
func (vm *VirtualMachine) AddAddress() error {
err := vm.LoadAddresses()
if err != nil {
return err
} else if len(vm.Addresses) >= cfg.Vm.MaximumIps {
return L.Errorf("vm_max_ips", cfg.Vm.MaximumIps)
} else if cfg.Vm.MaximumIps <= 0 {
return L.Error("ip_manage_disabled")
}
vmi, ok := vmGetInterface(vm.Region).(VMIAddresses)
if !ok {
return L.Error("operation_unsupported")
}
return vm.do(vmi.VmAddAddress)
}
func (vm *VirtualMachine) RemoveAddress(ip string, privateip string) error {
return vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMIAddresses)
if ok {
return vmi.VmRemoveAddress(vm, ip, privateip)
} else {
return L.Error("operation_unsupported")
}
})
}
func (vm *VirtualMachine) SetRdns(ip string, hostname string) error {
return vm.do(func(vm *VirtualMachine) error {
vmi, ok := vmGetInterface(vm.Region).(VMIAddresses)
if ok {
return vmi.VmSetRdns(vm, ip, hostname)
} else {
return L.Error("operation_unsupported")
}
})
}
func (vm *VirtualMachine) Delete(userId int) error {
if vm.UserId != userId {
return L.Error("invalid_vm")
} else if vm.Status == "provisioning" {
return L.Error("vm_not_ready")
}
log.Printf("vmDelete(%d, %d)", userId, vm.Id)
if vm.Identification != "" {
go func() {
ReportError(
vmGetInterface(vm.Region).VmDelete(vm),
"failed to delete VM",
fmt.Sprintf("vm_id=%d, vm_identification=%s", vm.Id, vm.Identification),
)
}()
}
vmBilling(vm.Id, true)
vmUpdateAdditionalBandwidth(vm)
db.Exec("DELETE FROM vms WHERE id = ?", vm.Id)
MailWrap(userId, "vmDeleted", VmDeletedEmail{Id: vm.Id, Name: vm.Name}, true)
return nil
}
func (vm *VirtualMachine) Suspend(auto bool) {
if auto {
db.Exec("UPDATE vms SET suspended = 'auto' WHERE id = ? AND suspended = 'no'", vm.Id)
} else {
db.Exec("UPDATE vms SET suspended = 'manual' WHERE id = ?", vm.Id)
}
// Try to stop the VM, and throw an error if it's not stopped after one minute
// We ignonre error from Stop function since it might throw error if VM already stopped
go func() {
defer errorHandler(nil, nil, true)
vm.Stop()
time.Sleep(time.Minute)
info, err := vmGetInterface(vm.Region).VmInfo(vm)
if err != nil {
ReportError(err, "failed to suspend VM", fmt.Sprintf("user_id: %d, vm_id: %d", vm.UserId, vm.Id))
} else if info.Status != "Offline" {
ReportError(errors.New("status not offline after one minute"), "failed to suspend VM", fmt.Sprintf("user_id: %d, vm_id: %d", vm.UserId, vm.Id))
}
}()
}
func (vm *VirtualMachine) Unsuspend() error {
db.Exec("UPDATE vms SET suspended = 'no' WHERE id = ?", vm.Id)
vm.Suspended = "no"
return vm.Start()
}
func (vm *VirtualMachine) SetMetadata(k string, v string) {
rows := db.Query("SELECT id FROM vm_metadata WHERE vm_id = ? AND k = ?", vm.Id, k)
if rows.Next() {
var rowId int
rows.Scan(&rowId)
rows.Close()
db.Exec("UPDATE vm_metadata SET v = ? WHERE id = ?", v, rowId)
} else {
db.Exec("INSERT INTO vm_metadata (vm_id, k, v) VALUES (?, ?, ?)", vm.Id, k, v)
}
}
// Returns the metadata value if set, or d (default) otherwise.
func (vm *VirtualMachine) Metadata(k string, d string) string {
rows := db.Query("SELECT v FROM vm_metadata WHERE vm_id = ? AND k = ?", vm.Id, k)
if rows.Next() {
var v string
rows.Scan(&v)
rows.Close()
return v
} else {
return d
}
}
// vmUpdateAdditionalBandwidth is called on VM deletion or resize, to add the VM's bandwidth to user's bandwidth pool
func vmUpdateAdditionalBandwidth(vm *VirtualMachine) {
// determine how much of the plan bandwidth to add to the user's bandwidth pool for current month
now := time.Now()
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
monthEnd := monthStart.AddDate(0, 1, 0)
var factor float64
if vm.CreatedTime.Before(monthStart) {
factor = float64(now.Sub(monthStart))
} else {
factor = float64(now.Sub(vm.CreatedTime))
}
factor /= float64(monthEnd.Sub(monthStart))
if factor > 1 {
factor = 1
}
additionalBandwidth := int64((factor*float64(vm.Plan.Bandwidth) + 15) * 1024 * 1024 * 1024)
rows := db.Query("SELECT id FROM region_bandwidth WHERE region = ? AND user_id = ?", vm.Region, vm.UserId)
if rows.Next() {
var rowId int
rows.Scan(&rowId)
rows.Close()
db.Exec(
"UPDATE region_bandwidth "+
"SET bandwidth_additional = bandwidth_additional + ? "+
"WHERE id = ?",
additionalBandwidth, rowId,
)
} else {
db.Exec(
"INSERT INTO region_bandwidth (user_id, region, bandwidth_additional) "+
"VALUES (?, ?, ?)",
vm.UserId, vm.Region, additionalBandwidth,
)
}
}
// Attempts to bill on the specified virtual machine.
// terminating should be set to true if the VM is about to be deleted, so that we:
// a) bill for the last used interval
// b) enforce BILLING_VM_MINIMUM
func vmBilling(vmId int, terminating bool) {
db.Exec("UPDATE vms SET time_billed = time_created WHERE time_billed = 0")
rows := db.Query("SELECT TIMESTAMPDIFF(MINUTE, time_billed, NOW()) FROM vms WHERE id = ?", vmId)
if !rows.Next() {
log.Printf("Warning: vmBilling called with non-existent virtual machine id=%d", vmId)
return
}
var minutes int
rows.Scan(&minutes)
rows.Close()
intervals := minutes / cfg.Billing.BillingInterval
if terminating {
intervals++
// enforce minimum billing intervals if needed
if cfg.Billing.BillingVmMinimum > 1 {
rows = db.Query("SELECT TIMESTAMPDIFF(MINUTE, time_created, time_billed) FROM vms WHERE id = ?", vmId)
if rows.Next() {
var alreadyBilledMinutes int
rows.Scan(&alreadyBilledMinutes)
rows.Close()
alreadyBilledIntervals := alreadyBilledMinutes / cfg.Billing.BillingInterval
if alreadyBilledIntervals+intervals < cfg.Billing.BillingVmMinimum {
intervals = cfg.Billing.BillingVmMinimum - alreadyBilledIntervals
}
}
}
}
if intervals == 0 {
return
}
vm := vmGet(vmId)
if vm == nil {
log.Printf("vmBilling: vm id=%d disappeared during execution", vmId)
return
} else if vm.Status != "active" {
return
}
amount := int64(intervals) * int64(vm.Plan.Price)
UserApplyCharge(vm.UserId, vm.Name, "Plan: "+vm.Plan.Name, fmt.Sprintf("vm-%d", vmId), amount)
db.Exec("UPDATE vms SET time_billed = DATE_ADD(time_billed, INTERVAL ? MINUTE) WHERE id = ?", intervals*cfg.Billing.BillingInterval, vmId)
// also bill for bandwidth usage
newBytesUsed := vmGetInterface(vm.Region).BandwidthAccounting(vm)
if newBytesUsed > 0 {
rows := db.Query("SELECT id FROM region_bandwidth WHERE user_id = ? AND region = ?", vm.UserId, vm.Region)
if rows.Next() {
var rowId int
rows.Scan(&rowId)
rows.Close()
db.Exec("UPDATE region_bandwidth SET bandwidth_used = bandwidth_used + ? WHERE id = ?", newBytesUsed, rowId)
} else {
db.Exec(
"INSERT INTO region_bandwidth (user_id, region, bandwidth_used) "+
"VALUES (?, ?, ?)",
vm.UserId, vm.Region, newBytesUsed,
)
}
}
}
// Bills for used storage space and other resources hourly.
func serviceBilling() {
db.Exec("UPDATE users SET time_billed = NOW() WHERE time_billed = 0")
rows := db.Query(
"SELECT id, TIMESTAMPDIFF(HOUR, time_billed, NOW()) " +
"FROM users " +
"WHERE status = 'active' AND TIMESTAMPDIFF(HOUR, time_billed, NOW()) > 0",
)
defer rows.Close()
for rows.Next() {
var userId, hours int
rows.Scan(&userId, &hours)
var hourlyCharge int64 = 0
// bill storage space
var storageBytes int64 = 0
for _, image := range imageList(userId) {
if image.UserId == userId {
details := imageInfo(userId, image.Id)
if details != nil && details.Info.Size > 0 {
storageBytes += details.Info.Size
}
}
}
creditPerGBHour := int64(cfg.Billing.StorageFee * BILLING_PRECISION)
hourlyCharge += storageBytes * creditPerGBHour / 1000 / 1000 / 1000
if hourlyCharge > 0 {
totalCharge := hourlyCharge * int64(hours)
log.Printf("Charging user %d for %d bytes (amount=%.5f)", userId, storageBytes, float64(totalCharge)/BILLING_PRECISION)
UserApplyCharge(userId, "Image storage space", fmt.Sprintf("%d MB", storageBytes/1000/1000), "storage", totalCharge)
}
db.Exec("UPDATE users SET time_billed = DATE_ADD(time_billed, INTERVAL ? HOUR) WHERE id = ?", hours, userId)
}
}