Merge "Refactor instance_usage_audit. Add audit tasklog."
This commit is contained in:
@@ -585,20 +585,26 @@ def instance_get_all_by_filters(context, filters, sort_key='created_at',
|
||||
sort_dir)
|
||||
|
||||
|
||||
def instance_get_active_by_window(context, begin, end=None, project_id=None):
|
||||
def instance_get_active_by_window(context, begin, end=None, project_id=None,
|
||||
host=None):
|
||||
"""Get instances active during a certain time window.
|
||||
|
||||
Specifying a project_id will filter for a certain project."""
|
||||
return IMPL.instance_get_active_by_window(context, begin, end, project_id)
|
||||
Specifying a project_id will filter for a certain project.
|
||||
Specifying a host will filter for instances on a given compute host.
|
||||
"""
|
||||
return IMPL.instance_get_active_by_window(context, begin, end,
|
||||
project_id, host)
|
||||
|
||||
|
||||
def instance_get_active_by_window_joined(context, begin, end=None,
|
||||
project_id=None):
|
||||
project_id=None, host=None):
|
||||
"""Get instances and joins active during a certain time window.
|
||||
|
||||
Specifying a project_id will filter for a certain project."""
|
||||
Specifying a project_id will filter for a certain project.
|
||||
Specifying a host will filter for instances on a given compute host.
|
||||
"""
|
||||
return IMPL.instance_get_active_by_window_joined(context, begin, end,
|
||||
project_id)
|
||||
project_id, host)
|
||||
|
||||
|
||||
def instance_get_all_by_project(context, project_id):
|
||||
@@ -1948,3 +1954,52 @@ def get_instance_uuid_by_ec2_id(context, instance_id):
|
||||
def ec2_instance_create(context, instance_ec2_id):
|
||||
"""Create the ec2 id to instance uuid mapping on demand"""
|
||||
return IMPL.ec2_instance_create(context, instance_ec2_id)
|
||||
|
||||
|
||||
####################
|
||||
|
||||
|
||||
def task_log_end_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
errors,
|
||||
message=None,
|
||||
session=None):
|
||||
"""Mark a task as complete for a given host/time period"""
|
||||
return IMPL.task_log_end_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
errors,
|
||||
message,
|
||||
session)
|
||||
|
||||
|
||||
def task_log_begin_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
task_items=None,
|
||||
message=None,
|
||||
session=None):
|
||||
"""Mark a task as started for a given host/time period"""
|
||||
return IMPL.task_log_begin_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
task_items,
|
||||
message,
|
||||
session)
|
||||
|
||||
|
||||
def task_log_get_all(context, task_name, period_beginning,
|
||||
period_ending, host=None, state=None, session=None):
|
||||
return IMPL.task_log_get_all(context, task_name, period_beginning,
|
||||
period_ending, host, state, session)
|
||||
|
||||
|
||||
def task_log_get(context, task_name, period_beginning,
|
||||
period_ending, host, state=None, session=None):
|
||||
return IMPL.task_log_get(context, task_name, period_beginning,
|
||||
period_ending, host, state, session)
|
||||
|
||||
@@ -1564,7 +1564,8 @@ def instance_get_all_by_filters(context, filters, sort_key, sort_dir):
|
||||
|
||||
|
||||
@require_context
|
||||
def instance_get_active_by_window(context, begin, end=None, project_id=None):
|
||||
def instance_get_active_by_window(context, begin, end=None,
|
||||
project_id=None, host=None):
|
||||
"""Return instances that were active during window."""
|
||||
session = get_session()
|
||||
query = session.query(models.Instance)
|
||||
@@ -1575,13 +1576,15 @@ def instance_get_active_by_window(context, begin, end=None, project_id=None):
|
||||
query = query.filter(models.Instance.launched_at < end)
|
||||
if project_id:
|
||||
query = query.filter_by(project_id=project_id)
|
||||
if host:
|
||||
query = query.filter_by(host=host)
|
||||
|
||||
return query.all()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def instance_get_active_by_window_joined(context, begin, end=None,
|
||||
project_id=None):
|
||||
project_id=None, host=None):
|
||||
"""Return instances and joins that were active during window."""
|
||||
session = get_session()
|
||||
query = session.query(models.Instance)
|
||||
@@ -1596,6 +1599,8 @@ def instance_get_active_by_window_joined(context, begin, end=None,
|
||||
query = query.filter(models.Instance.launched_at < end)
|
||||
if project_id:
|
||||
query = query.filter_by(project_id=project_id)
|
||||
if host:
|
||||
query = query.filter_by(host=host)
|
||||
|
||||
return query.all()
|
||||
|
||||
@@ -5189,3 +5194,89 @@ def get_instance_uuid_by_ec2_id(context, instance_id, session=None):
|
||||
@require_context
|
||||
def _ec2_instance_get_query(context, session=None):
|
||||
return model_query(context, models.InstanceIdMapping, session=session)
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def task_log_get(context, task_name, period_beginning,
|
||||
period_ending, host, state=None, session=None):
|
||||
query = model_query(context, models.TaskLog, session=session).\
|
||||
filter_by(task_name=task_name).\
|
||||
filter_by(period_beginning=period_beginning).\
|
||||
filter_by(period_ending=period_ending).\
|
||||
filter_by(host=host)
|
||||
if state is not None:
|
||||
query = query.filter_by(state=state)
|
||||
|
||||
return query.first()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def task_log_get_all(context, task_name, period_beginning,
|
||||
period_ending, host=None, state=None, session=None):
|
||||
query = model_query(context, models.TaskLog, session=session).\
|
||||
filter_by(task_name=task_name).\
|
||||
filter_by(period_beginning=period_beginning).\
|
||||
filter_by(period_ending=period_ending)
|
||||
if host is not None:
|
||||
query = query.filter_by(host=host)
|
||||
if state is not None:
|
||||
query = query.filter_by(state=state)
|
||||
return query.all()
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def task_log_begin_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
task_items=None,
|
||||
message=None,
|
||||
session=None):
|
||||
session = session or get_session()
|
||||
with session.begin():
|
||||
task = task_log_get(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
session=session)
|
||||
if task:
|
||||
#It's already run(ning)!
|
||||
raise exception.TaskAlreadyRunning(task_name=task_name, host=host)
|
||||
task = models.TaskLog()
|
||||
task.task_name = task_name
|
||||
task.period_beginning = period_beginning
|
||||
task.period_ending = period_ending
|
||||
task.host = host
|
||||
task.state = "RUNNING"
|
||||
if message:
|
||||
task.message = message
|
||||
if task_items:
|
||||
task.task_items = task_items
|
||||
task.save(session=session)
|
||||
return task
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def task_log_end_task(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
errors,
|
||||
message=None,
|
||||
session=None):
|
||||
session = session or get_session()
|
||||
with session.begin():
|
||||
task = task_log_get(context, task_name,
|
||||
period_beginning,
|
||||
period_ending,
|
||||
host,
|
||||
session=session)
|
||||
if not task:
|
||||
#It's not running!
|
||||
raise exception.TaskNotRunning(task_name=task_name, host=host)
|
||||
task.state = "DONE"
|
||||
if message:
|
||||
task.message = message
|
||||
task.errors = errors
|
||||
task.save(session=session)
|
||||
return task
|
||||
|
||||
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user