1203e641b31545843883a560dcf2332d8f80957b
[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     for d in data:
54         if d['node_id'] != node_id: continue
55         for sliver in d['slivers']:
56             rec = sliver.copy()
57             rec.setdefault('timestamp', d['timestamp'])
58
59             # convert attributes field to a proper dict
60             attr_dict = {}
61             for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
62
63             # squash keys
64             keys = rec.pop('keys')
65             rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
66
67             rec.setdefault('type', attr_dict.get('type', 'sliver.VServer'))
68             rec.setdefault('vref', attr_dict.get('vref', 'default'))
69             rec.setdefault('initscript', attr_dict.get('initscript', ''))
70             rec.setdefault('delegations', [])  # XXX - delegation not yet supported
71
72             # extract the implied rspec
73             rspec = {}
74             rec['rspec'] = rspec
75             for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
76                 try: amt = int(attr_dict[resname])
77                 except (KeyError, ValueError): amt = default_amt
78                 rspec[resname] = amt
79             database.db.deliver_record(rec)
80         if fullupdate: database.db.set_min_timestamp(d['timestamp'])
81     database.db.sync()
82
83     # handle requested startup
84     global start_requested
85     if start_requested:
86         start_requested = False
87         cumulative_delay = 0
88         for name in database.db.iterkeys():
89             accounts.get(name).start(delay=cumulative_delay)
90             cumulative_delay += 3
91
92 def deliver_ticket(data): return GetSlivers_callback(data, fullupdate=False)
93
94
95 def start(options, config):
96     accounts.register_class(sliver_vs.Sliver_VS)
97     accounts.register_class(delegate.Delegate)
98     global start_requested
99     start_requested = options.startup
100     database.start()
101     api.deliver_ticket = deliver_ticket
102     api.start()