From 3d8d354822c83612bd22f032208bfc1205ea1ad4 Mon Sep 17 00:00:00 2001 From: Frank Bergkemper Date: Tue, 5 Aug 2014 17:19:27 +0200 Subject: [PATCH] Issue #12 bconsole call bconsole call now configurable in config/autoload/local.php --- config/autoload/local.php.dist | 8 +++ .../Client/Controller/ClientController.php | 43 ++------------- .../Controller/DirectorController.php | 55 ++++++------------- .../Fileset/Controller/FilesetController.php | 46 +++------------- 4 files changed, 38 insertions(+), 114 deletions(-) diff --git a/config/autoload/local.php.dist b/config/autoload/local.php.dist index e7743204..691f87a2 100644 --- a/config/autoload/local.php.dist +++ b/config/autoload/local.php.dist @@ -52,5 +52,13 @@ return array( 'password' => '', ), + 'bconsole' => array( + // Set full path to bconsole command + 'exec_path' => '/usr/sbin/bconsole', + // Use sudo to execute bconsole command: true or false + 'sudo' => 'true', + // Set full path to bconsole configuration file + 'config_path' => '/etc/bareos/bconsole.conf', + ), ); diff --git a/module/Client/src/Client/Controller/ClientController.php b/module/Client/src/Client/Controller/ClientController.php index c0784052..8af789e4 100644 --- a/module/Client/src/Client/Controller/ClientController.php +++ b/module/Client/src/Client/Controller/ClientController.php @@ -4,13 +4,13 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; +use Bareos\BConsole\BConsoleConnector; class ClientController extends AbstractActionController { protected $clientTable; protected $jobTable; - protected $bconsoleOutput = array(); public function indexAction() { @@ -30,13 +30,15 @@ public function detailsAction() } $result = $this->getClientTable()->getClient($id); - $cmd = "status client=" . $result->name; - + $cmd = 'status client="' . $result->name . '"'; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); + return new ViewModel( array( 'client' => $this->getClientTable()->getClient($id), 'job' => $this->getJobTable()->getLastSuccessfulClientJob($id), - 'bconsoleOutput' => $this->getBConsoleOutput($cmd), + 'bconsoleOutput' => $bcon->getBConsoleOutput($cmd), ) ); @@ -59,39 +61,6 @@ public function getJobTable() } return $this->jobTable; } - - public function getBConsoleOutput($cmd) - { - - $descriptorspec = array( - 0 => array("pipe", "r"), - 1 => array("pipe", "w"), - 2 => array("pipe", "r") - ); - - $cwd = '/usr/sbin'; - $env = array('/usr/sbin'); - - $process = proc_open('sudo /usr/sbin/bconsole', $descriptorspec, $pipes, $cwd, $env); - - if(!is_resource($process)) - throw new \Exception("proc_open error"); - - if(is_resource($process)) - { - fwrite($pipes[0], $cmd); - fclose($pipes[0]); - while(!feof($pipes[1])) { - array_push($this->bconsoleOutput, fread($pipes[1],8192)); - } - fclose($pipes[1]); - } - - $return_value = proc_close($process); - - return $this->bconsoleOutput; - - } } diff --git a/module/Director/src/Director/Controller/DirectorController.php b/module/Director/src/Director/Controller/DirectorController.php index 5025c44a..f993f189 100644 --- a/module/Director/src/Director/Controller/DirectorController.php +++ b/module/Director/src/Director/Controller/DirectorController.php @@ -28,83 +28,62 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; +use Bareos\BConsole\BConsoleConnector; class DirectorController extends AbstractActionController { + protected $directorOutput = array(); public function indexAction() { $cmd = "status director"; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); return new ViewModel(array( - 'directorOutput' => $this->getBConsoleOutput($cmd), + 'directorOutput' => $bcon->getBConsoleOutput($cmd), )); } public function messagesAction() { $cmd = "messages"; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); return new ViewModel(array( - 'directorOutput' => $this->getBConsoleOutput($cmd), + 'directorOutput' => $bcon->getBConsoleOutput($cmd), )); } public function scheduleAction() { $cmd = "show schedule"; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); return new ViewModel(array( - 'directorOutput' => $this->getBConsoleOutput($cmd), + 'directorOutput' => $bcon->getBConsoleOutput($cmd), )); } public function schedulerstatusAction() { $cmd = "status scheduler"; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); return new ViewModel(array( - 'directorOutput' => $this->getBConsoleOutput($cmd), + 'directorOutput' => $bcon->getBConsoleOutput($cmd), )); } public function versionAction() { $cmd = "version"; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); return new ViewModel(array( - 'directorOutput' => $this->getBConsoleOutput($cmd), + 'directorOutput' => $bcon->getBConsoleOutput($cmd), )); } - public function getBConsoleOutput($cmd) - { - - $descriptorspec = array( - 0 => array("pipe", "r"), - 1 => array("pipe", "w"), - 2 => array("pipe", "r") - ); - - $cwd = '/usr/sbin'; - $env = array('/usr/sbin'); - - $process = proc_open('sudo /usr/sbin/bconsole', $descriptorspec, $pipes, $cwd, $env); - - if(!is_resource($process)) - throw new \Exception("proc_open error"); - - if(is_resource($process)) - { - fwrite($pipes[0], $cmd); - fclose($pipes[0]); - while(!feof($pipes[1])) { - array_push($this->directorOutput, fread($pipes[1],8192)); - } - fclose($pipes[1]); - } - - $return_value = proc_close($process); - - return $this->directorOutput; - - } - } diff --git a/module/Fileset/src/Fileset/Controller/FilesetController.php b/module/Fileset/src/Fileset/Controller/FilesetController.php index e41ec469..673f95d4 100644 --- a/module/Fileset/src/Fileset/Controller/FilesetController.php +++ b/module/Fileset/src/Fileset/Controller/FilesetController.php @@ -27,12 +27,12 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; +use Bareos\BConsole\BConsoleConnector; class FilesetController extends AbstractActionController { protected $filesetTable; - protected $bconsoleOutput = array(); public function indexAction() { @@ -46,7 +46,11 @@ public function indexAction() public function detailsAction() { $id = (int) $this->params()->fromRoute('id', 0); - + $fset = $this->getFilesetTable()->getFileSet($id); + $cmd = 'show fileset="' . $fset->fileset . '"'; + $config = $this->getServiceLocator()->get('Config'); + $bcon = new BConsoleConnector($config['bconsole']); + if (!$id) { return $this->redirect()->toRoute('fileset'); } @@ -55,7 +59,7 @@ public function detailsAction() array( 'fileset' => $this->getFilesetTable()->getFileset($id), 'history' => $this->getFilesetTable()->getFilesetHistory($id), - 'configuration' => $this->getFilesetConfig($id), + 'configuration' => $bcon->getBConsoleOutput($cmd), ) ); } @@ -69,41 +73,5 @@ public function getFilesetTable() return $this->filesetTable; } - public function getFilesetConfig($id) - { - - $fset = $this->getFilesetTable()->getFileSet($id); - $cmd = "show fileset=" . $fset->fileset; - - $descriptorspec = array( - 0 => array("pipe", "r"), - 1 => array("pipe", "w"), - 2 => array("pipe", "r") - ); - - $cwd = '/usr/sbin'; - $env = array('/usr/sbin'); - - $process = proc_open('sudo /usr/sbin/bconsole', $descriptorspec, $pipes, $cwd, $env); - - if(!is_resource($process)) { - throw new \Exception("proc_open error"); - } - - if(is_resource($process)) { - fwrite($pipes[0], $cmd); - fclose($pipes[0]); - while(!feof($pipes[1])) { - array_push($this->bconsoleOutput, fread($pipes[1], 8192)); - } - fclose($pipes[1]); - } - - $return_value = proc_close($process); - - return $this->bconsoleOutput; - - } - }