Support for -s. More comments. Removed out of date documentation and bwcap.
[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 sliver_vs
17
18
19 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}
20
21 start_requested = False  # set to True in order to request that all slivers be started
22
23
24 @database.synchronized
25 def GetSlivers_callback(data):
26     """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."""
27     for d in data:
28         for sliver in d['slivers']:
29             rec = sliver.copy()
30             rec.setdefault('timestamp', d['timestamp'])
31             rec.setdefault('type', 'sliver.VServer')
32
33             # convert attributes field to a proper dict
34             attr_dict = {}
35             for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
36
37             # squash keys
38             keys = rec.pop('keys')
39             rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
40
41             rec.setdefault('initscript', attr_dict.get('initscript', ''))
42             rec.setdefault('delegations', [])  # XXX - delegation not yet supported
43
44             # extract the implied rspec
45             rspec = {}
46             rec['rspec'] = rspec
47             for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
48                 try: amt = int(attr_dict[resname])
49                 except (KeyError, ValueError): amt = default_amt
50                 rspec[resname] = amt
51             database.db.deliver_record(rec)
52         database.db.set_min_timestamp(d['timestamp'])
53     database.db.sync()
54
55     # handle requested startup
56     global start_requested
57     if start_requested:
58         start_requested = False
59         cumulative_delay = 0
60         for name in database.db.iterkeys():
61             accounts.get(name).start(delay=cumulative_delay)
62             cumulative_delay += 3
63
64
65 def start(options):
66     accounts.register_class(sliver_vs.Sliver_VS)
67     accounts.register_class(delegate.Delegate)
68     global start_requested
69     start_requested = options.startup
70     database.start()
71     api.start()