- def refresh(self):
- """
- Update the cached list of slices
- """
- # Reload components list
- now = datetime.datetime.now()
- if not self.has_key('threshold') or not self.has_key('timestamp') or \
- now > datetime.datetime.fromtimestamp(time.mktime(time.strptime(self['threshold'], self.api.time_format))):
- if self.api.interface in ['aggregate']:
- self.refresh_slices_aggregate()
- elif self.api.interface in ['slicemgr']:
- self.refresh_slices_smgr()
-
- def refresh_slices_aggregate(self):
- slices = self.api.plshell.GetSlices(self.api.plauth, {'peer_id': None}, ['name'])
- slice_hrns = [slicename_to_hrn(self.api.hrn, slice['name']) for slice in slices]
-
- # update timestamp and threshold
- timestamp = datetime.datetime.now()
- hr_timestamp = timestamp.strftime(self.api.time_format)
- delta = datetime.timedelta(hours=self.ttl)
- threshold = timestamp + delta
- hr_threshold = threshold.strftime(self.api.time_format)
-
- slice_details = {'hrn': slice_hrns,
- 'timestamp': hr_timestamp,
- 'threshold': hr_threshold
- }
- self.update(slice_details)
- self.write()
-
-
- def refresh_slices_smgr(self):
- slice_hrns = []
- aggregates = Aggregates(self.api)
- credential = self.api.getCredential()
- for aggregate in aggregates:
- success = False
- # request hash is optional so lets try the call without it
- try:
- slices = aggregates[aggregate].get_slices(credential)
- slice_hrns.extend(slices)
- success = True
- except:
- print >> log, "%s" % (traceback.format_exc())
- print >> log, "Error calling slices at aggregate %(aggregate)s" % locals()
-
- # try sending the request hash if the previous call failed
- if not success:
- arg_list = [credential]
- request_hash = self.api.key.compute_hash(arg_list)
- try:
- slices = aggregates[aggregate].get_slices(credential, request_hash)
- slice_hrns.extend(slices)
- success = True
- except:
- print >> log, "%s" % (traceback.format_exc())
- print >> log, "Error calling slices at aggregate %(aggregate)s" % locals()
-
- # update timestamp and threshold
- timestamp = datetime.datetime.now()
- hr_timestamp = timestamp.strftime(self.api.time_format)
- delta = datetime.timedelta(hours=self.ttl)
- threshold = timestamp + delta
- hr_threshold = threshold.strftime(self.api.time_format)
-
- slice_details = {'hrn': slice_hrns,
- 'timestamp': hr_timestamp,
- 'threshold': hr_threshold
- }
- self.update(slice_details)
- self.write()
-
-
- def delete_slice(self, hrn):
- if self.api.interface in ['aggregate']:
- self.delete_slice_aggregate(hrn)
- elif self.api.interface in ['slicemgr']:
- self.delete_slice_smgr(hrn)
-
- def delete_slice_aggregate(self, hrn):
-
- slicename = hrn_to_pl_slicename(hrn)
- slices = self.api.plshell.GetSlices(self.api.plauth, {'name': slicename})
- if not slices:
- return 1
- slice = slices[0]
-
- # determine if this is a peer slice
- peer = self.get_peer(hrn)
- if peer:
- self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'slice', slice['slice_id'], peer)
- self.api.plshell.DeleteSliceFromNodes(self.api.plauth, slicename, slice['node_ids'])
- if peer:
- self.api.plshell.BindObjectToPeer(self.api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
- return 1
-
- def delete_slice_smgr(self, hrn):
- credential = self.api.getCredential()
- caller_cred = self.caller_cred
- aggregates = Aggregates(self.api)
- for aggregate in aggregates:
- success = False
- # request hash is optional so lets try the call without it
- try:
- aggregates[aggregate].delete_slice(credential, hrn, caller_cred)
- success = True
- except:
- print >> log, "%s" % (traceback.format_exc())
- print >> log, "Error calling list nodes at aggregate %s" % aggregate
-
- # try sending the request hash if the previous call failed
- if not success:
- try:
- arg_list = [credential, hrn]
- request_hash = self.api.key.compute_hash(arg_list)
- aggregates[aggregate].delete_slice(credential, hrn, request_hash, caller_cred)
- success = True
- except:
- print >> log, "%s" % (traceback.format_exc())
- print >> log, "Error calling list nodes at aggregate %s" % aggregate
-
- def create_slice(self, hrn, rspec):
-
- # check our slice policy before we procede
- whitelist = self.policy['slice_whitelist']
- blacklist = self.policy['slice_blacklist']
-
- if whitelist and hrn not in whitelist or \
- blacklist and hrn in blacklist:
- policy_file = self.policy.policy_file
- print >> log, "Slice %(hrn)s not allowed by policy %(policy_file)s" % locals()
- return 1
-
- if self.api.interface in ['aggregate']:
- self.create_slice_aggregate(hrn, rspec)
- elif self.api.interface in ['slicemgr']:
- self.create_slice_smgr(hrn, rspec)
-