~ubuntu-branches/ubuntu/raring/cinder/raring

« back to all changes in this revision

Viewing changes to cinder/scheduler/scheduler_options.py

  • Committer: Package Import Robot
  • Author(s): Chris J Arges
  • Date: 2013-01-15 10:10:28 UTC
  • mfrom: (7.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130115101028-i1jf2lyewii1xf2e
Tags: 2013.1~g2-0ubuntu2
debian/patches/series: Enable skip_failed_tests to fix FTBFS. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2011 OpenStack, LLC.
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
"""
 
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.
 
23
"""
 
24
 
 
25
import datetime
 
26
import json
 
27
import os
 
28
 
 
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
 
33
 
 
34
 
 
35
scheduler_json_config_location_opt = cfg.StrOpt(
 
36
        'scheduler_json_config_location',
 
37
        default='',
 
38
        help='Absolute path to scheduler configuration JSON file.')
 
39
 
 
40
FLAGS = flags.FLAGS
 
41
FLAGS.register_opt(scheduler_json_config_location_opt)
 
42
 
 
43
LOG = logging.getLogger(__name__)
 
44
 
 
45
 
 
46
class SchedulerOptions(object):
 
47
    """
 
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
 
51
    configuration.
 
52
    """
 
53
 
 
54
    def __init__(self):
 
55
        super(SchedulerOptions, self).__init__()
 
56
        self.data = {}
 
57
        self.last_modified = None
 
58
        self.last_checked = None
 
59
 
 
60
    def _get_file_handle(self, filename):
 
61
        """Get file handle. Broken out for testing."""
 
62
        return open(filename)
 
63
 
 
64
    def _get_file_timestamp(self, filename):
 
65
        """Get the last modified datetime. Broken out for testing."""
 
66
        try:
 
67
            return os.path.getmtime(filename)
 
68
        except os.error, e:
 
69
            LOG.exception(_("Could not stat scheduler options file "
 
70
                            "%(filename)s: '%(e)s'"), locals())
 
71
            raise
 
72
 
 
73
    def _load_file(self, handle):
 
74
        """Decode the JSON file. Broken out for testing."""
 
75
        try:
 
76
            return json.load(handle)
 
77
        except ValueError, e:
 
78
            LOG.exception(_("Could not decode scheduler options: "
 
79
                            "'%(e)s'") % locals())
 
80
            return {}
 
81
 
 
82
    def _get_time_now(self):
 
83
        """Get current UTC. Broken out for testing."""
 
84
        return timeutils.utcnow()
 
85
 
 
86
    def get_configuration(self, filename=None):
 
87
        """Check the json file for changes and load it if needed."""
 
88
        if not filename:
 
89
            filename = FLAGS.scheduler_json_config_location
 
90
        if not filename:
 
91
            return self.data
 
92
        if self.last_checked:
 
93
            now = self._get_time_now()
 
94
            if now - self.last_checked < datetime.timedelta(minutes=5):
 
95
                return self.data
 
96
 
 
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
 
102
        if not self.data:
 
103
            self.data = {}
 
104
 
 
105
        return self.data