Adds included-template-dir param for resolving template FileIncludes
authormarios <marios@redhat.com>
Fri, 13 Dec 2013 12:20:49 +0000 (14:20 +0200)
committermarios <marios@redhat.com>
Mon, 16 Dec 2013 15:02:48 +0000 (17:02 +0200)
In I587fa7a826f93f89e8e5c266af7f5765438fe738 there is a problem with
the overcloud.yaml Make target. Once the merge.py script is moved from
where the templates are, FileInclude paths cannot be resolved relative
to the script. The included-template-path defaults to './'

Change-Id: I220c5e52d8062e98aa28a1c582e29c7e844bc9ae

merge.py

index 7827f12..053a683 100644 (file)
--- a/merge.py
+++ b/merge.py
@@ -41,6 +41,7 @@ MERGABLE_TYPES = {'OS::Nova::Server':
                   'AWS::AutoScaling::LaunchConfiguration':
                   {},
                  }
+INCLUDED_TEMPLATE_DIR = os.getcwd()
 
 
 def resolve_includes(template, params=None):
@@ -85,12 +86,17 @@ def main(argv=None):
                         help='Translate slave_roles to this')
     parser.add_argument('--slave-roles', nargs='*',
                         help='Translate all of these to master_role')
+    parser.add_argument('--included-template-dir', nargs='?',
+                        default=INCLUDED_TEMPLATE_DIR,
+                        help='Path for resolving included templates')
     args = parser.parse_args(argv)
     templates = args.templates
-    merged_template = merge(templates, args.master_role, args.slave_roles)
+    merged_template = merge(templates, args.master_role, args.slave_roles,
+                            args.included_template_dir)
     sys.stdout.write(merged_template)
 
-def merge(templates, master_role=None, slave_roles=None):
+def merge(templates, master_role=None, slave_roles=None,
+          included_template_dir=INCLUDED_TEMPLATE_DIR):
     errors = []
     end_template={'HeatTemplateFormatVersion': '2012-12-12',
                   'Description': []}
@@ -156,13 +162,9 @@ def merge(templates, master_role=None, slave_roles=None):
                     end_template['Resources'][role]['Properties'][image_key] = {'Ref': ikey}
                     end_template['Parameters'][ikey] = ikey_val
             elif rbody['Type'] == 'FileInclude':
-                #make sure rbody['Path'] is absolute - required when this
-                #script invoked by import rather than command line
-                if os.path.dirname(rbody['Path']) == '':
-                    template_dir = os.path.dirname(__file__)
-                    filename = rbody['Path']
-                    rbody['Path'] = os.path.join(template_dir, filename)
-                with open(rbody['Path']) as rfile:
+                # we trust os.path.join to DTRT: if FileInclude path isn't
+                # absolute, join to included_template_dir (./)
+                with open(os.path.join(included_template_dir, rbody['Path'])) as rfile:
                     include_content = yaml.safe_load(rfile.read())
                     subkeys = rbody.get('SubKey','').split('.')
                     while len(subkeys) and subkeys[0]: