- # apply policy. Do not allow nodes found in blacklist, only allow nodes found in whitelist
- whitelist_policy = lambda node: node in self.policy['whitelist']
- blacklist_policy = lambda node: node not in self.policy['blacklist']
-
- if self.policy['blacklist']:
- self.components = blacklist_policy(self.components)
- if self.policy['whitelist']:
- self.components = whitelist_policy(self.components)
-
- # update timestamp and threshold
- self.timestamp = datetime.datetime.now()
- delta = datetime.timedelta(hours=self.components_ttl)
- self.threshold = self.timestamp + delta
-
- f = open(self.components_file, 'w')
- f.write(str(self.components))
- f.close()
- f = open(self.timestamp_file, 'w')
- f.write(str(self.threshold))
- f.close()
-
- def load_components(self):
- """
- Read cached list of nodes.
- """
- # Read component list from cached file
- if os.path.exists(self.components_file):
- f = open(self.components_file, 'r')
- self.components = eval(f.read())
- f.close()
-
- time_format = "%Y-%m-%d %H:%M:%S"
- if os.path.exists(self.timestamp_file):
- f = open(self.timestamp_file, 'r')
- timestamp = str(f.read()).split(".")[0]
- self.timestamp = datetime.datetime.fromtimestamp(time.mktime(time.strptime(timestamp, time_format)))
- delta = datetime.timedelta(hours=self.components_ttl)
- self.threshold = self.timestamp + delta
- f.close()
-
- def load_policy(self):
- """
- Read the list of blacklisted and whitelisted nodes.
- """
- whitelist = []
- blacklist = []
- if os.path.exists(self.whitelist_file):
- f = open(self.whitelist_file, 'r')
- lines = f.readlines()
- f.close()
- for line in lines:
- line = line.strip().replace(" ", "").replace("\n", "")
- whitelist.extend(line.split(","))
-
-
- if os.path.exists(self.blacklist_file):
- f = open(self.blacklist_file, 'r')
- lines = f.readlines()
- f.close()
- for line in lines:
- line = line.strip().replace(" ", "").replace("\n", "")
- blacklist.extend(line.split(","))
-
- self.policy['whitelist'] = whitelist
- self.policy['blacklist'] = blacklist
-
- def get_components(self):
- """
- Return a list of components at this aggregate.
- """
- # Reload components list
- now = datetime.datetime.now()
- #self.load_components()
- if not self.threshold or not self.timestamp or now > self.threshold:
- self.refresh_components()
- elif now < self.threshold and not self.components:
- self.load_components()
- return self.components
-
- def get_rspec(self, hrn, type):
- rspec = Rspec()
- rspec['nodespec'] = {'name': self.conf.GENI_INTERFACE_HRN}
- rsepc['nodespec']['nodes'] = []
- if type in ['node']:
- nodes = self.shell.GetNodes(self.auth)
- elif type in ['slice']:
- slicename = hrn_to_pl_slicename(hrn)
- slices = self.shell.GetSlices(self.auth, [slicename])
- node_ids = slices[0]['node_ids']
- nodes = self.shell.GetNodes(self.auth, node_ids)
- for node in nodes:
- nodespec = {'name': node['hostname'], 'type': 'std'}
- elif type in ['aggregate']:
- pass
-
- return rspec
-
- def get_resources(self, slice_hrn):
- """
- Return the current rspec for the specified slice.
- """
- slicename = hrn_to_plcslicename(slice_hrn)
- rspec = self.get_rspec(slicenamem, 'slice')
-
- return rspec
-
- def create_slice(self, slice_hrn, rspec, attributes = []):
- """
- Instantiate the specified slice according to whats defined in the rspec.
- """
- slicename = self.hrn_to_plcslicename(slice_hrn)
- spec = Rspec(rspec)
- nodespecs = spec.getDictsByTagName('NodeSpec')
- nodes = [nodespec['name'] for nodespec in nodespecs]
- self.shell.AddSliceToNodes(self.auth, slicename, nodes)
- for attribute in attributes:
- type, value, node, nodegroup = attribute['type'], attribute['value'], attribute['node'], attribute['nodegroup']
- shell.AddSliceAttribute(self.auth, slicename, type, value, node, nodegroup)