def validate(self): super(RemoteStack, self).validate() try: self.heat() except Exception as ex: exc_info = dict(region=self._region_name, exc=six.text_type(ex)) msg = _('Cannot establish connection to Heat endpoint at region ' '"%(region)s" due to "%(exc)s"') % exc_info raise exception.StackValidationFailed(message=msg) try: params = self.properties[self.PARAMETERS] env = environment.get_child_environment(self.stack.env, params) tmpl = template_format.parse(self.properties[self.TEMPLATE]) args = { 'template': tmpl, 'files': self.stack.t.files, 'environment': env.user_env_as_dict(), } self.heat().stacks.validate(**args) except Exception as ex: exc_info = dict(region=self._region_name, exc=six.text_type(ex)) LOG.error(_LE('exception: %s'), type(ex)) msg = _('Failed validating stack template using Heat endpoint at ' 'region "%(region)s" due to "%(exc)s"') % exc_info raise exception.StackValidationFailed(message=msg)
def generate_class_from_template(name, data, env): tmpl = template.Template(template_format.parse(data)) props, attrs = TemplateResource.get_schemas(tmpl, env.param_defaults) cls = type(name, (TemplateResource, ), { 'properties_schema': props, 'attributes_schema': attrs }) return cls
def get_parsed_template(self): if cfg.CONF.loadbalancer_template: with open(cfg.CONF.loadbalancer_template) as templ_fd: LOG.info(_LI('Using custom loadbalancer template %s'), cfg.CONF.loadbalancer_template) contents = templ_fd.read() else: contents = lb_template_default return template_format.parse(contents)
def child_template(self): try: template_data = urlfetch.get(self.properties[self.TEMPLATE_URL]) except (exceptions.RequestException, IOError) as r_exc: raise ValueError( _("Could not fetch remote template '%(url)s': " "%(exc)s") % { 'url': self.properties[self.TEMPLATE_URL], 'exc': r_exc }) return template_format.parse(template_data)
def handle_create(self): params = self.properties[self.PARAMETERS] env = environment.get_child_environment(self.stack.env, params) tmpl = template_format.parse(self.properties[self.TEMPLATE]) args = { 'stack_name': self.physical_resource_name_or_FnGetRefId(), 'template': tmpl, 'timeout_mins': self.properties[self.TIMEOUT], 'disable_rollback': True, 'parameters': params, 'files': self.stack.t.files, 'environment': env.user_env_as_dict(), } remote_stack_id = self.heat().stacks.create(**args)['stack']['id'] self.resource_id_set(remote_stack_id)
def update_with_template(self, child_template, user_params=None, timeout_mins=None): """Update the nested stack with the new template.""" if self.id is None: self._store() nested_stack = self.nested() if nested_stack is None: # if the create failed for some reason and the nested # stack was not created, we need to create an empty stack # here so that the update will work. def _check_for_completion(): while not self.check_create_complete(): yield empty_temp = template_format.parse( "heat_template_version: '2013-05-23'") self.create_with_template(empty_temp, {}) checker = scheduler.TaskRunner(_check_for_completion) checker(timeout=self.stack.timeout_secs()) nested_stack = self.nested() if timeout_mins is None: timeout_mins = self.stack.timeout_mins kwargs = self._stack_kwargs(user_params, child_template) cookie = { 'previous': { 'updated_at': nested_stack.updated_time, 'state': nested_stack.state } } kwargs.update({ 'stack_identity': dict(nested_stack.identifier()), 'args': { rpc_api.PARAM_TIMEOUT: timeout_mins } }) with self.translate_remote_exceptions: self.rpc_client().update_stack(self.context, **kwargs) return cookie
def handle_update(self, json_snippet, tmpl_diff, prop_diff): # Nested stack template may be changed even if the prop_diff is empty. self.properties = json_snippet.properties(self.properties_schema, self.context) try: template_data = urlfetch.get(self.properties[self.TEMPLATE_URL]) except (exceptions.RequestException, IOError) as r_exc: raise ValueError( _("Could not fetch remote template '%(url)s': " "%(exc)s") % { 'url': self.properties[self.TEMPLATE_URL], 'exc': r_exc }) template = template_format.parse(template_data) return self.update_with_template(template, self.properties[self.PARAMETERS], self.properties[self.TIMEOUT_IN_MINS])
def template(self): """Get template file contents. Get template file contents, either inline, from stack adopt data or from a URL, in JSON or YAML format. """ template_data = None if rpc_api.PARAM_ADOPT_STACK_DATA in self.data: adopt_data = self.data[rpc_api.PARAM_ADOPT_STACK_DATA] try: adopt_data = template_format.simple_parse(adopt_data) template_format.validate_template_limit( six.text_type(adopt_data['template'])) return adopt_data['template'] except (ValueError, KeyError) as ex: err_reason = _('Invalid adopt data: %s') % ex raise exc.HTTPBadRequest(err_reason) elif self.PARAM_TEMPLATE in self.data: template_data = self.data[self.PARAM_TEMPLATE] if isinstance(template_data, dict): template_format.validate_template_limit(six.text_type( template_data)) return template_data elif self.PARAM_TEMPLATE_URL in self.data: url = self.data[self.PARAM_TEMPLATE_URL] LOG.debug('TemplateUrl %s' % url) try: template_data = urlfetch.get(url) except IOError as ex: err_reason = _('Could not retrieve template: %s') % ex raise exc.HTTPBadRequest(err_reason) if template_data is None: if self.patch: return None else: raise exc.HTTPBadRequest(_("No template specified")) with self.parse_error_check('Template'): return template_format.parse(template_data)
def template(self): """Get template file contents. Get template file contents, either inline, from stack adopt data or from a URL, in JSON or YAML format. """ template_data = None if rpc_api.PARAM_ADOPT_STACK_DATA in self.data: adopt_data = self.data[rpc_api.PARAM_ADOPT_STACK_DATA] try: adopt_data = template_format.simple_parse(adopt_data) template_format.validate_template_limit( six.text_type(adopt_data['template'])) return adopt_data['template'] except (ValueError, KeyError) as ex: err_reason = _('Invalid adopt data: %s') % ex raise exc.HTTPBadRequest(err_reason) elif self.PARAM_TEMPLATE in self.data: template_data = self.data[self.PARAM_TEMPLATE] if isinstance(template_data, dict): template_format.validate_template_limit( six.text_type(template_data)) return template_data elif self.PARAM_TEMPLATE_URL in self.data: url = self.data[self.PARAM_TEMPLATE_URL] LOG.debug('TemplateUrl %s' % url) try: template_data = urlfetch.get(url) except IOError as ex: err_reason = _('Could not retrieve template: %s') % ex raise exc.HTTPBadRequest(err_reason) if template_data is None: if self.patch: return None else: raise exc.HTTPBadRequest(_("No template specified")) with self.parse_error_check('Template'): return template_format.parse(template_data)
def handle_update(self, json_snippet, tmpl_diff, prop_diff): # Always issue an update to the remote stack and let the individual # resources in it decide if they need updating. if self.resource_id: snippet = json_snippet.get('Properties', {}) self.properties = properties.Properties(self.properties_schema, snippet, function.resolve, self.name) params = self.properties[self.PARAMETERS] env = environment.get_child_environment(self.stack.env, params) tmpl = template_format.parse(self.properties[self.TEMPLATE]) fields = { 'stack_id': self.resource_id, 'parameters': params, 'template': tmpl, 'timeout_mins': self.properties[self.TIMEOUT], 'disable_rollback': self.stack.disable_rollback, 'files': self.stack.t.files, 'environment': env.user_env_as_dict(), } self.heat().stacks.update(**fields)
def update_with_template(self, child_template, user_params=None, timeout_mins=None): """Update the nested stack with the new template.""" if self.id is None: self._store() nested_stack = self.nested() if nested_stack is None: # if the create failed for some reason and the nested # stack was not created, we need to create an empty stack # here so that the update will work. def _check_for_completion(): while not self.check_create_complete(): yield empty_temp = template_format.parse( "heat_template_version: '2013-05-23'") self.create_with_template(empty_temp, {}) checker = scheduler.TaskRunner(_check_for_completion) checker(timeout=self.stack.timeout_secs()) nested_stack = self.nested() if timeout_mins is None: timeout_mins = self.stack.timeout_mins kwargs = self._stack_kwargs(user_params, child_template) cookie = {'previous': { 'updated_at': nested_stack.updated_time, 'state': nested_stack.state}} kwargs.update({ 'stack_identity': dict(nested_stack.identifier()), 'args': {rpc_api.PARAM_TIMEOUT: timeout_mins} }) with self.translate_remote_exceptions: self.rpc_client().update_stack(self.context, **kwargs) return cookie
def child_template(self): if not self._parsed_nested: self._parsed_nested = template_format.parse(self.template_data()) return self._parsed_nested