1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 OpenStack, LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
SchedulerOptions monitors a local .json file for changes and loads
20
it if needed. This file is converted to a data structure and passed
21
into the filtering and weighing functions which can use it for
22
dynamic configuration.
29
from cinder import flags
30
from cinder.openstack.common import cfg
31
from cinder.openstack.common import log as logging
32
from cinder.openstack.common import timeutils
35
scheduler_json_config_location_opt = cfg.StrOpt(
36
'scheduler_json_config_location',
38
help='Absolute path to scheduler configuration JSON file.')
41
FLAGS.register_opt(scheduler_json_config_location_opt)
43
LOG = logging.getLogger(__name__)
46
class SchedulerOptions(object):
48
SchedulerOptions monitors a local .json file for changes and loads it
49
if needed. This file is converted to a data structure and passed into
50
the filtering and weighing functions which can use it for dynamic
55
super(SchedulerOptions, self).__init__()
57
self.last_modified = None
58
self.last_checked = None
60
def _get_file_handle(self, filename):
61
"""Get file handle. Broken out for testing."""
64
def _get_file_timestamp(self, filename):
65
"""Get the last modified datetime. Broken out for testing."""
67
return os.path.getmtime(filename)
69
LOG.exception(_("Could not stat scheduler options file "
70
"%(filename)s: '%(e)s'"), locals())
73
def _load_file(self, handle):
74
"""Decode the JSON file. Broken out for testing."""
76
return json.load(handle)
78
LOG.exception(_("Could not decode scheduler options: "
79
"'%(e)s'") % locals())
82
def _get_time_now(self):
83
"""Get current UTC. Broken out for testing."""
84
return timeutils.utcnow()
86
def get_configuration(self, filename=None):
87
"""Check the json file for changes and load it if needed."""
89
filename = FLAGS.scheduler_json_config_location
93
now = self._get_time_now()
94
if now - self.last_checked < datetime.timedelta(minutes=5):
97
last_modified = self._get_file_timestamp(filename)
98
if (not last_modified or not self.last_modified or
99
last_modified > self.last_modified):
100
self.data = self._load_file(self._get_file_handle(filename))
101
self.last_modified = last_modified