def _read_temp(data): tout = StringIO() tout.write(data) tout.seek(0) output = tout.read().splitlines() # Discard newlines tout.close() return output
def _produce_output(report, failed, setup): ''' Produce output from the report dictionary generated by _generate_report ''' report_format = setup.get('report_format', 'yaml') log.debug('highstate output format: %s', report_format) if report_format == 'json': report_text = salt.utils.json.dumps(report) elif report_format == 'yaml': string_file = StringIO() salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False) string_file.seek(0) report_text = string_file.read() else: string_file = StringIO() _generate_html(report, string_file) string_file.seek(0) report_text = string_file.read() report_delivery = setup.get('report_delivery', 'file') log.debug('highstate report_delivery: %s', report_delivery) if report_delivery == 'file': output_file = _sprinkle(setup.get('file_output', '/tmp/test.rpt')) with salt.utils.files.fopen(output_file, 'w') as out: out.write(salt.utils.stringutils.to_str(report_text)) else: msg = MIMEText(report_text, report_format) sender = setup.get('smtp_sender', '') recipients = setup.get('smtp_recipients', '') if failed: subject = setup.get('smtp_failure_subject', 'Installation failure') else: subject = setup.get('smtp_success_subject', 'Installation success') subject = _sprinkle(subject) msg['Subject'] = subject msg['From'] = sender msg['To'] = recipients smtp = smtplib.SMTP(host=setup.get('smtp_server', '')) smtp.sendmail(sender, [x.strip() for x in recipients.split(',')], msg.as_string()) smtp.quit()
def _produce_output(report, failed, setup): """ Produce output from the report dictionary generated by _generate_report """ report_format = setup.get("report_format", "yaml") log.debug("highstate output format: %s", report_format) if report_format == "json": report_text = salt.utils.json.dumps(report) elif report_format == "yaml": string_file = StringIO() salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False) string_file.seek(0) report_text = string_file.read() else: string_file = StringIO() _generate_html(report, string_file) string_file.seek(0) report_text = string_file.read() report_delivery = setup.get("report_delivery", "file") log.debug("highstate report_delivery: %s", report_delivery) if report_delivery == "file": output_file = _sprinkle(setup.get("file_output", "/tmp/test.rpt")) with salt.utils.files.fopen(output_file, "w") as out: out.write(salt.utils.stringutils.to_str(report_text)) else: msg = MIMEText(report_text, report_format) sender = setup.get("smtp_sender", "") recipients = setup.get("smtp_recipients", "") if failed: subject = setup.get("smtp_failure_subject", "Installation failure") else: subject = setup.get("smtp_success_subject", "Installation success") subject = _sprinkle(subject) msg["Subject"] = subject msg["From"] = sender msg["To"] = recipients smtp = smtplib.SMTP(host=setup.get("smtp_server", "")) smtp.sendmail( sender, [x.strip() for x in recipients.split(",")], msg.as_string() ) smtp.quit()
def __test_html(): """ HTML generation test only used when called from the command line: python ./highstate.py Typical options for generating the report file: highstate: report_format: yaml report_delivery: file file_output: '/srv/salt/_returners/test.rpt' """ with salt.utils.files.fopen("test.rpt", "r") as input_file: data_text = salt.utils.stringutils.to_unicode(input_file.read()) data = salt.utils.yaml.safe_load(data_text) string_file = StringIO() _generate_html(data, string_file) string_file.seek(0) result = string_file.read() with salt.utils.files.fopen("test.html", "w") as output: output.write(salt.utils.stringutils.to_str(result))
def __test_html(): ''' HTML generation test only used when called from the command line: python ./highstate.py Typical options for generating the report file: highstate: report_format: yaml report_delivery: file file_output: '/srv/salt/_returners/test.rpt' ''' with salt.utils.fopen('test.rpt', 'r') as input_file: data_text = input_file.read() data = yaml.safe_load(data_text) string_file = StringIO() _generate_html(data, string_file) string_file.seek(0) result = string_file.read() with salt.utils.fopen('test.html', 'w') as output: output.write(result)
def compile_template(template, renderers, default, blacklist, whitelist, saltenv='base', sls='', input_data='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # if any error occurs, we return an empty dictionary ret = {} log.debug('compile template: %s', template) if 'env' in kwargs: # "env" is not supported; Use "saltenv". kwargs.pop('env') if template != ':string:': # Template was specified incorrectly if not isinstance(template, six.string_types): log.error('Template was specified incorrectly: %s', template) return ret # Template does not exist if not os.path.isfile(template): log.error('Template does not exist: %s', template) return ret # Template is an empty file if salt.utils.files.is_empty(template): log.debug('Template is an empty file: %s', template) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error('Template is nothing but whitespace: %s', template) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default, blacklist, whitelist, input_data) windows_newline = '\r\n' in input_data input_data = StringIO(input_data) for render, argline in render_pipe: if salt.utils.stringio.is_readable(input_data): input_data.seek(0) # pylint: disable=no-member render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( 'Time (in seconds) to render \'%s\' using \'%s\' renderer: %s', template, render.__module__.split('.')[-1], time.time() - start ) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member # If ret is not a StringIO (which means it was rendered using # yaml, mako, or another engine which renders to a data # structure) we don't want to log this. if salt.utils.stringio.is_readable(ret): log.debug( 'Rendered data from file: %s:\n%s', template, salt.utils.locales.sdecode(ret.read())) # pylint: disable=no-member ret.seek(0) # pylint: disable=no-member # Preserve newlines from original template if windows_newline: if salt.utils.stringio.is_readable(ret): is_stringio = True contents = ret.read() else: is_stringio = False contents = ret if isinstance(contents, six.string_types): if '\r\n' not in contents: contents = contents.replace('\n', '\r\n') ret = StringIO(contents) if is_stringio else contents else: if is_stringio: ret.seek(0) return ret
def _produce_output(report, failed, setup): """ Produce output from the report dictionary generated by _generate_report """ report_format = setup.get("report_format", "yaml") log.debug("highstate output format: %s", report_format) if report_format == "json": report_text = salt.utils.json.dumps(report) elif report_format == "yaml": string_file = StringIO() salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False) string_file.seek(0) report_text = string_file.read() else: string_file = StringIO() _generate_html(report, string_file) string_file.seek(0) report_text = string_file.read() report_delivery = setup.get("report_delivery", "file") log.debug("highstate report_delivery: %s", report_delivery) if report_delivery == "file": output_file = _sprinkle(setup.get("file_output", "/tmp/test.rpt")) with salt.utils.files.fopen(output_file, "w") as out: out.write(salt.utils.stringutils.to_str(report_text)) else: msg = MIMEText(report_text, report_format) sender = setup.get("smtp_sender", "") recipients = setup.get("smtp_recipients", "") host = setup.get('smtp_server', '') port = int(setup.get('smtp_port', 25)) tls = setup.get('smtp_tls') username = setup.get('smtp_username') password = setup.get('smtp_password') if failed: subject = setup.get("smtp_failure_subject", "Installation failure") else: subject = setup.get("smtp_success_subject", "Installation success") subject = _sprinkle(subject) msg["Subject"] = subject msg["From"] = sender msg["To"] = recipients log.debug('highstate smtp port: %d', port) smtp = smtplib.SMTP(host=host, port=port) if tls is True: smtp.starttls() log.debug('highstate smtp tls enabled') if username and password: smtp.login(username, password) log.debug('highstate smtp authenticated') smtp.sendmail(sender, [x.strip() for x in recipients.split(',')], msg.as_string()) log.debug('highstate message sent.') smtp.quit()
def compile_template( template, renderers, default, blacklist, whitelist, saltenv="base", sls="", input_data="", **kwargs ): """ Take the path to a template and return the high data structure derived from the template. Helpers: :param mask_value: Mask value for debugging purposes (prevent sensitive information etc) example: "mask_value="pass*". All "passwd", "password", "pass" will be masked (as text). """ # if any error occurs, we return an empty dictionary ret = {} log.debug("compile template: %s", template) if "env" in kwargs: # "env" is not supported; Use "saltenv". kwargs.pop("env") if template != ":string:": # Template was specified incorrectly if not isinstance(template, six.string_types): log.error("Template was specified incorrectly: %s", template) return ret # Template does not exist if not os.path.isfile(template): log.error("Template does not exist: %s", template) return ret # Template is an empty file if salt.utils.files.is_empty(template): log.debug("Template is an empty file: %s", template) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error("Template is nothing but whitespace: %s", template) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang( template, renderers, default, blacklist, whitelist, input_data ) windows_newline = "\r\n" in input_data input_data = StringIO(input_data) for render, argline in render_pipe: if salt.utils.stringio.is_readable(input_data): input_data.seek(0) # pylint: disable=no-member render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs["argline"] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( "Time (in seconds) to render '%s' using '%s' renderer: %s", template, render.__module__.split(".")[-1], time.time() - start, ) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member # If ret is not a StringIO (which means it was rendered using # yaml, mako, or another engine which renders to a data # structure) we don't want to log this. if salt.utils.stringio.is_readable(ret): log.debug( "Rendered data from file: %s:\n%s", template, salt.utils.sanitizers.mask_args_value( salt.utils.data.decode(ret.read()), kwargs.get("mask_value") ), ) # pylint: disable=no-member ret.seek(0) # pylint: disable=no-member # Preserve newlines from original template if windows_newline: if salt.utils.stringio.is_readable(ret): is_stringio = True contents = ret.read() else: is_stringio = False contents = ret if isinstance(contents, six.string_types): if "\r\n" not in contents: contents = contents.replace("\n", "\r\n") ret = StringIO(contents) if is_stringio else contents else: if is_stringio: ret.seek(0) return ret
def compile_template(template, renderers, default, blacklist, whitelist, saltenv='base', sls='', input_data='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # if any error occurs, we return an empty dictionary ret = {} log.debug('compile template: {0}'.format(template)) if 'env' in kwargs: salt.utils.warn_until( 'Oxygen', 'Parameter \'env\' has been detected in the argument list. This ' 'parameter is no longer used and has been replaced by \'saltenv\' ' 'as of Salt 2016.11.0. This warning will be removed in Salt Oxygen.' ) kwargs.pop('env') if template != ':string:': # Template was specified incorrectly if not isinstance(template, six.string_types): log.error( 'Template was specified incorrectly: {0}'.format(template)) return ret # Template does not exist if not os.path.isfile(template): log.error('Template does not exist: {0}'.format(template)) return ret # Template is an empty file if salt.utils.is_empty(template): log.warning('Template is an empty file: {0}'.format(template)) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error( 'Template is nothing but whitespace: {0}'.format(template)) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default, blacklist, whitelist, input_data) windows_newline = '\r\n' in input_data input_data = StringIO(input_data) for render, argline in render_pipe: # For GPG renderer, input_data can be an OrderedDict (from YAML) or dict (from py renderer). # Repress the error. if not isinstance(input_data, (dict, OrderedDict)): try: input_data.seek(0) except Exception as exp: log.error('error while compiling template \'{0}\': {1}'.format( template, exp)) render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( 'Time (in seconds) to render \'{0}\' using \'{1}\' renderer: {2}'. format(template, render.__module__.split('.')[-1], time.time() - start)) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member try: log.debug('Rendered data from file: {0}:\n{1}'.format( template, ret.read())) ret.seek(0) except Exception: # ret is not a StringIO, which means it was rendered using # yaml, mako, or another engine which renders to a data # structure. We don't want to log this, so ignore this # exception. pass # Preserve newlines from original template if windows_newline: if salt.utils.stringio.is_readable(ret): is_stringio = True contents = ret.read() else: is_stringio = False contents = ret if isinstance(contents, six.string_types): if '\r\n' not in contents: contents = contents.replace('\n', '\r\n') ret = StringIO(contents) if is_stringio else contents else: if is_stringio: ret.seek(0) return ret