3604df
From a3f0b6fce07d6849d7b98353067c4c9f1e926751 Mon Sep 17 00:00:00 2001
3604df
From: Aravinda VK <avishwan@redhat.com>
3604df
Date: Wed, 31 Aug 2016 08:33:44 +0530
3604df
Subject: [PATCH 44/86] eventsapi: Add Init scripts for different distributions
3604df
3604df
Added init scripts for
3604df
- SysvInit(CentOS 6 or Red Hat 6)
3604df
- rc.d (FreeBSD)
3604df
3604df
Most of the latest distributions are using systemd. Support to be
3604df
added for other distributions which are not using systemd.
3604df
3604df
Removed systemctl wrapper functions(start/stop/status) from
3604df
gluster-eventsapi CLI(peer_eventsapi.py). Status and Reload
3604df
re-implemented using pid file check.
3604df
3604df
Added pid file support for glustereventsd.
3604df
3604df
Following dependencies removed
3604df
python-flask - Only used for example dashboard. User can install
3604df
if required.
3604df
python-fasteners - Not available for EPEL 6, added custom code using
3604df
fcntl as replacement.
3604df
3604df
> Reviewed-on: http://review.gluster.org/15367
3604df
> Smoke: Gluster Build System <jenkins@build.gluster.org>
3604df
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
3604df
> Reviewed-by: Niels de Vos <ndevos@redhat.com>
3604df
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
3604df
> Reviewed-by: Kaleb KEITHLEY <kkeithle@redhat.com>
3604df
3604df
BUG: 1351589
3604df
Change-Id: I26792eae9b11e93304f70b3997cd7d8d03b067f4
3604df
Signed-off-by: Aravinda VK <avishwan@redhat.com>
3604df
Reviewed-on: https://code.engineering.redhat.com/gerrit/84747
3604df
Reviewed-by: Milind Changire <mchangir@redhat.com>
3604df
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
3604df
---
3604df
 configure.ac                             |    2 +
3604df
 events/src/eventsapiconf.py.in           |    1 +
3604df
 events/src/glustereventsd.py             |   21 ++++-
3604df
 events/src/peer_eventsapi.py             |  160 +++++++++---------------------
3604df
 events/src/utils.py                      |   77 ++++++++++++++
3604df
 extras/init.d/Makefile.am                |   11 ++-
3604df
 extras/init.d/glustereventsd-FreeBSD.in  |   19 ++++
3604df
 extras/init.d/glustereventsd-Redhat.in   |  129 ++++++++++++++++++++++++
3604df
 extras/systemd/glustereventsd.service.in |    3 +-
3604df
 glusterfs.spec.in                        |   19 +++-
3604df
 10 files changed, 322 insertions(+), 120 deletions(-)
3604df
 create mode 100644 extras/init.d/glustereventsd-FreeBSD.in
3604df
 create mode 100644 extras/init.d/glustereventsd-Redhat.in
3604df
3604df
diff --git a/configure.ac b/configure.ac
3604df
index d77f41f..d84398d 100644
3604df
--- a/configure.ac
3604df
+++ b/configure.ac
3604df
@@ -200,6 +200,8 @@ AC_CONFIG_FILES([Makefile
3604df
                 extras/init.d/glusterd-Redhat
3604df
                 extras/init.d/glusterd-FreeBSD
3604df
                 extras/init.d/glusterd-SuSE
3604df
+                extras/init.d/glustereventsd-Redhat
3604df
+                extras/init.d/glustereventsd-FreeBSD
3604df
                 extras/ganesha/Makefile
3604df
                 extras/ganesha/config/Makefile
3604df
                 extras/ganesha/scripts/Makefile
3604df
diff --git a/events/src/eventsapiconf.py.in b/events/src/eventsapiconf.py.in
3604df
index fad96ca..ecccd3d 100644
3604df
--- a/events/src/eventsapiconf.py.in
3604df
+++ b/events/src/eventsapiconf.py.in
3604df
@@ -23,3 +23,4 @@ INT_CONFIGS = ["port"]
3604df
 RESTART_CONFIGS = ["port"]
3604df
 EVENTS_ENABLED = @EVENTS_ENABLED@
3604df
 UUID_FILE = "@GLUSTERD_WORKDIR@/glusterd.info"
3604df
+PID_FILE = "@localstatedir@/run/glustereventsd.pid"
3604df
diff --git a/events/src/glustereventsd.py b/events/src/glustereventsd.py
3604df
index 91a0743..d057e09 100644
3604df
--- a/events/src/glustereventsd.py
3604df
+++ b/events/src/glustereventsd.py
3604df
@@ -15,12 +15,13 @@ import sys
3604df
 import signal
3604df
 import SocketServer
3604df
 import socket
3604df
+from argparse import ArgumentParser, RawDescriptionHelpFormatter
3604df
 
3604df
 from eventtypes import all_events
3604df
 import handlers
3604df
 import utils
3604df
-from eventsapiconf import SERVER_ADDRESS
3604df
-from utils import logger
3604df
+from eventsapiconf import SERVER_ADDRESS, PID_FILE
3604df
+from utils import logger, PidFile, PidFileLockFailed
3604df
 
3604df
 
3604df
 class GlusterEventsRequestHandler(SocketServer.BaseRequestHandler):
3604df
@@ -90,9 +91,23 @@ def init_event_server():
3604df
     server.serve_forever()
3604df
 
3604df
 
3604df
+def get_args():
3604df
+    parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
3604df
+                            description=__doc__)
3604df
+    parser.add_argument("-p", "--pid-file", help="PID File",
3604df
+                        default=PID_FILE)
3604df
+
3604df
+    return parser.parse_args()
3604df
+
3604df
+
3604df
 def main():
3604df
+    args = get_args()
3604df
     try:
3604df
-        init_event_server()
3604df
+        with PidFile(args.pid_file):
3604df
+            init_event_server()
3604df
+    except PidFileLockFailed as e:
3604df
+        sys.stderr.write("Failed to get lock for pid file({0}): {1}".format(
3604df
+            args.pid_file, e))
3604df
     except KeyboardInterrupt:
3604df
         sys.exit(1)
3604df
 
3604df
diff --git a/events/src/peer_eventsapi.py b/events/src/peer_eventsapi.py
3604df
index f444778..7f80f79 100644
3604df
--- a/events/src/peer_eventsapi.py
3604df
+++ b/events/src/peer_eventsapi.py
3604df
@@ -14,14 +14,17 @@ from __future__ import print_function
3604df
 import os
3604df
 import json
3604df
 from errno import EEXIST
3604df
+import fcntl
3604df
+from errno import EACCES, EAGAIN
3604df
+import signal
3604df
 
3604df
 import requests
3604df
-import fasteners
3604df
 from prettytable import PrettyTable
3604df
 
3604df
 from gluster.cliutils import (Cmd, execute, node_output_ok, node_output_notok,
3604df
                               sync_file_to_peers, GlusterCmdException,
3604df
                               output_error, execute_in_peers, runcli)
3604df
+from events.utils import LockedOpen
3604df
 
3604df
 from events.eventsapiconf import (WEBHOOKS_FILE_TO_SYNC,
3604df
                                   WEBHOOKS_FILE,
3604df
@@ -32,6 +35,7 @@ from events.eventsapiconf import (WEBHOOKS_FILE_TO_SYNC,
3604df
                                   CONFIG_KEYS,
3604df
                                   BOOL_CONFIGS,
3604df
                                   INT_CONFIGS,
3604df
+                                  PID_FILE,
3604df
                                   RESTART_CONFIGS)
3604df
 
3604df
 
3604df
@@ -78,67 +82,36 @@ def mkdirp(path, exit_on_err=False, logger=None):
3604df
             output_error("Fail to create dir %s: %s" % (path, e))
3604df
 
3604df
 
3604df
-def is_enabled(service):
3604df
-    rc, out, err = execute(["systemctl", "is-enabled", service])
3604df
-    return rc == 0
3604df
-
3604df
-
3604df
-def is_active(service):
3604df
-    rc, out, err = execute(["systemctl", "is-active", service])
3604df
-    return rc == 0
3604df
-
3604df
-
3604df
-def enable_service(service):
3604df
-    if not is_enabled(service):
3604df
-        cmd = ["systemctl", "enable", service]
3604df
-        return execute(cmd)
3604df
-
3604df
-    return (0, "", "")
3604df
-
3604df
-
3604df
-def disable_service(service):
3604df
-    if is_enabled(service):
3604df
-        cmd = ["systemctl", "disable", service]
3604df
-        return execute(cmd)
3604df
-
3604df
-    return (0, "", "")
3604df
-
3604df
-
3604df
-def start_service(service):
3604df
-    rc, out, err = enable_service(service)
3604df
-    if rc != 0:
3604df
-        return (rc, out, err)
3604df
-
3604df
-    cmd = ["systemctl", "start", service]
3604df
-    return execute(cmd)
3604df
-
3604df
-
3604df
-def stop_service(service):
3604df
-    rc, out, err = disable_service(service)
3604df
-    if rc != 0:
3604df
-        return (rc, out, err)
3604df
-
3604df
-    cmd = ["systemctl", "stop", service]
3604df
-    return execute(cmd)
3604df
-
3604df
-
3604df
-def restart_service(service):
3604df
-    rc, out, err = stop_service(service)
3604df
-    if rc != 0:
3604df
-        return (rc, out, err)
3604df
-
3604df
-    return start_service(service)
3604df
+def is_active():
3604df
+    state = False
3604df
+    try:
3604df
+        with open(PID_FILE, "a+") as f:
3604df
+            fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
3604df
+            state = False
3604df
+    except (IOError, OSError) as e:
3604df
+        if e.errno in (EACCES, EAGAIN):
3604df
+            # cannot grab. so, process still running..move on
3604df
+            state = True
3604df
+        else:
3604df
+            state = False
3604df
+    return state
3604df
 
3604df
 
3604df
-def reload_service(service):
3604df
-    if is_active(service):
3604df
-        cmd = ["systemctl", "reload", service]
3604df
-        return execute(cmd)
3604df
+def reload_service():
3604df
+    pid = None
3604df
+    if is_active():
3604df
+        with open(PID_FILE) as f:
3604df
+            try:
3604df
+                pid = int(f.read().strip())
3604df
+            except ValueError:
3604df
+                pid = None
3604df
+        if pid is not None:
3604df
+            os.kill(pid, signal.SIGUSR2)
3604df
 
3604df
     return (0, "", "")
3604df
 
3604df
 
3604df
-def sync_to_peers(restart=False):
3604df
+def sync_to_peers():
3604df
     if os.path.exists(WEBHOOKS_FILE):
3604df
         try:
3604df
             sync_file_to_peers(WEBHOOKS_FILE_TO_SYNC)
3604df
@@ -153,11 +126,7 @@ def sync_to_peers(restart=False):
3604df
             output_error("Failed to sync Config file: [Error: {0}]"
3604df
                          "{1}".format(e[0], e[2]))
3604df
 
3604df
-    action = "node-reload"
3604df
-    if restart:
3604df
-        action = "node-restart"
3604df
-
3604df
-    out = execute_in_peers(action)
3604df
+    out = execute_in_peers("node-reload")
3604df
     table = PrettyTable(["NODE", "NODE STATUS", "SYNC STATUS"])
3604df
     table.align["NODE STATUS"] = "r"
3604df
     table.align["SYNC STATUS"] = "r"
3604df
@@ -204,53 +173,11 @@ def action_handle(action):
3604df
     print (table)
3604df
 
3604df
 
3604df
-class NodeStart(Cmd):
3604df
-    name = "node-start"
3604df
-
3604df
-    def run(self, args):
3604df
-        node_output_handle(start_service(EVENTSD))
3604df
-
3604df
-
3604df
-class StartCmd(Cmd):
3604df
-    name = "start"
3604df
-
3604df
-    def run(self, args):
3604df
-        action_handle("start")
3604df
-
3604df
-
3604df
-class NodeStop(Cmd):
3604df
-    name = "node-stop"
3604df
-
3604df
-    def run(self, args):
3604df
-        node_output_handle(stop_service(EVENTSD))
3604df
-
3604df
-
3604df
-class StopCmd(Cmd):
3604df
-    name = "stop"
3604df
-
3604df
-    def run(self, args):
3604df
-        action_handle("stop")
3604df
-
3604df
-
3604df
-class NodeRestart(Cmd):
3604df
-    name = "node-restart"
3604df
-
3604df
-    def run(self, args):
3604df
-        node_output_handle(restart_service(EVENTSD))
3604df
-
3604df
-
3604df
-class RestartCmd(Cmd):
3604df
-    name = "restart"
3604df
-
3604df
-    def run(self, args):
3604df
-        action_handle("restart")
3604df
-
3604df
-
3604df
 class NodeReload(Cmd):
3604df
     name = "node-reload"
3604df
 
3604df
     def run(self, args):
3604df
-        node_output_handle(reload_service(EVENTSD))
3604df
+        node_output_handle(reload_service())
3604df
 
3604df
 
3604df
 class ReloadCmd(Cmd):
3604df
@@ -264,7 +191,7 @@ class NodeStatus(Cmd):
3604df
     name = "node-status"
3604df
 
3604df
     def run(self, args):
3604df
-        node_output_ok("UP" if is_active(EVENTSD) else "DOWN")
3604df
+        node_output_ok("UP" if is_active() else "DOWN")
3604df
 
3604df
 
3604df
 class StatusCmd(Cmd):
3604df
@@ -294,7 +221,7 @@ class WebhookAddCmd(Cmd):
3604df
     def run(self, args):
3604df
         create_webhooks_file_if_not_exists()
3604df
 
3604df
-        with fasteners.InterProcessLock(WEBHOOKS_FILE):
3604df
+        with LockedOpen(WEBHOOKS_FILE, 'r+'):
3604df
             data = json.load(open(WEBHOOKS_FILE))
3604df
             if data.get(args.url, None) is not None:
3604df
                 output_error("Webhook already exists")
3604df
@@ -316,7 +243,7 @@ class WebhookModCmd(Cmd):
3604df
     def run(self, args):
3604df
         create_webhooks_file_if_not_exists()
3604df
 
3604df
-        with fasteners.InterProcessLock(WEBHOOKS_FILE):
3604df
+        with LockedOpen(WEBHOOKS_FILE, 'r+'):
3604df
             data = json.load(open(WEBHOOKS_FILE))
3604df
             if data.get(args.url, None) is None:
3604df
                 output_error("Webhook does not exists")
3604df
@@ -336,7 +263,7 @@ class WebhookDelCmd(Cmd):
3604df
     def run(self, args):
3604df
         create_webhooks_file_if_not_exists()
3604df
 
3604df
-        with fasteners.InterProcessLock(WEBHOOKS_FILE):
3604df
+        with LockedOpen(WEBHOOKS_FILE, 'r+'):
3604df
             data = json.load(open(WEBHOOKS_FILE))
3604df
             if data.get(args.url, None) is None:
3604df
                 output_error("Webhook does not exists")
3604df
@@ -445,7 +372,9 @@ class ConfigSetCmd(Cmd):
3604df
         if args.name not in CONFIG_KEYS:
3604df
             output_error("Invalid Config item")
3604df
 
3604df
-        with fasteners.InterProcessLock(CUSTOM_CONFIG_FILE):
3604df
+        create_custom_config_file_if_not_exists()
3604df
+
3604df
+        with LockedOpen(CUSTOM_CONFIG_FILE, 'r+'):
3604df
             data = json.load(open(DEFAULT_CONFIG_FILE))
3604df
             if os.path.exists(CUSTOM_CONFIG_FILE):
3604df
                 config_json = read_file_content_json(CUSTOM_CONFIG_FILE)
3604df
@@ -456,7 +385,6 @@ class ConfigSetCmd(Cmd):
3604df
                 return
3604df
 
3604df
             # TODO: Validate Value
3604df
-            create_custom_config_file_if_not_exists()
3604df
             new_data = read_file_content_json(CUSTOM_CONFIG_FILE)
3604df
 
3604df
             v = args.value
3604df
@@ -474,7 +402,9 @@ class ConfigSetCmd(Cmd):
3604df
             if args.name in RESTART_CONFIGS:
3604df
                 restart = True
3604df
 
3604df
-            sync_to_peers(restart=restart)
3604df
+            sync_to_peers()
3604df
+            if restart:
3604df
+                print ("\nRestart glustereventsd in all nodes")
3604df
 
3604df
 
3604df
 class ConfigResetCmd(Cmd):
3604df
@@ -484,7 +414,9 @@ class ConfigResetCmd(Cmd):
3604df
         parser.add_argument("name", help="Config Name or all")
3604df
 
3604df
     def run(self, args):
3604df
-        with fasteners.InterProcessLock(CUSTOM_CONFIG_FILE):
3604df
+        create_custom_config_file_if_not_exists()
3604df
+
3604df
+        with LockedOpen(CUSTOM_CONFIG_FILE, 'r+'):
3604df
             changed_keys = []
3604df
             data = {}
3604df
             if os.path.exists(CUSTOM_CONFIG_FILE):
3604df
@@ -511,7 +443,9 @@ class ConfigResetCmd(Cmd):
3604df
                     restart = True
3604df
                     break
3604df
 
3604df
-            sync_to_peers(restart=restart)
3604df
+            sync_to_peers()
3604df
+            if restart:
3604df
+                print ("\nRestart glustereventsd in all nodes")
3604df
 
3604df
 
3604df
 class SyncCmd(Cmd):
3604df
diff --git a/events/src/utils.py b/events/src/utils.py
3604df
index 386e8f2..db8ebfe 100644
3604df
--- a/events/src/utils.py
3604df
+++ b/events/src/utils.py
3604df
@@ -12,6 +12,8 @@
3604df
 import json
3604df
 import os
3604df
 import logging
3604df
+import fcntl
3604df
+from errno import ESRCH, EBADF
3604df
 
3604df
 import requests
3604df
 from eventsapiconf import (LOG_FILE,
3604df
@@ -168,3 +170,78 @@ def plugin_webhook(message):
3604df
                             url=url,
3604df
                             event=message_json,
3604df
                             status_code=resp.status_code))
3604df
+
3604df
+
3604df
+class LockedOpen(object):
3604df
+
3604df
+    def __init__(self, filename, *args, **kwargs):
3604df
+        self.filename = filename
3604df
+        self.open_args = args
3604df
+        self.open_kwargs = kwargs
3604df
+        self.fileobj = None
3604df
+
3604df
+    def __enter__(self):
3604df
+        """
3604df
+        If two processes compete to update a file, The first process
3604df
+        gets the lock and the second process is blocked in the fcntl.flock()
3604df
+        call. When first process replaces the file and releases the lock,
3604df
+        the already open file descriptor in the second process now points
3604df
+        to a  "ghost" file(not reachable by any path name) with old contents.
3604df
+        To avoid that conflict, check the fd already opened is same or
3604df
+        not. Open new one if not same
3604df
+        """
3604df
+        f = open(self.filename, *self.open_args, **self.open_kwargs)
3604df
+        while True:
3604df
+            fcntl.flock(f, fcntl.LOCK_EX)
3604df
+            fnew = open(self.filename, *self.open_args, **self.open_kwargs)
3604df
+            if os.path.sameopenfile(f.fileno(), fnew.fileno()):
3604df
+                fnew.close()
3604df
+                break
3604df
+            else:
3604df
+                f.close()
3604df
+                f = fnew
3604df
+        self.fileobj = f
3604df
+        return f
3604df
+
3604df
+    def __exit__(self, _exc_type, _exc_value, _traceback):
3604df
+        self.fileobj.close()
3604df
+
3604df
+
3604df
+class PidFileLockFailed(Exception):
3604df
+    pass
3604df
+
3604df
+
3604df
+class PidFile(object):
3604df
+    def __init__(self, filename):
3604df
+        self.filename = filename
3604df
+        self.pid = os.getpid()
3604df
+        self.fh = None
3604df
+
3604df
+    def cleanup(self, remove_file=True):
3604df
+        try:
3604df
+            if self.fh is not None:
3604df
+                self.fh.close()
3604df
+        except IOError as exc:
3604df
+            if exc.errno != EBADF:
3604df
+                raise
3604df
+        finally:
3604df
+            if os.path.isfile(self.filename) and remove_file:
3604df
+                os.remove(self.filename)
3604df
+
3604df
+    def __enter__(self):
3604df
+        self.fh = open(self.filename, 'a+')
3604df
+        try:
3604df
+            fcntl.flock(self.fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
3604df
+        except IOError as exc:
3604df
+            self.cleanup(remove_file=False)
3604df
+            raise PidFileLockFailed(exc)
3604df
+
3604df
+        self.fh.seek(0)
3604df
+        self.fh.truncate()
3604df
+        self.fh.write("%d\n" % self.pid)
3604df
+        self.fh.flush()
3604df
+        self.fh.seek(0)
3604df
+        return self
3604df
+
3604df
+    def __exit__(self, _exc_type, _exc_value, _traceback):
3604df
+        self.cleanup()
3604df
diff --git a/extras/init.d/Makefile.am b/extras/init.d/Makefile.am
3604df
index 8c43e51..bd8837b 100644
3604df
--- a/extras/init.d/Makefile.am
3604df
+++ b/extras/init.d/Makefile.am
3604df
@@ -1,5 +1,7 @@
3604df
 
3604df
-EXTRA_DIST = glusterd-Debian glusterd-FreeBSD glusterd-Redhat glusterd-SuSE glusterd.plist rhel5-load-fuse.modules
3604df
+EXTRA_DIST = glusterd-Debian glusterd-FreeBSD glusterd-Redhat glusterd-SuSE \
3604df
+	glusterd.plist rhel5-load-fuse.modules \
3604df
+	glustereventsd-FreeBSD glustereventsd-Redhat
3604df
 
3604df
 CLEANFILES =
3604df
 
3604df
@@ -13,6 +15,13 @@ $(GF_DISTRIBUTION):
3604df
 		$(INSTALL_PROGRAM) glusterd-$(GF_DISTRIBUTION) $(DESTDIR)$(INIT_DIR)/glusterd; \
3604df
 	fi
3604df
 
3604df
+if BUILD_EVENTS
3604df
+	@if [ ! -d $(SYSTEMD_DIR) ]; then \
3604df
+		$(mkdir_p) $(DESTDIR)$(INIT_DIR); \
3604df
+		$(INSTALL_PROGRAM) glustereventsd-$(GF_DISTRIBUTION) $(DESTDIR)$(INIT_DIR)/glustereventsd; \
3604df
+	fi
3604df
+endif
3604df
+
3604df
 install-exec-local: $(GF_DISTRIBUTION)
3604df
 
3604df
 install-data-local:
3604df
diff --git a/extras/init.d/glustereventsd-FreeBSD.in b/extras/init.d/glustereventsd-FreeBSD.in
3604df
new file mode 100644
3604df
index 0000000..2e8303e
3604df
--- /dev/null
3604df
+++ b/extras/init.d/glustereventsd-FreeBSD.in
3604df
@@ -0,0 +1,19 @@
3604df
+#!/bin/sh
3604df
+#
3604df
+# $FreeBSD$
3604df
+#
3604df
+
3604df
+# PROVIDE: glustereventsd
3604df
+
3604df
+. /etc/rc.subr
3604df
+
3604df
+name="glustereventsd"
3604df
+rcvar=`set_rcvar`
3604df
+command=@prefix@/sbin/${name}
3604df
+command_interpreter=/usr/local/bin/python
3604df
+pidfile="/var/run/${name}.pid"
3604df
+glustereventsd_flags="-p /var/run/${name}.pid"
3604df
+start_cmd="/usr/sbin/daemon $command ${glustereventsd_flags}"
3604df
+
3604df
+load_rc_config $name
3604df
+run_rc_command "$1"
3604df
diff --git a/extras/init.d/glustereventsd-Redhat.in b/extras/init.d/glustereventsd-Redhat.in
3604df
new file mode 100644
3604df
index 0000000..d23ce4c
3604df
--- /dev/null
3604df
+++ b/extras/init.d/glustereventsd-Redhat.in
3604df
@@ -0,0 +1,129 @@
3604df
+#!/bin/bash
3604df
+#
3604df
+# glustereventsd   Startup script for the glusterfs Events server
3604df
+#
3604df
+# chkconfig:   - 20 80
3604df
+# description: Gluster Events Server
3604df
+
3604df
+### BEGIN INIT INFO
3604df
+# Provides: glustereventsd
3604df
+# Required-Start: $local_fs $network
3604df
+# Required-Stop: $local_fs $network
3604df
+# Should-Start:
3604df
+# Should-Stop:
3604df
+# Default-Start: 2 3 4 5
3604df
+# Default-Stop: 0 1 6
3604df
+# Short-Description: glusterfs Events server
3604df
+# Description:       GlusterFS Events Server
3604df
+### END INIT INFO
3604df
+#
3604df
+
3604df
+# Source function library.
3604df
+. /etc/rc.d/init.d/functions
3604df
+
3604df
+BASE=glustereventsd
3604df
+
3604df
+# Fedora File System Layout dictates /run
3604df
+[ -e /run ] && RUNDIR="/run"
3604df
+PIDFILE="${RUNDIR:-/var/run}/${BASE}.pid"
3604df
+
3604df
+PID=`test -f $PIDFILE && cat $PIDFILE`
3604df
+
3604df
+GLUSTEREVENTSD_BIN=@prefix@/sbin/$BASE
3604df
+GLUSTEREVENTSD_OPTS="--pid-file=$PIDFILE"
3604df
+GLUSTEREVENTSD="$GLUSTEREVENTSD_BIN $GLUSTEREVENTSD_OPTS"
3604df
+RETVAL=0
3604df
+
3604df
+LOCKFILE=/var/lock/subsys/${BASE}
3604df
+
3604df
+# Start the service $BASE
3604df
+start()
3604df
+{
3604df
+       if pidofproc -p $PIDFILE $GLUSTEREVENTSD_BIN &> /dev/null; then
3604df
+           echo "glustereventsd service is already running with pid $PID"
3604df
+           return 0
3604df
+       else
3604df
+           echo -n $"Starting $BASE:"
3604df
+           daemon $GLUSTEREVENTSD &
3604df
+           RETVAL=$?
3604df
+           echo
3604df
+           [ $RETVAL -eq 0 ] && touch $LOCKFILE
3604df
+           return $RETVAL
3604df
+       fi
3604df
+}
3604df
+
3604df
+# Stop the service $BASE
3604df
+stop()
3604df
+{
3604df
+    echo -n $"Stopping $BASE:"
3604df
+    if pidofproc -p $PIDFILE $GLUSTEREVENTSD_BIN &> /dev/null; then
3604df
+        killproc -p $PIDFILE $BASE
3604df
+    else
3604df
+        killproc $BASE
3604df
+    fi
3604df
+    RETVAL=$?
3604df
+    echo
3604df
+    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
3604df
+    return $RETVAL
3604df
+}
3604df
+
3604df
+restart()
3604df
+{
3604df
+    stop
3604df
+    start
3604df
+}
3604df
+
3604df
+reload()
3604df
+{
3604df
+    restart
3604df
+}
3604df
+
3604df
+force_reload()
3604df
+{
3604df
+    restart
3604df
+}
3604df
+
3604df
+rh_status()
3604df
+{
3604df
+    status $BASE
3604df
+}
3604df
+
3604df
+rh_status_q()
3604df
+{
3604df
+    rh_status &>/dev/null
3604df
+}
3604df
+
3604df
+
3604df
+### service arguments ###
3604df
+case $1 in
3604df
+    start)
3604df
+        rh_status_q && exit 0
3604df
+        $1
3604df
+        ;;
3604df
+    stop)
3604df
+        rh_status_q || exit 0
3604df
+        $1
3604df
+        ;;
3604df
+    restart)
3604df
+        $1
3604df
+        ;;
3604df
+    reload)
3604df
+        rh_status_q || exit 7
3604df
+        $1
3604df
+        ;;
3604df
+    force-reload)
3604df
+        force_reload
3604df
+        ;;
3604df
+    status)
3604df
+        rh_status
3604df
+        ;;
3604df
+    condrestart|try-restart)
3604df
+        rh_status_q || exit 0
3604df
+        restart
3604df
+        ;;
3604df
+    *)
3604df
+        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
3604df
+        exit 1
3604df
+esac
3604df
+
3604df
+exit $?
3604df
diff --git a/extras/systemd/glustereventsd.service.in b/extras/systemd/glustereventsd.service.in
3604df
index 75cca16..4bfcf42 100644
3604df
--- a/extras/systemd/glustereventsd.service.in
3604df
+++ b/extras/systemd/glustereventsd.service.in
3604df
@@ -5,9 +5,10 @@ After=syslog.target network.target
3604df
 [Service]
3604df
 Environment=PYTHONPATH=@BUILD_PYTHON_SITE_PACKAGES_EXPANDED@:$PYTHONPATH
3604df
 Type=simple
3604df
-ExecStart=@SBIN_DIR@/glustereventsd
3604df
+ExecStart=@SBIN_DIR@/glustereventsd --pid-file @localstatedir@/run/glustereventsd.pid
3604df
 ExecReload=/bin/kill -SIGUSR2 $MAINPID
3604df
 KillMode=control-group
3604df
+PIDFile=@localstatedir@/run/glustereventsd.pid
3604df
 
3604df
 [Install]
3604df
 WantedBy=multi-user.target
3604df
diff --git a/glusterfs.spec.in b/glusterfs.spec.in
3604df
index cb90eef..27032f4 100644
3604df
--- a/glusterfs.spec.in
3604df
+++ b/glusterfs.spec.in
3604df
@@ -617,8 +617,7 @@ This package provides the translators needed on any GlusterFS client.
3604df
 Summary:          GlusterFS Events
3604df
 Group:            Applications/File
3604df
 Requires:         %{name}-server%{?_isa} = %{version}-%{release}
3604df
-Requires:         python python-fasteners python-requests python-flask
3604df
-Requires:         python-prettytable
3604df
+Requires:         python python-requests python-prettytable
3604df
 Requires:         python-gluster = %{version}-%{release}
3604df
 %if ( 0%{?rhel} && 0%{?rhel} <= 6 )
3604df
 Requires:         python-argparse
3604df
@@ -942,6 +941,15 @@ fi
3604df
 %postun api
3604df
 /sbin/ldconfig
3604df
 
3604df
+%if 0%{?_build_server}
3604df
+%postun events
3604df
+%if ( 0%{!?_without_events:1} )
3604df
+%if ( 0%{?fedora} ) || ( 0%{?rhel} && 0%{?rhel} >= 6 )
3604df
+%_init_restart glustereventsd
3604df
+%endif
3604df
+%endif
3604df
+%endif
3604df
+
3604df
 %postun libs
3604df
 /sbin/ldconfig
3604df
 
3604df
@@ -1000,6 +1008,8 @@ exit 0
3604df
 %exclude %{_datadir}/glusterfs/scripts/eventsdash.py*
3604df
 %if ( 0%{?_with_systemd:1} )
3604df
 %exclude %{_unitdir}/glustereventsd.service
3604df
+%else
3604df
+%exclude %{_sysconfdir}/init.d/glustereventsd
3604df
 %endif
3604df
 # exclude server files
3604df
 %exclude %{_sharedstatedir}/glusterd/*
3604df
@@ -1400,6 +1410,8 @@ exit 0
3604df
 %{_datadir}/glusterfs/scripts/eventsdash.py*
3604df
 %if ( 0%{?_with_systemd:1} )
3604df
 %{_unitdir}/glustereventsd.service
3604df
+%else
3604df
+%{_sysconfdir}/init.d/glustereventsd
3604df
 %endif
3604df
 %endif
3604df
 %endif
3604df
@@ -1994,6 +2006,9 @@ end
3604df
 %endif
3604df
 
3604df
 %changelog
3604df
+* Fri Sep 16 2016 Aravinda VK <avishwan@redhat.com>
3604df
+- Added init script for glustereventsd (#1365395)
3604df
+
3604df
 * Thu Sep 15 2016 Aravinda VK <avishwan@redhat.com>
3604df
 - Added new subpackage events(glusterfs-events) (#1334044)
3604df
 
3604df
-- 
3604df
1.7.1
3604df