Example #1
0
    def run_playbook(self, playbook_name=None):
        """
        Run a playbook against the instance active servers
        """
        with open_repository(self.ansible_source_repo_url,
                             ref=self.configuration_version) as configuration_repo:
            playbook_path = os.path.join(configuration_repo.working_dir, 'playbooks')
            requirements_path = os.path.join(configuration_repo.working_dir, 'requirements.txt')

            self.log('info', 'Running playbook "{path}/{name}" for instance {instance}...'.format(
                path=playbook_path,
                name=playbook_name,
                instance=self,
            ))

            log_lines = []
            with ansible.run_playbook(
                requirements_path,
                self.inventory_str,
                self.vars_str,
                playbook_path,
                self.ansible_playbook_filename,
                username=settings.OPENSTACK_SANDBOX_SSH_USERNAME,
            ) as processus:
                for line in processus.stdout:
                    line = line.decode('utf-8').rstrip()
                    self.log('info', line)
                    log_lines.append([line.rstrip()])

        self.log('info', 'Playbook run completed for instance {}'.format(self))
        return log_lines
Example #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
Example #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
Example #4
0
    def test_run_playbook(self):
        """
        Run the ansible-playbook command
        """

        popen_result = object()

        with patch('instance.ansible.render_sandbox_creation_command', return_value="ANSIBLE CMD") as mock_render, \
                patch('instance.ansible.create_temp_dir') as mock_create_temp, \
                patch('instance.ansible.string_to_file_path', return_value='/tmp/string/file'), \
                patch('subprocess.Popen', return_value=popen_result) as mock_popen:

            mock_create_temp.return_value.__enter__.return_value = '/tmp/tempdir'

            with ansible.run_playbook(
                    requirements_path="/tmp/requirements.txt",
                    inventory_str="INVENTORY: 'str'",
                    vars_str="VARS: 'str2'",
                    playbook_path='/play/book',
                    playbook_name='playbook_name') as run_playbook_result:

                # This checks if run_playbook returns a Process object (or more precisely: object returned from Popen)
                self.assertEqual(run_playbook_result, popen_result)

            mock_render.assert_called_once_with(
                inventory_path='/tmp/string/file',
                vars_path='/tmp/string/file',
                requirements_path='/tmp/requirements.txt',
                playbook_name='playbook_name',
                remote_username='******',
                venv_path='/tmp/tempdir/venv')

            mock_popen.assert_called_once_with("ANSIBLE CMD",
                                               bufsize=1,
                                               stdout=-1,
                                               stderr=-1,
                                               cwd='/play/book',
                                               shell=True,
                                               env=mock.ANY)
            call_kwargs = mock_popen.mock_calls[0][2]
            self.assertIn('env', call_kwargs)
            self.assertEqual(call_kwargs['env']['TMPDIR'], '/tmp/tempdir')
Example #5
0
    def test_run_playbook(self):
        """
        Run the ansible-playbook command
        """

        popen_result = object()

        with patch('instance.ansible.render_sandbox_creation_command', return_value="ANSIBLE CMD") as mock_render, \
                patch('instance.ansible.create_temp_dir') as mock_create_temp, \
                patch('instance.ansible.string_to_file_path', return_value='/tmp/string/file'), \
                patch('subprocess.Popen', return_value=popen_result) as mock_popen:

            mock_create_temp.return_value.__enter__.return_value = '/tmp/tempdir'

            with ansible.run_playbook(
                requirements_path="/tmp/requirements.txt",
                inventory_str="INVENTORY: 'str'",
                vars_str="VARS: 'str2'",
                playbook_path='/play/book',
                playbook_name='playbook_name'
            ) as run_playbook_result:

                # This checks if run_playbook returns a Process object (or more precisely: object returned from Popen)
                self.assertEqual(run_playbook_result, popen_result)

            mock_render.assert_called_once_with(
                inventory_path='/tmp/string/file',
                vars_path='/tmp/string/file',
                requirements_path='/tmp/requirements.txt',
                playbook_name='playbook_name',
                remote_username='******',
                venv_path='/tmp/tempdir/venv'
            )

            mock_popen.assert_called_once_with(
                "ANSIBLE CMD", bufsize=1, stdout=-1, stderr=-1, cwd='/play/book', shell=True, env=mock.ANY
            )
            call_kwargs = mock_popen.mock_calls[0][2]
            self.assertIn('env', call_kwargs)
            self.assertEqual(call_kwargs['env']['TMPDIR'], '/tmp/tempdir')
Example #6
0
    def test_run_playbook(self, mock_string_to_file_path, mock_mkdtemp, mock_popen):
        """
        Run the ansible-playbook command
        """
        mock_string_to_file_path.return_value.__enter__.return_value = '/test/str2path'
        mock_mkdtemp.return_value = '/test/mkdtemp'

        with ansible.run_playbook('/requirements/path.txt',
                                  "INVENTORY: 'str'",
                                  "VARS: 'str2'",
                                  '/play/book',
                                  'playbook_name_str'):
            run_playbook_cmd = (
                'virtualenv -p /usr/bin/python /test/mkdtemp && '
                '/test/mkdtemp/bin/python -u /test/mkdtemp/bin/pip install -r /requirements/path.txt && '
                '/test/mkdtemp/bin/python -u /test/mkdtemp/bin/ansible-playbook -i /test/str2path '
                '-e @/test/str2path -u root playbook_name_str'
            )
            self.assertEqual(
                mock_popen.mock_calls,
                [call(run_playbook_cmd, bufsize=1, stdout=-1, stderr=-1, cwd='/play/book', shell=True)]
            )