def _j2_render_and_put(self, j2_template, j2_data, outfile_name=None, context=None): swift = self.get_object_client(context) yaml_f = outfile_name or j2_template.replace('.j2.yaml', '.yaml') # Search for templates relative to the current template path first template_base = os.path.dirname(yaml_f) j2_loader = J2SwiftLoader(swift, self.container, template_base) try: # Render the j2 template template = jinja2.Environment(loader=j2_loader).from_string( j2_template) r_template = template.render(**j2_data) except jinja2.exceptions.TemplateError as ex: error_msg = ("Error rendering template %s : %s" % (yaml_f, six.text_type(ex))) LOG.error(error_msg) raise Exception(error_msg) try: # write the template back to the plan container LOG.info("Writing rendered template %s" % yaml_f) swiftutils.put_object_string(swift, self.container, yaml_f, r_template) except swiftexceptions.ClientException: error_msg = ("Error storing file %s in container %s" % (yaml_f, self.container)) LOG.error(error_msg) raise Exception(error_msg)
def _j2_render_and_put(self, j2_template, j2_data, outfile_name=None, context=None): swift = self.get_object_client(context) yaml_f = outfile_name or j2_template.replace('.j2.yaml', '.yaml') # Search for templates relative to the current template path first template_base = os.path.dirname(yaml_f) j2_loader = J2SwiftLoader(swift, self.container, template_base) try: # Render the j2 template template = jinja2.Environment(loader=j2_loader).from_string( j2_template) r_template = template.render(**j2_data) except jinja2.exceptions.TemplateError as ex: error_msg = ("Error rendering template %s : %s" % (yaml_f, six.text_type(ex))) LOG.error(error_msg) raise Exception(error_msg) try: # write the template back to the plan container LOG.info("Writing rendered template %s" % yaml_f) swiftutils.put_object_string(swift, self.container, yaml_f, r_template) except swiftexceptions.ClientException as ex: error_msg = ("Error storing file %s in container %s" % (yaml_f, self.container)) LOG.error(error_msg) raise Exception(error_msg)
def run(self, context): params = default_image_params() swift = self.get_object_client(context) try: swiftutils.put_object_string( swift, self.container, constants.CONTAINER_DEFAULTS_ENVIRONMENT, yaml.safe_dump( {'parameter_defaults': params}, default_flow_style=False ) ) except swiftexceptions.ClientException as err: err_msg = ("Error updating %s for plan %s: %s" % ( constants.CONTAINER_DEFAULTS_ENVIRONMENT, self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) environments = {constants.CONTAINER_DEFAULTS_ENVIRONMENT: True} update_action = heat_capabilities.UpdateCapabilitiesAction( environments, container=self.container) return update_action.run(context)
def run(self, context): swift = self.get_object_client(context) swiftutils.create_container(swift, self.logging_container) self._rotate(swift) try: old_contents = swiftutils.get_object_string( swift, self.logging_container, constants.TRIPLEO_UI_LOG_FILENAME) new_contents = "%s\n%s" % (old_contents, self.logging_data) except swiftexceptions.ClientException: LOG.debug( "There is no existing logging data, starting a new file.") new_contents = self.logging_data try: swiftutils.put_object_string( swift, self.logging_container, constants.TRIPLEO_UI_LOG_FILENAME, new_contents) except swiftexceptions.ClientException as err: msg = "Failed to publish logs: %s" % err return actions.Result(error=msg)
def put_user_env(swift, container_name, env): """Convert given user environment to yaml and upload it to Swift.""" swiftutils.put_object_string( swift, container_name, constants.USER_ENVIRONMENT, yaml.safe_dump(env, default_flow_style=False) )
def put_env(swift, env): """Convert given environment to yaml and upload it to Swift.""" swiftutils.put_object_string( swift, env['name'], constants.PLAN_ENVIRONMENT, yaml.safe_dump(env, default_flow_style=False) )
def run(self, context): self.context = context swift = self.get_object_client(context) try: plan_env = plan_utils.get_env(swift, self.container) except swiftexceptions.ClientException as err: err_msg = ("Error retrieving environment for plan %s: %s" % ( self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) try: env_paths, temp_env_paths = plan_utils.build_env_paths( swift, self.container, plan_env) env_files, env = plan_utils.process_environments_and_files( swift, env_paths) # ensure every image parameter has a default value, even if prepare # didn't return it params = default_image_params() role_data = self._get_role_data(swift) image_params = kolla_builder.container_images_prepare_multi( env, role_data, dry_run=True) if image_params: params.update(image_params) except Exception as err: LOG.exception("Error occurred while processing plan files.") return actions.Result(error=six.text_type(err)) finally: # cleanup any local temp files for f in temp_env_paths: os.remove(f) try: swiftutils.put_object_string( swift, self.container, constants.CONTAINER_DEFAULTS_ENVIRONMENT, yaml.safe_dump( {'parameter_defaults': params}, default_flow_style=False ) ) except swiftexceptions.ClientException as err: err_msg = ("Error updating %s for plan %s: %s" % ( constants.CONTAINER_DEFAULTS_ENVIRONMENT, self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) environments = {constants.CONTAINER_DEFAULTS_ENVIRONMENT: True} update_action = heat_capabilities.UpdateCapabilitiesAction( environments, container=self.container) return update_action.run(context)
def run(self, context): self.context = context swift = self.get_object_client(context) try: plan_env = plan_utils.get_env(swift, self.container) except swiftexceptions.ClientException as err: err_msg = ("Error retrieving environment for plan %s: %s" % (self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) try: env_paths, temp_env_paths = plan_utils.build_env_paths( swift, self.container, plan_env) env_files, env = plan_utils.process_environments_and_files( swift, env_paths) # ensure every image parameter has a default value, even if prepare # didn't return it params = default_image_params() role_data = self._get_role_data(swift) image_params = kolla_builder.container_images_prepare_multi( env, role_data, dry_run=True) if image_params: params.update(image_params) except Exception as err: LOG.exception("Error occurred while processing plan files.") return actions.Result(error=six.text_type(err)) finally: # cleanup any local temp files for f in temp_env_paths: os.remove(f) try: swiftutils.put_object_string( swift, self.container, constants.CONTAINER_DEFAULTS_ENVIRONMENT, yaml.safe_dump({'parameter_defaults': params}, default_flow_style=False)) except swiftexceptions.ClientException as err: err_msg = ("Error updating %s for plan %s: %s" % (constants.CONTAINER_DEFAULTS_ENVIRONMENT, self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) environments = {constants.CONTAINER_DEFAULTS_ENVIRONMENT: True} update_action = heat_capabilities.UpdateCapabilitiesAction( environments, container=self.container) return update_action.run(context)
def run(self, context): try: swift = self.get_object_client(context) # Upload template dir to tmp container container_tmp = '%s-tmp' % self.container with tempfile.NamedTemporaryFile() as tmp_tarball: tarball.create_tarball(self.templates_dir, tmp_tarball.name) tarball.tarball_extract_to_swift_container( swift, tmp_tarball.name, container_tmp) # Get all new templates: new_templates = swiftutils.get_object_string(swift, container_tmp, '').splitlines() old_templates = swiftutils.get_object_string(swift, self.container, '').splitlines() exclude_user_data = [constants.PLAN_ENVIRONMENT, constants.OVERCLOUD_J2_ROLES_NAME, constants.OVERCLOUD_J2_NETWORKS_NAME, constants.OVERCLOUD_J2_EXCLUDES] # Update the old container for new in new_templates: # if doesn't exist, push it: if new not in old_templates: swiftutils.put_object_string( swift, self.container, new, swiftutils.get_object_string(swift, container_tmp, new) ) else: content_new = swiftutils.get_object_string(swift, container_tmp, new) content_old = swiftutils.get_object_string(swift, self.container, new) if (not content_new == content_old and new not in exclude_user_data): swiftutils.put_object_string( swift, self.container, new, swiftutils.get_object_string(swift, container_tmp, new) ) except swiftexceptions.ClientException as err: msg = "Error attempting an operation on container: %s" % err LOG.exception(msg) return actions.Result(error=msg) except Exception as err: msg = "Error while updating plan: %s" % err LOG.exception(msg) return actions.Result(error=msg)
def run(self, context): params = default_image_params() swift = self.get_object_client(context) try: swiftutils.put_object_string( swift, self.container, constants.CONTAINER_DEFAULTS_ENVIRONMENT, yaml.safe_dump({'parameter_defaults': params}, default_flow_style=False)) except swiftexceptions.ClientException as err: err_msg = ("Error updating %s for plan %s: %s" % (constants.CONTAINER_DEFAULTS_ENVIRONMENT, self.container, err)) LOG.exception(err_msg) return actions.Result(error=err_msg) environments = {constants.CONTAINER_DEFAULTS_ENVIRONMENT: True} update_action = heat_capabilities.UpdateCapabilitiesAction( environments, container=self.container) return update_action.run(context)
def run(self, context): swift = self.get_object_client(context) swiftutils.create_container(swift, self.logging_container) self._rotate(swift) try: old_contents = swiftutils.get_object_string( swift, self.logging_container, constants.TRIPLEO_UI_LOG_FILENAME) new_contents = "%s\n%s" % (old_contents, self.logging_data) except swiftexceptions.ClientException: LOG.debug( "There is no existing logging data, starting a new file.") new_contents = self.logging_data try: swiftutils.put_object_string(swift, self.logging_container, constants.TRIPLEO_UI_LOG_FILENAME, new_contents) except swiftexceptions.ClientException as err: msg = "Failed to publish logs: %s" % err return actions.Result(error=msg)
def test_put_object_string_from_bytes(self): put_mock = mock.MagicMock() self.swiftclient.put_object = put_mock swift_utils.put_object_string(self.swiftclient, 'foo', 'bar', b'foo') put_mock.assert_called_once_with('foo', 'bar', str('foo'))