Exemple #1
0
    def run_ansible_playbook_module(self, install_file_path):
        """
        Run the install.yml file through an Ansible playbook using the dedicated neuron !

        :param sudo_password: local machine sudo password required to install libraries
        :param install_file_path: the path of the Ansible playbook to run.
        :return:
        """
        logger.debug("[ResourcesManager] Run ansible playbook")
        Utils.print_info("Starting neuron installation")
        # ask the sudo password
        if self.sudo_password is not None:
            pswd = self.sudo_password
        else:
            pswd = getpass.getpass('Sudo password:')
        if not pswd or pswd == "":
            Utils.print_warning("You must enter a sudo password")
            return False
        else:
            ansible_neuron_parameters = {
                "task_file": install_file_path,
                "sudo": True,
                "sudo_user": "******",
                "sudo_password": pswd
            }
            neuron = Neuron(name="ansible_playbook",
                            parameters=ansible_neuron_parameters)
            NeuronLauncher.start_neuron(neuron)
            return True
 def _run_synapse(cls, synapse):
     """
     Start all neurons in the synapse
     :param synapse: Synapse for which we run neurons
     :return:
     """
     for neuron in synapse.neurons:
         NeuronLauncher.start_neuron(neuron)
     return True
Exemple #3
0
    def test_start_neuron(self):
        """
        Test the Neuron Launcher trying to start a Neuron
        """
        neuron = Neuron(name='neurone1', parameters={'var1': 'val1'})
        sl = SettingLoader()
        resources = Resources(neuron_folder='/var/tmp/test/resources')
        sl.settings.resources = resources
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_class_instantiation:
            NeuronLauncher.start_neuron(neuron=neuron)

            mock_get_class_instantiation.assert_called_once_with(
                package_name="neurons",
                module_name=neuron.name,
                parameters=neuron.parameters,
                resources_dir=sl.settings.resources.neuron_folder)
            mock_get_class_instantiation.reset_mock()
Exemple #4
0
    def _process_neuron_list(self, matched_synapse):
        """
        Process the neuron list of the matched_synapse
        Execute the Neuron
        Executing a Neuron creates a NeuronModule object. This one can have 3 status:
        - waiting for an answer: The neuron wait for an answer from the caller. The api response object is returned.
                                 The neuron is not removed from the matched synapse to be executed again
        - want to execute a synapse: The neuron add a list of synapse to execute to the lifo. 
                                     The LIFO restart over to process it.The neuron is removed from the matched synapse
        - complete: The neuron has been executed and its not waiting for an answer and doesn't want to start a synapse
                    The neuron is removed from the matched synapse
        :param matched_synapse: MatchedSynapse object to process
        """

        logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
        # while we have synapse to process in the list of synapse
        while matched_synapse.neuron_fifo_list:
            # get the first neuron in the FIFO neuron list
            neuron = matched_synapse.neuron_fifo_list[0]
            # from here, we are back into the last neuron we were processing.
            if self.answer is not None:  # we give the answer if exist to the first neuron
                neuron.parameters["answer"] = self.answer
                # the next neuron should not get this answer
                self.answer = None
            # todo fix this when we have a full client/server call. The client would be the voice or api call
            neuron.parameters["is_api_call"] = self.is_api_call
            logger.debug("[LIFOBuffer] process_neuron_list: is_api_call: %s" % (self.is_api_call))
            # execute the neuron
            instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
                                                              parameters_dict=matched_synapse.parameters)

            # the status of an execution is "complete" if no neuron are waiting for an answer
            self.api_response.status = "complete"
            if instantiated_neuron is not None:
                if instantiated_neuron.is_waiting_for_answer:  # the neuron is waiting for an answer
                    logger.debug("[LIFOBuffer] Wait for answer mode")
                    self.api_response.status = "waiting_for_answer"
                    self.is_running = False
                    raise Serialize
                else:
                    logger.debug("[LIFOBuffer] complete mode")
                    # we add the instantiated neuron to the neuron_module_list.
                    # This one contains info about generated text
                    matched_synapse.neuron_module_list.append(instantiated_neuron)
                    # the neuron is fully processed we can remove it from the list
                    matched_synapse.neuron_fifo_list.remove(neuron)

                if self.reset_lifo:  # the last executed neuron want to run a synapse
                    logger.debug("[LIFOBuffer] Last executed neuron want to run a synapse. Restart the LIFO")
                    # we have added a list of synapse to the LIFO ! this one must start over.
                    # break all while loop until the execution is back to the LIFO loop
                    self.reset_lifo = False
                    raise SynapseListAddedToLIFO
            else:
                # the neuron has not been processed but we still need to remove it from the list
                matched_synapse.neuron_fifo_list.remove(neuron)
    def run_ansible_playbook_module(install_file_path):
        """
        Run the install.yml file through an Ansible playbook using the dedicated neuron !

        :param install_file_path: the path of the Ansible playbook to run.
        :return:
        """
        logger.debug("[ResourcesManager] Run ansible playbook")
        Utils.print_info("Starting neuron installation")
        # ask the sudo password
        pswd = getpass.getpass('Sudo password:')
        ansible_neuron_parameters = {
            "task_file": install_file_path,
            "sudo": True,
            "sudo_user": "******",
            "sudo_password": pswd
        }
        neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters)
        NeuronLauncher.start_neuron(neuron)
Exemple #6
0
    def _start_neuron(neuron, params):
        """
        Associate params and Starts a neuron.

        :param neuron: the neuron to start
        :param params: the params to check and associate to the neuron args.
        """

        problem_in_neuron_found = False
        if isinstance(neuron.parameters, dict):
            # print neuron.parameters
            if "args" in neuron.parameters:
                logger.debug("The neuron waits for parameter")
                # check that the user added parameters to his order
                if params is None:
                    # we don't raise an error and break the program but we don't run the neuron
                    problem_in_neuron_found = True
                    Utils.print_danger(
                        "Error: The neuron %s is waiting for argument. "
                        "Argument found in bracket in the given order" %
                        neuron.name)
                else:
                    # we add wanted arguments the existing neuron parameter dict
                    for arg in neuron.parameters["args"]:
                        if arg in params:
                            logger.debug(
                                "Parameter %s added to the current parameter "
                                "of the neuron: %s" % (arg, neuron.name))
                            neuron.parameters[arg] = params[arg]
                        else:
                            # we don't raise an error and break the program but
                            # we don't run the neuron
                            problem_in_neuron_found = True
                            Utils.print_danger(
                                "Error: Argument \"%s\" not found in the"
                                " order" % arg)

        # if no error detected, we run the neuron
        if not problem_in_neuron_found:
            NeuronLauncher.start_neuron(neuron)
        else:
            Utils.print_danger("A problem has been found in the Synapse.")
Exemple #7
0
    def test_start_neuron(self):
        """
        Testing params association and starting a Neuron
        """

        with mock.patch("kalliope.core.NeuronLauncher.launch_neuron"
                        ) as mock_launch_neuron_method:
            # Assert to the neuron is launched with not parameter from order
            neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})

            NeuronLauncher.start_neuron(neuron=neuron1)
            mock_launch_neuron_method.assert_called_with(neuron1)
            mock_launch_neuron_method.reset_mock()

            # Assert the params are well passed to the neuron
            neuron2 = Neuron(name='neurone2',
                             parameters={
                                 'var2': 'val2',
                                 'var3': "{{ var3 }}"
                             })
            params = {'var3': 'value3'}
            NeuronLauncher.start_neuron(neuron=neuron2, parameters_dict=params)
            neuron2_params = Neuron(name='neurone2',
                                    parameters={
                                        'var2': 'val2',
                                        'var3': 'value3'
                                    })

            mock_launch_neuron_method.assert_called_with(neuron2_params)
            mock_launch_neuron_method.reset_mock()

            # Assert the Neuron is not started when missing args
            neuron3 = Neuron(name='neurone3',
                             parameters={
                                 'var3': 'val3',
                                 'var4': '{{val4}}'
                             })
            params = {'not_exist': 'test'}
            NeuronLauncher.start_neuron(neuron=neuron3, parameters_dict=params)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()

            # Assert no neuron is launched when waiting for args and none are given
            neuron4 = Neuron(name='neurone4',
                             parameters={
                                 'var5': 'val5',
                                 'var6': '{{val6}}'
                             })

            NeuronLauncher.start_neuron(neuron=neuron4)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()
    def test_start_neuron(self):
        """
        Testing params association and starting a Neuron
        """

        with mock.patch("kalliope.core.NeuronLauncher.launch_neuron") as mock_launch_neuron_method:
            # Assert to the neuron is launched with not parameter from order
            neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})

            NeuronLauncher.start_neuron(neuron=neuron1)
            mock_launch_neuron_method.assert_called_with(neuron1)
            mock_launch_neuron_method.reset_mock()

            # Assert the params are well passed to the neuron
            neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': "{{ var3 }}"})
            params = {
                'var3': 'value3'
            }
            NeuronLauncher.start_neuron(neuron=neuron2,
                                        parameters_dict=params)
            neuron2_params = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': 'value3'})

            mock_launch_neuron_method.assert_called_with(neuron2_params)
            mock_launch_neuron_method.reset_mock()

            # Assert the Neuron is not started when missing args
            neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3', 'var4': '{{val4}}'})
            params = {
                'not_exist': 'test'
            }
            NeuronLauncher.start_neuron(neuron=neuron3,
                                        parameters_dict=params)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()

            # Assert no neuron is launched when waiting for args and none are given
            neuron4 = Neuron(name='neurone4', parameters={'var5': 'val5', 'var6': '{{val6}}'})

            NeuronLauncher.start_neuron(neuron=neuron4)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()
Exemple #9
0
    def _process_neuron_list(self, matched_synapse):
        """
        Process the neuron list of the matched_synapse
        Execute the Neuron
        Executing a Neuron creates a NeuronModule object. This one can have 3 status:
        - waiting for an answer: The neuron wait for an answer from the caller. The api response object is returned.
                                 The neuron is not removed from the matched synapse to be executed again
        - want to execute a synapse: The neuron add a list of synapse to execute to the lifo. 
                                     The LIFO restart over to process it.The neuron is removed from the matched synapse
        - complete: The neuron has been executed and its not waiting for an answer and doesn't want to start a synapse
                    The neuron is removed from the matched synapse
        :param matched_synapse: MatchedSynapse object to process
        """

        logger.debug("[LIFOBuffer] number of neuron to process: %s" %
                     len(matched_synapse.neuron_fifo_list))
        # while we have synapse to process in the list of synapse
        while matched_synapse.neuron_fifo_list:
            # get the first neuron in the FIFO neuron list
            neuron = matched_synapse.neuron_fifo_list[0]
            # from here, we are back into the last neuron we were processing.
            if self.answer is not None:  # we give the answer if exist to the first neuron
                neuron.parameters["answer"] = self.answer
                # the next neuron should not get this answer
                self.answer = None
            # todo fix this when we have a full client/server call. The client would be the voice or api call
            neuron.parameters["is_api_call"] = self.is_api_call
            logger.debug("[LIFOBuffer] process_neuron_list: is_api_call: %s" %
                         self.is_api_call)
            # execute the neuron
            instantiated_neuron = NeuronLauncher.start_neuron(
                neuron=neuron, parameters_dict=matched_synapse.parameters)

            # the status of an execution is "complete" if no neuron are waiting for an answer
            self.api_response.status = "complete"
            if instantiated_neuron is not None:
                if instantiated_neuron.is_waiting_for_answer:  # the neuron is waiting for an answer
                    logger.debug("[LIFOBuffer] Wait for answer mode")
                    self.api_response.status = "waiting_for_answer"
                    self.is_running = False
                    raise Serialize
                else:
                    logger.debug("[LIFOBuffer] complete mode")
                    # we add the instantiated neuron to the neuron_module_list.
                    # This one contains info about generated text
                    matched_synapse.neuron_module_list.append(
                        instantiated_neuron)
                    # the neuron is fully processed we can remove it from the list
                    matched_synapse.neuron_fifo_list.remove(neuron)

                if self.reset_lifo:  # the last executed neuron want to run a synapse
                    logger.debug(
                        "[LIFOBuffer] Last executed neuron want to run a synapse. Restart the LIFO"
                    )
                    # we have added a list of synapse to the LIFO ! this one must start over.
                    # break all while loop until the execution is back to the LIFO loop
                    self.reset_lifo = False
                    raise SynapseListAddedToLIFO
            else:
                # the neuron has not been processed but we still need to remove it from the list
                matched_synapse.neuron_fifo_list.remove(neuron)