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.
10 # $Id: sm.py,v 1.28 2007/07/27 18:02:36 dhozac Exp $
12 try: from bwlimit import bwmin, bwmax
13 except ImportError: bwmin, bwmax = 8, 1000*1000*1000
24 DEFAULT_ALLOCATION = {
27 'cpu_pct': 0, # percent CPU reserved
28 'cpu_share': 1, # proportional share
29 # bandwidth parameters
30 'net_min_rate': bwmin / 1000, # kbps
31 'net_max_rate': bwmax / 1000, # kbps
32 'net_share': 1, # proportional share
33 # bandwidth parameters over routes exempt from node bandwidth limits
34 'net_i2_min_rate': bwmin / 1000, # kbps
35 'net_i2_max_rate': bwmax / 1000, # kbps
36 'net_i2_share': 1, # proportional share
37 'net_max_kbyte' : 5662310, #Kbyte
38 'net_thresh_kbyte': 4529848, #Kbyte
39 'net_i2_max_kbyte': 17196646,
40 'net_i2_thresh_kbyte': 13757316,
42 'disk_max': 5000000, # bytes
46 'ip_addresses': '0.0.0.0',
48 # NOTE: this table is further populated with resource names and
49 # default amounts via the start() function below. This probably
50 # should be changeg and these values should be obtained via the
54 start_requested = False # set to True in order to request that all slivers be started
56 @database.synchronized
57 def GetSlivers(data, fullupdate=True):
58 """This function has two purposes. One, convert GetSlivers() data
59 into a more convenient format. Two, even if no updates are coming
60 in, use the GetSlivers() heartbeat as a cue to scan for expired
63 logger.verbose("Entering sm:GetSlivers with fullupdate=%r"%fullupdate)
64 for key in data.keys():
65 logger.verbose('GetSlivers key : ' + key)
69 f = open('/etc/planetlab/node_id')
70 try: node_id = int(f.read())
72 except: logger.log_exc()
74 if data.has_key('node_id') and data['node_id'] != node_id: return
76 if data.has_key('networks'):
77 for network in data['networks']:
78 if network['is_primary'] and network['bwlimit'] is not None:
79 DEFAULT_ALLOCATION['net_max_rate'] = network['bwlimit'] / 1000
81 # Take intscripts (global) returned by API, make dict
83 for is_rec in data['initscripts']:
84 logger.verbose("initscript: %s" % is_rec['name'])
85 initscripts[str(is_rec['name'])] = is_rec['script']
87 for sliver in data['slivers']:
88 logger.verbose("sm:GetSlivers in slivers loop")
90 rec.setdefault('timestamp', data['timestamp'])
92 # convert attributes field to a proper dict
94 for attr in rec.pop('attributes'): attr_dict[attr['name']] = attr['value']
97 keys = rec.pop('keys')
98 rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
100 # Handle nm controller here
101 rec.setdefault('type', attr_dict.get('type', 'sliver.VServer'))
102 if rec['instantiation'] == 'nm-controller':
103 # type isn't returned by GetSlivers() for whatever reason. We're overloading
104 # instantiation here, but i suppose its the ssame thing when you think about it. -FA
105 rec['type'] = 'delegate'
107 # set the vserver reference. If none, set to default.
108 rec.setdefault('vref', attr_dict.get('vref', 'default'))
110 # set initscripts. first check if exists, if not, leave empty.
111 is_name = attr_dict.get('initscript')
112 if is_name is not None and is_name in initscripts:
113 rec['initscript'] = initscripts[is_name]
115 rec['initscript'] = ''
117 # set delegations, if none, set empty
118 rec.setdefault('delegations', attr_dict.get("delegations", []))
120 # extract the implied rspec
123 for resname, default_amt in DEFAULT_ALLOCATION.iteritems():
125 t = type(default_amt)
126 amt = t.__new__(t, attr_dict[resname])
127 except (KeyError, ValueError): amt = default_amt
130 database.db.deliver_record(rec)
131 if fullupdate: database.db.set_min_timestamp(data['timestamp'])
133 accounts.Startingup = False
135 def deliver_ticket(data): return GetSlivers(data, fullupdate=False)
138 def start(options, config):
139 for resname, default_amt in sliver_vs.DEFAULT_ALLOCATION.iteritems():
140 DEFAULT_ALLOCATION[resname]=default_amt
142 accounts.register_class(sliver_vs.Sliver_VS)
143 accounts.register_class(delegate.Delegate)
144 accounts.Startingup = options.startup
146 api_calls.deliver_ticket = deliver_ticket