- base_template = template_dir + template_base_name
- new_template = template_dir + template_name
- new_template += "_" + str(counter) + template_extension
- shutil.copy(base_template, new_template)
-
- metadata = dict()
- for var in heat_template_vars:
- if var.get_variable_name():
- common.replace_in_file(new_template, "#" +
- var.get_variable_name(),
- var.get_variable_value())
- metadata[var.get_variable_name()] = var.get_variable_value()
-
- # Save the metadata on a JSON file
- with open(new_template + ".json", 'w') as outfile:
- json.dump(metadata, outfile)
-
- common.LOG.debug("Heat Templates and Metadata file " + str(counter) +
- " created")
- counter += 1
-
- # Creation of the template files
- common.LOG.info(str(counter - 1) + " Heat Templates and Metadata files "
- "created")
-
-
-def get_all_heat_templates(template_dir, template_extension):
- template_files = list()
- for dirname, dirnames, filenames in os.walk(template_dir):
- for filename in filenames:
- if template_extension in filename and filename.endswith(
- template_extension) and template_name in filename:
- template_files.append(filename)
- template_files.sort()
- return template_files
+ if heat_template:
+ self._template = heat_template
+ else:
+ sys.exit("can't init template file!")
+
+ # holds results of requested output after deployment
+ self.outputs = {}
+
+ log.debug("template object '%s' created", name)
+
+ def create(self, block=True):
+ '''creates a template in the target cloud using heat
+ returns a dict with the requested output values from the template'''
+ log.info("Creating stack '%s'", self.name)
+
+ # create stack early to support cleanup, e.g. ctrl-c while waiting
+ stack = HeatStack(self.name)
+
+ heat = self._get_heat_client()
+ end_time = start_time = time.time()
+ print(self._template)
+ stack.uuid = self.uuid = heat.stacks.create(
+ stack_name=self.name, template=self._template,
+ parameters=self.heat_parameters)['stack']['id']
+
+ status = self.status()
+
+ if block:
+ while status != u'CREATE_COMPLETE':
+ log.debug("stack state %s", status)
+ if status == u'CREATE_FAILED':
+ raise RuntimeError(getattr(heat.stacks.get(self.uuid),
+ 'stack_status_reason'))
+
+ time.sleep(2)
+ status = self.status()
+
+ end_time = time.time()
+ outputs = getattr(heat.stacks.get(self.uuid), 'outputs')
+
+ for output in outputs:
+ self.outputs[output["output_key"].encode("ascii")] = \
+ output["output_value"].encode("ascii")
+
+ log.info("Created stack '%s' in %d secs",
+ self.name, end_time - start_time)
+
+ stack.outputs = self.outputs
+ return stack