Adds config-file arg option

Currently, the configurations are being loaded from a
statically defined file (/etc/monasca/agent/agent.yaml).

Adds CLI arg option for the configuration file (--config-file).
Also added this argument to the forwarder's tornado options, in
order to avoid exception due to unrecognised argument.

Co-Authored-By: Craig Bryant <craig.bryant@hpe.com>

Partially-Implements: blueprint add-windows-support

Change-Id: I400fdb3db6562a7171ecadcb27ee63ae4eaf8006
This commit is contained in:
Claudiu Belu 2016-05-18 03:59:29 +03:00
parent 7d8ac478b3
commit 5c616b2afa
6 changed files with 50 additions and 1 deletions

View File

@ -236,6 +236,14 @@ A plugin config is specified something like this:
env: stage
newDim: test
monasca-collector service can receive a `--config-file` argument, which represents an alternate agent configuration file, instead of the default /etc/monasca/agent.yaml.
example:
```
monasca-collector --config-file="/path/to/monasca_agent.yaml"
```
# Running
The monasca-setup command will create an appropriate startup script for the agent and so the agent can be run by using the standard daemon control tool for your operating system. If you have configured manually the startup script templates can be found in the code under the packaging directory.

View File

@ -27,8 +27,15 @@ log = logging.getLogger(__name__)
class Config(object):
def __init__(self, configFile=None):
# importing it here, in order to avoid a circular import
# as monasca_agent.common.util imports this module.
from monasca_agent.common import util
options, args = util.get_parsed_args()
if configFile is not None:
self._configFile = configFile
elif options.config_file is not None:
self._configFile = options.config_file
elif os.path.exists(DEFAULT_CONFIG_FILE):
self._configFile = DEFAULT_CONFIG_FILE
elif os.path.exists(os.getcwd() + '/agent.yaml'):

View File

@ -444,6 +444,9 @@ def get_parsed_args():
parser.add_option('-c', '--clean', action='store_true', default=False, dest='clean')
parser.add_option('-v', '--verbose', action='store_true', default=False, dest='verbose',
help='Print out stacktraces for errors in checks')
parser.add_option('-f', '--config-file', default=None, dest='config_file',
help='Location for an alternate config rather than '
'using the default config location.')
try:
options, args = parser.parse_args()

View File

@ -207,6 +207,9 @@ def main():
tornado.options.define("sslcheck", default=1, help="Verify SSL hostname, on by default")
tornado.options.define("use_simple_http_client", default=0,
help="Use Tornado SimpleHTTPClient instead of CurlAsyncHTTPClient")
tornado.options.define("config_file", default=None,
help="Location for an alternate config rather than "
"using the default config location.")
args = tornado.options.parse_command_line()
skip_ssl_validation = False
use_simple_http_client = False

View File

@ -88,7 +88,7 @@ class MonascaStatsd(object):
def main():
"""The main entry point for the unix version of monasca_statsd. """
parser = argparse.ArgumentParser(description='Monasca statsd - statsd server supporting metric dimensions')
parser.add_argument('--config', '-c',
parser.add_argument('--config', '--config-file', '-c',
help="Location for an alternate config rather than using the default config location.")
args = parser.parse_args()

View File

@ -1,8 +1,10 @@
# -*- coding: latin-1 -*-
import unittest
import mock
import os.path
import tempfile
from monasca_agent.common import config
from monasca_agent.common.config import Config
from monasca_agent.common.util import PidFile, is_valid_hostname
@ -84,6 +86,32 @@ class TestConfig(unittest.TestCase):
self.assertTrue(conf_1 is conf_2)
self.assertTrue(conf_1 is conf_3)
@mock.patch.object(Config, '_read_config')
@mock.patch('monasca_agent.common.util.get_parsed_args')
def testConfigFromParsedArgs(self, mock_parsed_args, mock_read_config):
mock_options = mock.Mock()
mock_parsed_args.return_value = (mock_options, mock.sentinel.args)
conf = Config()
# object is singleton, for this case, it needs to be reloaded.
conf.__init__()
self.assertEqual(mock_options.config_file, conf._configFile)
@mock.patch.object(Config, '_read_config')
@mock.patch('monasca_agent.common.util.get_parsed_args')
@mock.patch('monasca_agent.common.config.os')
def testConfigFileFromDefault(self, mock_os, mock_parsed_args, mock_read_config):
mock_os.path.exists = mock.create_autospec(mock_os.path.exists, return_value=True)
mock_options = mock.Mock()
mock_options.config_file = None
mock_parsed_args.return_value = (mock_options, mock.sentinel.args)
conf = Config()
# object is singleton, for this case, it needs to be reloaded.
conf.__init__()
self.assertEqual(config.DEFAULT_CONFIG_FILE, conf._configFile)
mock_os.path.exists.assert_called_once_with(config.DEFAULT_CONFIG_FILE)
if __name__ == '__main__':
unittest.main()