def test_instance_generation(self): try: instancesconf = InstancesConf() instancesconf.instances_conf_path = self.filename for instance in instancesconf: self.assertEquals(instance.name, self.instance_name) self.assertEquals(instance.number_of_processes, self.zorpctl_argv["num_of_processes"]) self.assertEquals(instance.auto_restart, self.zorpctl_argv["auto_restart"]) self.assertEquals(instance.auto_start, self.zorpctl_argv["auto_start"]) except IOError as e: self.assertFalse("Something went wrong while initializing InstancesConf object: %s" % e.message)
def searchInstance(instance_name): try: for instance in InstancesConf(): if instance.name == instance_name: return instance return CommandResultFailure("instance %s not found!" % instance_name) except IOError as e: return CommandResultFailure(e.message)
def callAlgorithmToAllInstances(algorithm): result = [] try: for instance in InstancesConf(): result += InstanceHandler.executeAlgorithmOnInstanceProcesses(instance, algorithm) return result except BaseException as e: return CommandResultFailure(e.message)
def callAlgorithmToAllInstances(algorithm): result = [] try: manually_started_instances = ZorpHandler.findManullyStartedInstances( ) for manually_started_instance in manually_started_instances: result.extend( InstanceHandler.executeAlgorithmOnInstanceProcesses( manually_started_instance, algorithm)) for instance in InstancesConf(): result.extend( InstanceHandler.executeAlgorithmOnInstanceProcesses( instance, algorithm)) return result except Exception as e: return [CommandResultFailure(e.message)]
class RunningInstances(object): def __init__(self): self.instancesconf = InstancesConf() def __iter__(self): return self def next(self): instance = self.instancesconf.next() instance.process_num = 0 algorithm = ProcessAlgorithm() algorithm.setInstance(instance) if algorithm.isRunning(instance.process_name): return instance else: return self.next()
def findManullyStartedInstances(): import glob, os, re, subprocess paths = glob.glob("/var/run/zorp/zorp-*.pid") instance_names = [] for path in paths: path_splitted = re.split('[-\.]', os.path.basename(path)) instance_names.append(path_splitted[1]) for zorpctl_instances in InstancesConf(): regex = re.compile( zorpctl_instances.process_name.split('#')[0] + r'#[0-9]*') instance_names = filter(lambda i: not regex.search(i), instance_names) instances = [] for instance_name in instance_names: instances.append( Instance(name=instance_name, process_name=instance_name, number_of_processes=1, manually_started=True)) return instances
def searchInstance(instance_name): instances = [] try: manually_started_instances = ZorpHandler.findManullyStartedInstances( ) for manually_started_instance in manually_started_instances: if manually_started_instance.name == instance_name: instances.append(manually_started_instance) for instance in InstancesConf(): if instance.name == instance_name: instances.append(instance) if instances: return instances else: return [ CommandResultFailure( "Instance {0} not found!".format(instance_name), instance_name) ] except IOError as e: return [CommandResultFailure(e.message)]
def __init__(self): self.instancesconf = InstancesConf()