Exemplo n.º 1
0
    def __is_queue_head_running(self, params):
        """
        Private method that is used for checking whether the queue head is running. Queue head has
        a different configuration of machine type and should be used for celery configuration.

        Args
            params   A dictionary of parameters

        Return
            A boolean value of wether the queue head is running or not
        """
        logging.info("Trying to check if queue head is running...")

        try:
            all_vms = self.agent.describe_instances(params, prefix=params["key_prefix"])
            if all_vms == None:
                logging.info("No vms were found!")
                return False

            queue_head_tag = AgentConfig.get_queue_head_key_tag(agent_type=self.agent_type)
            key_prefix = AgentConfig.get_agent_key_prefix(
                agent_type=self.agent_type, key_prefix=params.get("key_prefix", "")
            )

            # Just need one running vm with the QUEUEHEAD_KEY_TAG in the name of the keypair
            for vm in all_vms:
                if vm != None and vm["state"] == "running":
                    if vm["key_name"].endswith(queue_head_tag) and vm["key_name"].startswith(key_prefix):
                        logging.info("Found queue head: {0}".format(vm))
                        return True
            return False

        except Exception as e:
            logging.error("Error in testing whether queue_head is running! {0}".format(e))
            return False
    def __prepare_queue_head(self, parameters):
        logging.debug(
            "__prepare_queue_head(): parameters = {0}".format(parameters))

        num_vms = 0

        if not self.__is_queue_head_running(parameters):
            logging.info(
                "Queue head is not running, so create a new queue head...")
            if 'head_node' not in parameters:
                logging.error("Head node is needed to run StochSS!")
                return None, None

            num_vms = self.__launch_ec2_queue_head(parameters)

        else:
            logging.info("Found queue head running...")

            # Queue head is already running, downgrading to normal worker
            if "queue_head" in parameters and parameters["queue_head"] == True:
                parameters["keyname"] = parameters["keyname"].replace(
                    AgentConfig.get_queue_head_key_tag(
                        agent_type=self.agent_type), '')
                logging.info(
                    'After downgrading from queue head to normal worker: keyname = {0}'
                    .format(parameters["keyname"]))

            parameters["queue_head"] = False
            self.agent.configure_instance_security(parameters)

            # set shutdown behavior to "terminate"
            parameters["shutdown"] = "terminate"
            for vm in parameters["vms"]:
                parameters["instance_type"] = vm["instance_type"]
                parameters["num_vms"] = vm["num_vms"]
                num_vms += vm["num_vms"]
                try:
                    self.agent.prepare_instances(parameters)
                except Exception as e:
                    raise Exception(
                        'Errors in running instances in agent: {0}'.format(
                            str(e)))

        return num_vms, parameters
    def __is_queue_head_running(self, params):
        '''
        Private method that is used for checking whether the queue head is running. Queue head has
        a different configuration of machine type and should be used for celery configuration.

        Args
            params   A dictionary of parameters

        Return
            A boolean value of wether the queue head is running or not
        '''
        logging.info('Trying to check if queue head is running...')

        try:
            all_vms = self.agent.describe_instances(
                params, prefix=params['key_prefix'])
            if all_vms == None:
                logging.info('No vms were found!')
                return False

            queue_head_tag = AgentConfig.get_queue_head_key_tag(
                agent_type=self.agent_type)
            key_prefix = AgentConfig.get_agent_key_prefix(
                agent_type=self.agent_type,
                key_prefix=params.get('key_prefix', ''))

            # Just need one running vm with the QUEUEHEAD_KEY_TAG in the name of the keypair
            for vm in all_vms:
                if vm != None and vm['state'] == 'running':
                    if vm['key_name'].endswith(queue_head_tag) and vm[
                            'key_name'].startswith(key_prefix):
                        logging.info('Found queue head: {0}'.format(vm))
                        return True
            return False

        except Exception as e:
            logging.error(
                'Error in testing whether queue_head is running! {0}'.format(
                    e))
            return False
Exemplo n.º 4
0
    def __prepare_queue_head(self, parameters):
        logging.debug("__prepare_queue_head(): parameters = {0}".format(parameters))

        num_vms = 0

        if not self.__is_queue_head_running(parameters):
            logging.info("Queue head is not running, so create a new queue head...")
            if "head_node" not in parameters:
                logging.error("Head node is needed to run StochSS!")
                return None, None

            num_vms = self.__launch_ec2_queue_head(parameters)

        else:
            logging.info("Found queue head running...")

            # Queue head is already running, downgrading to normal worker
            if "queue_head" in parameters and parameters["queue_head"] == True:
                parameters["keyname"] = parameters["keyname"].replace(
                    AgentConfig.get_queue_head_key_tag(agent_type=self.agent_type), ""
                )
                logging.info(
                    "After downgrading from queue head to normal worker: keyname = {0}".format(parameters["keyname"])
                )

            parameters["queue_head"] = False
            self.agent.configure_instance_security(parameters)

            # set shutdown behavior to "terminate"
            parameters["shutdown"] = "terminate"
            for vm in parameters["vms"]:
                parameters["instance_type"] = vm["instance_type"]
                parameters["num_vms"] = vm["num_vms"]
                num_vms += vm["num_vms"]
                try:
                    self.agent.prepare_instances(parameters)
                except Exception as e:
                    raise Exception("Errors in running instances in agent: {0}".format(str(e)))

        return num_vms, parameters