<commit_before>from nova.exception import ImageNotFound
from nova.api.ec2.cloud import CloudController
from sfa.util.faults import SfaAPIError
from sfa.rspecs.rspec import RSpec
from sfa.rspecs.elements.hardware_type import HardwareType
from sfa.rspecs.elements.node import Node
from sfa.rspecs.elements.sliver import Sliver
from sfa.rspecs.elements.login import Login
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.services import Services
from sfa.util.xrn import Xrn
from sfa.util.osxrn import OSXrn
from sfa.rspecs.version_manager import VersionManager


def disk_image_to_rspec_object(image):
    img = DiskImage()
    img['name'] = image['name']
    img['description'] = image['name']
    img['os'] = image['name']
    img['version'] = image['name']
    return img
    

def instance_to_sliver(instance, slice_xrn=None):
    # should include?
    # * instance.image_ref
    # * instance.kernel_id
    # * instance.ramdisk_id
    import nova.db.sqlalchemy.models
    name=None
    type=None
    sliver_id = None
    if isinstance(instance, dict):
        # this is an isntance type dict
        name = instance['name']
        type = instance['name']
    elif isinstance(instance, nova.db.sqlalchemy.models.Instance):
        # this is an object that describes a running instance
        name = instance.display_name
        type = instance.instance_type.name
    else:
        raise SfaAPIError("instnace must be an instance_type dict or" + \
                           " a nova.db.sqlalchemy.models.Instance object")
    if slice_xrn:
        xrn = Xrn(slice_xrn, 'slice')
        sliver_id = xrn.get_sliver_id(instance.project_id, instance.hostname, instance.id)

    sliver = Sliver({'slice_id': sliver_id,
                     'name': name,
                     'type': 'plos-' + type,
                     'tags': []})
    return sliver
            

class OSAggregate:

    def __init__(self, driver):
        self.driver = driver

    def get_machine_image_details(self, image):
        """
        Returns a dict that contains the ami, aki and ari details for the specified
        ami image. 
        """
        disk_image = {}
        if image['container_format'] == 'ami':
            disk_image['ami'] = image
            disk_image['aki'] = self.driver.shell.image_manager.show(image['kernel_id'])
            disk_image['ari'] = self.driver.shell.image_manager.show(image['ramdisk_id'])
        return disk_image
        
    def get_disk_image(self, id=None, name=None):
        """
        Look up a image bundle using the specifeid id or name  
        """
        disk_image = None    
        try:
            if id:
                image = self.driver.shell.image_manager.show(image_id)
            elif name:
                image = self.driver.shell.image_manager.show_by_name(image_name)
            if image['container_format'] == 'ami':
                disk_image = self.get_machine_image_details(image)
        except ImageNotFound:
                pass
        return disk_image

    def get_available_disk_images(self):
        # get image records
        disk_images = []
        for image in self.driver.shell.image_manager.detail():
            if image['container_format'] == 'ami':
                disk_images.append(self.get_machine_image_details(image))
        return disk_images 

    def get_rspec(self, slice_xrn=None, version=None, options={}):
        version_manager = VersionManager()
        version = version_manager.get_version(version)
        if not slice_xrn:
            rspec_version = version_manager._get_version(version.type, version.version, 'ad')
            nodes = self.get_aggregate_nodes()
        else:
            rspec_version = version_manager._get_version(version.type, version.version, 'manifest')
            nodes = self.get_slice_nodes(slice_xrn)
        rspec = RSpec(version=rspec_version, user_options=options)
        rspec.version.add_nodes(nodes)
        return rspec.toxml()

    def get_slice_nodes(self, slice_xrn):
        name = OSXrn(xrn = slice_xrn).name
        instances = self.driver.shell.db.instance_get_all_by_project(name)
        rspec_nodes = []
        for instance in instances:
            rspec_node = Node()
            xrn = OSXrn(instance.hostname, 'node')
            rspec_node['component_id'] = xrn.urn
            rspec_node['component_name'] = xrn.name
            rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn()   
            sliver = instance_to_sliver(instance)
            disk_image = self.get_disk_image(instance.image_ref)
            sliver['disk_images'] = [disk_image_to_rspec_object(disk_image)]
            rspec_node['slivers'] = [sliver]
            rspec_nodes.append(rspec_node)
        return rspec_nodes

    def get_aggregate_nodes(self):
                
        zones = self.driver.shell.db.zone_get_all()
        if not zones:
            zones = ['cloud']
        else:
            zones = [zone.name for zone in zones]

        # available sliver/instance/vm types
        instances = self.driver.shell.db.instance_type_get_all().values()
        # available images
        disk_images = self.get_available_disk_images()
        disk_image_objects = [disk_image_to_rspec_object(image) \
                               for image in disk_image]  
        rspec_nodes = []
        for zone in zones:
            rspec_node = Node()
            xrn = OSXrn(zone, 'node')
            rspec_node['component_id'] = xrn.urn
            rspec_node['component_name'] = xrn.name
            rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn()
            rspec_node['exclusive'] = 'false'
            rspec_node['hardware_types'] = [HardwareType({'name': 'plos-pc'}),
                                                HardwareType({'name': 'pc'})]
            slivers = []
            for instance in instances:
                sliver = instance_to_sliver(instance)
                sliver['disk_images'] = disk_image_objects
                slivers.append(sliver)
        
            rspec_node['slivers'] = slivers
            rspec_nodes.append(rspec_node) 

        return rspec_nodes 


    def create_project(self, slicename, users, options={}):
        """
        Create the slice if it doesn't alredy exist  
        """
        import nova.exception.ProjectNotFound
        try:
            slice = self.driver.shell.auth_manager.get_project(slicename)
        except nova.exception.ProjectNotFound:
            # convert urns to user names<commit_msg>fix bugs in disk_image_to_rspec_object(), get_machine_image_details()<commit_after>from nova.exception import ImageNotFound
from nova.api.ec2.cloud import CloudController
from sfa.util.faults import SfaAPIError
from sfa.rspecs.rspec import RSpec
from sfa.rspecs.elements.hardware_type import HardwareType
from sfa.rspecs.elements.node import Node
from sfa.rspecs.elements.sliver import Sliver
from sfa.rspecs.elements.login import Login
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.services import Services
from sfa.util.xrn import Xrn
from sfa.util.osxrn import OSXrn
from sfa.rspecs.version_manager import VersionManager


def disk_image_to_rspec_object(image):
    img = DiskImage()
    img['name'] = image['ami']['name']
    img['description'] = image['ami']['name']
    img['os'] = image['ami']['name']
    img['version'] = image['ami']['name']
    return img
    

def instance_to_sliver(instance, slice_xrn=None):
    # should include?
    # * instance.image_ref
    # * instance.kernel_id
    # * instance.ramdisk_id
    import nova.db.sqlalchemy.models
    name=None
    type=None
    sliver_id = None
    if isinstance(instance, dict):
        # this is an isntance type dict
        name = instance['name']
        type = instance['name']
    elif isinstance(instance, nova.db.sqlalchemy.models.Instance):
        # this is an object that describes a running instance
        name = instance.display_name
        type = instance.instance_type.name
    else:
        raise SfaAPIError("instnace must be an instance_type dict or" + \
                           " a nova.db.sqlalchemy.models.Instance object")
    if slice_xrn:
        xrn = Xrn(slice_xrn, 'slice')
        sliver_id = xrn.get_sliver_id(instance.project_id, instance.hostname, instance.id)

    sliver = Sliver({'slice_id': sliver_id,
                     'name': name,
                     'type': 'plos-' + type,
                     'tags': []})
    return sliver
            

class OSAggregate:

    def __init__(self, driver):
        self.driver = driver

    def get_machine_image_details(self, image):
        """
        Returns a dict that contains the ami, aki and ari details for the specified
        ami image. 
        """
        disk_image = {}
        if image['container_format'] == 'ami':
            kernel_id = image['properties']['kernel_id']
            ramdisk_id = image['properties']['ramdisk_id']
            disk_image['ami'] = image
            disk_image['aki'] = self.driver.shell.image_manager.show(kernel_id)
            disk_image['ari'] = self.driver.shell.image_manager.show(ramdisk_id)
        return disk_image
        
    def get_disk_image(self, id=None, name=None):
        """
        Look up a image bundle using the specifeid id or name  
        """
        disk_image = None    
        try:
            if id:
                image = self.driver.shell.image_manager.show(image_id)
            elif name:
                image = self.driver.shell.image_manager.show_by_name(image_name)
            if image['container_format'] == 'ami':
                disk_image = self.get_machine_image_details(image)
        except ImageNotFound:
                pass
        return disk_image

    def get_available_disk_images(self):
        # get image records
        disk_images = []
        for image in self.driver.shell.image_manager.detail():
            if image['container_format'] == 'ami':
                disk_images.append(self.get_machine_image_details(image))
        return disk_images 

    def get_rspec(self, slice_xrn=None, version=None, options={}):
        version_manager = VersionManager()
        version = version_manager.get_version(version)
        if not slice_xrn:
            rspec_version = version_manager._get_version(version.type, version.version, 'ad')
            nodes = self.get_aggregate_nodes()
        else:
            rspec_version = version_manager._get_version(version.type, version.version, 'manifest')
            nodes = self.get_slice_nodes(slice_xrn)
        rspec = RSpec(version=rspec_version, user_options=options)
        rspec.version.add_nodes(nodes)
        return rspec.toxml()

    def get_slice_nodes(self, slice_xrn):
        name = OSXrn(xrn = slice_xrn).name
        instances = self.driver.shell.db.instance_get_all_by_project(name)
        rspec_nodes = []
        for instance in instances:
            rspec_node = Node()
            xrn = OSXrn(instance.hostname, 'node')
            rspec_node['component_id'] = xrn.urn
            rspec_node['component_name'] = xrn.name
            rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn()   
            sliver = instance_to_sliver(instance)
            disk_image = self.get_disk_image(instance.image_ref)
            sliver['disk_images'] = [disk_image_to_rspec_object(disk_image)]
            rspec_node['slivers'] = [sliver]
            rspec_nodes.append(rspec_node)
        return rspec_nodes

    def get_aggregate_nodes(self):
                
        zones = self.driver.shell.db.zone_get_all()
        if not zones:
            zones = ['cloud']
        else:
            zones = [zone.name for zone in zones]

        # available sliver/instance/vm types
        instances = self.driver.shell.db.instance_type_get_all().values()
        # available images
        disk_images = self.get_available_disk_images()
        disk_image_objects = [disk_image_to_rspec_object(image) \
                               for image in disk_images]  
        rspec_nodes = []
        for zone in zones:
            rspec_node = Node()
            xrn = OSXrn(zone, 'node')
            rspec_node['component_id'] = xrn.urn
            rspec_node['component_name'] = xrn.name
            rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn()
            rspec_node['exclusive'] = 'false'
            rspec_node['hardware_types'] = [HardwareType({'name': 'plos-pc'}),
                                                HardwareType({'name': 'pc'})]
            slivers = []
            for instance in instances:
                sliver = instance_to_sliver(instance)
                sliver['disk_images'] = disk_image_objects
                slivers.append(sliver)
        
            rspec_node['slivers'] = slivers
            rspec_nodes.append(rspec_node) 

        return rspec_nodes 


    def create_project(self, slicename, users, options={}):
        """
        Create the slice if it doesn't alredy exist  
        """
        import nova.exception.ProjectNotFound
        try:
            slice = self.driver.shell.auth_manager.get_project(slicename)
        except nova.exception.ProjectNotFound:
            # convert urns to user names
{'fraction_non_alphanumeric': 0.09850925162933553, 'fraction_numerical': 0.00014982395685070042, 'mean_word_length': 2.300370828182942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4484897', 'n_tokens_mistral': 3990, 'n_tokens_neox': 3931, 'n_words': 793}