Yedidyah Bar David has uploaded a new change for review.

Change subject: packaging: setup: config broker notifications
......................................................................

packaging: setup: config broker notifications

Change-Id: Ib49f0534b5f8a9918558a82d40cc9828ccf87868
Bug-Url: https://bugzilla.redhat.com/1030437
Signed-off-by: Yedidyah Bar David <[email protected]>
---
M src/ovirt_hosted_engine_setup/constants.py
M src/plugins/ovirt-hosted-engine-setup/ha/Makefile.am
M src/plugins/ovirt-hosted-engine-setup/ha/__init__.py
A src/plugins/ovirt-hosted-engine-setup/ha/ha_notifications.py
4 files changed, 252 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-setup 
refs/changes/15/21415/1

diff --git a/src/ovirt_hosted_engine_setup/constants.py 
b/src/ovirt_hosted_engine_setup/constants.py
index 15d13c3..3282937 100644
--- a/src/ovirt_hosted_engine_setup/constants.py
+++ b/src/ovirt_hosted_engine_setup/constants.py
@@ -167,6 +167,14 @@
         'libvirt',
         'qemu.conf'
     )
+    ENGINE_HA_CONFDIR = os.path.join(
+        SYSCONFDIR,
+        'ovirt-hosted-engine-ha'
+    )
+    NOTIFY_CONF_FILE = os.path.join(
+        ENGINE_HA_CONFDIR,
+        'broker.conf'
+    )
 
 
 @util.export
@@ -179,6 +187,10 @@
     HOSTED_ENGINE_VM_NAME = 'HostedEngine'
     METADATA_CHUNK_SIZE = 4096
     MAX_HOST_ID = 250
+    HA_NOTIF_SMTP_SERVER = 'smtp-server'
+    HA_NOTIF_SMTP_PORT = 'smtp-port'
+    HA_NOTIF_SMTP_SOURCE_EMAIL = 'source-email'
+    HA_NOTIF_SMTP_DEST_EMAILS = 'destination-emails'
 
 
 @util.export
@@ -487,6 +499,40 @@
 
 @util.export
 @util.codegen
+@ohostedattrsclass
+class NotificationsEnv(object):
+    @ohostedattrs(
+        answerfile=True,
+    )
+    def SMTP_SERVER(self):
+        return 'OVEHOSTED_NOTIF/smtpServer'
+
+    @ohostedattrs(
+        answerfile=True,
+    )
+    def SMTP_PORT
+        return 'OVEHOSTED_NOTIF/smtpPort'
+
+    @ohostedattrs(
+        answerfile=True,
+    )
+    def SOURCE_EMAIL
+        return 'OVEHOSTED_NOTIF/sourceEmail'
+
+    @ohostedattrs(
+        answerfile=True,
+    )
+    def DEST_EMAIL
+        return 'OVEHOSTED_NOTIF/destEmail'
+
+    DEFAULT_SMTP_SERVER = 'localhost'
+    DEFAULT_SMTP_PORT = 25
+    DEFAULT_SOURCE_EMAIL = 'root'
+    DEFAULT_DEST_EMAIL = 'root'
+
+
[email protected]
[email protected]
 class Stages(object):
     CONFIG_BOOT_DEVICE = 'ohosted.boot.configuration.available'
     CONFIG_STORAGE = 'ohosted.storage.configuration.available'
diff --git a/src/plugins/ovirt-hosted-engine-setup/ha/Makefile.am 
b/src/plugins/ovirt-hosted-engine-setup/ha/Makefile.am
index c176193..c2fc379 100644
--- a/src/plugins/ovirt-hosted-engine-setup/ha/Makefile.am
+++ b/src/plugins/ovirt-hosted-engine-setup/ha/Makefile.am
@@ -27,6 +27,7 @@
 dist_my_PYTHON = \
        __init__.py \
        ha_services.py \
+       ha_notifications.py \
        $(NULL)
 
 clean-local: \
diff --git a/src/plugins/ovirt-hosted-engine-setup/ha/__init__.py 
b/src/plugins/ovirt-hosted-engine-setup/ha/__init__.py
index 0072ff6..ab965cd 100644
--- a/src/plugins/ovirt-hosted-engine-setup/ha/__init__.py
+++ b/src/plugins/ovirt-hosted-engine-setup/ha/__init__.py
@@ -24,11 +24,13 @@
 from otopi import util
 
 
+from . import ha_notifications
 from . import ha_services
 
 
 @util.export
 def createPlugins(context):
+    ha_notifications.Plugin(context=context)
     ha_services.Plugin(context=context)
 
 
diff --git a/src/plugins/ovirt-hosted-engine-setup/ha/ha_notifications.py 
b/src/plugins/ovirt-hosted-engine-setup/ha/ha_notifications.py
new file mode 100644
index 0000000..cc3fef8
--- /dev/null
+++ b/src/plugins/ovirt-hosted-engine-setup/ha/ha_notifications.py
@@ -0,0 +1,203 @@
+#
+# ovirt-hosted-engine-setup -- ovirt hosted engine setup
+# Copyright (C) 2013 Red Hat, Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+
+"""
+HA notifications configuration
+"""
+
+
+import ConfigParser
+import gettext
+import re
+import StringIO
+
+
+from otopi import util
+from otopi import plugin
+from otopi import filetransaction
+
+
+from ovirt_hosted_engine_setup import constants as ohostedcons
+
+
+_ = lambda m: gettext.dgettext(message=m, domain='ovirt-hosted-engine-setup')
+
+
[email protected]
+class Plugin(plugin.PluginBase):
+    """
+    HA notifications plugin.
+    """
+
+    _RE_HOSTNAME = re.compile(
+        flags=re.VERBOSE,
+        pattern=r"""
+            ^
+            [a-z0-9.-]+
+            $
+        """
+    )
+
+    _RE_PORT = re.compile(
+        flags=re.VERBOSE,
+        pattern=r"""
+            ^
+            [0-9]+
+            $
+        """
+    )
+
+    _RE_EMAIL_ADDRESS = re.compile(
+        flags=re.VERBOSE,
+        pattern=r"""
+            [a-zA-Z0-9_.]+
+            @
+            [a-z0-9.-]+
+        """
+    )
+
+    def __init__(self, context):
+        super(Plugin, self).__init__(context=context)
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_INIT,
+    )
+    def _init(self):
+        self._conffile = ohostedcons.FileLocations.NOTIFY_CONF_FILE
+        self._enabled = os.path.exists(self._conffile)
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_CUSTOMIZATION,
+        condition=lambda self: self._enabled,
+    )
+    def _customization(self):
+        smtp_config = {
+            ohostedcons.Const.HA_NOTIF_SMTP_SERVER:
+                ohostedcons.NotificationsEnv.DEFAULT_SMTP_SERVER,
+            ohostedcons.Const.HA_NOTIF_SMTP_PORT:
+                ohostedcons.NotificationsEnv.DEFAULT_SMTP_PORT,
+            ohostedcons.Const.HA_NOTIF_SMTP_SOURCE_EMAIL:
+                ohostedcons.NotificationsEnv.DEFAULT_SOURCE_EMAIL,
+            ohostedcons.Const.HA_NOTIF_SMTP_DEST_EMAILS:
+                ohostedcons.NotificationsEnv.DEFAULT_DEST_EMAIL,
+        }
+        self._cfg = ConfigParser.SafeConfigParser()
+        self._cfg.read(self._conffile)
+        if self._cfg.has_section('email'):
+            for name, value in dict(self._cfg.items('email')):
+                if name in smtp_config:
+                    smtp_config[name] = value
+
+        interactions = (
+            {
+                'name': ohostedcons.Const.HA_NOTIF_SMTP_SERVER,
+                'envkey': ohostedcons.NotificationsEnv.SMTP_SERVER,
+                'note': _(
+                    'Please provide the name of the SMTP server through which'
+                    'we will send notifications: '
+                ),
+                'validation': lambda value: self._RE_HOSTNAME.match(value),
+            },
+            {
+                'name': ohostedcons.Const.HA_NOTIF_SMTP_PORT,
+                'envkey': ohostedcons.NotificationsEnv.SMTP_PORT,
+                'note': _(
+                    'Please provide the TCP port number of the SMTP server: '
+                ),
+                'validation': lambda value: self._RE_PORT.match(value),
+            },
+            {
+                'name': ohostedcons.Const.HA_NOTIF_SMTP_SOURCE_EMAIL,
+                'envkey': ohostedcons.NotificationsEnv.SOURCE_EMAIL,
+                'note': _(
+                    'Please provide the email address from which notifications 
'
+                    'will be sent: '
+                ),
+                'validation': lambda value: 
self._RE_EMAIL_ADDRESS.match.(value),
+            },
+            {
+                'name': ohostedcons.Const.HA_NOTIF_SMTP_DEST_EMAILS,
+                'envkey': ohostedcons.NotificationsEnv.DEST_EMAIL,
+                'note': _(
+                    'Please provide a comma-separated list of email addresses '
+                    'which will get notifications: '
+                ),
+                'validation': lambda value: (
+                    None in [
+                        _RE_EMAIL_ADDRESS.match(addr) for addr in 
value.split(',')
+                    ]
+                ),
+            },
+        )
+
+        for item in interactions:
+            interactive = self.environment[item['envkey']] is None
+            valid = False
+            while not valid:
+                if interactive:
+                    self.environment[item['envkey']] = self.dialog.queryString(
+                        name='DIALOG' + item['envkey'],
+                        note=item['note'],
+                        prompt=True,
+                        caseSensitive=True,
+                    )
+                if not item['validation'](self.environment[item['envkey']]):
+                    self.logger.debug(
+                        'input %s for %s failed validation' % (
+                            self.environment[item['envkey']],
+                            item['envkey'],
+                        )
+                    )
+                    if interactive:
+                        self.logger.error(
+                            _('Invalid input, please try again')
+                        )
+                    else:
+                        raise RuntimeError(
+                            _('Invalid input for environment value %s' % (
+                                item['envkey'],
+                            ),
+                        )
+            self._cfg.set(
+                'email',
+                item['name'],
+                self.environment[item['envkey']]
+            )
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_MISC,
+        condition=lambda self: self._enabled,
+    )
+    def _misc(self):
+        f = StringIO.StringIO()
+        self._cfg.write(f)
+        f.close()
+        self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append(
+            filetransaction.FileTransaction(
+                name=self._conffile,
+                content=f.getvalue(),
+                modifiedList=self.environment[
+                    otopicons.CoreEnv.MODIFIED_FILES
+                ],
+            )
+        )
+
+
+# vim: expandtab tabstop=4 shiftwidth=4


-- 
To view, visit http://gerrit.ovirt.org/21415
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib49f0534b5f8a9918558a82d40cc9828ccf87868
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-hosted-engine-setup
Gerrit-Branch: master
Gerrit-Owner: Yedidyah Bar David <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to