2008-01-12 21:46:28 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
2008-04-04 20:36:46 +02:00
|
|
|
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
|
|
|
# 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 2 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, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
2008-06-07 23:25:35 +02:00
|
|
|
"""This module defines the BackendEntity base class."""
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import tempfile
|
2007-07-09 08:46:36 +02:00
|
|
|
|
2008-06-07 23:25:35 +02:00
|
|
|
from gnuviechadmin.exceptions import MissingFieldsError
|
|
|
|
from gnuviechadmin.backend.settings import config
|
2007-07-25 18:04:40 +02:00
|
|
|
from gnuviechadmin.util import gpgmail
|
2008-06-07 23:25:35 +02:00
|
|
|
from subprocess import Popen, PIPE
|
2008-04-04 20:36:46 +02:00
|
|
|
from sqlalchemy.orm import object_mapper
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
class BackendEntity(object):
|
|
|
|
"""This is the abstract base class for all backend entity classes."""
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def __init__(self, delegateto, verbose = False):
|
|
|
|
self.logger = logging.getLogger("%s.%s" % (
|
|
|
|
self.__class__.__module__, self.__class__.__name__))
|
|
|
|
self.delegateto = delegateto
|
2007-07-02 11:14:47 +02:00
|
|
|
self.verbose = verbose
|
|
|
|
|
|
|
|
def __repr__(self):
|
2007-07-09 08:46:36 +02:00
|
|
|
return self.delegateto.__repr__(verbose = self.verbose)
|
2007-07-02 11:14:47 +02:00
|
|
|
|
|
|
|
def sucommand(self, cmdline, pipedata = None):
|
|
|
|
"""Executes a command as root using the configured suwrapper
|
|
|
|
command. If a pipe is specified it is used as stdin of the
|
|
|
|
subprocess."""
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.debug("sucommand called: %s (pipedata=%s)", cmdline,
|
|
|
|
str(pipedata))
|
|
|
|
suwrapper = config.get('common', 'suwrapper')
|
2007-07-02 11:14:47 +02:00
|
|
|
toexec = "%s %s" % (suwrapper, cmdline)
|
|
|
|
if pipedata:
|
2008-06-07 23:25:35 +02:00
|
|
|
pipeproc = Popen(toexec, shell = True, stdin=PIPE)
|
|
|
|
pipe = pipeproc.stdin
|
|
|
|
print >> pipe, pipedata
|
2007-07-02 11:14:47 +02:00
|
|
|
pipe.close()
|
2008-06-07 23:25:35 +02:00
|
|
|
sts = os.waitpid(pipeproc.pid, 0)
|
2007-07-02 11:14:47 +02:00
|
|
|
if self.verbose:
|
|
|
|
print "%s|%s: %d" % (pipedata, toexec, sts[1])
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.info("%s|%s: %d", pipedata, toexec, sts[1])
|
2007-07-02 11:14:47 +02:00
|
|
|
else:
|
2008-06-07 23:25:35 +02:00
|
|
|
pipeproc = Popen(toexec, shell = True)
|
|
|
|
sts = os.waitpid(pipeproc.pid, 0)
|
2007-07-02 11:14:47 +02:00
|
|
|
if self.verbose:
|
|
|
|
print "%s: %s" % (toexec, sts[1])
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.info("%s: %s", toexec, sts[1])
|
2007-07-02 11:14:47 +02:00
|
|
|
return sts[1]
|
|
|
|
|
2008-01-12 21:46:28 +01:00
|
|
|
def supipe(self, cmdlines):
|
|
|
|
"""Executes multiple commands as root and pipes the output of
|
|
|
|
the commands to the input of the next commands."""
|
|
|
|
self.logger.debug("supipe called: %s", " | ".join(cmdlines))
|
|
|
|
suwrapper = config.get('common', 'suwrapper')
|
|
|
|
predecessor = None
|
|
|
|
for cmdline in cmdlines:
|
|
|
|
toexec = "%s %s" % (suwrapper, cmdline)
|
|
|
|
if predecessor is None:
|
2008-06-07 23:25:35 +02:00
|
|
|
pipeproc = Popen(toexec, shell = True, stdout = PIPE)
|
2008-01-12 21:46:28 +01:00
|
|
|
else:
|
2008-06-07 23:25:35 +02:00
|
|
|
pipeproc = Popen(toexec, shell = True,
|
|
|
|
stdin = predecessor.stdout, stdout = PIPE)
|
|
|
|
predecessor = pipeproc
|
2008-01-12 21:46:28 +01:00
|
|
|
output = predecessor.communicate()[0]
|
|
|
|
return predecessor.wait()
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def send_mail(self, subject, text):
|
|
|
|
"""This method sends a mail with the given text and subject
|
|
|
|
and signs it usign GnuPG. If a public key of the recipient is
|
|
|
|
available the mail is encrypted."""
|
2007-07-25 18:04:40 +02:00
|
|
|
gpgmail.send_mail(subject, text)
|
2007-07-09 08:46:36 +02:00
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
def validate(self):
|
|
|
|
"""Validates whether all mandatory fields of the entity have
|
|
|
|
values."""
|
|
|
|
missingfields = []
|
|
|
|
for key in [col.name for col in \
|
2008-04-04 20:36:46 +02:00
|
|
|
object_mapper(
|
2007-07-09 08:46:36 +02:00
|
|
|
self.delegateto).local_table.columns \
|
2007-07-02 11:14:47 +02:00
|
|
|
if not col.primary_key and not col.nullable]:
|
2007-07-09 08:46:36 +02:00
|
|
|
if self.delegateto.__getattribute__(key) is None:
|
2007-07-02 11:14:47 +02:00
|
|
|
missingfields.append(key)
|
|
|
|
if missingfields:
|
|
|
|
raise MissingFieldsError(missingfields)
|
2007-07-09 08:46:36 +02:00
|
|
|
|
|
|
|
def write_to_file(self, filename, template):
|
|
|
|
"""Write the data from template to the specified file."""
|
|
|
|
tmp = tempfile.NamedTemporaryFile()
|
2008-04-05 20:48:46 +02:00
|
|
|
tmp.write(template.encode('utf_8'))
|
2007-07-09 08:46:36 +02:00
|
|
|
tmp.flush()
|
|
|
|
cmd = 'cp "%s" "%s"' % (tmp.name, filename)
|
|
|
|
self.sucommand(cmd)
|
|
|
|
tmp.close()
|