mirror of
https://github.com/ansible/awx.git
synced 2026-02-28 00:08:44 -03:30
Fixes https://trello.com/c/rHOgeb2W - Remove temp cache dir created for EC2 inventory sync.
This commit is contained in:
@@ -12,6 +12,7 @@ import os
|
|||||||
import signal
|
import signal
|
||||||
import pipes
|
import pipes
|
||||||
import re
|
import re
|
||||||
|
import shutil
|
||||||
import stat
|
import stat
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
@@ -210,13 +211,21 @@ class BaseTask(Task):
|
|||||||
for this task.
|
for this task.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def build_private_data_dir(self, instance, **kwargs):
|
||||||
|
'''
|
||||||
|
Create a temporary directory for job-related files.
|
||||||
|
'''
|
||||||
|
path = tempfile.mkdtemp(prefix='ansible_tower_')
|
||||||
|
os.chmod(path, stat.S_IRUSR|stat.S_IWUSR|stat.S_IXUSR)
|
||||||
|
return path
|
||||||
|
|
||||||
def build_private_data_file(self, instance, **kwargs):
|
def build_private_data_file(self, instance, **kwargs):
|
||||||
'''
|
'''
|
||||||
Create a temporary file containing the private data.
|
Create a temporary file containing the private data.
|
||||||
'''
|
'''
|
||||||
private_data = self.build_private_data(instance, **kwargs)
|
private_data = self.build_private_data(instance, **kwargs)
|
||||||
if private_data is not None:
|
if private_data is not None:
|
||||||
handle, path = tempfile.mkstemp()
|
handle, path = tempfile.mkstemp(dir=kwargs.get('private_data_dir', None))
|
||||||
f = os.fdopen(handle, 'w')
|
f = os.fdopen(handle, 'w')
|
||||||
f.write(private_data)
|
f.write(private_data)
|
||||||
f.close()
|
f.close()
|
||||||
@@ -383,6 +392,7 @@ class BaseTask(Task):
|
|||||||
raise RuntimeError('not starting %s task' % instance.status)
|
raise RuntimeError('not starting %s task' % instance.status)
|
||||||
# Fetch ansible version once here to support version-dependent features.
|
# Fetch ansible version once here to support version-dependent features.
|
||||||
kwargs['ansible_version'] = get_ansible_version()
|
kwargs['ansible_version'] = get_ansible_version()
|
||||||
|
kwargs['private_data_dir'] = self.build_private_data_dir(instance, **kwargs)
|
||||||
kwargs['private_data_file'] = self.build_private_data_file(instance, **kwargs)
|
kwargs['private_data_file'] = self.build_private_data_file(instance, **kwargs)
|
||||||
kwargs['passwords'] = self.build_passwords(instance, **kwargs)
|
kwargs['passwords'] = self.build_passwords(instance, **kwargs)
|
||||||
args = self.build_args(instance, **kwargs)
|
args = self.build_args(instance, **kwargs)
|
||||||
@@ -402,9 +412,9 @@ class BaseTask(Task):
|
|||||||
if status != 'canceled':
|
if status != 'canceled':
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
finally:
|
finally:
|
||||||
if kwargs.get('private_data_file', ''):
|
if kwargs.get('private_data_dir', ''):
|
||||||
try:
|
try:
|
||||||
os.remove(kwargs['private_data_file'])
|
shutil.rmtree(kwargs['private_data_dir'], True)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
@@ -846,7 +856,9 @@ class RunInventoryUpdate(BaseTask):
|
|||||||
ec2_opts.setdefault('all_rds_instances', 'False')
|
ec2_opts.setdefault('all_rds_instances', 'False')
|
||||||
ec2_opts.setdefault('rds', 'False')
|
ec2_opts.setdefault('rds', 'False')
|
||||||
ec2_opts.setdefault('nested_groups', 'True')
|
ec2_opts.setdefault('nested_groups', 'True')
|
||||||
ec2_opts.setdefault('cache_path', tempfile.mkdtemp(prefix='awx_ec2_'))
|
if 'cache_path' not in ec2_opts:
|
||||||
|
cache_path = tempfile.mkdtemp(prefix='ec2_cache', dir=kwargs.get('private_data_dir', None))
|
||||||
|
ec2_opts['cache_path'] = cache_path
|
||||||
ec2_opts.setdefault('cache_max_age', '300')
|
ec2_opts.setdefault('cache_max_age', '300')
|
||||||
for k,v in ec2_opts.items():
|
for k,v in ec2_opts.items():
|
||||||
cp.set(section, k, str(v))
|
cp.set(section, k, str(v))
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
# Python
|
# Python
|
||||||
import datetime
|
import datetime
|
||||||
|
import glob
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@@ -1454,6 +1455,8 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
group.name = 'AWS Inventory'
|
group.name = 'AWS Inventory'
|
||||||
group.save()
|
group.save()
|
||||||
self.group = group
|
self.group = group
|
||||||
|
cache_path_pattern = os.path.join(tempfile.gettempdir(), 'awx_ec2_*')
|
||||||
|
old_cache_paths = set(glob.glob(cache_path_pattern))
|
||||||
inventory_source = self.update_inventory_source(self.group,
|
inventory_source = self.update_inventory_source(self.group,
|
||||||
source='ec2', credential=credential, source_regions=source_regions,
|
source='ec2', credential=credential, source_regions=source_regions,
|
||||||
source_vars='---') # nested_groups is true by default.
|
source_vars='---') # nested_groups is true by default.
|
||||||
@@ -1475,8 +1478,12 @@ class InventoryUpdatesTest(BaseTransactionTest):
|
|||||||
self.assertTrue('keys' in child_names)
|
self.assertTrue('keys' in child_names)
|
||||||
self.assertTrue('security_groups' in child_names)
|
self.assertTrue('security_groups' in child_names)
|
||||||
self.assertTrue('tags' in child_names)
|
self.assertTrue('tags' in child_names)
|
||||||
# Print out group/host tree for debugging.
|
# Make sure we clean up the cache path when finished (when one is not
|
||||||
|
# provided explicitly via source_vars).
|
||||||
|
new_cache_paths = set(glob.glob(cache_path_pattern))
|
||||||
|
self.assertEqual(old_cache_paths, new_cache_paths)
|
||||||
return
|
return
|
||||||
|
# Print out group/host tree for debugging.
|
||||||
print
|
print
|
||||||
def draw_tree(g, d=0):
|
def draw_tree(g, d=0):
|
||||||
print (' ' * d) + '+ ' + g.name
|
print (' ' * d) + '+ ' + g.name
|
||||||
|
|||||||
Reference in New Issue
Block a user