Commit f9698ea0 authored by Nikos Skalkotos's avatar Nikos Skalkotos

Create a syslog output module

If --syslog option is passed to snf-mkimage or snf-image-creator, the
program will log its output to syslog too.

This resolves #42
parent dc54d5c5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011-2014 GRNET S.A.
# Copyright (C) 2011-2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -36,6 +36,7 @@ from image_creator.util import FatalError
from image_creator.output.cli import SimpleOutput
from image_creator.output.dialog import GaugeOutput
from image_creator.output.composite import CompositeOutput
from image_creator.output.syslog import SyslogOutput
from image_creator.disk import Disk
from image_creator.dialog_wizard import start_wizard
from image_creator.dialog_menu import main_menu
......@@ -160,9 +161,14 @@ def _dialog_form(self, text, height=20, width=60, form_height=15, fields=[],
return (code, output.splitlines())
def dialog_main(media, logfile, tmpdir, snapshot):
def dialog_main(media, **kwargs):
"""Main function for the dialog-based version of the program"""
tmpdir = kwargs['tmpdir'] if 'tmpdir' in kwargs else None
snapshot = kwargs['snapshot'] if 'snapshot' in kwargs else True
logfile = kwargs['logfile'] if 'logfile' in kwargs else None
syslog = kwargs['syslog'] if 'syslog' in kwargs else False
# In openSUSE dialog is buggy under xterm
if os.environ['TERM'] == 'xterm':
os.environ['TERM'] = 'linux'
......@@ -204,20 +210,26 @@ def dialog_main(media, logfile, tmpdir, snapshot):
tmplog = None if logfile else tempfile.NamedTemporaryFile(prefix='fatal-',
delete=False)
logs = []
try:
stream = logfile if logfile else tmplog
log = SimpleOutput(colored=False, stderr=stream, stdout=stream)
logs.append(SimpleOutput(colored=False, stderr=stream, stdout=stream))
if syslog:
logs.append(SyslogOutput())
while 1:
try:
out = CompositeOutput([log])
out = CompositeOutput(logs)
out.info("Starting %s v%s ..." % (PROGNAME, version))
ret = create_image(d, media, out, tmpdir, snapshot)
break
except Reset:
log.info("Resetting everything ...")
for log in logs:
log.info("Resetting everything ...")
except FatalError as error:
log.error(str(error))
for log in logs:
log.error(str(error))
msg = 'A fatal error occured. See %s for a full log.' % log.stderr.name
d.infobox(msg, width=WIDTH, title="Fatal Error")
return 1
......@@ -246,6 +258,8 @@ def main():
help="don't snapshot the input media. (THIS IS "
"DANGEROUS AS IT WILL ALTER THE ORIGINAL MEDIA!!!)",
action="store_false")
parser.add_option("--syslog", dest="syslog", default=False,
help="log to syslog", action="store_true")
parser.add_option("--tmpdir", type="string", dest="tmp", default=None,
help="create large temporary image files under DIR",
metavar="DIR")
......@@ -271,7 +285,8 @@ def main():
# Save the terminal attributes
attr = termios.tcgetattr(sys.stdin.fileno())
try:
ret = dialog_main(media, logfile, opts.tmp, opts.snapshot)
ret = dialog_main(media, logfile=logfile, tmpdir=opts.tmp,
snapshot=opts.snapshot, syslog=opts.syslog)
finally:
# Restore the terminal attributes. If an error occurs make sure
# that the terminal turns back to normal.
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011-2014 GRNET S.A.
# Copyright (C) 2011-2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -25,6 +25,8 @@ from image_creator.disk import Disk
from image_creator.util import FatalError
from image_creator.output.cli import SilentOutput, SimpleOutput, \
OutputWthProgress
from image_creator.output.composite import CompositeOutput
from image_creator.output.syslog import SyslogOutput
from image_creator.kamaki_wrapper import Kamaki, ClientError
import sys
import os
......@@ -135,6 +137,9 @@ def parse_options(input_args):
parser.add_option("-s", "--silent", dest="silent", default=False,
help="output only errors", action="store_true")
parser.add_option('--syslog', dest="syslog", default=False,
help="log to syslog", action="store_true")
parser.add_option("--sysprep-param", dest="sysprep_params", default=[],
help="add KEY=VALUE system preparation parameter",
action="append")
......@@ -223,6 +228,9 @@ def image_creator():
out.info(title)
out.info('=' * len(title))
if options.syslog:
out = CompositeOutput([out, SyslogOutput()])
if os.geteuid() != 0:
raise FatalError("You must run %s as root"
% os.path.basename(sys.argv[0]))
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011-2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This module provides the Syslog output class"""
from __future__ import absolute_import
from image_creator.output import Output
import syslog
class SyslogOutput(Output):
def __init__(self, ident=None, logoption=None, facility=None):
"""Initialize a Syslog instance"""
self.ident = ident if ident is not None else 'snf-image-creator'
self.logoption = logoption if logoption is not None else syslog.LOG_PID
self.facility = facility if facility is not None else syslog.LOG_USER
self.line = []
syslog.openlog(self.ident, self.logoption, self.facility)
def error(self, msg):
"""Print an error to syslog"""
if len(self.line):
syslog.syslog(syslog.LOG_INFO, " ".join(self.line))
self.line = []
syslog.syslog(syslog.LOG_ERR, "[ERROR] %s" % msg)
def warn(self, msg):
"""Print a warning"""
if len(self.line):
syslog.syslog(syslog.LOG_INFO, "[INFO] %s " % " ".join(self.line))
self.line = []
syslog.syslog(syslog.LOG_WARNING, "[WARNING] %s" % msg)
def success(self, msg):
"""Print msg after an action is completed"""
self.line.append(msg)
syslog.syslog(syslog.LOG_INFO, "[INFO] %s" % " ".join(self.line))
self.line = []
def info(self, msg='', new_line=True):
"""Print normal program output"""
if len(msg) == 0:
return
self.line.append(msg)
if new_line:
syslog.syslog(syslog.LOG_INFO, "[INFO] %s" % " ".join(self.line))
self.line = []
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment