Add support for a "global" configuration file

Prior to this patch, virutalbmc could only be configured in a per-user
basis. The configuration file as well the path where the VBMCs are going
to be created always lived in the users home directory.

This patch is adding support for a "global" configuration file which
will live in /etc/virtualbmc/virtualbmc.conf. When vbmc does not find a
configuration file in the user directory it will then fallback to the
global (if it exist, otherwise it will just uses the defaults values).

Also, a new configuration option called "config_dir" has been added.
This allows users to configure whatever directory they want to save the
created VirtualBMCs where before it always pointed to ~/.vbmc/config.

Closes-Bug: #1647341
Change-Id: Id8bca620d1543436f32ecc650fc28cbc3f13f373
This commit is contained in:
Lucas Alvares Gomes
2016-12-05 11:11:40 +00:00
parent 89345c62c5
commit ba679b9109
5 changed files with 32 additions and 16 deletions

View File

@@ -18,16 +18,24 @@ from virtualbmc import utils
__all__ = ['get_config']
CONFIG_FILE = os.path.join(utils.CONFIG_PATH, 'virtualbmc.conf')
_CONFIG_FILE_PATHS = (
os.path.join(os.path.expanduser('~'), '.vbmc', 'virtualbmc.conf'),
'/etc/virtualbmc/virtualbmc.conf')
CONFIG = None
CONFIG_FILE = ''
for config in _CONFIG_FILE_PATHS:
if os.path.exists(config):
CONFIG_FILE = config
break
class VirtualBMCConfig(object):
DEFAULTS = {
'default': {
'show_passwords': 'false'
'show_passwords': 'false',
'config_dir': os.path.join(os.path.expanduser('~'), '.vbmc'),
},
'log': {
'logfile': None,

View File

@@ -36,8 +36,12 @@ CONF = vbmc_config.get_config()
class VirtualBMCManager(object):
def __init__(self):
super(VirtualBMCManager, self).__init__()
self.config_dir = CONF['default']['config_dir']
def _parse_config(self, domain_name):
config_path = os.path.join(utils.CONFIG_PATH, domain_name, 'config')
config_path = os.path.join(self.config_dir, domain_name, 'config')
if not os.path.exists(config_path):
raise exception.DomainNotFound(domain=domain_name)
@@ -63,7 +67,7 @@ class VirtualBMCManager(object):
def _show(self, domain_name):
running = False
try:
pidfile_path = os.path.join(utils.CONFIG_PATH, domain_name, 'pid')
pidfile_path = os.path.join(self.config_dir, domain_name, 'pid')
with open(pidfile_path, 'r') as f:
pid = int(f.read())
@@ -89,7 +93,7 @@ class VirtualBMCManager(object):
sasl_username=libvirt_sasl_username,
sasl_password=libvirt_sasl_password)
domain_path = os.path.join(utils.CONFIG_PATH, domain_name)
domain_path = os.path.join(self.config_dir, domain_name)
try:
os.makedirs(domain_path)
except OSError as e:
@@ -119,7 +123,7 @@ class VirtualBMCManager(object):
config.write(f)
def delete(self, domain_name):
domain_path = os.path.join(utils.CONFIG_PATH, domain_name)
domain_path = os.path.join(self.config_dir, domain_name)
if not os.path.exists(domain_path):
raise exception.DomainNotFound(domain=domain_name)
@@ -131,7 +135,7 @@ class VirtualBMCManager(object):
shutil.rmtree(domain_path)
def start(self, domain_name):
domain_path = os.path.join(utils.CONFIG_PATH, domain_name)
domain_path = os.path.join(self.config_dir, domain_name)
if not os.path.exists(domain_path):
raise exception.DomainNotFound(domain=domain_name)
@@ -174,7 +178,7 @@ class VirtualBMCManager(object):
def stop(self, domain_name):
LOG.debug('Stopping Virtual BMC for domain %s', domain_name)
domain_path = os.path.join(utils.CONFIG_PATH, domain_name)
domain_path = os.path.join(self.config_dir, domain_name)
if not os.path.exists(domain_path):
raise exception.DomainNotFound(domain=domain_name)
@@ -198,8 +202,8 @@ class VirtualBMCManager(object):
def list(self):
bmcs = []
try:
for domain in os.listdir(utils.CONFIG_PATH):
if os.path.isdir(os.path.join(utils.CONFIG_PATH, domain)):
for domain in os.listdir(self.config_dir):
if os.path.isdir(os.path.join(self.config_dir, domain)):
bmcs.append(self._show(domain))
except OSError as e:
if e.errno == errno.EEXIST:

View File

@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import mock
from six.moves import configparser
@@ -28,7 +30,8 @@ class VirtualBMCConfigTestCase(base.TestCase):
def setUp(self):
super(VirtualBMCConfigTestCase, self).setUp()
self.vbmc_config = config.VirtualBMCConfig()
self.config_dict = {'default': {'show_passwords': 'true'},
self.config_dict = {'default': {'show_passwords': 'true',
'config_dir': '/foo'},
'log': {'debug': 'true', 'logfile': '/foo/bar'},
'ipmi': {'session_timeout': '30'}}
@@ -44,10 +47,13 @@ class VirtualBMCConfigTestCase(base.TestCase):
mock__as_dict.assert_called_once_with(config)
mock__validate.assert_called_once_with()
def test__as_dict(self):
@mock.patch.object(os.path, 'exists')
def test__as_dict(self, mock_exists):
mock_exists.side_effect = (False, True)
config = mock.Mock()
config.sections.side_effect = ['default', 'log', 'ipmi'],
config.items.side_effect = [[('show_passwords', 'true')],
config.items.side_effect = [[('show_passwords', 'true'),
('config_dir', mock.ANY)],
[('logfile', '/foo/bar'),
('debug', 'true')],
[('session_timeout', '30')]]

View File

@@ -31,12 +31,12 @@ from virtualbmc import utils
_CONFIG_PATH = '/foo'
@mock.patch('virtualbmc.utils.CONFIG_PATH', _CONFIG_PATH)
class VirtualBMCManagerTestCase(base.TestCase):
def setUp(self):
super(VirtualBMCManagerTestCase, self).setUp()
self.manager = manager.VirtualBMCManager()
self.manager.config_dir = _CONFIG_PATH
self.domain0 = test_utils.get_domain()
self.domain1 = test_utils.get_domain(domain_name='Patrick', port=321)
self.domain_name0 = self.domain0['domain_name']

View File

@@ -15,8 +15,6 @@ import os
from virtualbmc import exception
CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.vbmc')
class libvirt_open(object):