2013-08-23 16:39:02 +00:00
|
|
|
import os
|
|
|
|
import yaml
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
2014-07-09 20:28:24 +00:00
|
|
|
def init_logging():
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
return log
|
2013-08-23 16:39:02 +00:00
|
|
|
|
2014-07-09 20:28:24 +00:00
|
|
|
log = init_logging()
|
|
|
|
|
|
|
|
|
|
|
|
class YamlConfig(object):
|
2014-07-11 16:41:16 +00:00
|
|
|
"""
|
|
|
|
A configuration object populated by parsing a yaml file, with optional
|
|
|
|
default values.
|
|
|
|
|
|
|
|
Note that modifying the _defaults attribute of an instance can potentially
|
|
|
|
yield confusing results; if you need to do modify defaults, use the class
|
|
|
|
variable or create a subclass.
|
|
|
|
"""
|
|
|
|
_defaults = dict()
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def __init__(self, yaml_path=None):
|
|
|
|
self.yaml_path = yaml_path
|
|
|
|
if self.yaml_path:
|
|
|
|
self.load()
|
|
|
|
else:
|
2014-07-11 16:33:43 +00:00
|
|
|
self._conf = dict()
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
if os.path.exists(self.yaml_path):
|
2014-07-11 16:33:43 +00:00
|
|
|
self._conf = yaml.safe_load(file(self.yaml_path))
|
2014-07-09 20:28:24 +00:00
|
|
|
else:
|
|
|
|
log.debug("%s not found", self.yaml_path)
|
2014-07-11 16:33:43 +00:00
|
|
|
self._conf = dict()
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def update(self, in_dict):
|
|
|
|
"""
|
|
|
|
Update an existing configuration using dict.update()
|
|
|
|
|
|
|
|
:param in_dict: The dict to use to update
|
|
|
|
"""
|
2014-07-11 16:33:43 +00:00
|
|
|
self._conf.update(in_dict)
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, in_dict):
|
|
|
|
"""
|
|
|
|
Build a config object from a dict.
|
|
|
|
|
|
|
|
:param in_dict: The dict to use
|
|
|
|
:returns: The config object
|
|
|
|
"""
|
|
|
|
conf_obj = cls()
|
2014-07-11 16:33:43 +00:00
|
|
|
conf_obj._conf = in_dict
|
2014-07-09 20:28:24 +00:00
|
|
|
return conf_obj
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
"""
|
|
|
|
:returns: A shallow copy of the configuration as a dict
|
|
|
|
"""
|
2014-07-11 16:33:43 +00:00
|
|
|
return dict(self._conf)
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_str(cls, in_str):
|
|
|
|
"""
|
|
|
|
Build a config object from a string or yaml stream.
|
|
|
|
|
|
|
|
:param in_str: The stream or string
|
|
|
|
:returns: The config object
|
|
|
|
"""
|
|
|
|
conf_obj = cls()
|
2014-07-11 16:33:43 +00:00
|
|
|
conf_obj._conf = yaml.safe_load(in_str)
|
2014-07-09 20:28:24 +00:00
|
|
|
return conf_obj
|
|
|
|
|
|
|
|
def to_str(self):
|
|
|
|
"""
|
|
|
|
:returns: str(self)
|
|
|
|
"""
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
2014-07-11 16:33:43 +00:00
|
|
|
return yaml.safe_dump(self._conf, default_flow_style=False).strip()
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def __getitem__(self, name):
|
2014-07-11 16:33:43 +00:00
|
|
|
return self._conf.__getitem__(name)
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2014-07-11 16:41:16 +00:00
|
|
|
return self._conf.get(name, self._defaults.get(name))
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
2014-07-11 16:33:43 +00:00
|
|
|
if name.endswith('_conf') or name in ('yaml_path'):
|
2014-07-09 20:28:24 +00:00
|
|
|
object.__setattr__(self, name, value)
|
|
|
|
else:
|
2014-07-11 16:33:43 +00:00
|
|
|
self._conf[name] = value
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
def __delattr__(self, name):
|
2014-07-11 16:33:43 +00:00
|
|
|
del self._conf[name]
|
2014-07-09 20:28:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TeuthologyConfig(YamlConfig):
|
2013-08-26 17:36:01 +00:00
|
|
|
"""
|
|
|
|
This class is intended to unify teuthology's many configuration files and
|
|
|
|
objects. Currently it serves as a convenient interface to
|
|
|
|
~/.teuthology.yaml and nothing else.
|
|
|
|
"""
|
2014-07-11 19:20:29 +00:00
|
|
|
yaml_path = os.path.join(os.path.expanduser('~/.teuthology.yaml'))
|
2014-07-11 16:41:16 +00:00
|
|
|
_defaults = {
|
2013-12-09 22:40:27 +00:00
|
|
|
'archive_base': '/var/lib/teuthworker/archive',
|
2014-07-03 16:18:01 +00:00
|
|
|
'automated_scheduling': False,
|
2013-09-30 19:18:59 +00:00
|
|
|
'ceph_git_base_url': 'https://github.com/ceph/',
|
|
|
|
'lock_server': 'http://teuthology.front.sepia.ceph.com/locker/lock',
|
2014-01-16 16:38:39 +00:00
|
|
|
'max_job_time': 259200, # 3 days
|
2014-05-22 13:49:26 +00:00
|
|
|
'results_server': 'http://paddles.front.sepia.ceph.com/',
|
2014-07-10 07:12:33 +00:00
|
|
|
'src_base_path': os.path.expanduser('~/src'),
|
2013-09-30 19:18:59 +00:00
|
|
|
'verify_host_keys': True,
|
2013-12-05 23:37:25 +00:00
|
|
|
'watchdog_interval': 600,
|
2013-09-30 19:18:59 +00:00
|
|
|
}
|
2013-09-20 20:11:13 +00:00
|
|
|
|
2013-08-23 16:39:02 +00:00
|
|
|
def __init__(self):
|
2014-07-09 20:28:24 +00:00
|
|
|
super(TeuthologyConfig, self).__init__(self.yaml_path)
|
2013-09-20 20:11:13 +00:00
|
|
|
|
2013-08-23 16:39:02 +00:00
|
|
|
|
2014-07-09 20:28:24 +00:00
|
|
|
class JobConfig(YamlConfig):
|
|
|
|
pass
|
2013-09-30 19:18:59 +00:00
|
|
|
|
2013-08-23 16:39:02 +00:00
|
|
|
|
2014-07-09 20:28:24 +00:00
|
|
|
config = TeuthologyConfig()
|