Merge pull request #27017 from cachedout/issue_25013

Avoid starting minion starting if already running
This commit is contained in:
Pedro Algarvio 2015-09-11 08:44:44 +01:00
commit c3c5a2c0d8
3 changed files with 51 additions and 0 deletions

View File

@ -238,6 +238,12 @@ class Minion(parsers.MinionOptionParser): # pylint: disable=no-init
)
)
migrations.migrate_paths(self.config)
# Bail out if we find a process running and it matches out pidfile
if self.check_running():
logger.exception('Salt minion is already running. Exiting.')
self.shutdown(1)
# TODO: AIO core is separate from transport
if self.config['transport'].lower() in ('zeromq', 'tcp'):
# Late import so logging works correctly

View File

@ -843,6 +843,10 @@ class DaemonMixIn(six.with_metaclass(MixInMeta, object)):
import salt.utils
salt.utils.daemonize()
def is_daemonized(self, pid):
import salt.utils.process
return salt.utils.process.os_is_running(pid)
class PidfileMixin(six.with_metaclass(MixInMeta, object)):
_mixin_prio_ = 40
@ -860,6 +864,20 @@ class PidfileMixin(six.with_metaclass(MixInMeta, object)):
from salt.utils.process import set_pidfile
set_pidfile(self.config['pidfile'], self.config['user'])
def check_pidfile(self):
'''
Report whether a pidfile exists
'''
from salt.utils.process import check_pidfile
return check_pidfile(self.config['pidfile'])
def get_pidfile(self):
'''
Return a pid contained in a pidfile
'''
from salt.utils.process import get_pidfile
return get_pidfile(self.config['pidfile'])
class TargetOptionsMixIn(six.with_metaclass(MixInMeta, object)):
@ -1516,6 +1534,14 @@ class MasterOptionParser(six.with_metaclass(OptionParserMeta,
def setup_config(self):
return config.master_config(self.get_config_file_path())
def check_running(self):
'''
Check if a pid file exists and if it is associated with
a running process.
'''
if self.check_pidfile():
return self.is_daemonized(self.get_pidfile())
class MinionOptionParser(six.with_metaclass(OptionParserMeta, MasterOptionParser)): # pylint: disable=no-init

View File

@ -102,6 +102,23 @@ def set_pidfile(pidfile, user):
log.debug('Chowned pidfile: {0} to user: {1}'.format(pidfile, user))
def check_pidfile(pidfile):
'''
Determine if a pidfile has been written out
'''
return os.path.isfile(pidfile)
def get_pidfile(pidfile):
'''
Return the pid from a pidfile as an integer
'''
with salt.utils.fopen(pidfile) as pdf:
pid = pdf.read()
return int(pid)
def clean_proc(proc, wait_for_kill=10):
'''
Generic method for cleaning up multiprocessing procs
@ -133,6 +150,8 @@ def os_is_running(pid):
'''
Use OS facilities to determine if a process is running
'''
if isinstance(pid, six.string_types):
pid = int(pid)
if HAS_PSUTIL:
return psutil.pid_exists(pid)
else: