summaryrefslogtreecommitdiffstats
path: root/inventory
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2015-05-07 11:09:00 -0400
committerKenny Woodson <kwoodson@redhat.com>2015-05-07 14:34:58 -0400
commitb54e8f81469807cdd6cc57d3c03b22ee1212b4cc (patch)
treebffa2b1efd52913ac1a713ef9d882074fbd28efd /inventory
parent4c16fcb11043133e162f4c0a966032a175082c9a (diff)
downloadopenshift-b54e8f81469807cdd6cc57d3c03b22ee1212b4cc.tar.gz
openshift-b54e8f81469807cdd6cc57d3c03b22ee1212b4cc.tar.bz2
openshift-b54e8f81469807cdd6cc57d3c03b22ee1212b4cc.tar.xz
openshift-b54e8f81469807cdd6cc57d3c03b22ee1212b4cc.zip
Allow option in multi_ec2 to set cache location.
Diffstat (limited to 'inventory')
-rwxr-xr-xinventory/multi_ec2.py26
-rw-r--r--inventory/multi_ec2.yaml.example3
2 files changed, 27 insertions, 2 deletions
diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py
index b839a33ea..b99212dd5 100755
--- a/inventory/multi_ec2.py
+++ b/inventory/multi_ec2.py
@@ -11,9 +11,12 @@ import yaml
import os
import subprocess
import json
+import errno
+import fcntl
CONFIG_FILE_NAME = 'multi_ec2.yaml'
+DEFAULT_CACHE_PATH = os.path.expanduser('~/.ansible/tmp/multi_ec2_inventory.cache')
class MultiEc2(object):
'''
@@ -27,7 +30,6 @@ class MultiEc2(object):
self.config = None
self.all_ec2_results = {}
self.result = {}
- self.cache_path = os.path.expanduser('~/.ansible/tmp/multi_ec2_inventory.cache')
self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
same_dir_config_file = os.path.join(self.file_path, CONFIG_FILE_NAME)
@@ -48,10 +50,12 @@ class MultiEc2(object):
self.config = self.load_yaml_config()
elif os.environ.has_key("AWS_ACCESS_KEY_ID") and \
os.environ.has_key("AWS_SECRET_ACCESS_KEY"):
+ # Build a default config
self.config = {}
self.config['accounts'] = [
{
'name': 'default',
+ 'cache_location': DEFAULT_CACHE_PATH,
'provider': 'aws/hosts/ec2.py',
'env_vars': {
'AWS_ACCESS_KEY_ID': os.environ["AWS_ACCESS_KEY_ID"],
@@ -64,6 +68,11 @@ class MultiEc2(object):
else:
raise RuntimeError("Could not find valid ec2 credentials in the environment.")
+ # Set the default cache path but if its defined we'll assign it.
+ self.cache_path = DEFAULT_CACHE_PATH
+ if self.config.has_key('cache_location'):
+ self.cache_path = self.config['cache_location']
+
if self.args.refresh_cache:
self.get_inventory()
self.write_to_cache()
@@ -222,9 +231,22 @@ class MultiEc2(object):
def write_to_cache(self):
''' Writes data in JSON format to a file '''
+ # if it does not exist, try and create it.
+ if not os.path.isfile(self.cache_path):
+ path = os.path.dirname(self.cache_path)
+ try:
+ os.makedirs(path)
+ except OSError as exc:
+ if exc.errno != errno.EEXIST or not os.path.isdir(path):
+ raise
+
json_data = MultiEc2.json_format_dict(self.result, True)
with open(self.cache_path, 'w') as cache:
- cache.write(json_data)
+ try:
+ fcntl.flock(cache, fcntl.LOCK_EX)
+ cache.write(json_data)
+ finally:
+ fcntl.flock(cache, fcntl.LOCK_UN)
def get_inventory_from_cache(self):
''' Reads the inventory from the cache file and returns it as a JSON
diff --git a/inventory/multi_ec2.yaml.example b/inventory/multi_ec2.yaml.example
index 91e7c7970..d8361a49f 100644
--- a/inventory/multi_ec2.yaml.example
+++ b/inventory/multi_ec2.yaml.example
@@ -1,4 +1,7 @@
# multi ec2 inventory configs
+#
+cache_location: ~/.ansible/tmp/multi_ec2_inventory.cache
+
accounts:
- name: aws1
provider: aws/hosts/ec2.py