Package virtualization :: Module schedule_poller
[hide private]
[frames] | no frames]

Source Code for Module virtualization.schedule_poller

 1  # 
 2  # Copyright (c) 2008--2013 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15   
16  import sys 
17  try: 
18      # Python 2 
19      import commands 
20  except ImportError: 
21      import subprocess as commands 
22   
23  from spacewalk.common.usix import StringType 
24  from distutils.sysconfig import get_python_lib 
25   
26  COMMAND = "python%s %s/virtualization/poller.py" % (sys.version[0], get_python_lib()) 
27   
28 -def create_crontab_line(minute = None,\ 29 hour = None,\ 30 dom = None,\ 31 month = None,\ 32 dow = None, 33 command = COMMAND):
34 user = "root" 35 36 if minute == None: 37 minute = "*" 38 if hour == None: 39 hour = "*" 40 if dom == None: 41 dom = "*" 42 if month == None: 43 month = "*" 44 if dow == None: 45 dow = "*" 46 47 if type(minute) != StringType: 48 minute = str(minute).strip() 49 if type(hour) != StringType: 50 hour = str(hour).strip() 51 if type(dom) != StringType: 52 dom = str(dom).strip() 53 if type(month) != StringType: 54 month = str(month).strip() 55 if type(dow) != StringType: 56 dow = str(dow).strip() 57 58 str_template = "%s %s %s %s %s %s %s\n" 59 60 output_string = str_template % (minute, hour, dom, month, dow, user, command) 61 return output_string
62 63
64 -def schedule_poller(minute=None, hour=None, dom=None, month=None, dow=None):
65 try: 66 #create a crontab file 67 filename = "/etc/cron.d/rhn-virtualization.cron" 68 cronfile = open(filename, "w") 69 70 #create a crontab line 71 cron_line = create_crontab_line(minute, hour, dom, month, dow) 72 73 #write crontab line to the temp file 74 cronfile.write(cron_line) 75 76 #close the temp file 77 cronfile.close() 78 79 except Exception: 80 e = sys.exc_info()[1] 81 return (1, str(e)) 82 83 #pass the temp file to crontab 84 status, output = commands.getstatusoutput("/sbin/service crond restart") 85 86 if status != 0: 87 return (1, "Attempt to schedule poller failed: %s, %s" % (str(status), str(output))) 88 else: 89 return (0, "Scheduling of poller succeeded!")
90 91 92 93 if __name__ == "__main__": 94 schedule_poller(minute="0-59/2") 95