Sandro Bonazzola has uploaded a new change for review.

Change subject: packaging: setup: validate host id
......................................................................

packaging: setup: validate host id

Added host id validation on additional hosts setup.
Requires: http://gerrit.ovirt.org/19140

Rebased the code for being compatible with
http://gerrit.ovirt.org/19140

Bug-Url: https://bugzilla.redhat.com/1012417
Change-Id: I2487abfe1f722cb60e6f7c2a8daa4a81fb654bfd
Signed-off-by: Sandro Bonazzola <[email protected]>
---
M src/bin/hosted-engine.in
M src/ovirt_hosted_engine_setup/constants.py
M src/ovirt_hosted_engine_setup/util.py
M src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
M src/plugins/ovirt-hosted-engine-setup/storage/storage.py
5 files changed, 124 insertions(+), 33 deletions(-)


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

diff --git a/src/bin/hosted-engine.in b/src/bin/hosted-engine.in
index e92dd83..8d93a34 100644
--- a/src/bin/hosted-engine.in
+++ b/src/bin/hosted-engine.in
@@ -80,7 +80,11 @@
             fi
         ;;
         --vm-status)
-            python -m ovirt_hosted_engine_setup.vm_status
+            if [ -r "${conf}" ] ; then
+                python -m ovirt_hosted_engine_setup.vm_status
+            else
+                echo "You must run --deploy first"
+            fi
         ;;
         --add-console-password=*)
             if [ -n "${vmid}" ] ; then
diff --git a/src/ovirt_hosted_engine_setup/constants.py 
b/src/ovirt_hosted_engine_setup/constants.py
index 694ecde..9b224f1 100644
--- a/src/ovirt_hosted_engine_setup/constants.py
+++ b/src/ovirt_hosted_engine_setup/constants.py
@@ -192,6 +192,7 @@
     DEPLOY_PROCEED = 'OVEHOSTED_CORE/deployProceed'
     SCREEN_PROCEED = 'OVEHOSTED_CORE/screenProceed'
     CONFIRM_SETTINGS = 'OVEHOSTED_CORE/confirmSettings'
+    RE_DEPLOY = 'OVEHOSTED_CORE/additionalHostReDeployment'
 
 
 @util.export
diff --git a/src/ovirt_hosted_engine_setup/util.py 
b/src/ovirt_hosted_engine_setup/util.py
index d292c47..8b8be3e 100644
--- a/src/ovirt_hosted_engine_setup/util.py
+++ b/src/ovirt_hosted_engine_setup/util.py
@@ -21,10 +21,14 @@
 """Utils."""
 
 
+import os
 import random
 
 
 from otopi import util
+
+
+from . import constants as ohostedcons
 
 
 @util.export
@@ -49,4 +53,30 @@
     return ':'.join(mac)
 
 
+class VirtUserContext(object):
+    """
+    Switch to vdsm:kvm user with provided umask
+    """
+
+    def __init__(self, environment, umask):
+        super(VirtUserContext, self).__init__()
+        self.environment = environment
+        self._euid = None
+        self._egid = None
+        self._umask = umask
+        self._old_umask = None
+
+    def __enter__(self):
+        self._euid = os.geteuid()
+        self._egid = os.getegid()
+        self._old_umask = os.umask(self._umask)
+        os.setegid(self.environment[ohostedcons.VDSMEnv.KVM_GID])
+        os.seteuid(self.environment[ohostedcons.VDSMEnv.VDSM_UID])
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        os.seteuid(self._euid)
+        os.setegid(self._egid)
+        os.umask(self._umask)
+
+
 # vim: expandtab tabstop=4 shiftwidth=4
diff --git a/src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py 
b/src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
index 72c6c52..288e1ce 100644
--- a/src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
+++ b/src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
@@ -34,35 +34,10 @@
 
 
 from ovirt_hosted_engine_setup import constants as ohostedcons
+from ovirt_hosted_engine_setup import util as ohostedutil
 
 
 _ = lambda m: gettext.dgettext(message=m, domain='ovirt-hosted-engine-setup')
-
-
-class VirtUserContext(object):
-    """
-    Switch to vdsm:kvm user with provided umask
-    """
-
-    def __init__(self, environment, umask):
-        super(VirtUserContext, self).__init__()
-        self.environment = environment
-        self._euid = None
-        self._egid = None
-        self._umask = umask
-        self._old_umask = None
-
-    def __enter__(self):
-        self._euid = os.geteuid()
-        self._egid = os.getegid()
-        self._old_umask = os.umask(self._umask)
-        os.setegid(self.environment[ohostedcons.VDSMEnv.KVM_GID])
-        os.seteuid(self.environment[ohostedcons.VDSMEnv.VDSM_UID])
-
-    def __exit__(self, exc_type, exc_val, exc_tb):
-        os.seteuid(self._euid)
-        os.setegid(self._egid)
-        os.umask(self._umask)
 
 
 @util.export
@@ -154,7 +129,7 @@
         )
         if not os.path.isdir(metadatadir):
             self.logger.debug('Creating metadata directory')
-            with VirtUserContext(
+            with ohostedutil.VirtUserContext(
                 environment=self.environment,
                 umask=stat.S_IWGRP | stat.S_IWOTH,
             ):
@@ -163,7 +138,7 @@
             self.logger.info(_('sanlock lockspace already initialized'))
         else:
             self.logger.info(_('Initializing sanlock lockspace'))
-            with VirtUserContext(
+            with ohostedutil.VirtUserContext(
                 environment=self.environment,
                 umask=stat.S_IXUSR | stat.S_IXGRP | stat.S_IRWXO,
             ):
@@ -176,7 +151,7 @@
             self.logger.info(_('sanlock metadata already initialized'))
         else:
             self.logger.info(_('Initializing sanlock metadata'))
-            with VirtUserContext(
+            with ohostedutil.VirtUserContext(
                 environment=self.environment,
                 umask=stat.S_IXUSR | stat.S_IXGRP | stat.S_IRWXO,
             ):
diff --git a/src/plugins/ovirt-hosted-engine-setup/storage/storage.py 
b/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
index 2bf64ab..51c6ca9 100644
--- a/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
+++ b/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
@@ -22,9 +22,11 @@
 Local storage domain plugin.
 """
 
+import glob
 import os
 import uuid
 import gettext
+import stat
 import tempfile
 import time
 
@@ -36,6 +38,10 @@
 from ovirt_hosted_engine_setup import constants as ohostedcons
 from ovirt_hosted_engine_setup import domains as ohosteddomains
 from ovirt_hosted_engine_setup import tasks
+from ovirt_hosted_engine_setup import util as ohostedutil
+
+
+from ovirt_hosted_engine_ha.client import client
 
 
 _ = lambda m: gettext.dgettext(message=m, domain='ovirt-hosted-engine-setup')
@@ -104,6 +110,48 @@
                 )
         return rc
 
+    def _get_dom_md_path(self):
+        """
+        Return path of storage domain holding engine vm
+        """
+        domains = glob.glob(
+            os.path.join(
+                ohostedcons.FileLocations.SD_MOUNT_PARENT_DIR,
+                '*',
+                self.environment[ohostedcons.StorageEnv.SD_UUID],
+            )
+        )
+        if not domains:
+            raise RuntimeError(
+                _(
+                    'Path to storage domain {sd_uuid} not found in {root}'
+                ).format(
+                    sd_uuid=self.environment[ohostedcons.StorageEnv.SD_UUID],
+                    root=ohostedcons.FileLocations.SD_MOUNT_PARENT_DIR,
+                )
+            )
+        return domains[0]
+
+    def _re_deploying_host(self):
+        interactive = self.environment[ohostedcons.CoreEnv.RE_DEPLOY] is None
+        if interactive:
+            self.environment[
+                ohostedcons.CoreEnv.RE_DEPLOY
+            ] = self.dialog.queryString(
+                name='OVEHOSTED_RE_DEPLOY_HOST',
+                note=_(
+                    'The Host ID is already known. '
+                    'Is this a re-deployment on an additional host that was '
+                    'previously set up '
+                    '(@VALUES@)[@DEFAULT@]? '
+                ),
+                prompt=True,
+                validValues=(_('Yes'), _('No')),
+                caseSensitive=False,
+                default=_('Yes')
+            ) == _('Yes').lower()
+        return self.environment[ohostedcons.CoreEnv.RE_DEPLOY]
+
     def _handleHostId(self):
         if not self.environment[
             ohostedcons.CoreEnv.ADDITIONAL_HOST_ENABLED
@@ -159,7 +207,7 @@
                         default=ohostedcons.Const.FIRST_HOST_ID + 1,
                     )
                 try:
-                    valid = True
+                    # ensure it's an int and not the FIRST_HOST_ID.
                     if int(
                         self.environment[ohostedcons.StorageEnv.HOST_ID]
                     ) == ohostedcons.Const.FIRST_HOST_ID:
@@ -172,6 +220,36 @@
                             raise RuntimeError(
                                 _('Cannot use the same ID used by first host')
                             )
+                    # ensure nobody else is using it
+                    all_host_stats = {}
+                    with ohostedutil.VirtUserContext(
+                        environment=self.environment,
+                        umask=stat.S_IWGRP | stat.S_IWOTH,
+                    ):
+                        ha_cli = client.HAClient()
+                        all_host_stats = ha_cli.get_all_host_stats_direct(
+                            dom_path=self._get_dom_md_path(),
+                            service_type=self.environment[
+                                ohostedcons.SanlockEnv.LOCKSPACE_NAME
+                            ],
+                        )
+                    if (
+                        int(
+                            self.environment[ohostedcons.StorageEnv.HOST_ID]
+                        ) in all_host_stats.keys() and
+                        not self._re_deploying_host()
+                    ):
+                        valid = False
+                        if interactive:
+                            self.logger.error(
+                                _('Invalid value for Host ID: already used')
+                            )
+                        else:
+                            raise RuntimeError(
+                                _('Invalid value for Host ID: already used')
+                            )
+                    else:
+                        valid = True
                 except ValueError:
                     valid = False
                     if interactive:
@@ -245,14 +323,13 @@
                 self.environment[
                     ohostedcons.CoreEnv.ADDITIONAL_HOST_ENABLED
                 ] = True
-                self._handleHostId()
                 self.environment[
                     ohostedcons.StorageEnv.STORAGE_DOMAIN_NAME
                 ] = domain_info['name']
-
                 self.environment[
                     ohostedcons.StorageEnv.SD_UUID
                 ] = sdUUID
+                self._handleHostId()
                 pool_list = domain_info['pool']
                 if pool_list:
                     self.pool_exists = True
@@ -531,6 +608,10 @@
             ohostedcons.CoreEnv.IS_ADDITIONAL_HOST,
             None
         )
+        self.environment.setdefault(
+            ohostedcons.CoreEnv.RE_DEPLOY,
+            None
+        )
 
     @plugin.event(
         stage=plugin.Stages.STAGE_SETUP,


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2487abfe1f722cb60e6f7c2a8daa4a81fb654bfd
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-hosted-engine-setup
Gerrit-Branch: ovirt-hosted-engine-setup-1.0
Gerrit-Owner: Sandro Bonazzola <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to