def deploy(self): """ Deploy instance to the active servers """ for attempt in range(self.attempts): 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') log = ('Running playbook "{path}/{name}" attempt {attempt} of ' '{attempts}:').format( path=playbook_path, name=self.ansible_playbook_name, attempts=self.attempts, attempt=attempt + 1) self.logger.info(log) log, returncode = self._run_playbook(requirements_path, playbook_path) if returncode != 0: self.logger.error( 'Playbook failed for instance {}'.format(self)) continue else: break if returncode == 0: self.logger.info('Playbook completed for instance {}'.format(self)) return (log, returncode)
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
def deploy(self): """ Deploy instance to the active servers """ for attempt in range(self.attempts): 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') log = ('Running playbook "{path}/{name}" attempt {attempt} of ' '{attempts}:').format(path=playbook_path, name=self.ansible_playbook_name, attempts=self.attempts, attempt=attempt + 1) self.logger.info(log) log, returncode = self._run_playbook(requirements_path, playbook_path) if returncode != 0: self.logger.error( 'Playbook failed for instance {}'.format(self)) continue else: break if returncode == 0: self.logger.info('Playbook completed for instance {}'.format(self)) return (log, returncode)
def _checkout_playbook(playbook): """ If playbook's `version` attribute is present, checks out the playbook from git and yields the working directory. Otherwise assumes the playbook is local and simply yields the value of the `source_repo` attribute. """ if playbook.version is None: yield playbook.source_repo else: with open_repository(playbook.source_repo, ref=playbook.version) as configuration_repo: yield configuration_repo.working_dir
def test_open_repository(self, mock_clone_from, mock_git_class): """ Get a repo object on a temporary directory """ tmp_dir_path = None with repo.open_repository('http://example.com/repo.git', ref='test-branch') as mock_repo: tmp_dir_path = mock_clone_from.mock_calls[0][1][1] mock_clone_from.assert_called_once_with('http://example.com/repo.git', tmp_dir_path) self.assertTrue(os.path.isdir(tmp_dir_path)) self.assertEqual(mock_repo.mock_calls, [call.checkout('test-branch')]) self.assertFalse(os.path.isdir(tmp_dir_path))
def test_open_repository(self, mock_clone_from, mock_git_class): """ Get a repo object on a temporary directory """ tmp_dir_path = None with repo.open_repository('http://example.com/repo.git', ref='test-branch') as mock_repo: tmp_dir_path = mock_clone_from.mock_calls[0][1][1] mock_clone_from.assert_called_once_with( 'http://example.com/repo.git', tmp_dir_path) self.assertTrue(os.path.isdir(tmp_dir_path)) self.assertEqual(mock_repo.mock_calls, [call.checkout('test-branch')]) self.assertFalse(os.path.isdir(tmp_dir_path))
def run_ansible_playbooks(self): """ Provision the server using ansible """ log = [] for playbook in self.get_playbooks(): with open_repository(playbook.source_repo, ref=playbook.version) as configuration_repo: self.logger.info('Running playbook "%s" from "%s"', playbook.playbook_path, playbook.source_repo) playbook_log, returncode = self._run_playbook(configuration_repo.working_dir, playbook) log += playbook_log if returncode != 0: self.logger.error('Playbook failed for AppServer %s', self) break else: self.logger.info('Playbooks completed for AppServer %s', self) return (log, returncode)
def run_ansible_playbooks(self): """ Provision the server using ansible """ log = [] for playbook in self.get_playbooks(): with open_repository(playbook.source_repo, ref=playbook.version) as configuration_repo: self.logger.info('Running playbook "%s" from "%s"', playbook.playbook_path, playbook.source_repo) playbook_log, returncode = self._run_playbook(configuration_repo.working_dir, playbook) log += playbook_log if returncode != 0: self.logger.error('Playbook failed for AppServer %s', self) break else: self.logger.info('Playbooks completed for AppServer %s', self) return (log, returncode)
def deploy(self): """ Deploy instance to the 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.logger.info( 'Running playbook "{path}/{name}":'.format(path=playbook_path, name=self.ansible_playbook_name) ) log, returncode = self._run_playbook(requirements_path, playbook_path) playbook_result = 'completed' if returncode == 0 else 'failed' self.logger.error('Playbook {result} for instance {instance}'.format(result=playbook_result, instance=self)) return (log, returncode)