Merge "Refactor instance_usage_audit. Add audit tasklog."

This commit is contained in:
Jenkins 2012-07-11 17:06:42 +00:00 committed by Gerrit Code Review
commit 437a7271d4
5 changed files with 16 additions and 86 deletions

View File

@ -1,83 +0,0 @@
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 Openstack, LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Cron script to generate usage notifications for instances existing
during the audit period.
Together with the notifications generated by compute on instance
create/delete/resize, over that time period, this allows an external
system consuming usage notification feeds to calculate instance usage
for each tenant.
Time periods are specified as 'hour', 'month', 'day' or 'year'
hour = previous hour. If run at 9:07am, will generate usage for 8-9am.
month = previous month. If the script is run April 1, it will generate
usages for March 1 through March 31.
day = previous day. if run on July 4th, it generates usages for July 3rd.
year = previous year. If run on Jan 1, it generates usages for
Jan 1 through Dec 31 of the previous year.
"""
import datetime
import gettext
import os
import sys
import time
import traceback
# If ../nova/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir,
os.pardir))
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
sys.path.insert(0, POSSIBLE_TOPDIR)
gettext.install('nova', unicode=1)
import nova.compute.utils
from nova import context
from nova import db
from nova import exception
from nova import flags
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import utils
FLAGS = flags.FLAGS
if __name__ == '__main__':
admin_context = context.get_admin_context()
flags.parse_args(sys.argv)
logging.setup("nova")
begin, end = utils.last_completed_audit_period()
print "Starting instance usage audit"
print "Creating usages for %s until %s" % (str(begin), str(end))
instances = db.instance_get_active_by_window_joined(admin_context,
begin,
end)
print "Found %d instances" % len(instances)
for instance_ref in instances:
try:
nova.compute.utils.notify_usage_exists(
admin_context, instance_ref,
ignore_missing_network_data=False)
except Exception, e:
print traceback.format_exc(e)
print "Instance usage audit completed"

View File

@ -1100,6 +1100,14 @@ class CouldNotFetchImage(NovaException):
message = _("Could not fetch image %(image_id)s")
class TaskAlreadyRunning(NovaException):
message = _("Task %(task_name) is already running on host %(host)")
class TaskNotRunning(NovaException):
message = _("Task %(task_name) is not running on host %(host)")
def get_context_from_function_and_args(function, args, kwargs):
"""Find an arg of type RequestContext and return it.

View File

@ -98,6 +98,7 @@
"compute_extension:floating_ips": [],
"compute_extension:hosts": [],
"compute_extension:hypervisors": [],
"compute_extension:instance_usage_audit_log": [],
"compute_extension:keypairs": [],
"compute_extension:multinic": [],
"compute_extension:networks": [],

View File

@ -299,7 +299,7 @@ EASIER_PASSWORD_SYMBOLS = ('23456789', # Removed: 0, 1
'ABCDEFGHJKLMNPQRSTUVWXYZ') # Removed: I, O
def last_completed_audit_period(unit=None):
def last_completed_audit_period(unit=None, before=None):
"""This method gives you the most recently *completed* audit period.
arguments:
@ -311,6 +311,8 @@ def last_completed_audit_period(unit=None):
like so: 'day@18' This will begin the period at 18:00
UTC. 'month@15' starts a monthly period on the 15th,
and year@3 begins a yearly one on March 1st.
before: Give the audit period most recently completed before
<timestamp>. Defaults to now.
returns: 2 tuple of datetimes (begin, end)
@ -324,7 +326,10 @@ def last_completed_audit_period(unit=None):
unit, offset = unit.split("@", 1)
offset = int(offset)
rightnow = timeutils.utcnow()
if before is not None:
rightnow = before
else:
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':

View File

@ -48,7 +48,6 @@ setuptools.setup(name='nova',
'bin/nova-console',
'bin/nova-consoleauth',
'bin/nova-dhcpbridge',
'bin/nova-instance-usage-audit',
'bin/nova-manage',
'bin/nova-network',
'bin/nova-novncproxy',