Statistics
| Branch: | Tag: | Revision:

root / db / charger.py @ 52194743

History | View | Annotate | Download (1 kB)

1
#
2
# Charger - Administration script
3
#
4
# Executed hourly to charge vm usage for each user
5
#
6
# Copyright 2010 Greek Research and Technology Network
7
#
8

    
9
from db.models import *
10

    
11
from datetime import datetime
12

    
13
def stop_virtual_machine(vm):
14
    """Send message to stop a virtual machine instance"""
15
    
16
    # send the message to ganeti
17
    
18
    return
19

    
20
def charge():
21
    """Scan all virtual machines and charge each user"""
22
    all_vms = VirtualMachine.objects.all()
23
    
24
    if len(all_vms) == 0:
25
        print "No virtual machines found"
26
    
27
    for vm in all_vms:
28
        cost = 0
29
        
30
        # Running and Stopped is charged, else the cost is zero
31
        
32
        
33
        start = vm.charged
34
        end = datetime.now()
35
        user_credits = vm.owner.charge_credits(cost, start, end)
36
        vm.charged = end
37
        
38
        # update the values in the database
39
        vm.save()
40
        vm.owner.save()
41
        
42
        if user_credits <= 0:
43
            stop_virtual_machine(vm)
44

    
45
# vim: set ts=4 sts=4 sw=4 et ai :