Пример #1
0
    def test_poll_streams(self):
        """
        Ensure that the lines read are in the order they were written in each stream.
        """
        process = subprocess.Popen([
            "echo line1; echo line1 >&2; echo line2; echo line2 >&2; echo line3"
        ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        lines = poll_streams(process.stdout, process.stderr)

        expected = [
            (process.stdout, b"line1\n"),
            (process.stderr, b"line1\n"),
            (process.stdout, b"line2\n"),
            (process.stderr, b"line2\n"),
            (process.stdout, b"line3\n"),
        ]

        def key(entry):
            """
            Helper function used together with sorting routines in order to
            identify which attribute to sort by.
            """
            return entry[0].fileno()

        self.assertEqual(sorted(lines, key=key), sorted(expected, key=key))
Пример #2
0
 def _run_playbook(self, requirements_path, playbook_path):
     """
     Run a playbook against the instance active servers
     """
     log_lines = []
     with ansible.run_playbook(
         requirements_path,
         self.inventory_str,
         self.ansible_settings,
         playbook_path,
         self.ansible_playbook_filename,
         username=settings.OPENSTACK_SANDBOX_SSH_USERNAME,
     ) as process:
         try:
             log_line_generator = poll_streams(
                 process.stdout,
                 process.stderr,
                 line_timeout=settings.ANSIBLE_LINE_TIMEOUT,
                 global_timeout=settings.ANSIBLE_GLOBAL_TIMEOUT,
             )
             for f, line in log_line_generator:
                 line = line.decode('utf-8').rstrip()
                 if f == process.stdout:
                     self.logger.info(line)
                 elif f == process.stderr:
                     self.logger.error(line)
                 log_lines.append(line)
         except TimeoutError:
             self.logger.error('Playbook run timed out.  Terminating the Ansible process.')
             process.terminate()
         process.wait()
         return log_lines, process.returncode
Пример #3
0
 def _run_playbook(self, requirements_path, playbook_path):
     """
     Run a playbook against the instance active servers
     """
     log_lines = []
     with ansible.run_playbook(
             requirements_path,
             self.inventory_str,
             self.ansible_settings,
             playbook_path,
             self.ansible_playbook_filename,
             username=settings.OPENSTACK_SANDBOX_SSH_USERNAME,
     ) as process:
         try:
             log_line_generator = poll_streams(
                 process.stdout,
                 process.stderr,
                 line_timeout=settings.ANSIBLE_LINE_TIMEOUT,
                 global_timeout=settings.ANSIBLE_GLOBAL_TIMEOUT,
             )
             for f, line in log_line_generator:
                 line = line.decode('utf-8').rstrip()
                 if f == process.stdout:
                     self.logger.info(line)
                 elif f == process.stderr:
                     self.logger.error(line)
                 log_lines.append(line)
         except TimeoutError:
             self.logger.error(
                 'Playbook run timed out.  Terminating the Ansible process.'
             )
             process.terminate()
         process.wait()
         return log_lines, process.returncode
Пример #4
0
def capture_playbook_output(requirements_path,
                            inventory_str,
                            vars_str,
                            playbook_path,
                            username='******',
                            logger_=None,
                            collect_logs=False):
    """
    Convenience wrapper for run_playbook() that captures the output of the playbook run.
    """
    with run_playbook(
            requirements_path=requirements_path,
            inventory_str=inventory_str,
            vars_str=vars_str,
            playbook_path=os.path.dirname(playbook_path),
            playbook_name=os.path.basename(playbook_path),
            username=username,
    ) as process:
        try:
            log_line_generator = poll_streams(
                process.stdout,
                process.stderr,
                line_timeout=settings.ANSIBLE_LINE_TIMEOUT,
                global_timeout=settings.ANSIBLE_GLOBAL_TIMEOUT,
            )
            log_lines = []
            for f, line in log_line_generator:
                line = line.decode('utf-8').rstrip()
                if logger_ is not None:
                    if f == process.stdout:
                        logger_.info(line)
                    elif f == process.stderr:
                        logger_.error(line)
                if collect_logs:
                    log_lines.append(line)
        except TimeoutError:
            if logger_ is not None:
                logger_.error(
                    'Playbook run timed out.  Terminating the Ansible process.'
                )
            process.terminate()
        process.wait()
        if collect_logs:
            return log_lines, process.returncode
        else:
            return process.returncode
Пример #5
0
def capture_playbook_output(
        requirements_path, inventory_str, vars_str, playbook_path, username='******', logger_=None, collect_logs=False
):
    """
    Convenience wrapper for run_playbook() that captures the output of the playbook run.
    """
    with run_playbook(
        requirements_path=requirements_path,
        inventory_str=inventory_str,
        vars_str=vars_str,
        playbook_path=os.path.dirname(playbook_path),
        playbook_name=os.path.basename(playbook_path),
        username=username,
    ) as process:
        try:
            log_line_generator = poll_streams(
                process.stdout,
                process.stderr,
                line_timeout=settings.ANSIBLE_LINE_TIMEOUT,
                global_timeout=settings.ANSIBLE_GLOBAL_TIMEOUT,
            )
            log_lines = []
            for f, line in log_line_generator:
                line = line.decode('utf-8').rstrip()
                if logger_ is not None:
                    if f == process.stdout:
                        logger_.info(line)
                    elif f == process.stderr:
                        logger_.error(line)
                if collect_logs:
                    log_lines.append(line)
        except TimeoutError:
            if logger_ is not None:
                logger_.error('Playbook run timed out.  Terminating the Ansible process.')
            process.terminate()
        process.wait()
        if collect_logs:
            return log_lines, process.returncode
        else:
            return process.returncode