- GetSlivers() now returns a single struct
[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(data, fullupdate=True):
41     """This function has two purposes.  One, convert GetSlivers() data
42     into a more convenient format.  Two, even if no updates are coming
43     in, use the GetSlivers() heartbeat as a cue to scan for expired
44     slivers."""
45
46     node_id = None
47     try:
48         f = open('/etc/planetlab/node_id')
49         try: node_id = int(f.read())
50         finally: f.close()
51     except: logger.log_exc()
52
53     if data['node_id'] != node_id: continue
54     for sliver in data['slivers']:
55         rec = sliver.copy()
56         rec.setdefault('timestamp', data['timestamp'])
57
58         # convert attributes field to a proper dict
59         attr_dict = {}
60         for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
61
62         # squash keys
63         keys = rec.pop('keys')
64         rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
65
66         rec.setdefault('type', attr_dict.get('type', 'sliver.VServer'))
67         rec.setdefault('vref', attr_dict.get('vref', 'default'))
68         rec.setdefault('initscript', attr_dict.get('initscript', ''))
69         rec.setdefault('delegations', [])  # XXX - delegation not yet supported
70
71         # extract the implied rspec
72         rspec = {}
73         rec['rspec'] = rspec
74         for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
75             try: amt = int(attr_dict[resname])
76             except (KeyError, ValueError): amt = default_amt
77             rspec[resname] = amt
78         database.db.deliver_record(rec)
79     if fullupdate: database.db.set_min_timestamp(data['timestamp'])
80     database.db.sync()
81
82     # handle requested startup
83     global start_requested
84     if start_requested:
85         start_requested = False
86         cumulative_delay = 0
87         for name in database.db.iterkeys():
88             accounts.get(name).start(delay=cumulative_delay)
89             cumulative_delay += 3
90
91 def deliver_ticket(data): return GetSlivers_callback(data, fullupdate=False)
92
93
94 def start(options, config):
95     accounts.register_class(sliver_vs.Sliver_VS)
96     accounts.register_class(delegate.Delegate)
97     global start_requested
98     start_requested = options.startup
99     database.start()
100     api.deliver_ticket = deliver_ticket
101     api.start()