Sandro Bonazzola has uploaded a new change for review.

Change subject: packaging: setup: sanlock lockspace init
......................................................................

packaging: setup: sanlock lockspace init

Added sanlock lockspace initialization.
Code adapted from
http://gerrit.ovirt.org/#/c/17609/1/ovirt_hosted_engine_ha/agent/hosted_engine.py

Change-Id: Ife56590bf3e6c17368a24ef8e9af6d42adc5ea42
Signed-off-by: Sandro Bonazzola <[email protected]>
---
M configure.ac
M ovirt-hosted-engine-setup.spec.in
M src/ovirt_hosted_engine_setup/constants.py
M src/plugins/ovirt-hosted-engine-setup/Makefile.am
A src/plugins/ovirt-hosted-engine-setup/sanlock/Makefile.am
A src/plugins/ovirt-hosted-engine-setup/sanlock/__init__.py
A src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
M src/plugins/ovirt-hosted-engine-setup/vm/image.py
8 files changed, 251 insertions(+), 1 deletion(-)


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

diff --git a/configure.ac b/configure.ac
index 6334f57..0b38354 100644
--- a/configure.ac
+++ b/configure.ac
@@ -98,6 +98,7 @@
        src/plugins/ovirt-hosted-engine-setup/ha/Makefile
         src/plugins/ovirt-hosted-engine-setup/network/Makefile
        src/plugins/ovirt-hosted-engine-setup/pki/Makefile
+       src/plugins/ovirt-hosted-engine-setup/sanlock/Makefile
        src/plugins/ovirt-hosted-engine-setup/storage/Makefile
        src/plugins/ovirt-hosted-engine-setup/system/Makefile
        src/plugins/ovirt-hosted-engine-setup/vm/Makefile
diff --git a/ovirt-hosted-engine-setup.spec.in 
b/ovirt-hosted-engine-setup.spec.in
index ffb558d..6850fe6 100644
--- a/ovirt-hosted-engine-setup.spec.in
+++ b/ovirt-hosted-engine-setup.spec.in
@@ -50,6 +50,8 @@
 Requires:       openssl
 Requires:       sudo
 Requires:       bind-utils
+Requires:       sanlock >= 2.4-2
+Requires:       sanlock-python
 BuildRequires:  gettext
 BuildRequires:  otopi-devel >= 1.1.0
 BuildRequires:  python2-devel
diff --git a/src/ovirt_hosted_engine_setup/constants.py 
b/src/ovirt_hosted_engine_setup/constants.py
index 516fe7b..f7bccfd 100644
--- a/src/ovirt_hosted_engine_setup/constants.py
+++ b/src/ovirt_hosted_engine_setup/constants.py
@@ -60,6 +60,8 @@
     LOCALSTATEDIR = '/var'
     DATADIR = '/usr/share'
     LIBEXECDIR = '/usr/libexec'
+    SD_MOUNT_PARENT_DIR = '/rhev/data-center/mnt'
+    SD_METADATA_DIR_NAME = 'ha_agent'
     OVIRT_HOSTED_ENGINE = 'ovirt-hosted-engine'
     OVIRT_HOSTED_ENGINE_SETUP = 'ovirt-hosted-engine-setup'
     VDS_CLIENT_DIR = os.path.join(
@@ -396,6 +398,13 @@
 
 @util.export
 @util.codegen
+class SanlockEnv(object):
+    SANLOCK_SERVICE = 'OVEHOSTED_SANLOCK/serviceName'
+    LOCKSPACE_NAME = 'OVEHOSTED_SANLOCK/lockspaceName'
+
+
[email protected]
[email protected]
 class Stages(object):
     CONFIG_BOOT_DEVICE = 'ohosted.boot.configuration.available'
     CONFIG_STORAGE = 'ohosted.storage.configuration.available'
@@ -404,6 +413,7 @@
     VDSMD_START = 'ohosted.vdsm.started'
     VDSMD_PKI = 'ohosted.vdsm.pki.available'
     VDSMD_CONFIGURED = 'ohosted.vdsm.configured'
+    SANLOCK_INITIALIZED = 'ohosted.sanlock.initialized'
     STORAGE_AVAILABLE = 'ohosted.storage.available'
     VM_IMAGE_AVAILABLE = 'ohosted.vm.image.available'
     VM_CONFIGURED = 'ohosted.vm.state.configured'
@@ -430,6 +440,8 @@
     DEFAULT_STORAGE_DOMAIN_NAME = 'hosted_storage'
     DEFAULT_STORAGE_DATACENTER_NAME = 'hosted_datacenter'
     DEFAULT_VDSMD_SERVICE = 'vdsmd'
+    DEFAULT_SANLOCK_SERVICE = 'sanlock'
+    DEFAULT_LOCKSPACE_NAME = 'hosted-engine'
     DEFAULT_IMAGE_DESC = 'Hosted Engine Image'
     DEFAULT_IMAGE_SIZE_GB = 25  # based on minimum requirements.
     DEFAULT_MEM_SIZE_MB = 4096  # based on minimum requirements.
diff --git a/src/plugins/ovirt-hosted-engine-setup/Makefile.am 
b/src/plugins/ovirt-hosted-engine-setup/Makefile.am
index 11be18f..05cb021 100644
--- a/src/plugins/ovirt-hosted-engine-setup/Makefile.am
+++ b/src/plugins/ovirt-hosted-engine-setup/Makefile.am
@@ -26,6 +26,7 @@
        ha \
        network \
        pki \
+       sanlock \
        storage \
        system \
        vm \
diff --git a/src/plugins/ovirt-hosted-engine-setup/sanlock/Makefile.am 
b/src/plugins/ovirt-hosted-engine-setup/sanlock/Makefile.am
new file mode 100644
index 0000000..b6dbc3d
--- /dev/null
+++ b/src/plugins/ovirt-hosted-engine-setup/sanlock/Makefile.am
@@ -0,0 +1,38 @@
+#
+# ovirt-hosted-engine-setup -- ovirt with a manager in a VM
+# 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
+#
+
+include $(top_srcdir)/build/python.inc
+
+MAINTAINERCLEANFILES = \
+       $(srcdir)/Makefile.in \
+       $(NULL)
+
+mydir=$(ovirthostedengineplugindir)/ovirt-hosted-engine-setup/sanlock
+dist_my_PYTHON = \
+       __init__.py \
+       lockspace.py \
+       $(NULL)
+
+clean-local: \
+       python-clean \
+       $(NULL)
+
+all-local: \
+       python-syntax-check \
+       $(NULL)
diff --git a/src/plugins/ovirt-hosted-engine-setup/sanlock/__init__.py 
b/src/plugins/ovirt-hosted-engine-setup/sanlock/__init__.py
new file mode 100644
index 0000000..af3b015
--- /dev/null
+++ b/src/plugins/ovirt-hosted-engine-setup/sanlock/__init__.py
@@ -0,0 +1,35 @@
+#
+# 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
+#
+
+
+"""hosted engine sanlock configuration plugin."""
+
+
+from otopi import util
+
+
+from . import lockspace
+
+
[email protected]
+def createPlugins(context):
+    lockspace.Plugin(context=context)
+
+
+# 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
new file mode 100644
index 0000000..d54c839
--- /dev/null
+++ b/src/plugins/ovirt-hosted-engine-setup/sanlock/lockspace.py
@@ -0,0 +1,161 @@
+#
+# 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
+#
+
+
+"""
+sanlock lockspace initialization plugin.
+"""
+
+import gettext
+import glob
+import os
+import sanlock
+import stat
+
+
+from otopi import util
+from otopi import plugin
+
+
+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):
+    """
+    sanlock lockspace initialization plugin.
+    """
+
+    def __init__(self, context):
+        super(Plugin, self).__init__(context=context)
+
+    def _get_metadata_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 os.path.join(
+            domains[0],
+            ohostedcons.FileLocations.SD_METADATA_DIR_NAME,
+        )
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_INIT
+    )
+    def _init(self):
+        self.environment.setdefault(
+            ohostedcons.SanlockEnv.SANLOCK_SERVICE,
+            ohostedcons.Defaults.DEFAULT_SANLOCK_SERVICE
+        )
+        self.environment.setdefault(
+            ohostedcons.SanlockEnv.LOCKSPACE_NAME,
+            ohostedcons.Defaults.DEFAULT_LOCKSPACE_NAME
+        )
+
+    @plugin.event(
+        stage=plugin.Stages.STAGE_MISC,
+        name=ohostedcons.Stages.SANLOCK_INITIALIZED,
+        condition=lambda self: not self.environment[
+            ohostedcons.CoreEnv.IS_ADDITIONAL_HOST
+        ],
+        after=(
+            ohostedcons.Stages.STORAGE_AVAILABLE,
+        ),
+    )
+    def _misc(self):
+        self.logger.info(_('Verifying sanlock lockspace initialization'))
+        self.services.state(
+            name=self.environment[
+                ohostedcons.SanlockEnv.SANLOCK_SERVICE
+            ],
+            state=True,
+        )
+        metadatadir = self._get_metadata_path()
+        lockspace = self.environment[ohostedcons.SanlockEnv.LOCKSPACE_NAME]
+        lease_file = os.path.join(
+            metadatadir,
+            lockspace + '.lockspace'
+        )
+        host_id = self.environment[ohostedcons.StorageEnv.HOST_ID]
+        self.logger.debug(
+            (
+                'Ensuring lease for lockspace {lockspace}, '
+                'host id {host_id} '
+                'is acquired (file: {lease_file})'
+            ).format(
+                lockspace=lockspace,
+                host_id=host_id,
+                lease_file=lease_file,
+            )
+        )
+        if not os.path.isdir(metadatadir):
+            self.logger.debug('Creating metadata directory')
+            os.mkdir(metadatadir)
+            os.chown(
+                metadatadir,
+                self.environment[ohostedcons.VDSMEnv.VDSM_UID],
+                self.environment[ohostedcons.VDSMEnv.KVM_GID]
+            )
+        if os.path.exists(lease_file):
+            self.logger.info(_('sanlock lockspace already initialized'))
+        else:
+            self.logger.info(_('Initializing sanlock lockspace'))
+            open(lease_file, 'w').close()
+            os.chmod(
+                lease_file,
+                stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP
+            )
+            os.chown(
+                lease_file,
+                self.environment[ohostedcons.VDSMEnv.VDSM_UID],
+                self.environment[ohostedcons.VDSMEnv.KVM_GID]
+            )
+            if hasattr(sanlock, 'write_lockspace'):
+                #new sanlock (Fedora >= 19)
+                sanlock.write_lockspace(
+                    lockspace=lockspace,
+                    path=lease_file,
+                )
+            else:
+                #legacy sanlock (Fedora <= 18)
+                sanlock.init_lockspace(
+                    lockspace=lockspace,
+                    path=lease_file,
+                )
+
+
+# vim: expandtab tabstop=4 shiftwidth=4
diff --git a/src/plugins/ovirt-hosted-engine-setup/vm/image.py 
b/src/plugins/ovirt-hosted-engine-setup/vm/image.py
index e03011b..458ef26 100644
--- a/src/plugins/ovirt-hosted-engine-setup/vm/image.py
+++ b/src/plugins/ovirt-hosted-engine-setup/vm/image.py
@@ -142,7 +142,7 @@
     @plugin.event(
         stage=plugin.Stages.STAGE_MISC,
         after=(
-            ohostedcons.Stages.STORAGE_AVAILABLE,
+            ohostedcons.Stages.SANLOCK_INITIALIZED,
         ),
         name=ohostedcons.Stages.VM_IMAGE_AVAILABLE,
         condition=lambda self: not self.environment[


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

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

Reply via email to