From 7e4d149aadd80062dfee1c461a841ba71d345ed5 Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Mon, 12 Dec 2016 07:14:02 -0500
Subject: [PATCH 1/6] added initial support for simpleinit-msb

---
 lib/ansible/modules/system/service.py | 62 +++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)

diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
index 0afe11cce75bf..99c8974ffc7ad 100644
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -23,7 +23,7 @@
 short_description:  Manage services.
 description:
     - Controls services on remote hosts. Supported init systems include BSD init,
-      OpenRC, SysV, Solaris SMF, systemd, upstart.
+      OpenRC, SysV, Solaris SMF, systemd, upstart, simpleinit-msb.
     - For Windows targets, use the M(win_service) module instead.
 options:
     name:
@@ -433,7 +433,7 @@ class LinuxService(Service):
     def get_service_tools(self):
 
         paths = ['/sbin', '/usr/sbin', '/bin', '/usr/bin']
-        binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'systemctl', 'start', 'stop', 'restart', 'insserv']
+        binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'telinit', 'systemctl', 'start', 'stop', 'restart', 'insserv']
         initpaths = ['/etc/init.d']
         location = dict()
 
@@ -476,6 +476,10 @@ def check_systemd():
             self.svc_cmd = location['systemctl']
             self.enable_cmd = location['systemctl']
 
+        elif location.get('telinit', False) and os.path.exists("/etc/init.d/smgl_init"):
+            self.svc_cmd = location['telinit']
+            self.enable_cmd = location['telinit']
+
         elif location.get('initctl', False) and os.path.exists("/etc/init/%s.conf" % self.name):
             # service is managed by upstart
             self.enable_cmd = location['initctl']
@@ -879,6 +883,44 @@ def write_to_override_file(file_name, file_contents, ):
                 return (rc, out, err)
 
         #
+        # telinit (Source Mage GNU/Linux)
+        #
+        if self.enable_cmd.endswith("telinit"):
+            (rc, out, err) = self.execute_command("%s list" % self.enable_cmd)
+
+            service_exists = False
+
+            rex = re.compile('^\w+\s+%s$' % self.name)
+
+            for line in out.splitlines():
+                if rex.match(line):
+                    service_exists = True
+                    break
+
+            if not service_exists:
+                self.module.fail_json(msg='telinit could not find the requested service: %s' % self.name)
+
+            if self.enable:
+                action = "bootenable"
+            else:
+                action = "bootdisable"
+
+            (rc, out, err) = self.execute_command("%s %s %s" % (self.enable_cmd, action, self.name))
+
+            for line in err.splitlines():
+                if self.enable and line.find('already enabled') != -1:
+                    self.changed = False
+                    break
+                if not self.enable and line.find('already disabled') != -1:
+                    self.changed = False
+                    break
+
+            if not self.changed:
+                return
+
+            return (rc, out, err)
+
+        #
         # If we've gotten to the end, the service needs to be updated
         #
         self.changed = True
@@ -917,6 +959,22 @@ def service_control(self):
                     # initctl commands take the form <cmd> <action> <name>
                     svc_cmd = self.svc_cmd
                     arguments = "%s %s" % (self.name, arguments)
+                # telinit
+                elif self.svc_cmd.endswith("telinit"):
+                    (rc, out, err) = self.execute_command("%s list" % self.enable_cmd)
+
+                    service_exists = False
+
+                    rex = re.compile('^\w+\s+%s$' % self.name)
+
+                    for line in out.splitlines():
+                        if rex.match(line):
+                            service_exists = True
+                            break
+
+                    if not service_exists:
+                        self.module.fail_json(msg='telinit could not find the requested service: %s' % self.name)
+                    svc_cmd = "%s run %s" % (self.svc_cmd, self.name)
                 else:
                     # SysV and OpenRC take the form <cmd> <name> <action>
                     svc_cmd = "%s %s" % (self.svc_cmd, self.name)

From e0786ba20586cfef05d031844db46c23f9155ea3 Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Mon, 12 Dec 2016 13:48:53 -0500
Subject: [PATCH 2/6] described basic telinit syntax in the comment

---
 lib/ansible/modules/system/service.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
index 99c8974ffc7ad..17c12a4f991bf 100644
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -959,7 +959,7 @@ def service_control(self):
                     # initctl commands take the form <cmd> <action> <name>
                     svc_cmd = self.svc_cmd
                     arguments = "%s %s" % (self.name, arguments)
-                # telinit
+                # telinit takes the form <cmd> run <name> <action>
                 elif self.svc_cmd.endswith("telinit"):
                     (rc, out, err) = self.execute_command("%s list" % self.enable_cmd)
 

From b4ca112247e7b9f31fa59393ca59f051774191d4 Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Mon, 12 Dec 2016 13:50:02 -0500
Subject: [PATCH 3/6] missing newline

---
 lib/ansible/modules/system/service.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
index 17c12a4f991bf..9b9f6b9775540 100644
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -974,6 +974,7 @@ def service_control(self):
 
                     if not service_exists:
                         self.module.fail_json(msg='telinit could not find the requested service: %s' % self.name)
+
                     svc_cmd = "%s run %s" % (self.svc_cmd, self.name)
                 else:
                     # SysV and OpenRC take the form <cmd> <name> <action>

From 052ec1e547ff92f7bc03ce3641b26928e807007c Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Thu, 2 Feb 2017 06:30:00 -0500
Subject: [PATCH 4/6] Correct word-wrap for binaries array

---
 lib/ansible/modules/system/service.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
index 9b9f6b9775540..09091415db553 100644
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -433,7 +433,8 @@ class LinuxService(Service):
     def get_service_tools(self):
 
         paths = ['/sbin', '/usr/sbin', '/bin', '/usr/bin']
-        binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'telinit', 'systemctl', 'start', 'stop', 'restart', 'insserv']
+        binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'telinit', 'systemctl', 'start', 'stop', 'restart',
+                    'insserv']
         initpaths = ['/etc/init.d']
         location = dict()
 

From 95d428e75dc4e665efb97254e0adf0d84bc703db Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Mon, 27 Mar 2017 22:02:41 -0400
Subject: [PATCH 5/6] Fix init system comment: it's simpleinit-msb, telinit is
 a script

---
 lib/ansible/modules/system/service.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
index 09091415db553..3715ca2b71936 100644
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -884,7 +884,7 @@ def write_to_override_file(file_name, file_contents, ):
                 return (rc, out, err)
 
         #
-        # telinit (Source Mage GNU/Linux)
+        # simpleinit-msb (Source Mage GNU/Linux)
         #
         if self.enable_cmd.endswith("telinit"):
             (rc, out, err) = self.execute_command("%s list" % self.enable_cmd)

From 5b5c5af4e6dc97b9db692295860b48de0312874d Mon Sep 17 00:00:00 2001
From: Vlad Glagolev <scm@vaygr.net>
Date: Sun, 30 Jul 2017 10:31:37 -0400
Subject: [PATCH 6/6] Add 'service_mgr' fact

---
 lib/ansible/module_utils/facts/system/service_mgr.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/ansible/module_utils/facts/system/service_mgr.py b/lib/ansible/module_utils/facts/system/service_mgr.py
index ff7ee7ab4a168..ecf6163936975 100644
--- a/lib/ansible/module_utils/facts/system/service_mgr.py
+++ b/lib/ansible/module_utils/facts/system/service_mgr.py
@@ -124,6 +124,8 @@ def collect(self, module=None, collected_facts=None):
             # FIXME: mv is_systemd_managed
             if self.is_systemd_managed(module=module):
                 service_mgr_name = 'systemd'
+            elif module.get_bin_path('telinit') and os.path.exists("/etc/init.d/smgl_init"):
+                service_mgr_name = 'simpleinit'
             elif module.get_bin_path('initctl') and os.path.exists("/etc/init/"):
                 service_mgr_name = 'upstart'
             elif os.path.exists('/sbin/openrc'):
