print "%-25s\t%-15s" % (_('host'),
_('zone'))
ctxt = context.get_admin_context()
- now = utils.utcnow()
services = db.service_get_all(ctxt)
if zone:
services = [s for s in services if s['availability_zone'] == zone]
"""Super simple fake memcache client."""
from cinder import utils
+from cinder.openstack.common import timeutils
class Client(object):
for k in self.cache.keys():
(timeout, _value) = self.cache[k]
- if timeout and utils.utcnow_ts() >= timeout:
+ if timeout and timeutils.utcnow_ts() >= timeout:
del self.cache[k]
return self.cache.get(key, (0, None))[1]
"""Sets the value for a key."""
timeout = 0
if time != 0:
- timeout = utils.utcnow_ts() + time
+ timeout = timeutils.utcnow_ts() + time
self.cache[key] = (timeout, value)
return True
from cinder import log as logging
from cinder.openstack.common import local
+from cinder.openstack.common import timeutils
from cinder import utils
self.read_deleted = read_deleted
self.remote_address = remote_address
if not timestamp:
- timestamp = utils.utcnow()
+ timestamp = timeutils.utcnow()
if isinstance(timestamp, basestring):
- timestamp = utils.parse_strtime(timestamp)
+ timestamp = timeutils.parse_strtime(timestamp)
self.timestamp = timestamp
if not request_id:
request_id = generate_request_id()
'read_deleted': self.read_deleted,
'roles': self.roles,
'remote_address': self.remote_address,
- 'timestamp': utils.strtime(self.timestamp),
+ 'timestamp': timeutils.strtime(self.timestamp),
'request_id': self.request_id,
'auth_token': self.auth_token,
'quota_class': self.quota_class}
session.query(models.Volume).\
filter_by(id=volume_id).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
session.query(models.IscsiTarget).\
filter_by(volume_id=volume_id).\
session.query(models.VolumeMetadata).\
filter_by(volume_id=volume_id).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
_volume_metadata_get_query(context, volume_id).\
filter_by(key=key).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
session.query(models.Snapshot).\
filter_by(id=snapshot_id).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
@require_admin_context
def migration_get_all_unconfirmed(context, confirm_window, session=None):
- confirm_window = datetime.datetime.utcnow() - datetime.timedelta(
+ confirm_window = timeutils.utcnow() - datetime.timedelta(
seconds=confirm_window)
return model_query(context, models.Migration, session=session,
session.query(models.VolumeTypes).\
filter_by(id=volume_type_id).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
session.query(models.VolumeTypeExtraSpecs).\
filter_by(volume_type_id=volume_type_id).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
_volume_type_extra_specs_query(context, volume_type_id).\
filter_by(key=key).\
update({'deleted': True,
- 'deleted_at': utils.utcnow(),
+ 'deleted_at': timeutils.utcnow(),
'updated_at': literal_column('updated_at')})
from cinder import exception
from cinder import flags
from cinder import utils
+from cinder.openstack.common import timeutils
FLAGS = flags.FLAGS
"""Base class for Cinder Models."""
__table_args__ = {'mysql_engine': 'InnoDB'}
__table_initialized__ = False
- created_at = Column(DateTime, default=utils.utcnow)
- updated_at = Column(DateTime, onupdate=utils.utcnow)
+ created_at = Column(DateTime, default=timeutils.utcnow)
+ updated_at = Column(DateTime, onupdate=timeutils.utcnow)
deleted_at = Column(DateTime)
deleted = Column(Boolean, default=False)
metadata = None
def delete(self, session=None):
"""Delete this object."""
self.deleted = True
- self.deleted_at = utils.utcnow()
+ self.deleted_at = timeutils.utcnow()
self.save(session=session)
def __setitem__(self, key, value):
from cinder import log as logging
from cinder.openstack.common import cfg
from cinder.openstack.common import importutils
+from cinder.openstack.common import timeutils
LOG = logging.getLogger(__name__)
{'message_id': str(uuid.uuid4()),
'publisher_id': 'compute.host1',
- 'timestamp': utils.utcnow(),
+ 'timestamp': timeutils.utcnow(),
'priority': 'WARN',
'event_type': 'compute.create_instance',
'payload': {'instance_id': 12, ... }}
event_type=event_type,
priority=priority,
payload=payload,
- timestamp=str(utils.utcnow()))
+ timestamp=str(timeutils.utcnow()))
try:
driver.notify(msg)
except Exception, e:
from cinder import log as logging
from cinder.openstack.common import cfg
from cinder.openstack.common import importutils
+from cinder.openstack.common import timeutils
from cinder import rpc
from cinder.rpc import common as rpc_common
from cinder import utils
if update_db:
volume_id = kwargs.get('volume_id', None)
if volume_id is not None:
- now = utils.utcnow()
+ now = timeutils.utcnow()
db.volume_update(context, volume_id,
{'host': host, 'scheduled_at': now})
rpc.cast(context,
from cinder import flags
from cinder import log as logging
from cinder.openstack.common import cfg
+from cinder.openstack.common import timeutils
from cinder import utils
from cinder import service
from cinder import tests
# NOTE(vish): We need a better method for creating fixtures for tests
# now that we have some required db setup for the system
# to work properly.
- self.start = utils.utcnow()
+ self.start = timeutils.utcnow()
tests.reset_db()
# emulate some of the mox stuff, we can't use the metaclass
from cinder import exception as exc
from cinder import utils
from cinder import wsgi
+from cinder.openstack.common import timeutils
FAKE_UUID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
@staticmethod
def auth_token_create(context, token):
- fake_token = FakeToken(created_at=utils.utcnow(), **token)
+ fake_token = FakeToken(created_at=timeutils.utcnow(), **token)
FakeAuthDatabase.data[fake_token.token_hash] = fake_token
FakeAuthDatabase.data['id_%i' % fake_token.id] = fake_token
return fake_token
from cinder import flags
from cinder.notifier import api as notifier
from cinder import rpc
+from cinder.openstack.common import timeutils
from cinder.rpc import common as rpc_common
from cinder.scheduler import driver
from cinder.scheduler import manager
'extra_arg': 'meow'}
queue = 'fake_queue'
- self.mox.StubOutWithMock(utils, 'utcnow')
+ self.mox.StubOutWithMock(timeutils, 'utcnow')
self.mox.StubOutWithMock(db, 'volume_update')
self.mox.StubOutWithMock(db, 'queue_get_for')
self.mox.StubOutWithMock(rpc, 'cast')
- utils.utcnow().AndReturn('fake-now')
+ timeutils.utcnow().AndReturn('fake-now')
db.volume_update(self.context, 31337,
{'host': host, 'scheduled_at': 'fake-now'})
db.queue_get_for(self.context,
import cinder
from cinder import exception
from cinder import flags
+from cinder.openstack.common import timeutils
from cinder import test
from cinder import utils
down_time = 5
self.flags(service_down_time=down_time)
- self.mox.StubOutWithMock(utils, 'utcnow')
+ self.mox.StubOutWithMock(timeutils, 'utcnow')
# Up (equal)
- utils.utcnow().AndReturn(fts_func(fake_now))
+ timeutils.utcnow().AndReturn(fts_func(fake_now))
service = {'updated_at': fts_func(fake_now - down_time),
'created_at': fts_func(fake_now - down_time)}
self.mox.ReplayAll()
self.mox.ResetAll()
# Up
- utils.utcnow().AndReturn(fts_func(fake_now))
+ timeutils.utcnow().AndReturn(fts_func(fake_now))
service = {'updated_at': fts_func(fake_now - down_time + 1),
'created_at': fts_func(fake_now - down_time + 1)}
self.mox.ReplayAll()
self.mox.ResetAll()
# Down
- utils.utcnow().AndReturn(fts_func(fake_now))
+ timeutils.utcnow().AndReturn(fts_func(fake_now))
service = {'updated_at': fts_func(fake_now - down_time - 1),
'created_at': fts_func(fake_now - down_time - 1)}
self.mox.ReplayAll()
self.assertEquals(h1, h2)
-class Iso8601TimeTest(test.TestCase):
-
- def _instaneous(self, timestamp, yr, mon, day, hr, min, sec, micro):
- self.assertEquals(timestamp.year, yr)
- self.assertEquals(timestamp.month, mon)
- self.assertEquals(timestamp.day, day)
- self.assertEquals(timestamp.hour, hr)
- self.assertEquals(timestamp.minute, min)
- self.assertEquals(timestamp.second, sec)
- self.assertEquals(timestamp.microsecond, micro)
-
- def _do_test(self, str, yr, mon, day, hr, min, sec, micro, shift):
- DAY_SECONDS = 24 * 60 * 60
- timestamp = utils.parse_isotime(str)
- self._instaneous(timestamp, yr, mon, day, hr, min, sec, micro)
- offset = timestamp.tzinfo.utcoffset(None)
- self.assertEqual(offset.seconds + offset.days * DAY_SECONDS, shift)
-
- def test_zulu(self):
- str = '2012-02-14T20:53:07Z'
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, 0)
-
- def test_zulu_micros(self):
- str = '2012-02-14T20:53:07.123Z'
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 123000, 0)
-
- def test_offset_east(self):
- str = '2012-02-14T20:53:07+04:30'
- offset = 4.5 * 60 * 60
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, offset)
-
- def test_offset_east_micros(self):
- str = '2012-02-14T20:53:07.42+04:30'
- offset = 4.5 * 60 * 60
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 420000, offset)
-
- def test_offset_west(self):
- str = '2012-02-14T20:53:07-05:30'
- offset = -5.5 * 60 * 60
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, offset)
-
- def test_offset_west_micros(self):
- str = '2012-02-14T20:53:07.654321-05:30'
- offset = -5.5 * 60 * 60
- self._do_test(str, 2012, 02, 14, 20, 53, 7, 654321, offset)
-
- def test_compare(self):
- zulu = utils.parse_isotime('2012-02-14T20:53:07')
- east = utils.parse_isotime('2012-02-14T20:53:07-01:00')
- west = utils.parse_isotime('2012-02-14T20:53:07+01:00')
- self.assertTrue(east > west)
- self.assertTrue(east > zulu)
- self.assertTrue(zulu > west)
-
- def test_compare_micros(self):
- zulu = utils.parse_isotime('2012-02-14T20:53:07.6544')
- east = utils.parse_isotime('2012-02-14T19:53:07.654321-01:00')
- west = utils.parse_isotime('2012-02-14T21:53:07.655+01:00')
- self.assertTrue(east < west)
- self.assertTrue(east < zulu)
- self.assertTrue(zulu < west)
-
- def test_zulu_roundtrip(self):
- str = '2012-02-14T20:53:07Z'
- zulu = utils.parse_isotime(str)
- self.assertEquals(zulu.tzinfo, iso8601.iso8601.UTC)
- self.assertEquals(utils.isotime(zulu), str)
-
- def test_east_roundtrip(self):
- str = '2012-02-14T20:53:07-07:00'
- east = utils.parse_isotime(str)
- self.assertEquals(east.tzinfo.tzname(None), '-07:00')
- self.assertEquals(utils.isotime(east), str)
-
- def test_west_roundtrip(self):
- str = '2012-02-14T20:53:07+11:30'
- west = utils.parse_isotime(str)
- self.assertEquals(west.tzinfo.tzname(None), '+11:30')
- self.assertEquals(utils.isotime(west), str)
-
- def test_now_roundtrip(self):
- str = utils.isotime()
- now = utils.parse_isotime(str)
- self.assertEquals(now.tzinfo, iso8601.iso8601.UTC)
- self.assertEquals(utils.isotime(now), str)
-
- def test_zulu_normalize(self):
- str = '2012-02-14T20:53:07Z'
- zulu = utils.parse_isotime(str)
- normed = utils.normalize_time(zulu)
- self._instaneous(normed, 2012, 2, 14, 20, 53, 07, 0)
-
- def test_east_normalize(self):
- str = '2012-02-14T20:53:07-07:00'
- east = utils.parse_isotime(str)
- normed = utils.normalize_time(east)
- self._instaneous(normed, 2012, 2, 15, 03, 53, 07, 0)
-
- def test_west_normalize(self):
- str = '2012-02-14T20:53:07+21:00'
- west = utils.parse_isotime(str)
- normed = utils.normalize_time(west)
- self._instaneous(normed, 2012, 2, 13, 23, 53, 07, 0)
-
-
class TestGreenLocks(test.TestCase):
def test_concurrent_green_lock_succeeds(self):
"""Verify spawn_n greenthreads with two locks run concurrently.
day=5,
month=3,
year=2012)
- utils.set_time_override(override_time=self.test_time)
+ timeutils.set_time_override(override_time=self.test_time)
def tearDown(self):
- utils.clear_time_override()
+ timeutils.clear_time_override()
super(AuditPeriodTest, self).tearDown()
def test_hour(self):
from cinder import log as logging
from cinder.openstack.common import cfg
from cinder.openstack.common import importutils
+from cinder.openstack.common import timeutils
LOG = logging.getLogger(__name__)
unit, offset = unit.split("@", 1)
offset = int(offset)
- rightnow = utcnow()
+ rightnow = timeutils.utcnow()
if unit not in ('month', 'day', 'year', 'hour'):
raise ValueError('Time period must be hour, day, month or year')
if unit == 'month':
" :%(ex)s") % locals())
-def utcnow():
- """Overridable version of utils.utcnow."""
- if utcnow.override_time:
- return utcnow.override_time
- return datetime.datetime.utcnow()
-
-
-utcnow.override_time = None
-
-
-def is_older_than(before, seconds):
- """Return True if before is older than seconds."""
- return utcnow() - before > datetime.timedelta(seconds=seconds)
-
-
-def utcnow_ts():
- """Timestamp version of our utcnow function."""
- return time.mktime(utcnow().timetuple())
-
-
-def set_time_override(override_time=datetime.datetime.utcnow()):
- """Override utils.utcnow to return a constant time."""
- utcnow.override_time = override_time
-
-
-def advance_time_delta(timedelta):
- """Advance overriden time using a datetime.timedelta."""
- assert(not utcnow.override_time is None)
- utcnow.override_time += timedelta
-
-
-def advance_time_seconds(seconds):
- """Advance overriden time by seconds."""
- advance_time_delta(datetime.timedelta(0, seconds))
-
-
-def clear_time_override():
- """Remove the overridden time."""
- utcnow.override_time = None
-
-
-def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
- """Returns formatted utcnow."""
- if not at:
- at = utcnow()
- return at.strftime(fmt)
-
-
-def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
- """Turn a formatted time back into a datetime."""
- return datetime.datetime.strptime(timestr, fmt)
-
-
-def isotime(at=None):
- """Stringify time in ISO 8601 format"""
- if not at:
- at = datetime.datetime.utcnow()
- str = at.strftime(ISO_TIME_FORMAT)
- tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
- str += ('Z' if tz == 'UTC' else tz)
- return str
-
-
-def parse_isotime(timestr):
- """Turn an iso formatted time back into a datetime."""
- try:
- return iso8601.parse_date(timestr)
- except (iso8601.ParseError, TypeError) as e:
- raise ValueError(e.message)
-
-
-def normalize_time(timestamp):
- """Normalize time in arbitrary timezone to UTC"""
- offset = timestamp.utcoffset()
- return timestamp.replace(tzinfo=None) - offset if offset else timestamp
-
-
def parse_mailmap(mailmap='.mailmap'):
mapping = {}
if os.path.exists(mailmap):
"""Check whether a service is up based on last heartbeat."""
last_heartbeat = service['updated_at'] or service['created_at']
# Timestamps in DB are UTC.
- elapsed = total_seconds(utcnow() - last_heartbeat)
+ elapsed = total_seconds(timeutils.utcnow() - last_heartbeat)
return abs(elapsed) <= FLAGS.service_down_time
from cinder import flags
from cinder import log as logging
import cinder.policy
+from cinder.openstack.common import timeutils
from cinder import quota
from cinder import rpc
from cinder import utils
msg = _("Volume still has %d dependent snapshots") % len(snapshots)
raise exception.InvalidVolume(reason=msg)
- now = utils.utcnow()
+ now = timeutils.utcnow()
self.db.volume_update(context, volume_id, {'status': 'deleting',
'terminated_at': now})
host = volume['host']
from cinder import manager
from cinder.openstack.common import cfg
from cinder.openstack.common import importutils
+from cinder.openstack.common import timeutils
from cinder import rpc
from cinder import utils
from cinder.volume import volume_types
self.db.volume_update(context,
volume_ref['id'], {'status': 'error'})
- now = utils.utcnow()
+ now = timeutils.utcnow()
self.db.volume_update(context,
volume_ref['id'], {'status': 'available',
'launched_at': now})