<commit_before>import os
import sys
import datetime
import time

from util.geniserver import *
from util.geniclient import *
from util.cert import *
from util.trustedroot import *
from util.excep import *
from util.misc import *
from util.config import Config

class SliceMgr(GeniServer):

    hrn = None
    key_file = None
    cert_file = None
    components_file = None
    slices_file = None	
    components_ttl = None
    components = []
    slices = []	
    policy = {}
    timestamp = None
    threshold = None	
    shell = None
    aggregates = {}
	
  
    ##
    # Create a new slice manager object.
    #
    # @param ip the ip address to listen on
    # @param port the port to listen on
    # @param key_file private key filename of registry
    # @param cert_file certificate filename containing public key (could be a GID file)     

    def __init__(self, ip, port, key_file, cert_file, config = "/usr/share/geniwrapper/util/geni_config"):
        GeniServer.__init__(ip, port, key_file, cert_file)
	    self.key_file = key_file
	    self.cert_file = cert_file
	    self.conf = Config(config)
        basedir = self.conf.GENI_BASE_DIR + os.sep
        server_basedir = basedir + os.sep + "plc" + os.sep
	    self.hrn = conf.GENI_INTERFACE_HRN
	
	    # Get list of aggregates this sm talks to
	    aggregates_file = server_basedir + os.sep + 'aggregates'
	    self.load_aggregates(aggregates_file) 
	    self.components_file = os.sep.join([server_basedir, 'components', 'slicemgr.' + hrn + '.comp'])
	    self.slices_file = os.sep.join([server_basedir, 'components', 'slicemgr' + hrn + '.slices'])
	    self.timestamp_file = os.sep.join([server_basedir, 'components', 'slicemgr' + hrn + '.timestamp']) 
	    self.components_ttl = components_ttl
	    self.policy['whitelist'] = []
        self.policy['blacklist'] = []
	    self.connect()

    def load_aggregates(self, aggregates_file):
	    """
	    Get info about the aggregates available to us from file and create 
        an xmlrpc connection to each. If any info is invalid, skip it. 
	    """
	    lines = []
        try:
	        f = open(aggregates_file, 'r')
	        lines = f.readlines()
	        f.close()
	    except: raise 
	
	    for line in lines:
	        # Skip comments
	        if line.strip.startswith("#"):
		        continue
	        agg_info = line.split("\t").split(" ")
	    
	        # skip invalid info
	        if len(agg_info) != 3:
		        continue

	        # create xmlrpc connection using GeniClient
	        hrn, address, port = agg_info[0], agg_info[1], agg_info[2]
	        url = 'https://%(address)s:%(port)s' % locals()
	        self.aggregates[hrn] = GeniClient(url, self.key_file, self.cert_file)


    def item_hrns(self, items):
	    """
	    Take a list of items (components or slices) and return a dictionary where
	    the key is the authoritative hrn and the value is a list of items at that 
	    hrn.
	    """
	    item_hrns = {}
	    agg_hrns = self.aggregates.keys()
	    for agg_hrn in agg_hrns:
	        item_hrns[agg_hrn] = []
	    for item in items:
	        for agg_hrn in agg_hrns:
		        if item.startswith(agg_hrn):
		            item_hrns[agg_hrn] = item

	    return item_hrns	
		 	

    def hostname_to_hrn(self, login_base, hostname):
	    """
	    Convert hrn to plantelab name.
	    """
        genihostname = "_".join(hostname.split("."))
        return ".".join([self.hrn, login_base, genihostname])

    def slicename_to_hrn(self, slicename):
	    """
	    Convert hrn to planetlab name.
	    """
        slicename = slicename.replace("_", ".")
        return ".".join([self.hrn, slicename])

    def refresh_components(self):
	    """
	    Update the cached list of nodes.
	    """
	    print "refreshing"
	
	    aggregates = self.aggregates.keys()
	    all_nodes = []
	    all_slices = []
	    for aggregate in aggregates:
	        try:
		        # resolve components hostnames
	            nodes = self.aggregates[aggregate].get_components()
		        all_nodes.extend(nodes)	
		        # update timestamp and threshold
		        self.timestamp = datetime.datetime.now()
		        delta = datetime.timedelta(hours=self.components_ttl)
		        self.threshold = self.timestamp + delta 
	        except:
		    # XX print out to some error log
		    pass	
   
	    self.components = all_nodes
	    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 and slices.
	"""
	print "loading 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 load_slices(self):
	    """
 	    Read current slice instantiation states.
	    """<commit_msg>formatting<commit_after>import os
import sys
import datetime
import time

from util.geniserver import *
from util.geniclient import *
from util.cert import *
from util.trustedroot import *
from util.excep import *
from util.misc import *
from util.config import Config

class SliceMgr(GeniServer):

    hrn = None
    key_file = None
    cert_file = None
    components_file = None
    slices_file = None	
    components_ttl = None
    components = []
    slices = []	
    policy = {}
    timestamp = None
    threshold = None	
    shell = None
    aggregates = {}
	
  
    ##
    # Create a new slice manager object.
    #
    # @param ip the ip address to listen on
    # @param port the port to listen on
    # @param key_file private key filename of registry
    # @param cert_file certificate filename containing public key (could be a GID file)     

    def __init__(self, ip, port, key_file, cert_file, config = "/usr/share/geniwrapper/util/geni_config"):
        GeniServer.__init__(ip, port, key_file, cert_file)
	    self.key_file = key_file
	    self.cert_file = cert_file
	    self.conf = Config(config)
        basedir = self.conf.GENI_BASE_DIR + os.sep
        server_basedir = basedir + os.sep + "plc" + os.sep
	    self.hrn = conf.GENI_INTERFACE_HRN
	
	    # Get list of aggregates this sm talks to
	    aggregates_file = server_basedir + os.sep + 'aggregates'
	    self.load_aggregates(aggregates_file) 
	    self.components_file = os.sep.join([server_basedir, 'components', 'slicemgr.' + hrn + '.comp'])
	    self.slices_file = os.sep.join([server_basedir, 'components', 'slicemgr' + hrn + '.slices'])
	    self.timestamp_file = os.sep.join([server_basedir, 'components', 'slicemgr' + hrn + '.timestamp']) 
	    self.components_ttl = components_ttl
	    self.policy['whitelist'] = []
        self.policy['blacklist'] = []
	    self.connect()

    def load_aggregates(self, aggregates_file):
	    """
	    Get info about the aggregates available to us from file and create 
        an xmlrpc connection to each. If any info is invalid, skip it. 
	    """
	    lines = []
        try:
	        f = open(aggregates_file, 'r')
	        lines = f.readlines()
	        f.close()
	    except: raise 
	
	    for line in lines:
	        # Skip comments
	        if line.strip.startswith("#"):
		        continue
	        agg_info = line.split("\t").split(" ")
	    
	        # skip invalid info
	        if len(agg_info) != 3:
		        continue

	        # create xmlrpc connection using GeniClient
	        hrn, address, port = agg_info[0], agg_info[1], agg_info[2]
	        url = 'https://%(address)s:%(port)s' % locals()
	        self.aggregates[hrn] = GeniClient(url, self.key_file, self.cert_file)


    def item_hrns(self, items):
	    """
	    Take a list of items (components or slices) and return a dictionary where
	    the key is the authoritative hrn and the value is a list of items at that 
	    hrn.
	    """
	    item_hrns = {}
	    agg_hrns = self.aggregates.keys()
	    for agg_hrn in agg_hrns:
	        item_hrns[agg_hrn] = []
	    for item in items:
	        for agg_hrn in agg_hrns:
		        if item.startswith(agg_hrn):
		            item_hrns[agg_hrn] = item

	    return item_hrns	
		 	

    def hostname_to_hrn(self, login_base, hostname):
	    """
	    Convert hrn to plantelab name.
	    """
        genihostname = "_".join(hostname.split("."))
        return ".".join([self.hrn, login_base, genihostname])

    def slicename_to_hrn(self, slicename):
	    """
	    Convert hrn to planetlab name.
	    """
        slicename = slicename.replace("_", ".")
        return ".".join([self.hrn, slicename])

    def refresh_components(self):
	    """
	    Update the cached list of nodes.
	    """
	    print "refreshing"
	
	    aggregates = self.aggregates.keys()
	    all_nodes = []
	    all_slices = []
	    for aggregate in aggregates:
	        try:
		        # resolve components hostnames
	            nodes = self.aggregates[aggregate].get_components()
		        all_nodes.extend(nodes)	
		        # update timestamp and threshold
		        self.timestamp = datetime.datetime.now()
		        delta = datetime.timedelta(hours=self.components_ttl)
		        self.threshold = self.timestamp + delta 
	        except:
		    # XX print out to some error log
		    pass	
   
	    self.components = all_nodes
	    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 and slices.
	    """
	    print "loading 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 load_slices(self):
	    """
 	    Read current slice instantiation states.
	    """
{'fraction_non_alphanumeric': 0.11647658335355496, 'fraction_numerical': 0.000808865162177465, 'mean_word_length': 2.2752317880794704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3167279', 'n_tokens_mistral': 4112, 'n_tokens_neox': 3772, 'n_words': 942}