mirror of
https://github.com/ansible/awx.git
synced 2026-09-05 03:18:29 -02:30
AC-382, AC-352. Added status and last_updated fields for projects API. Various other updates to support projects using SCM.
This commit is contained in:
@@ -14,21 +14,27 @@ from django.utils.dateparse import parse_datetime
|
||||
from django.utils.timezone import now, is_aware, make_aware
|
||||
|
||||
# AWX
|
||||
from awx.main.models import Job
|
||||
from awx.main.models import ProjectUpdate, Job
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
'''
|
||||
Management command to cleanup old jobs.
|
||||
Management command to cleanup old jobs and project updates.
|
||||
'''
|
||||
|
||||
help = 'Remove old jobs and events from the database.'
|
||||
help = 'Remove old jobs and project updates from the database.'
|
||||
|
||||
option_list = NoArgsCommand.option_list + (
|
||||
make_option('--days', dest='days', type='int', default=90, metavar='N',
|
||||
help='Remove jobs executed more than N days ago'),
|
||||
help='Remove jobs/updates executed more than N days ago'),
|
||||
make_option('--dry-run', dest='dry_run', action='store_true',
|
||||
default=False, help='Dry run mode (show items that would '
|
||||
'be removed)'),
|
||||
make_option('--jobs', dest='only_jobs', action='store_true',
|
||||
default=False,
|
||||
help='Only remove jobs (leave project updates alone)'),
|
||||
make_option('--project-updates', dest='only_project_updates',
|
||||
action='store_true', default=False,
|
||||
help='Only remove project updates (leave jobs alone)'),
|
||||
)
|
||||
|
||||
def cleanup_jobs(self):
|
||||
@@ -38,7 +44,7 @@ class Command(NoArgsCommand):
|
||||
job_display = '"%s" (started %s, %d host summaries, %d events)' % \
|
||||
(unicode(job), unicode(job.created),
|
||||
job.job_host_summaries.count(), job.job_events.count())
|
||||
if job.status in ('pending', 'running'):
|
||||
if job.status in ('pending', 'waiting', 'running'):
|
||||
action_text = 'would skip' if self.dry_run else 'skipping'
|
||||
self.logger.debug('%s %s job %s', action_text, job.status, job_display)
|
||||
elif job.created >= self.cutoff:
|
||||
@@ -50,6 +56,24 @@ class Command(NoArgsCommand):
|
||||
if not self.dry_run:
|
||||
job.delete()
|
||||
|
||||
def cleanup_project_updates(self):
|
||||
for pu in ProjectUpdate.objects.all():
|
||||
pu_display = '"%s" (started %s)' % (unicode(pu), unicode(pu.created))
|
||||
if pu.status in ('pending', 'waiting', 'running'):
|
||||
action_text = 'would skip' if self.dry_run else 'skipping'
|
||||
self.logger.debug('%s %s project update %s', action_text, pu.status, pu_display)
|
||||
if pu in (pu.project.current_update, pu.project.last_update) and pu.project.scm_type:
|
||||
action_text = 'would skip' if self.dry_run else 'skipping'
|
||||
self.logger.debug('%s %s', action_text, pu_display)
|
||||
elif pu.created >= self.cutoff:
|
||||
action_text = 'would skip' if self.dry_run else 'skipping'
|
||||
self.logger.debug('%s %s', action_text, pu_display)
|
||||
else:
|
||||
action_text = 'would delete' if self.dry_run else 'deleting'
|
||||
self.logger.info('%s %s', action_text, pu_display)
|
||||
if not self.dry_run:
|
||||
pu.delete()
|
||||
|
||||
def init_logging(self):
|
||||
log_levels = dict(enumerate([logging.ERROR, logging.INFO,
|
||||
logging.DEBUG, 0]))
|
||||
@@ -67,4 +91,9 @@ class Command(NoArgsCommand):
|
||||
self.days = int(options.get('days', 90))
|
||||
self.dry_run = bool(options.get('dry_run', False))
|
||||
self.cutoff = now() - datetime.timedelta(days=self.days)
|
||||
self.cleanup_jobs()
|
||||
self.only_jobs = bool(options.get('only_jobs', False))
|
||||
self.only_project_updates = bool(options.get('only_project_updates', False))
|
||||
if self.only_jobs or (not self.only_jobs and not self.only_project_updates):
|
||||
self.cleanup_jobs()
|
||||
if self.only_project_updates or (not self.only_jobs and not self.only_project_updates):
|
||||
self.cleanup_project_updates()
|
||||
|
||||
Reference in New Issue
Block a user