From 03278bd97377822d82eb580b57633bb72c13ffe0 Mon Sep 17 00:00:00 2001 From: Cameron Norman Date: Wed, 17 Jun 2015 00:51:28 -0700 Subject: [PATCH 1/3] Improve Upstart enable/disable handling * Properly search for manual stanza instead of just override * Handle "manual" stanzas in both override and regular conf files * Do not try to disable/enable already enabled services * Do not overwrite override files (use append) on disable * Only delete empty override files on enable --- salt/modules/upstart.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/salt/modules/upstart.py b/salt/modules/upstart.py index ba0f101a12..6fbf89709d 100644 --- a/salt/modules/upstart.py +++ b/salt/modules/upstart.py @@ -43,6 +43,7 @@ from __future__ import absolute_import # Import python libs import glob import os +import re # Import salt libs import salt.utils @@ -170,8 +171,12 @@ def _upstart_is_disabled(name): NOTE: An Upstart service can also be disabled by placing "manual" in /etc/init/[name].conf. ''' - return os.access('/etc/init/{0}.override'.format(name), os.R_OK) - + files = ['/etc/init/{0}.conf'.format(name), '/etc/init/{0}.override'.format(name)] + for file_name in filter(os.path.isfile, files): + with salt.utils.fopen(file_name) as fp_: + if re.search('^\s*manual', fp_.read(), re.MULTILINE): + return True + return False def _upstart_is_enabled(name): ''' @@ -436,9 +441,11 @@ def _upstart_disable(name): ''' Disable an Upstart service. ''' + if _upstart_is_disabled(name): + return _upstart_is_disabled(name) override = '/etc/init/{0}.override'.format(name) - with salt.utils.fopen(override, 'w') as ofile: - ofile.write('manual') + with salt.utils.fopen(override, 'a') as ofile: + ofile.write('manual\n') return _upstart_is_disabled(name) @@ -446,8 +453,17 @@ def _upstart_enable(name): ''' Enable an Upstart service. ''' + if _upstart_is_enabled(name): + return _upstart_is_enabled(name) override = '/etc/init/{0}.override'.format(name) - if os.access(override, os.R_OK): + files = ['/etc/init/{0}.conf'.format(name), override] + for file_name in filter(os.path.isfile, files): + with salt.utils.fopen(file_name, 'r+') as fp_: + new_text = re.sub('^\s*manual\n?', '', fp_.read(), 0, re.MULTILINE) + fp_.seek(0) + fp_.write(new_text) + fp_.truncate() + if os.access(override, os.R_OK) and os.path.getsize(override) == 0: os.unlink(override) return _upstart_is_enabled(name) From 26e406a5ce5c8d17176b0bd73c1559b448afa29a Mon Sep 17 00:00:00 2001 From: Cameron Norman Date: Wed, 17 Jun 2015 10:18:35 -0700 Subject: [PATCH 2/3] salt.modules.upstart: fix lint errors --- salt/modules/upstart.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/salt/modules/upstart.py b/salt/modules/upstart.py index 6fbf89709d..5bdfe6b354 100644 --- a/salt/modules/upstart.py +++ b/salt/modules/upstart.py @@ -172,12 +172,13 @@ def _upstart_is_disabled(name): in /etc/init/[name].conf. ''' files = ['/etc/init/{0}.conf'.format(name), '/etc/init/{0}.override'.format(name)] - for file_name in filter(os.path.isfile, files): + for file_name in itertools.ifilter(os.path.isfile, files): with salt.utils.fopen(file_name) as fp_: - if re.search('^\s*manual', fp_.read(), re.MULTILINE): + if re.search(r'^\s*manual', fp_.read(), re.MULTILINE): return True return False + def _upstart_is_enabled(name): ''' Assume that if an Upstart service is not disabled then it must be @@ -457,9 +458,9 @@ def _upstart_enable(name): return _upstart_is_enabled(name) override = '/etc/init/{0}.override'.format(name) files = ['/etc/init/{0}.conf'.format(name), override] - for file_name in filter(os.path.isfile, files): + for file_name in itertools.ifilter(os.path.isfile, files): with salt.utils.fopen(file_name, 'r+') as fp_: - new_text = re.sub('^\s*manual\n?', '', fp_.read(), 0, re.MULTILINE) + new_text = re.sub(r'^\s*manual\n?', '', fp_.read(), 0, re.MULTILINE) fp_.seek(0) fp_.write(new_text) fp_.truncate() From f3078174c85229e0ea99e1b551a081009fad3075 Mon Sep 17 00:00:00 2001 From: Cameron Norman Date: Wed, 17 Jun 2015 10:30:47 -0700 Subject: [PATCH 3/3] Add missing import --- salt/modules/upstart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/modules/upstart.py b/salt/modules/upstart.py index 5bdfe6b354..681aaf42f4 100644 --- a/salt/modules/upstart.py +++ b/salt/modules/upstart.py @@ -44,6 +44,7 @@ from __future__ import absolute_import import glob import os import re +import itertools # Import salt libs import salt.utils