Statistics
| Branch: | Tag: | Revision:

root / db / charger.py @ d08a5f6f

History | View | Annotate | Download (1.2 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
        if vm.state == 'PE_VM_RUNNING':
32
            cost = vm.flavor.cost_active
33
        elif vm.state == 'PE_VM_STOPPED':
34
            cost = vm.flavor.cost_inactive
35
        
36
        start = vm.charged
37
        end = datetime.now()
38
        user_credits = vm.owner.charge_credits(cost, start, end)
39
        vm.charged = end
40
        
41
        # update the values in the database
42
        vm.save()
43
        vm.owner.save()
44
        
45
        if user_credits <= 0:
46
            stop_virtual_machine(vm)
47

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