[stable-2.9] cron - Only run get_bin_path() once (#62554)

(cherry picked from commit b7897e3a8d)

Co-authored-by: Sam Doran <sdoran@redhat.com>
pull/62567/head
Sam Doran 5 years ago committed by Toshio Kuratomi
parent b1ba759862
commit 6aa02a74fe

@ -0,0 +1,2 @@
bugfixes:
- cron cronvar - only run ``get_bin_path()`` once

@ -235,6 +235,7 @@ class CronTab(object):
self.lines = None self.lines = None
self.ansible = "#Ansible: " self.ansible = "#Ansible: "
self.existing = '' self.existing = ''
self.cron_cmd = self.module.get_bin_path('crontab', required=True)
if cron_file: if cron_file:
if os.path.isabs(cron_file): if os.path.isabs(cron_file):
@ -509,30 +510,29 @@ class CronTab(object):
Returns the command line for reading a crontab Returns the command line for reading a crontab
""" """
user = '' user = ''
cron_cmd = self.module.get_bin_path('crontab', required=True)
if self.user: if self.user:
if platform.system() == 'SunOS': if platform.system() == 'SunOS':
return "su %s -c '%s -l'" % (shlex_quote(self.user), shlex_quote(cron_cmd)) return "su %s -c '%s -l'" % (shlex_quote(self.user), shlex_quote(self.cron_cmd))
elif platform.system() == 'AIX': elif platform.system() == 'AIX':
return "%s -l %s" % (shlex_quote(cron_cmd), shlex_quote(self.user)) return "%s -l %s" % (shlex_quote(self.cron_cmd), shlex_quote(self.user))
elif platform.system() == 'HP-UX': elif platform.system() == 'HP-UX':
return "%s %s %s" % (cron_cmd, '-l', shlex_quote(self.user)) return "%s %s %s" % (self.cron_cmd, '-l', shlex_quote(self.user))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % shlex_quote(self.user) user = '-u %s' % shlex_quote(self.user)
return "%s %s %s" % (cron_cmd, user, '-l') return "%s %s %s" % (self.cron_cmd, user, '-l')
def _write_execute(self, path): def _write_execute(self, path):
""" """
Return the command line for writing a crontab Return the command line for writing a crontab
""" """
cron_cmd = self.module.get_bin_path('crontab', required=True)
user = '' user = ''
if self.user: if self.user:
if platform.system() in ['SunOS', 'HP-UX', 'AIX']: if platform.system() in ['SunOS', 'HP-UX', 'AIX']:
return "chown %s %s ; su '%s' -c '%s %s'" % (shlex_quote(self.user), shlex_quote(path), shlex_quote(self.user), cron_cmd, shlex_quote(path)) return "chown %s %s ; su '%s' -c '%s %s'" % (
shlex_quote(self.user), shlex_quote(path), shlex_quote(self.user), self.cron_cmd, shlex_quote(path))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % shlex_quote(self.user) user = '-u %s' % shlex_quote(self.user)
return "%s %s %s" % (cron_cmd, user, shlex_quote(path)) return "%s %s %s" % (self.cron_cmd, user, shlex_quote(path))
def main(): def main():

@ -126,6 +126,7 @@ class CronVar(object):
self.user = user self.user = user
self.lines = None self.lines = None
self.wordchars = ''.join(chr(x) for x in range(128) if chr(x) not in ('=', "'", '"',)) self.wordchars = ''.join(chr(x) for x in range(128) if chr(x) not in ('=', "'", '"',))
self.cron_cmd = self.module.get_bin_path('cronvar', required=True)
if cron_file: if cron_file:
self.cron_file = "" self.cron_file = ""
@ -290,32 +291,31 @@ class CronVar(object):
""" """
Returns the command line for reading a crontab Returns the command line for reading a crontab
""" """
cron_cmd = self.module.get_bin_path('crontab', required=True)
user = '' user = ''
if self.user: if self.user:
if platform.system() == 'SunOS': if platform.system() == 'SunOS':
return "su %s -c '%s -l'" % (shlex_quote(self.user), shlex_quote(cron_cmd)) return "su %s -c '%s -l'" % (shlex_quote(self.user), shlex_quote(self.cron_cmd))
elif platform.system() == 'AIX': elif platform.system() == 'AIX':
return "%s -l %s" % (shlex_quote(cron_cmd), shlex_quote(self.user)) return "%s -l %s" % (shlex_quote(self.cron_cmd), shlex_quote(self.user))
elif platform.system() == 'HP-UX': elif platform.system() == 'HP-UX':
return "%s %s %s" % (cron_cmd, '-l', shlex_quote(self.user)) return "%s %s %s" % (self.cron_cmd, '-l', shlex_quote(self.user))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % shlex_quote(self.user) user = '-u %s' % shlex_quote(self.user)
return "%s %s %s" % (cron_cmd, user, '-l') return "%s %s %s" % (self.cron_cmd, user, '-l')
def _write_execute(self, path): def _write_execute(self, path):
""" """
Return the command line for writing a crontab Return the command line for writing a crontab
""" """
cron_cmd = self.module.get_bin_path('crontab', required=True)
user = '' user = ''
if self.user: if self.user:
if platform.system() in ['SunOS', 'HP-UX', 'AIX']: if platform.system() in ['SunOS', 'HP-UX', 'AIX']:
return "chown %s %s ; su '%s' -c '%s %s'" % (shlex_quote(self.user), shlex_quote(path), shlex_quote(self.user), cron_cmd, shlex_quote(path)) return "chown %s %s ; su '%s' -c '%s %s'" % (
shlex_quote(self.user), shlex_quote(path), shlex_quote(self.user), self.cron_cmd, shlex_quote(path))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % shlex_quote(self.user) user = '-u %s' % shlex_quote(self.user)
return "%s %s %s" % (cron_cmd, user, shlex_quote(path)) return "%s %s %s" % (self.cron_cmd, user, shlex_quote(path))
# ================================================== # ==================================================

Loading…
Cancel
Save