Merge pull request #24741 from CameronNemo/upstart-enable-fix

Improve Upstart enable/disable handling
This commit is contained in:
Thomas S Hatch 2015-06-17 11:59:22 -07:00
commit 6e78a55d3e

View File

@ -43,6 +43,8 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import glob import glob
import os import os
import re
import itertools
# Import salt libs # Import salt libs
import salt.utils import salt.utils
@ -170,7 +172,12 @@ def _upstart_is_disabled(name):
NOTE: An Upstart service can also be disabled by placing "manual" NOTE: An Upstart service can also be disabled by placing "manual"
in /etc/init/[name].conf. 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 itertools.ifilter(os.path.isfile, files):
with salt.utils.fopen(file_name) as fp_:
if re.search(r'^\s*manual', fp_.read(), re.MULTILINE):
return True
return False
def _upstart_is_enabled(name): def _upstart_is_enabled(name):
@ -436,9 +443,11 @@ def _upstart_disable(name):
''' '''
Disable an Upstart service. Disable an Upstart service.
''' '''
if _upstart_is_disabled(name):
return _upstart_is_disabled(name)
override = '/etc/init/{0}.override'.format(name) override = '/etc/init/{0}.override'.format(name)
with salt.utils.fopen(override, 'w') as ofile: with salt.utils.fopen(override, 'a') as ofile:
ofile.write('manual') ofile.write('manual\n')
return _upstart_is_disabled(name) return _upstart_is_disabled(name)
@ -446,8 +455,17 @@ def _upstart_enable(name):
''' '''
Enable an Upstart service. Enable an Upstart service.
''' '''
if _upstart_is_enabled(name):
return _upstart_is_enabled(name)
override = '/etc/init/{0}.override'.format(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 itertools.ifilter(os.path.isfile, files):
with salt.utils.fopen(file_name, 'r+') as fp_:
new_text = re.sub(r'^\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) os.unlink(override)
return _upstart_is_enabled(name) return _upstart_is_enabled(name)