Statistics
| Branch: | Tag: | Revision:

root / db / charger.py @ 52194743

History | View | Annotate | Download (1 kB)

1 fcbc5bb3 Vassilios Karakoidas
#
2 fcbc5bb3 Vassilios Karakoidas
# Charger - Administration script
3 fcbc5bb3 Vassilios Karakoidas
#
4 fcbc5bb3 Vassilios Karakoidas
# Executed hourly to charge vm usage for each user
5 fcbc5bb3 Vassilios Karakoidas
#
6 578b725c Vassilios Karakoidas
# Copyright 2010 Greek Research and Technology Network
7 fcbc5bb3 Vassilios Karakoidas
#
8 fcbc5bb3 Vassilios Karakoidas
9 fcbc5bb3 Vassilios Karakoidas
from db.models import *
10 fcbc5bb3 Vassilios Karakoidas
11 04a6d900 Vassilios Karakoidas
from datetime import datetime
12 04a6d900 Vassilios Karakoidas
13 b90da243 Vassilios Karakoidas
def stop_virtual_machine(vm):
14 bfd0754e Vassilios Karakoidas
    """Send message to stop a virtual machine instance"""
15 04a6d900 Vassilios Karakoidas
    
16 04a6d900 Vassilios Karakoidas
    # send the message to ganeti
17 04a6d900 Vassilios Karakoidas
    
18 fcbc5bb3 Vassilios Karakoidas
    return
19 578b725c Vassilios Karakoidas
20 578b725c Vassilios Karakoidas
def charge():
21 b90da243 Vassilios Karakoidas
    """Scan all virtual machines and charge each user"""
22 578b725c Vassilios Karakoidas
    all_vms = VirtualMachine.objects.all()
23 578b725c Vassilios Karakoidas
    
24 578b725c Vassilios Karakoidas
    if len(all_vms) == 0:
25 578b725c Vassilios Karakoidas
        print "No virtual machines found"
26 fcbc5bb3 Vassilios Karakoidas
    
27 578b725c Vassilios Karakoidas
    for vm in all_vms:
28 578b725c Vassilios Karakoidas
        cost = 0
29 578b725c Vassilios Karakoidas
        
30 04a6d900 Vassilios Karakoidas
        # Running and Stopped is charged, else the cost is zero
31 62c6d260 Vassilios Karakoidas
        
32 61b80d9a Vassilios Karakoidas
        
33 61b80d9a Vassilios Karakoidas
        start = vm.charged
34 61b80d9a Vassilios Karakoidas
        end = datetime.now()
35 61b80d9a Vassilios Karakoidas
        user_credits = vm.owner.charge_credits(cost, start, end)
36 61b80d9a Vassilios Karakoidas
        vm.charged = end
37 04a6d900 Vassilios Karakoidas
        
38 04a6d900 Vassilios Karakoidas
        # update the values in the database
39 04a6d900 Vassilios Karakoidas
        vm.save()
40 04a6d900 Vassilios Karakoidas
        vm.owner.save()
41 578b725c Vassilios Karakoidas
        
42 578b725c Vassilios Karakoidas
        if user_credits <= 0:
43 b90da243 Vassilios Karakoidas
            stop_virtual_machine(vm)
44 d08a5f6f Vangelis Koukis
45 d08a5f6f Vangelis Koukis
# vim: set ts=4 sts=4 sw=4 et ai :