Alon Bar-Lev has uploaded a new change for review.

Change subject: packaging: engine-service: systemd usage
......................................................................

packaging: engine-service: systemd usage

Use features of systemd instead our own implementation.

1. detach from controlling terminal.
2. Change user/group.
3. stop service by intercepting signal.
4. set rlimit.

This makes it simpler for us, as only the start method of the
engine-service is actually used, in both systemd and openrc.

After this we can also modify the system-v to use the proper
/etc/init.d/function and use the pidfile for status and stop, so that
only start will be left in the service script.

The background mode is left only for system-v, but this is the only
feature required, other features are provided by the daemon function.

Change-Id: Ie02f0d44bbba7d1a7af6bcf6b808610e85b907c6
Signed-off-by: Alon Bar-Lev <alo...@redhat.com>
---
M .gitignore
M Makefile
M packaging/fedora/engine-service.py.in
D packaging/fedora/engine-service.systemd
A packaging/fedora/engine-service.systemd.in
M packaging/fedora/spec/ovirt-engine.spec.in
6 files changed, 87 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/88/13488/1

diff --git a/.gitignore b/.gitignore
index 62b12fb..ad418b5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,4 +49,5 @@
 backend/manager/tools/src/main/conf/engine-notifier-log4j.xml
 packaging/fedora/engine-service.py
 packaging/fedora/engine-service.limits
+packaging/fedora/engine-service.systemd
 packaging/fedora/spec/ovirt-engine.spec
diff --git a/Makefile b/Makefile
index 5bd1d18..d90d355 100644
--- a/Makefile
+++ b/Makefile
@@ -125,6 +125,7 @@
        backend/manager/tools/src/main/conf/engine-notifier-log4j.xml \
        packaging/fedora/engine-service.py \
        packaging/fedora/engine-service.limits \
+       packaging/fedora/engine-service.systemd \
        packaging/fedora/spec/ovirt-engine.spec \
        $(NULL)
 
@@ -236,7 +237,6 @@
        @install -dm 755 $(DESTDIR)$(PKG_SYSCONF_DIR)/engine-config
        @install -dm 755 $(DESTDIR)$(PKG_SYSCONF_DIR)/engine-manage-domains
        @install -dm 755 $(DESTDIR)$(SYSCONF_DIR)/cron.daily
-       @install -dm 755 $(DESTDIR)$(SYSCONF_DIR)/security/limits.d
        @install -dm 755 $(DESTDIR)$(SYSCONF_DIR)/rc.d/init.d
        @install -dm 755 $(DESTDIR)$(SYSCONF_DIR)/firewalld/services
 
@@ -418,7 +418,6 @@
        install -m 644 packaging/fedora/engine-service.xml.in 
$(DESTDIR)$(DATA_DIR)/service
        install -m 644 packaging/fedora/engine-service-logging.properties.in 
$(DESTDIR)$(DATA_DIR)/service
        install -m 755 packaging/fedora/engine-service.py 
$(DESTDIR)$(DATA_DIR)/service
-       install -m 644 packaging/fedora/engine-service.limits 
$(DESTDIR)$(SYSCONF_DIR)/security/limits.d/10-$(ENGINE_NAME).conf
 
        # Install the links:
        ln -sf $(DATA_DIR)/service/engine-service.py 
$(DESTDIR)$(BIN_DIR)/engine-service
diff --git a/packaging/fedora/engine-service.py.in 
b/packaging/fedora/engine-service.py.in
index 76ed4dc..fdf85af 100644
--- a/packaging/fedora/engine-service.py.in
+++ b/packaging/fedora/engine-service.py.in
@@ -377,6 +377,36 @@
         os.remove(enginePidFile)
 
 
+def stopPid(pid):
+    # Get the time to wait for the engine to stop from the configuration:
+    stopTime = engineConfig.getInteger("ENGINE_STOP_TIME")
+    stopInterval = engineConfig.getInteger("ENGINE_STOP_INTERVAL")
+
+    # Kill the process softly and wait for it to dissapear or for the timeout
+    # to expire:
+    os.kill(pid, signal.SIGTERM)
+    initialTime = time.time()
+    timeElapsed = 0
+    while isProcessRunning(pid):
+        # remove zombies
+        os.waitpid(-1, os.WNOHANG)
+
+        syslog.syslog(syslog.LOG_INFO, "Waiting up to %d seconds for process 
%d to finish." % ((stopTime - timeElapsed), pid))
+        timeElapsed = time.time() - initialTime
+        if timeElapsed > stopTime:
+            break
+        time.sleep(stopInterval)
+
+    # If the process didn't dissapear after the allowed time then we forcibly
+    # kill it:
+    if isProcessRunning(pid):
+        syslog.syslog(syslog.LOG_WARNING, "The process %d didn't finish after 
waiting %d seconds, killing it." % (pid, timeElapsed))
+        os.kill(pid, signal.SIGKILL)
+        syslog.syslog(syslog.LOG_WARNING, "Killed process %d." % pid)
+    else:
+        syslog.syslog(syslog.LOG_INFO, "Stopped process %d." % pid)
+
+
 def startEngine():
     # perform checks:
     checkInstallation()
@@ -580,13 +610,44 @@
         stderr=engineConsoleLog,
     ):
         saveEnginePid(os.getpid())
-        os.execvpe(
-            file=javaLauncher,
-            args=engineArgs,
-            env=engineEnv,
-        )
 
-    raise Exception("Can't execute \"%s\"." % javaLauncher)
+        child = 0
+        if foreground:
+            child = os.fork()
+
+        if child == 0:
+            os.execvpe(
+                file=javaLauncher,
+                args=engineArgs,
+                env=engineEnv,
+            )
+            raise Exception("Can't execute \"%s\"." % javaLauncher)
+
+        #
+        # jboss exists with non zero
+        # exit code when receiving SIGTERM
+        # so we need a wrapper to fix that
+        # when running foreground
+        #
+        def myterm(signum, frame):
+            stopPid(child)
+            sys.exit(0)
+        signal.signal(signal.SIGTERM, myterm)
+
+        #
+        # notice: only foreground reaches here
+        #
+        while True:
+            pid, status = os.waitpid(child, 0)
+            if pid == child:
+                if os.WIFEXITED(status):
+                    if os.WEXITSTATUS(status) != 0:
+                        raise Exception("Engine terminated with status code 
%s" % os.WEXITSTATUS(status))
+                    break
+                elif os.WIFSIGNALED(status) != 0:
+                    if os.WTERMSIG(status) != signal.SIGTERM:
+                        raise Exception("Engine terminated by signal %s" % 
os.WTERMSIG(status))
+                    break
 
 
 def stopEngine():
@@ -605,30 +666,7 @@
         removeEnginePid()
         return
 
-    # Get the time to wait for the engine to stop from the configuration:
-    stopTime = engineConfig.getInteger("ENGINE_STOP_TIME")
-    stopInterval = engineConfig.getInteger("ENGINE_STOP_INTERVAL")
-
-    # Kill the process softly and wait for it to dissapear or for the timeout
-    # to expire:
-    os.kill(enginePid, signal.SIGTERM)
-    initialTime = time.time()
-    timeElapsed = 0
-    while isProcessRunning(enginePid):
-        syslog.syslog(syslog.LOG_INFO, "Waiting up to %d seconds for engine 
process %d to finish." % ((stopTime - timeElapsed), enginePid))
-        timeElapsed = time.time() - initialTime
-        if timeElapsed > stopTime:
-            break
-        time.sleep(stopInterval)
-
-    # If the process didn't dissapear after the allowed time then we forcibly
-    # kill it:
-    if isProcessRunning(enginePid):
-        syslog.syslog(syslog.LOG_WARNING, "The engine process %d didn't finish 
after waiting %d seconds, killing it." % (enginePid, timeElapsed))
-        os.kill(enginePid, signal.SIGKILL)
-        syslog.syslog(syslog.LOG_WARNING, "Killed engine process %d." % 
enginePid)
-    else:
-        syslog.syslog(syslog.LOG_INFO, "Stopped engine process %d." % 
enginePid)
+    stopPid(enginePid)
 
     # Remove the PID file:
     removeEnginePid()
diff --git a/packaging/fedora/engine-service.systemd 
b/packaging/fedora/engine-service.systemd
deleted file mode 100644
index 3c81cdd..0000000
--- a/packaging/fedora/engine-service.systemd
+++ /dev/null
@@ -1,12 +0,0 @@
-[Unit]
-Description=oVirt Engine
-After=network.service postgresql.service
-
-[Service]
-Type=forking
-PIDFile=/var/run/ovirt-engine.pid
-ExecStart=/usr/bin/engine-service start
-ExecStop=/usr/bin/engine-service stop
-
-[Install]
-WantedBy=multi-user.target
diff --git a/packaging/fedora/engine-service.systemd.in 
b/packaging/fedora/engine-service.systemd.in
new file mode 100644
index 0000000..7772c71
--- /dev/null
+++ b/packaging/fedora/engine-service.systemd.in
@@ -0,0 +1,13 @@
+[Unit]
+Description=oVirt Engine
+After=network.service postgresql.service
+
+[Service]
+Type=simple
+User=@ENGINE_USER@
+Group=@ENGINE_GROUP@
+LimitNOFILE=65535
+ExecStart=/usr/bin/engine-service --foreground --quiet start
+
+[Install]
+WantedBy=multi-user.target
diff --git a/packaging/fedora/spec/ovirt-engine.spec.in 
b/packaging/fedora/spec/ovirt-engine.spec.in
index c127b8f..32e6809 100644
--- a/packaging/fedora/spec/ovirt-engine.spec.in
+++ b/packaging/fedora/spec/ovirt-engine.spec.in
@@ -352,6 +352,8 @@
 
 # Install System V init scripts:
 %if %{install_systemv}
+install -d -m 755 %{buildroot}%{_sysconfdir}/security/limits.d
+install -d -m 644 packaging/fedora/engine-service.limits 
%{buildroot}%{_sysconfdir}/security/limits.d/10-ovirt-engine.conf
 install -dm 755 %{buildroot}%{_sysconfdir}/rc.d/init.d
 ln -s %{engine_data}/service/engine-service.py 
%{buildroot}%{_sysconfdir}/rc.d/init.d/ovirt-engine
 %endif
@@ -414,6 +416,7 @@
 %dir %{engine_data}/scripts
 %dir %{engine_run}
 %dir %attr(-, %{engine_user}, %{engine_group}) %{engine_log}
+%dir %attr(-, %{engine_user}, %{engine_group}) %{engine_cache}
 
 # Log rotation script:
 %{engine_data}/scripts/ovirtlogrot.sh
@@ -434,13 +437,13 @@
 %{engine_data}/conf/version
 
 # Files needed by the service:
-%{_sysconfdir}/security/limits.d/10-ovirt-engine.conf
 %{engine_data}/service
 %{_bindir}/engine-service
 %if %{install_systemd}
 %{_unitdir}/%{engine_name}.service
 %endif
 %if %{install_systemv}
+%{_sysconfdir}/security/limits.d/10-ovirt-engine.conf
 %{_initddir}/ovirt-engine
 %endif
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie02f0d44bbba7d1a7af6bcf6b808610e85b907c6
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alon Bar-Lev <alo...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to