TICKETS!!!
[nodemanager.git] / sm.py
1 """Sliver manager.
2
3 The sliver manager has several functions.  It is responsible for
4 creating, resource limiting, starting, stopping, and destroying
5 slivers.  It provides an API for users to access these functions and
6 also to make inter-sliver resource loans.  The sliver manager is also
7 responsible for handling delegation accounts.
8 """
9
10 try: from bwlimit import bwmin, bwmax
11 except ImportError: bwmin, bwmax = 8, 1000*1000*1000
12 import accounts
13 import api
14 import database
15 import delegate
16 import logger
17 import sliver_vs
18
19
20 DEFAULT_ALLOCATION = {'enabled': 1, 'cpu_min': 0, 'cpu_share': 32, 'net_min': bwmin, 'net_max': bwmax, 'net2_min': bwmin, 'net2_max': bwmax, 'net_share': 1, 'disk_max': 5000000}
21
22 start_requested = False  # set to True in order to request that all slivers be started
23
24
25 @database.synchronized
26 def GetSlivers_callback(data, fullupdate=True):
27     """This function has two purposes.  One, convert GetSlivers() data into a more convenient format.  Two, even if no updates are coming in, use the GetSlivers() heartbeat as a cue to scan for expired slivers."""
28
29     node_id = None
30     try:
31         f = open('/etc/planetlab/node_id')
32         try: node_id = int(f.read())
33         finally: f.close()
34     except: logger.log_exc()
35
36     for d in data:
37         if d['node_id'] != node_id: continue
38         for sliver in d['slivers']:
39             rec = sliver.copy()
40             rec.setdefault('timestamp', d['timestamp'])
41             rec.setdefault('type', 'sliver.VServer')
42
43             # convert attributes field to a proper dict
44             attr_dict = {}
45             for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
46
47             # squash keys
48             keys = rec.pop('keys')
49             rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
50
51             rec.setdefault('initscript', attr_dict.get('initscript', ''))
52             rec.setdefault('delegations', [])  # XXX - delegation not yet supported
53
54             # extract the implied rspec
55             rspec = {}
56             rec['rspec'] = rspec
57             for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
58                 try: amt = int(attr_dict[resname])
59                 except (KeyError, ValueError): amt = default_amt
60                 rspec[resname] = amt
61             database.db.deliver_record(rec)
62         if fullupdate: database.db.set_min_timestamp(d['timestamp'])
63     database.db.sync()
64
65     # handle requested startup
66     global start_requested
67     if start_requested:
68         start_requested = False
69         cumulative_delay = 0
70         for name in database.db.iterkeys():
71             accounts.get(name).start(delay=cumulative_delay)
72             cumulative_delay += 3
73
74 def deliver_ticket(data): return GetSlivers_callback(data, fullupdate=False)
75
76
77 def start(options):
78     accounts.register_class(sliver_vs.Sliver_VS)
79     accounts.register_class(delegate.Delegate)
80     global start_requested
81     start_requested = options.startup
82     database.start()
83     api.deliver_ticket = deliver_ticket
84     api.start()