<commit_before>'''
Created on Mar 15, 2012

@author: clarkmatthew
'''

import re

class ServiceState:
    ENABLED=1
    DISABLED=2
    STOPPED=3
    NOT_READY=4
    BROKEN=5
    
class DescribeString:
    #SERVICE    storage  PARTI00  SC_61  ENABLED  16 http://192.168.51.32:8773/services/Storage    arn:euca:eucalyptus:PARTI00:storage:SC_61/
    name=1
    location=2
    component=3
    state=4
    num=5
    uri=6
    path=7


class ServiceName:
    clc="eucalyptus"
    sc="storage"
    cc="cluster"
    walrus="walrus"
    euare="euare"
    properties="properties"
    jetty="jetty"
    bootstrap="bootstrap"
    configuration="configuration"
    component="component"
    notifications="notifications"
    dns="dns"
    
    
class Euservice:

    def __init__(self, service_string, tester = None):
        values = service_string.split()
        self.type = values[1]
        self.partition = values[2]
        self.name = values[3]
        self.state = values[4]
        self.uri = values[6]
        self.fullname = values[7]
        self.hostname = self.uri.split(":")[1].split("/")[2]
        self.running = True
        if tester is not None:
            self.tester = tester
            self.machine = tester.get_machine_by_ip(self.hostname)
        
    def isEnabled(self):
        if re.search('ENABLED',self.state) :
            return True
        else:
            return False
        
    def isDisabled(self):
        if not re.search('ENABLED', self.state):
            return True
        else:
            return False
    
    def disable(self):
        self.tester.service_manager.disable(self)
        
    def enable(self):
        self.tester.service_manager.enable(self)
    
    def stop(self):
        self.tester.service_manager.stop(self)
    
    def start(self):
        self.tester.service_manager.start(self)
    
        
class Partition:
    name = ""
    ccs = []
    scs = []
    vbs = []
    ncs = []
    volumes = []
    instances = []
    
    def __init__(self, name, service_manager ):
        self.name = name
        self.service_manager = service_manager
        
    def get_enabled(self, list):
        self.service_manager.update()
        for service in list:
            if service.isEnabled():
                return service
        return None
    
    def get_disabled(self, list):
        self.service_manager.update()
        for service in list:
            if not service.isEnabled():
                return service
        return None
    
    def get_enabled_cc(self):
        return self.get_enabled(self.ccs)
    
    def get_enabled_sc(self):
        return self.get_enabled(self.scs)
    
    def get_enabled_vb(self):
        return self.get_enabled(self.vbs)
    
    def get_disabled_cc(self):
        return self.get_disabled(self.ccs)
    
    def get_disabled_sc(self):
        return self.get_disabled(self.scs)
    
    def get_disabled_vb(self):
        return self.get_disabled(self.vbs)
 
class EuserviceManager(object):
   
        
    def __init__(self, tester ):
        '''
        SERVICE    storage            PARTI00            SC_61              ENABLED       16      http://192.168.51.32:8773/services/Storage    arn:euca:eucalyptus:PARTI00:storage:SC_61/
        update this service based up on the information parsed out of the "describestring"
        '''
        ### Make sure i have the right connection to make first contact with euca-describe-services
        self.walruses= []
        self.clcs = []
        self.arbitrators = []
        self.partitions = {}
        self.internal_components = []
        self.dns = None
        self.tester = tester
        self.eucaprefix = ". " + self.tester.credpath + "/eucarc && " + self.tester.eucapath
        if self.tester.clc is None:
            raise AttributeError("Tester object does not have CLC machine to use for SSH")
        
        self.update()

    
    def get(self, type=None, partition=None, attempt_both=True):
        if type is not None:
            type = " -T " + str(type) 
        else:
            type = ""
        #### This is a hack around the fact that the -P filter is not working need to fix this once that functionality is fixed
        if partition is not None:
            partition = " | grep " + str(partition) 
        else:
            partition = ""
        try:
            describe_services = self.tester.clc.sys(self.eucaprefix + "/usr/sbin/euca-describe-services --system-internal " + str(type)  +  "| grep SERVICE" + str(partition), timeout=15)
            if len(describe_services) < 1:
                raise IndexError("Did not receive proper response from describe services when looking for " + str(type))
        except Exception, e:
            if len(self.get_component_machines("clc")) is 1:
                raise Exception("Unable to get service information from the only clc: " + self.tester.clc.hostname )
            if attempt_both:
                self.tester.swap_clc()
                describe_services = self.tester.clc.sys(self.eucaprefix + "/usr/sbin/euca-describe-services --system-internal " + str(type)  + " | grep SERVICE "  + str(partition)  , timeout=15)
            if len(describe_services) < 1:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                    raise IndexError("Did not receive proper response from describe services when looking for " + str(type))
        services = []
        for service_line in describe_services:
            services.append(Euservice(service_line, self.tester))
        return services
    
    def reset(self):
        self.walruses= []
        self.clcs = []<commit_msg>Fix for getting enaabled services properly<commit_after>'''
Created on Mar 15, 2012

@author: clarkmatthew
'''

import re

class ServiceState:
    ENABLED=1
    DISABLED=2
    STOPPED=3
    NOT_READY=4
    BROKEN=5
    
class DescribeString:
    #SERVICE    storage  PARTI00  SC_61  ENABLED  16 http://192.168.51.32:8773/services/Storage    arn:euca:eucalyptus:PARTI00:storage:SC_61/
    name=1
    location=2
    component=3
    state=4
    num=5
    uri=6
    path=7


class ServiceName:
    clc="eucalyptus"
    sc="storage"
    cc="cluster"
    walrus="walrus"
    euare="euare"
    properties="properties"
    jetty="jetty"
    bootstrap="bootstrap"
    configuration="configuration"
    component="component"
    notifications="notifications"
    dns="dns"
    
    
class Euservice:

    def __init__(self, service_string, tester = None):
        values = service_string.split()
        self.type = values[1]
        self.partition = values[2]
        self.name = values[3]
        self.state = values[4]
        self.uri = values[6]
        self.fullname = values[7]
        self.hostname = self.uri.split(":")[1].split("/")[2]
        self.running = True
        if tester is not None:
            self.tester = tester
            self.machine = tester.get_machine_by_ip(self.hostname)
        
    def isEnabled(self):
        if re.search('ENABLED',self.state) :
            return True
        else:
            return False
        
    def isDisabled(self):
        if not re.search('ENABLED', self.state):
            return True
        else:
            return False
    
    def disable(self):
        self.tester.service_manager.disable(self)
        
    def enable(self):
        self.tester.service_manager.enable(self)
    
    def stop(self):
        self.tester.service_manager.stop(self)
    
    def start(self):
        self.tester.service_manager.start(self)
    
        
class Partition:
    name = ""
    ccs = []
    scs = []
    vbs = []
    ncs = []
    volumes = []
    instances = []
    
    def __init__(self, name, service_manager ):
        self.name = name
        self.service_manager = service_manager
        
    def get_enabled(self, list):
        self.service_manager.update()
        for service in list:
            if service.isEnabled():
                return service
        return None
    
    def get_disabled(self, list):
        self.service_manager.update()
        for service in list:
            if not service.isEnabled():
                return service
        return None
    
    def get_enabled_cc(self):
        return self.get_enabled(self.ccs)
    
    def get_enabled_sc(self):
        return self.get_enabled(self.scs)
    
    def get_enabled_vb(self):
        return self.get_enabled(self.vbs)
    
    def get_disabled_cc(self):
        return self.get_disabled(self.ccs)
    
    def get_disabled_sc(self):
        return self.get_disabled(self.scs)
    
    def get_disabled_vb(self):
        return self.get_disabled(self.vbs)
 
class EuserviceManager(object):
   
        
    def __init__(self, tester ):
        '''
        SERVICE    storage            PARTI00            SC_61              ENABLED       16      http://192.168.51.32:8773/services/Storage    arn:euca:eucalyptus:PARTI00:storage:SC_61/
        update this service based up on the information parsed out of the "describestring"
        '''
        ### Make sure i have the right connection to make first contact with euca-describe-services
        self.walruses= []
        self.clcs = []
        self.arbitrators = []
        self.partitions = {}
        self.internal_components = []
        self.dns = None
        self.tester = tester
        self.eucaprefix = ". " + self.tester.credpath + "/eucarc && " + self.tester.eucapath
        if self.tester.clc is None:
            raise AttributeError("Tester object does not have CLC machine to use for SSH")
        
        self.update()

    
    def get(self, type=None, partition=None, attempt_both=True):
        if type is not None:
            type = " -T " + str(type) 
        else:
            type = ""
        #### This is a hack around the fact that the -P filter is not working need to fix this once that functionality is fixed
        if partition is not None:
            partition = " | grep " + str(partition) 
        else:
            partition = ""
        try:
            describe_services = self.tester.clc.sys(self.eucaprefix + "/usr/sbin/euca-describe-services --system-internal " + str(type)  +  "| grep SERVICE" + str(partition), timeout=15)
            if len(describe_services) < 1:
                raise IndexError("Did not receive proper response from describe services when looking for " + str(type))
        except Exception, e:
            if len(self.tester.get_component_machines("clc")) is 1:
                raise Exception("Unable to get service information from the only clc: " + self.tester.clc.hostname )
            if attempt_both:
                self.tester.swap_clc()
                describe_services = self.tester.clc.sys(self.eucaprefix + "/usr/sbin/euca-describe-services --system-internal " + str(type)  + " | grep SERVICE "  + str(partition)  , timeout=15)
            if len(describe_services) < 1:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                    raise IndexError("Did not receive proper response from describe services when looking for " + str(type))
        services = []
        for service_line in describe_services:
            services.append(Euservice(service_line, self.tester))
        return services
    
    def reset(self):
        self.walruses= []
        self.clcs = []
{'fraction_non_alphanumeric': 0.05906564036775711, 'fraction_numerical': 0.008659397049390635, 'mean_word_length': 0.6640576358623144, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 374, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4514349', 'n_tokens_mistral': 3467, 'n_tokens_neox': 3336, 'n_words': 777}