3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('Application.Common.Class.ConfigFileModule');
26 * Manage Bacula configuration.
28 * @author Marcin Haba <marcin.haba@bacula.pl>
30 class BaculaConfig extends ConfigFileModule {
33 * Bacula config file format
35 const CONFIG_FILE_FORMAT = 'bacula';
38 * Get (read) Bacula config.
41 * @param string $component_type Bacula component type
42 * @param array $params requested config parameters
43 * @return array config
45 public function getConfig($component_type, $params = array()) {
47 $result = $this->getModule('json_tools')->execCommand($component_type, $params);
48 if ($result['exitcode'] === 0 && is_array($result['output'])) {
49 $config = $result['output'];
55 * Set (save) Bacula config.
58 * @param string $component_type Bacula component type
59 * @param array $config config
60 * @return array validation result, validation output and write to config result
62 public function setConfig($component_type, array $config) {
63 $result = array('is_valid' => false, 'save_result' => false, 'output' => null);
64 $config_content = $this->prepareConfig($config, self::CONFIG_FILE_FORMAT);
65 $validation = $this->validateConfig($component_type, $config_content);
66 $result['is_valid'] = $validation['is_valid'];
67 $result['result'] = $validation['result'];
68 if ($result['is_valid'] === true) {
69 $tool_config = $this->getModule('api_config')->getJSONToolConfig($component_type);
71 * @TODO: Add option to save config in a specific directory. Users may want
72 * to see config and put it manually to production.
74 $result['save_result'] = $this->writeConfig($config, $tool_config['cfg'], self::CONFIG_FILE_FORMAT);
80 * Validate Bacula config.
81 * Config validation should be used as early as config data is available.
82 * Validation is done in write config method. In read config method it isn't
83 * required because both reading config and validating config are done by
84 * the same tool (Bacula JSON program)
87 * @param string $component_type Bacula component type
88 * @param string $config config
89 * @return array validation output and exitcode
91 private function validateConfig($component_type, $config) {
92 $ret = array('is_valid' => false, 'result' => null);
93 $params = array('test_config' => true);
94 $result = $this->getModule('json_tools')->execCommand($component_type, $params, $config);
95 if ($result['exitcode'] === 0) {
96 $ret['is_valid'] = true;
98 $ret['result'] = $result;
100 if ($ret['is_valid'] === false) {
101 $error = is_array($result['output']) ? implode('', $result['output']) : $result['output'];
102 $emsg = "ERROR [$component_type] $error";
103 $this->getModule('logging')->log(
106 Logging::CATEGORY_APPLICATION,