mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 01:47:35 -02:30
AC-589 Update host enabled flag from inventory import.
This commit is contained in:
@@ -410,6 +410,13 @@ class Command(NoArgsCommand):
|
|||||||
make_option('--source', dest='source', type='str', default=None,
|
make_option('--source', dest='source', type='str', default=None,
|
||||||
metavar='s', help='inventory directory, file, or script '
|
metavar='s', help='inventory directory, file, or script '
|
||||||
'to load'),
|
'to load'),
|
||||||
|
make_option('--enabled-var', dest='enabled_var', type='str',
|
||||||
|
default=None, metavar='v', help='host variable used to '
|
||||||
|
'set/clear enabled flag when host is online/offline'),
|
||||||
|
make_option('--enabled-value', dest='enabled_value', type='str',
|
||||||
|
default=None, metavar='v', help='value of host variable '
|
||||||
|
'specified by --enabled-var that indicates host is '
|
||||||
|
'enabled/online.'),
|
||||||
)
|
)
|
||||||
|
|
||||||
def init_logging(self):
|
def init_logging(self):
|
||||||
@@ -512,7 +519,7 @@ class Command(NoArgsCommand):
|
|||||||
for group in del_groups:
|
for group in del_groups:
|
||||||
group_name = group.name
|
group_name = group.name
|
||||||
group.delete()
|
group.delete()
|
||||||
self.logger.info('Deleted group "%s"', group_name)
|
self.logger.info('Group "%s" deleted', group_name)
|
||||||
|
|
||||||
# If overwrite is set, clear all invalid child relationships for groups
|
# If overwrite is set, clear all invalid child relationships for groups
|
||||||
# and all invalid host memberships. When importing from a cloud
|
# and all invalid host memberships. When importing from a cloud
|
||||||
@@ -532,7 +539,7 @@ class Command(NoArgsCommand):
|
|||||||
if db_child not in db_group.children.all():
|
if db_child not in db_group.children.all():
|
||||||
continue
|
continue
|
||||||
db_group.children.remove(db_child)
|
db_group.children.remove(db_child)
|
||||||
self.logger.info('Removed group "%s" from group "%s"',
|
self.logger.info('Group "%s" removed from group "%s"',
|
||||||
db_child.name, db_group.name)
|
db_child.name, db_group.name)
|
||||||
db_hosts = db_group.hosts.all()
|
db_hosts = db_group.hosts.all()
|
||||||
mem_hosts = self.all_group.all_groups[db_group.name].hosts
|
mem_hosts = self.all_group.all_groups[db_group.name].hosts
|
||||||
@@ -541,7 +548,7 @@ class Command(NoArgsCommand):
|
|||||||
if db_host not in db_group.hosts.all():
|
if db_host not in db_group.hosts.all():
|
||||||
continue
|
continue
|
||||||
db_group.hosts.remove(db_host)
|
db_group.hosts.remove(db_host)
|
||||||
self.logger.info('Removed host "%s" from group "%s"',
|
self.logger.info('Host "%s" removed from group "%s"',
|
||||||
db_host.name, db_group.name)
|
db_host.name, db_group.name)
|
||||||
|
|
||||||
# Update/overwrite variables from "all" group. If importing from a
|
# Update/overwrite variables from "all" group. If importing from a
|
||||||
@@ -563,9 +570,9 @@ class Command(NoArgsCommand):
|
|||||||
all_obj.variables = json.dumps(db_variables)
|
all_obj.variables = json.dumps(db_variables)
|
||||||
all_obj.save(update_fields=['variables'])
|
all_obj.save(update_fields=['variables'])
|
||||||
if self.overwrite_vars or self.overwrite:
|
if self.overwrite_vars or self.overwrite:
|
||||||
self.logger.info('Replaced %s variables from "all" group', all_name)
|
self.logger.info('%s variables replaced from "all" group', all_name.capitalize())
|
||||||
else:
|
else:
|
||||||
self.logger.info('Updated %s variables from "all" group', all_name)
|
self.logger.info('%s variables updated from "all" group', all_name.capitalize())
|
||||||
else:
|
else:
|
||||||
self.logger.info('%s variables unmodified', all_name.capitalize())
|
self.logger.info('%s variables unmodified', all_name.capitalize())
|
||||||
|
|
||||||
@@ -581,7 +588,7 @@ class Command(NoArgsCommand):
|
|||||||
group, created = self.inventory.groups.get_or_create(name=k,
|
group, created = self.inventory.groups.get_or_create(name=k,
|
||||||
defaults=defaults)
|
defaults=defaults)
|
||||||
if created:
|
if created:
|
||||||
self.logger.info('Added new group "%s"', k)
|
self.logger.info('Group "%s" added', k)
|
||||||
else:
|
else:
|
||||||
db_variables = group.variables_dict
|
db_variables = group.variables_dict
|
||||||
if self.overwrite_vars or self.overwrite:
|
if self.overwrite_vars or self.overwrite:
|
||||||
@@ -592,11 +599,11 @@ class Command(NoArgsCommand):
|
|||||||
group.variables = json.dumps(db_variables)
|
group.variables = json.dumps(db_variables)
|
||||||
group.save(update_fields=['variables'])
|
group.save(update_fields=['variables'])
|
||||||
if self.overwrite_vars or self.overwrite:
|
if self.overwrite_vars or self.overwrite:
|
||||||
self.logger.info('Replaced variables for group "%s"', k)
|
self.logger.info('Group "%s" variables replaced', k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Updated variables for group "%s"', k)
|
self.logger.info('Group "%s" variables updated', k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Variables unmodified for group "%s"', k)
|
self.logger.info('Group "%s" variables unmodified', k)
|
||||||
if self.inventory_source.group:
|
if self.inventory_source.group:
|
||||||
self.inventory_source.group.children.add(group)
|
self.inventory_source.group.children.add(group)
|
||||||
group.inventory_sources.add(self.inventory_source)
|
group.inventory_sources.add(self.inventory_source)
|
||||||
@@ -608,25 +615,49 @@ class Command(NoArgsCommand):
|
|||||||
for k,v in self.all_group.all_hosts.iteritems():
|
for k,v in self.all_group.all_hosts.iteritems():
|
||||||
variables = json.dumps(v.variables)
|
variables = json.dumps(v.variables)
|
||||||
defaults = dict(variables=variables, description='imported')
|
defaults = dict(variables=variables, description='imported')
|
||||||
|
enabled = None
|
||||||
|
if self.enabled_var and self.enabled_var in v.variables:
|
||||||
|
value = v.variables[self.enabled_var]
|
||||||
|
if self.enabled_value is not None:
|
||||||
|
enabled = bool(unicode(self.enabled_value) == unicode(value))
|
||||||
|
else:
|
||||||
|
enabled = bool(value)
|
||||||
|
defaults['enabled'] = enabled
|
||||||
host, created = self.inventory.hosts.get_or_create(name=k,
|
host, created = self.inventory.hosts.get_or_create(name=k,
|
||||||
defaults=defaults)
|
defaults=defaults)
|
||||||
if created:
|
if created:
|
||||||
self.logger.info('Added new host "%s"', k)
|
if enabled is False:
|
||||||
|
self.logger.info('Host "%s" added (disabled)', k)
|
||||||
|
else:
|
||||||
|
self.logger.info('Host "%s" added', k)
|
||||||
|
#self.logger.info('Host variables: %s', variables)
|
||||||
else:
|
else:
|
||||||
db_variables = host.variables_dict
|
db_variables = host.variables_dict
|
||||||
if self.overwrite_vars or self.overwrite:
|
if self.overwrite_vars or self.overwrite:
|
||||||
db_variables = v.variables
|
db_variables = v.variables
|
||||||
else:
|
else:
|
||||||
db_variables.update(v.variables)
|
db_variables.update(v.variables)
|
||||||
|
update_fields = []
|
||||||
if db_variables != host.variables_dict:
|
if db_variables != host.variables_dict:
|
||||||
host.variables = json.dumps(db_variables)
|
host.variables = json.dumps(db_variables)
|
||||||
host.save(update_fields=['variables'])
|
update_fields.append('variables')
|
||||||
|
if enabled is not None and host.enabled != enabled:
|
||||||
|
host.enabled = enabled
|
||||||
|
update_fields.append('enabled')
|
||||||
|
if update_fields:
|
||||||
|
host.save(update_fields=update_fields)
|
||||||
|
if 'variables' in update_fields:
|
||||||
if self.overwrite_vars or self.overwrite:
|
if self.overwrite_vars or self.overwrite:
|
||||||
self.logger.info('Replaced variables for host "%s"', k)
|
self.logger.info('Host "%s" variables replaced', k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Updated variables for host "%s"', k)
|
self.logger.info('Host "%s" variables updated', k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Variables unmodified for host "%s"', k)
|
self.logger.info('Host "%s" variables unmodified', k)
|
||||||
|
if 'enabled' in update_fields:
|
||||||
|
if enabled:
|
||||||
|
self.logger.info('Host "%s" is now enabled', k)
|
||||||
|
else:
|
||||||
|
self.logger.info('Host "%s" is now disabled', k)
|
||||||
if self.inventory_source.group:
|
if self.inventory_source.group:
|
||||||
self.inventory_source.group.hosts.add(host)
|
self.inventory_source.group.hosts.add(host)
|
||||||
host.inventory_sources.add(self.inventory_source)
|
host.inventory_sources.add(self.inventory_source)
|
||||||
@@ -642,7 +673,7 @@ class Command(NoArgsCommand):
|
|||||||
db_host = self.inventory.hosts.get(name=h.name)
|
db_host = self.inventory.hosts.get(name=h.name)
|
||||||
if db_host not in db_group.hosts.all():
|
if db_host not in db_group.hosts.all():
|
||||||
db_group.hosts.add(db_host)
|
db_group.hosts.add(db_host)
|
||||||
self.logger.info('Added host "%s" to group "%s"', h.name, k)
|
self.logger.info('Host "%s" added to group "%s"', h.name, k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Host "%s" already in group "%s"', h.name, k)
|
self.logger.info('Host "%s" already in group "%s"', h.name, k)
|
||||||
|
|
||||||
@@ -655,7 +686,7 @@ class Command(NoArgsCommand):
|
|||||||
db_child = self.inventory.groups.get(name=g.name)
|
db_child = self.inventory.groups.get(name=g.name)
|
||||||
if db_child not in db_group.hosts.all():
|
if db_child not in db_group.hosts.all():
|
||||||
db_group.children.add(db_child)
|
db_group.children.add(db_child)
|
||||||
self.logger.info('Added group "%s" as child of "%s"', g.name, k)
|
self.logger.info('Group "%s" added as child of "%s"', g.name, k)
|
||||||
else:
|
else:
|
||||||
self.logger.info('Group "%s" already child of group "%s"', g.name, k)
|
self.logger.info('Group "%s" already child of group "%s"', g.name, k)
|
||||||
|
|
||||||
@@ -686,6 +717,8 @@ class Command(NoArgsCommand):
|
|||||||
self.overwrite_vars = bool(options.get('overwrite_vars', False))
|
self.overwrite_vars = bool(options.get('overwrite_vars', False))
|
||||||
self.keep_vars = bool(options.get('keep_vars', False))
|
self.keep_vars = bool(options.get('keep_vars', False))
|
||||||
self.source = options.get('source', None)
|
self.source = options.get('source', None)
|
||||||
|
self.enabled_var = options.get('enabled_var', None)
|
||||||
|
self.enabled_value = options.get('enabled_value', None)
|
||||||
|
|
||||||
# Load inventory and related objects from database.
|
# Load inventory and related objects from database.
|
||||||
if self.inventory_name and self.inventory_id:
|
if self.inventory_name and self.inventory_id:
|
||||||
|
|||||||
@@ -796,7 +796,6 @@ class RunInventoryUpdate(BaseTask):
|
|||||||
'''
|
'''
|
||||||
Build command line argument list for running inventory import.
|
Build command line argument list for running inventory import.
|
||||||
'''
|
'''
|
||||||
# FIXME
|
|
||||||
inventory_source = inventory_update.inventory_source
|
inventory_source = inventory_update.inventory_source
|
||||||
inventory = inventory_source.group.inventory
|
inventory = inventory_source.group.inventory
|
||||||
args = ['awx-manage', 'inventory_import']
|
args = ['awx-manage', 'inventory_import']
|
||||||
@@ -809,9 +808,15 @@ class RunInventoryUpdate(BaseTask):
|
|||||||
if inventory_source.source == 'ec2':
|
if inventory_source.source == 'ec2':
|
||||||
ec2_path = self.get_path_to('..', 'plugins', 'inventory', 'ec2.py')
|
ec2_path = self.get_path_to('..', 'plugins', 'inventory', 'ec2.py')
|
||||||
args.append(ec2_path)
|
args.append(ec2_path)
|
||||||
|
args.extend(['--enabled-var', 'ec2_state'])
|
||||||
|
args.extend(['--enabled-value', 'running'])
|
||||||
|
#args.extend(['--instance-id', 'ec2_id'])
|
||||||
elif inventory_source.source == 'rackspace':
|
elif inventory_source.source == 'rackspace':
|
||||||
rax_path = self.get_path_to('..', 'plugins', 'inventory', 'rax.py')
|
rax_path = self.get_path_to('..', 'plugins', 'inventory', 'rax.py')
|
||||||
args.append(rax_path)
|
args.append(rax_path)
|
||||||
|
args.extend(['--enabled-var', 'rax_status'])
|
||||||
|
args.extend(['--enabled-value', 'ACTIVE'])
|
||||||
|
#args.extend(['--instance-id', 'rax_id'])
|
||||||
elif inventory_source.source == 'file':
|
elif inventory_source.source == 'file':
|
||||||
args.append(inventory_source.source_path)
|
args.append(inventory_source.source_path)
|
||||||
verbosity = getattr(settings, 'INVENTORY_UPDATE_VERBOSITY', 1)
|
verbosity = getattr(settings, 'INVENTORY_UPDATE_VERBOSITY', 1)
|
||||||
|
|||||||
@@ -1006,12 +1006,13 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
pass # If should_fail is None, we don't care.
|
pass # If should_fail is None, we don't care.
|
||||||
return inventory_update
|
return inventory_update
|
||||||
|
|
||||||
def check_inventory_source(self, inventory_source):
|
def check_inventory_source(self, inventory_source, initial=True):
|
||||||
inventory_source = InventorySource.objects.get(pk=inventory_source.pk)
|
inventory_source = InventorySource.objects.get(pk=inventory_source.pk)
|
||||||
inventory = inventory_source.group.inventory
|
inventory = inventory_source.group.inventory
|
||||||
self.assertTrue(inventory_source.can_update)
|
self.assertTrue(inventory_source.can_update)
|
||||||
self.assertEqual(inventory.groups.count(), 1)
|
if initial:
|
||||||
self.assertEqual(inventory.hosts.count(), 0)
|
self.assertEqual(inventory.groups.count(), 1)
|
||||||
|
self.assertEqual(inventory.hosts.count(), 0)
|
||||||
inventory_update = self.check_inventory_update(inventory_source)
|
inventory_update = self.check_inventory_update(inventory_source)
|
||||||
inventory_source = InventorySource.objects.get(pk=inventory_source.pk)
|
inventory_source = InventorySource.objects.get(pk=inventory_source.pk)
|
||||||
self.assertNotEqual(inventory.groups.count(), 1)
|
self.assertNotEqual(inventory.groups.count(), 1)
|
||||||
@@ -1020,6 +1021,7 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
source_pks = host.inventory_sources.values_list('pk', flat=True)
|
source_pks = host.inventory_sources.values_list('pk', flat=True)
|
||||||
self.assertTrue(inventory_source.pk in source_pks)
|
self.assertTrue(inventory_source.pk in source_pks)
|
||||||
self.assertTrue(host.has_inventory_sources)
|
self.assertTrue(host.has_inventory_sources)
|
||||||
|
self.assertTrue(host.enabled)
|
||||||
for group in inventory.groups.all():
|
for group in inventory.groups.all():
|
||||||
source_pks = group.inventory_sources.values_list('pk', flat=True)
|
source_pks = group.inventory_sources.values_list('pk', flat=True)
|
||||||
self.assertTrue(inventory_source.pk in source_pks)
|
self.assertTrue(inventory_source.pk in source_pks)
|
||||||
@@ -1042,6 +1044,11 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
source='ec2', credential=credential, source_regions=source_regions,
|
source='ec2', credential=credential, source_regions=source_regions,
|
||||||
source_vars='---')
|
source_vars='---')
|
||||||
self.check_inventory_source(inventory_source)
|
self.check_inventory_source(inventory_source)
|
||||||
|
# Manually disable all hosts, verify a new update re-enables them.
|
||||||
|
for host in self.inventory.hosts.all():
|
||||||
|
host.enabled = False
|
||||||
|
host.save()
|
||||||
|
self.check_inventory_source(inventory_source, initial=False)
|
||||||
|
|
||||||
def test_update_from_rackspace(self):
|
def test_update_from_rackspace(self):
|
||||||
source_username = getattr(settings, 'TEST_RACKSPACE_USERNAME', '')
|
source_username = getattr(settings, 'TEST_RACKSPACE_USERNAME', '')
|
||||||
@@ -1057,6 +1064,11 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
source='rackspace', credential=credential,
|
source='rackspace', credential=credential,
|
||||||
source_regions=source_regions)
|
source_regions=source_regions)
|
||||||
self.check_inventory_source(inventory_source)
|
self.check_inventory_source(inventory_source)
|
||||||
|
# Manually disable all hosts, verify a new update re-enables them.
|
||||||
|
for host in self.inventory.hosts.all():
|
||||||
|
host.enabled = False
|
||||||
|
host.save()
|
||||||
|
self.check_inventory_source(inventory_source, initial=False)
|
||||||
# If test source regions is given, test again with empty string.
|
# If test source regions is given, test again with empty string.
|
||||||
if source_regions:
|
if source_regions:
|
||||||
inventory_source2 = self.update_inventory_source(self.group2,
|
inventory_source2 = self.update_inventory_source(self.group2,
|
||||||
|
|||||||
@@ -497,7 +497,13 @@ class Ec2Inventory(object):
|
|||||||
key = self.to_safe('ec2_' + key)
|
key = self.to_safe('ec2_' + key)
|
||||||
|
|
||||||
# Handle complex types
|
# Handle complex types
|
||||||
if type(value) in [int, bool]:
|
if key == 'ec2__state':
|
||||||
|
instance_vars['ec2_state'] = instance.state or ''
|
||||||
|
instance_vars['ec2_state_code'] = instance.state_code
|
||||||
|
elif key == 'ec2__previous_state':
|
||||||
|
instance_vars['ec2_previous_state'] = instance.previous_state or ''
|
||||||
|
instance_vars['ec2_previous_state_code'] = instance.previous_state_code
|
||||||
|
elif type(value) in [int, bool]:
|
||||||
instance_vars[key] = value
|
instance_vars[key] = value
|
||||||
elif type(value) in [str, unicode]:
|
elif type(value) in [str, unicode]:
|
||||||
instance_vars[key] = value.strip()
|
instance_vars[key] = value.strip()
|
||||||
@@ -518,7 +524,7 @@ class Ec2Inventory(object):
|
|||||||
instance_vars["ec2_security_group_ids"] = ','.join(group_ids)
|
instance_vars["ec2_security_group_ids"] = ','.join(group_ids)
|
||||||
instance_vars["ec2_security_group_names"] = ','.join(group_names)
|
instance_vars["ec2_security_group_names"] = ','.join(group_names)
|
||||||
else:
|
else:
|
||||||
pass
|
pass#instance_vars[key] = u'FIXME: ' + unicode(value)
|
||||||
# TODO Product codes if someone finds them useful
|
# TODO Product codes if someone finds them useful
|
||||||
#print key
|
#print key
|
||||||
#print type(value)
|
#print type(value)
|
||||||
|
|||||||
Reference in New Issue
Block a user