Update projects when a new primary is set.

This commit is contained in:
Luke Sneeringer 2015-01-28 12:08:43 -06:00
parent acb061f0be
commit 00a38fd75f
3 changed files with 24 additions and 3 deletions

View File

@ -1,7 +1,14 @@
from django.core.management.base import BaseCommand, CommandError
# Copyright (c) 2014 Ansible, Inc.
# All Rights Reserved.
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from awx.main.models import Project
class OptionEnforceError(Exception):
def __init__(self, value):
self.value = value
@ -136,4 +143,11 @@ class BaseCommandInstance(BaseCommand):
@staticmethod
def instance_str(instance):
return BaseCommandInstance.__instance_str(instance, ('uuid', 'hostname', 'role'))
return BaseCommandInstance.__instance_str(instance, ('uuid', 'hostname', 'role'))
def update_projects(self, instance):
"""Update all projects, ensuring the job runs against this instance,
which is the primary instance.
"""
for project in Project.objects.all():
project.update()

View File

@ -57,5 +57,9 @@ class Command(BaseCommandInstance):
instance = Instance(uuid=uuid, primary=self.is_option_primary(), hostname=self.get_option_hostname())
instance.save()
# If this is a primary instance, update projects.
if instance.primary:
self.update_projects(instance)
# Done!
print('Successfully registered instance %s.' % instance_str(instance))

View File

@ -56,5 +56,8 @@ class Command(BaseCommandInstance):
instance.primary = self.is_option_primary()
instance.save()
# If this is a primary instance, update projects.
self.update_projects_if_primary(instance)
# Done!
print('Successfully updated instance role %s' % instance_str(instance))
print('Successfully updated instance role %s' % instance_str(instance))