mirror of
https://github.com/ansible/awx.git
synced 2026-02-26 23:46:05 -03:30
Differentiate between an expired license and a non-existant license
This commit is contained in:
@@ -31,6 +31,14 @@ from awx.main.task_engine import TaskSerializer as LicenseReader
|
|||||||
|
|
||||||
logger = logging.getLogger('awx.main.commands.inventory_import')
|
logger = logging.getLogger('awx.main.commands.inventory_import')
|
||||||
|
|
||||||
|
LICENSE_EXPIRED_MESSAGE = '''\
|
||||||
|
License expired.
|
||||||
|
See http://www.ansible.com/renew for license extension information.'''
|
||||||
|
|
||||||
|
LICENSE_NON_EXISTANT_MESSAGE = '''\
|
||||||
|
No license.
|
||||||
|
See http://www.ansible.com/renew for license information.'''
|
||||||
|
|
||||||
LICENSE_MESSAGE = '''\
|
LICENSE_MESSAGE = '''\
|
||||||
Number of licensed instances exceeded, would bring available instances to %(new_count)d, system is licensed for %(available_instances)d.
|
Number of licensed instances exceeded, would bring available instances to %(new_count)d, system is licensed for %(available_instances)d.
|
||||||
See http://www.ansible.com/renew for license extension information.'''
|
See http://www.ansible.com/renew for license extension information.'''
|
||||||
@@ -635,6 +643,7 @@ class Command(NoArgsCommand):
|
|||||||
# Load inventory source if specified via environment variable (when
|
# Load inventory source if specified via environment variable (when
|
||||||
# inventory_import is called from an InventoryUpdate task).
|
# inventory_import is called from an InventoryUpdate task).
|
||||||
inventory_source_id = os.getenv('INVENTORY_SOURCE_ID', None)
|
inventory_source_id = os.getenv('INVENTORY_SOURCE_ID', None)
|
||||||
|
inventory_update_id = os.getenv('INVENTORY_UPDATE_ID', None)
|
||||||
if inventory_source_id:
|
if inventory_source_id:
|
||||||
try:
|
try:
|
||||||
self.inventory_source = InventorySource.objects.get(pk=inventory_source_id,
|
self.inventory_source = InventorySource.objects.get(pk=inventory_source_id,
|
||||||
@@ -643,7 +652,11 @@ class Command(NoArgsCommand):
|
|||||||
except InventorySource.DoesNotExist:
|
except InventorySource.DoesNotExist:
|
||||||
raise CommandError('Inventory source with id=%s not found' %
|
raise CommandError('Inventory source with id=%s not found' %
|
||||||
inventory_source_id)
|
inventory_source_id)
|
||||||
self.inventory_update = None
|
try:
|
||||||
|
self.inventory_update = InventoryUpdate.objects.get(pk=inventory_update_id)
|
||||||
|
except InventorySource.DoesNotExist:
|
||||||
|
raise CommandError('Inventory update with id=%s not found' %
|
||||||
|
inventory_update_id)
|
||||||
# Otherwise, create a new inventory source to capture this invocation
|
# Otherwise, create a new inventory source to capture this invocation
|
||||||
# via command line.
|
# via command line.
|
||||||
else:
|
else:
|
||||||
@@ -1167,12 +1180,15 @@ class Command(NoArgsCommand):
|
|||||||
def check_license(self):
|
def check_license(self):
|
||||||
reader = LicenseReader()
|
reader = LicenseReader()
|
||||||
license_info = reader.from_file()
|
license_info = reader.from_file()
|
||||||
|
if not license_info or len(license_info) == 0:
|
||||||
|
self.logger.error(LICENSE_NON_EXISTANT_MESSAGE)
|
||||||
|
raise CommandError('No Tower license found!')
|
||||||
available_instances = license_info.get('available_instances', 0)
|
available_instances = license_info.get('available_instances', 0)
|
||||||
free_instances = license_info.get('free_instances', 0)
|
free_instances = license_info.get('free_instances', 0)
|
||||||
time_remaining = license_info.get('time_remaining', 0)
|
time_remaining = license_info.get('time_remaining', 0)
|
||||||
new_count = Host.objects.active_count()
|
new_count = Host.objects.active_count()
|
||||||
if time_remaining <= 0 and not license_info.get('demo', False):
|
if time_remaining <= 0 and not license_info.get('demo', False):
|
||||||
self.logger.error('License has expired')
|
self.logger.error(LICENSE_EXPIRED_MESSAGE)
|
||||||
raise CommandError("License has expired!")
|
raise CommandError("License has expired!")
|
||||||
if free_instances < 0:
|
if free_instances < 0:
|
||||||
d = {
|
d = {
|
||||||
@@ -1185,6 +1201,10 @@ class Command(NoArgsCommand):
|
|||||||
self.logger.error(LICENSE_MESSAGE % d)
|
self.logger.error(LICENSE_MESSAGE % d)
|
||||||
raise CommandError('License count exceeded!')
|
raise CommandError('License count exceeded!')
|
||||||
|
|
||||||
|
def mark_license_failure(self, save=True):
|
||||||
|
self.inventory_update.license_error = True
|
||||||
|
self.inventory_update.save(update_fields=['license_error'])
|
||||||
|
|
||||||
def handle_noargs(self, **options):
|
def handle_noargs(self, **options):
|
||||||
self.verbosity = int(options.get('verbosity', 1))
|
self.verbosity = int(options.get('verbosity', 1))
|
||||||
self.init_logging()
|
self.init_logging()
|
||||||
@@ -1220,10 +1240,15 @@ class Command(NoArgsCommand):
|
|||||||
except re.error:
|
except re.error:
|
||||||
raise CommandError('invalid regular expression for --host-filter')
|
raise CommandError('invalid regular expression for --host-filter')
|
||||||
|
|
||||||
self.check_license()
|
|
||||||
begin = time.time()
|
begin = time.time()
|
||||||
self.load_inventory_from_database()
|
self.load_inventory_from_database()
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.check_license()
|
||||||
|
except CommandError as e:
|
||||||
|
self.mark_license_failure(save=True)
|
||||||
|
raise e
|
||||||
|
|
||||||
status, tb, exc = 'error', '', None
|
status, tb, exc = 'error', '', None
|
||||||
try:
|
try:
|
||||||
if settings.SQL_DEBUG:
|
if settings.SQL_DEBUG:
|
||||||
@@ -1232,7 +1257,7 @@ class Command(NoArgsCommand):
|
|||||||
# Update inventory update for this command line invocation.
|
# Update inventory update for this command line invocation.
|
||||||
with ignore_inventory_computed_fields():
|
with ignore_inventory_computed_fields():
|
||||||
iu = self.inventory_update
|
iu = self.inventory_update
|
||||||
if iu and iu.status != 'running':
|
if iu.status != 'running':
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.inventory_update.status = 'running'
|
self.inventory_update.status = 'running'
|
||||||
self.inventory_update.save()
|
self.inventory_update.save()
|
||||||
@@ -1263,7 +1288,11 @@ class Command(NoArgsCommand):
|
|||||||
if settings.SQL_DEBUG:
|
if settings.SQL_DEBUG:
|
||||||
self.logger.warning('update computed fields took %d queries',
|
self.logger.warning('update computed fields took %d queries',
|
||||||
len(connection.queries) - queries_before2)
|
len(connection.queries) - queries_before2)
|
||||||
self.check_license()
|
try:
|
||||||
|
self.check_license()
|
||||||
|
except CommandError as e:
|
||||||
|
self.mark_license_failure(save=True)
|
||||||
|
raise e
|
||||||
|
|
||||||
if self.inventory_source.group:
|
if self.inventory_source.group:
|
||||||
inv_name = 'group "%s"' % (self.inventory_source.group.name)
|
inv_name = 'group "%s"' % (self.inventory_source.group.name)
|
||||||
@@ -1295,15 +1324,13 @@ class Command(NoArgsCommand):
|
|||||||
else:
|
else:
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
exc = e
|
exc = e
|
||||||
if self.inventory_update:
|
transaction.rollback()
|
||||||
transaction.rollback()
|
|
||||||
|
|
||||||
if self.inventory_update:
|
with ignore_inventory_computed_fields():
|
||||||
with ignore_inventory_computed_fields():
|
self.inventory_update = InventoryUpdate.objects.get(pk=self.inventory_update.pk)
|
||||||
self.inventory_update = InventoryUpdate.objects.get(pk=self.inventory_update.pk)
|
self.inventory_update.result_traceback = tb
|
||||||
self.inventory_update.result_traceback = tb
|
self.inventory_update.status = status
|
||||||
self.inventory_update.status = status
|
self.inventory_update.save(update_fields=['status', 'result_traceback'])
|
||||||
self.inventory_update.save(update_fields=['status', 'result_traceback'])
|
|
||||||
|
|
||||||
if exc and isinstance(exc, CommandError):
|
if exc and isinstance(exc, CommandError):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
@@ -1223,12 +1223,6 @@ class InventoryUpdate(UnifiedJob, InventorySourceOptions):
|
|||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
update_fields = kwargs.get('update_fields', [])
|
update_fields = kwargs.get('update_fields', [])
|
||||||
if bool(('license' in self.result_stdout or 'licensed' in self.result_stdout) and
|
|
||||||
'exceeded' in self.result_stdout and not self.license_error) or \
|
|
||||||
bool(any(x in self.result_stdout for x in ('License has expired', 'License count exceeded'))):
|
|
||||||
self.license_error = True
|
|
||||||
if 'license_error' not in update_fields:
|
|
||||||
update_fields.append('license_error')
|
|
||||||
inventory_source = self.inventory_source
|
inventory_source = self.inventory_source
|
||||||
if self.active and inventory_source.inventory and self.name == inventory_source.name:
|
if self.active and inventory_source.inventory and self.name == inventory_source.name:
|
||||||
if inventory_source.group:
|
if inventory_source.group:
|
||||||
|
|||||||
@@ -1093,6 +1093,7 @@ class RunInventoryUpdate(BaseTask):
|
|||||||
|
|
||||||
# Pass inventory source ID to inventory script.
|
# Pass inventory source ID to inventory script.
|
||||||
env['INVENTORY_SOURCE_ID'] = str(inventory_update.inventory_source_id)
|
env['INVENTORY_SOURCE_ID'] = str(inventory_update.inventory_source_id)
|
||||||
|
env['INVENTORY_UPDATE_ID'] = str(inventory_update.pk)
|
||||||
|
|
||||||
# Set environment variables specific to each source.
|
# Set environment variables specific to each source.
|
||||||
#
|
#
|
||||||
|
|||||||
Reference in New Issue
Block a user