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.
22 sliver_default_type='sliver.LXC'
23 sliver_class_to_register = sliver_lxc.Sliver_LXC
24 sliver_password_shell = sliver_lxc.Sliver_LXC.SHELL
28 sliver_default_type='sliver.VServer'
29 sliver_class_to_register = sliver_vs.Sliver_VS
30 sliver_password_shell = sliver_vs.Sliver_VS.SHELL
33 try : from plnode.bwlimit import bwmin, bwmax
34 except: bwmin, bwmax = 8, 1000*1000*1000
39 DEFAULT_ALLOCATION = {
42 'cpu_pct': 0, # percent CPU reserved
43 'cpu_share': 1, # proportional share
44 'cpu_cores': "0b", # reserved cpu cores <num_cores>[b]
45 # bandwidth parameters
46 'net_min_rate': bwmin / 1000, # kbps
47 'net_max_rate': bwmax / 1000, # kbps
48 'net_share': 1, # proportional share
49 # bandwidth parameters over routes exempt from node bandwidth limits
50 'net_i2_min_rate': bwmin / 1000, # kbps
51 'net_i2_max_rate': bwmax / 1000, # kbps
52 'net_i2_share': 1, # proportional share
53 'net_max_kbyte' : 10546875, #Kbyte
54 'net_thresh_kbyte': 9492187, #Kbyte
55 'net_i2_max_kbyte': 31640625,
56 'net_i2_thresh_kbyte': 28476562,
58 'disk_max': 10000000, # bytes
62 'ip_addresses': '0.0.0.0',
64 # NOTE: this table is further populated with resource names and
65 # default amounts via the start() function below. This probably
66 # should be changeg and these values should be obtained via the
70 start_requested = False # set to True in order to request that all slivers be started
72 # check leases and adjust the 'reservation_alive' field in slivers
73 # this is not expected to be saved as it will change for the next round
74 def adjustReservedSlivers (data):
76 On reservable nodes, tweak the 'reservation_alive' field to instruct cyclic loop
77 about what to do with slivers.
79 # only impacts reservable nodes
80 if 'reservation_policy' not in data: return
81 policy=data['reservation_policy']
82 if policy not in ['lease_or_idle', 'lease_or_shared']:
83 if policy is not None:
84 logger.log ("unexpected reservation_policy %(policy)s"%locals())
87 logger.log("slivermanager.adjustReservedSlivers")
89 # scan leases that are expected to show in ascending order
91 for lease in data['leases']:
92 if lease['t_from'] <= now and now <= lease['t_until']:
96 def is_system_sliver (sliver):
97 for d in sliver['attributes']:
98 if d['tagname']=='system' and d['value']:
102 # mark slivers as appropriate
103 for sliver in data['slivers']:
104 # system slivers must be kept alive
105 if is_system_sliver(sliver):
106 sliver['reservation_alive']=True
111 # with 'idle_or_shared', just let the field out, behave like a shared node
112 # otherwise, mark all slivers as being turned down
113 if policy == 'lease_or_idle':
114 sliver['reservation_alive']=False
116 # there is an active lease, mark it alive and the other not
117 sliver['reservation_alive'] = sliver['name']==active_lease['name']
119 @database.synchronized
120 def GetSlivers(data, config = None, plc=None, fullupdate=True):
121 """This function has two purposes. One, convert GetSlivers() data
122 into a more convenient format. Two, even if no updates are coming
123 in, use the GetSlivers() heartbeat as a cue to scan for expired
126 logger.verbose("slivermanager: Entering GetSlivers with fullupdate=%r"%fullupdate)
127 for key in data.keys():
128 logger.verbose('slivermanager: GetSlivers key : ' + key)
132 f = open('/etc/planetlab/node_id')
133 try: node_id = int(f.read())
135 except: logger.log_exc("slivermanager: GetSlivers failed to read /etc/planetlab/node_id")
137 if data.has_key('node_id') and data['node_id'] != node_id: return
139 if data.has_key('networks'):
140 for network in data['networks']:
141 if network['is_primary'] and network['bwlimit'] is not None:
142 DEFAULT_ALLOCATION['net_max_rate'] = network['bwlimit'] / 1000
144 # Take initscripts (global) returned by API, build a hash scriptname->code
146 if 'initscripts' not in data:
147 logger.log_missing_data("slivermanager.GetSlivers",'initscripts')
149 for initscript_rec in data['initscripts']:
150 logger.verbose("slivermanager: initscript: %s" % initscript_rec['name'])
151 iscripts_hash[str(initscript_rec['name'])] = initscript_rec['script']
153 adjustReservedSlivers (data)
154 for sliver in data['slivers']:
155 logger.verbose("slivermanager: %s: slivermanager.GetSlivers in slivers loop"%sliver['name'])
157 rec.setdefault('timestamp', data['timestamp'])
159 # convert attributes field to a proper dict
161 for attr in rec.pop('attributes'): attributes[attr['tagname']] = attr['value']
162 rec.setdefault("attributes", attributes)
165 keys = rec.pop('keys')
166 rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys]))
168 ## 'Type' isn't returned by GetSlivers() for whatever reason. We're overloading
169 ## instantiation here, but i suppose its the same thing when you think about it. -FA
170 # Handle nm-controller here
171 if rec['instantiation'].lower() == 'nm-controller':
172 rec.setdefault('type', attributes.get('type', 'controller.Controller'))
174 rec.setdefault('type', attributes.get('type', sliver_default_type))
176 # set the vserver reference. If none, set to default.
177 rec.setdefault('vref', attributes.get('vref', 'default'))
179 ### set initscripts; set empty rec['initscript'] if not
180 # if tag 'initscript_code' is set, that's what we use
181 iscode = attributes.get('initscript_code','')
183 rec['initscript']=iscode
185 isname = attributes.get('initscript')
186 if isname is not None and isname in iscripts_hash:
187 rec['initscript'] = iscripts_hash[isname]
189 rec['initscript'] = ''
191 # set delegations, if none, set empty
192 rec.setdefault('delegations', attributes.get("delegations", []))
194 # extract the implied rspec
197 for resname, default_amount in DEFAULT_ALLOCATION.iteritems():
199 t = type(default_amount)
200 amount = t.__new__(t, attributes[resname])
201 except (KeyError, ValueError): amount = default_amount
202 rspec[resname] = amount
204 # add in sysctl attributes into the rspec
205 for key in attributes.keys():
206 if key.find("sysctl.") == 0:
207 rspec[key] = attributes[key]
209 # also export tags in rspec so they make it to the sliver_vs.start call
210 rspec['tags']=attributes
212 database.db.deliver_record(rec)
213 if fullupdate: database.db.set_min_timestamp(data['timestamp'])
214 # slivers are created here.
217 def deliver_ticket(data):
218 return GetSlivers(data, fullupdate=False)
221 # No default allocation values for LXC yet, think if its necessary given
222 # that they are also default allocation values in this module
223 if implementation == 'vs':
224 for resname, default_amount in sliver_vs.DEFAULT_ALLOCATION.iteritems():
225 DEFAULT_ALLOCATION[resname]=default_amount
227 account.register_class(sliver_class_to_register)
228 account.register_class(controller.Controller)
230 api_calls.deliver_ticket = deliver_ticket
233 ### check if a sliver is running
234 ### a first step to a unified code for codemux
235 def is_running (name):
236 if implementation=='vs':
238 return vserver.VServer(name).is_running()
243 conn = libvirt.open('lxc://')
244 dom = conn.lookupByName(name)
245 running = dom.info()[0] == libvirt.VIR_DOMAIN_RUNNING