fecfbf6feea5464bb256b85a04266a2835003a64
[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 = {
21     'enabled': 1,
22     # CPU parameters
23     'cpu_min': 0, # ms/s
24     'cpu_share': 32, # proportional share
25     # bandwidth parameters
26     'net_min': bwmin, # bps
27     'net_max': bwmax, # bps
28     'net_share': 1, # proportional share
29     # bandwidth parameters over routes exempt from node bandwidth limits
30     'net2_min': bwmin, # bps
31     'net2_max': bwmax, # bps
32     'net2_share': 1, # proportional share
33     'disk_max': 5000000 # bytes
34     }
35
36 start_requested = False  # set to True in order to request that all slivers be started
37
38
39 @database.synchronized
40 def GetSlivers_callback(data, fullupdate=True):
41     """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."""
42
43     node_id = None
44     try:
45         f = open('/etc/planetlab/node_id')
46         try: node_id = int(f.read())
47         finally: f.close()
48     except: logger.log_exc()
49
50     for d in data:
51         if d['node_id'] != node_id: continue
52         for sliver in d['slivers']:
53             rec = sliver.copy()
54             rec.setdefault('timestamp', d['timestamp'])
55
56             # convert attributes field to a proper dict
57             attr_dict = {}
58             for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
59
60             # squash keys
61             keys = rec.pop('keys')
62             rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
63
64             rec.setdefault('type', attr_dict.get('type', 'vserver'))
65             rec.setdefault('vref', attr_dict.get('vref', 'default'))
66             rec.setdefault('initscript', attr_dict.get('initscript', ''))
67             rec.setdefault('delegations', [])  # XXX - delegation not yet supported
68
69             # extract the implied rspec
70             rspec = {}
71             rec['rspec'] = rspec
72             for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
73                 try: amt = int(attr_dict[resname])
74                 except (KeyError, ValueError): amt = default_amt
75                 rspec[resname] = amt
76             database.db.deliver_record(rec)
77         if fullupdate: database.db.set_min_timestamp(d['timestamp'])
78     database.db.sync()
79
80     # handle requested startup
81     global start_requested
82     if start_requested:
83         start_requested = False
84         cumulative_delay = 0
85         for name in database.db.iterkeys():
86             accounts.get(name).start(delay=cumulative_delay)
87             cumulative_delay += 3
88
89 def deliver_ticket(data): return GetSlivers_callback(data, fullupdate=False)
90
91
92 def start(options):
93     accounts.register_class(sliver_vs.Sliver_VS)
94     accounts.register_class(delegate.Delegate)
95     global start_requested
96     start_requested = options.startup
97     database.start()
98     api.deliver_ticket = deliver_ticket
99     api.start()