3 # Copyright (c) 2003 Intel Corporation
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
18 # * Neither the name of the Intel Corporation nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
26 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
35 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
36 # export regulations applicable in licensee's jurisdiction. Under
37 # CURRENT (May 2000) U.S. export regulations this software is eligible
38 # for export from the U.S. and can be downloaded by or otherwise
39 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
40 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
41 # Afghanistan and any other country to which the U.S. has embargoed
46 from random import Random
50 #PLANETLAB_BIN = "/usr/local/planetlab/bin/"
51 #sys.path.append(PLANETLAB_BIN)
52 #import BootServerRequest
55 NODEUPDATE_PID_FILE= "/var/run/NodeUpdate.pid"
57 # variables for cron file creation
58 TARGET_SCRIPT = '/usr/local/planetlab/bin/NodeUpdate.py'
59 TARGET_DESC = 'Update node RPMs periodically'
61 CRON_FILE = '/etc/cron.d/NodeUpdate.cron'
63 YUM_PATH = "/usr/bin/yum"
65 # location of file containing http/https proxy info, if needed
66 PROXY_FILE = '/etc/planetlab/http_proxy'
68 # this is the flag that indicates an update needs to restart
69 # the system to take effect. it is created by the rpm that requested
71 REBOOT_FLAG = '/etc/planetlab/update-reboot'
73 # location of directory containing boot server ssl certs
74 SSL_CERT_DIR='/mnt/cdrom/bootme/cacert/'
77 # print out a message only if we are displaying output
83 # print out a message only if we are displaying output
88 # create an entry in /etc/cron.d so we run periodically
89 # we will be run once an hour at a 0-59 random offset
92 randomMinute= Random().randrange( 0, 59, 1 );
94 f = open( CRON_FILE, 'w' );
95 f.write( "# %s\n" % (TARGET_DESC) );
96 f.write( "MAILTO=%s\n" % (TARGET_USER) );
97 f.write( "%s * * * * %s %s\n\n" % (randomMinute, TARGET_USER, TARGET_SCRIPT) );
100 print( "Created new cron.d entry." )
102 print( "Unable to create cron.d entry." )
105 # simply remove the cron file we created
106 def RemoveCronFile():
108 os.unlink( CRON_FILE )
109 print( "Deleted cron.d entry." )
111 print( "Unable to delete cron.d entry." )
117 def __init__( self, doReboot ):
118 if self.CheckProxy():
119 os.environ['http_proxy']= self.HTTP_PROXY
120 os.environ['HTTP_PROXY']= self.HTTP_PROXY
122 self.doReboot= doReboot
126 def CheckProxy( self ):
127 Message( "Checking existance of proxy config file..." )
129 if os.access(PROXY_FILE, os.R_OK) and os.path.isfile(PROXY_FILE):
130 self.HTTP_PROXY= string.strip(file(PROXY_FILE,'r').readline())
131 Message( "Using proxy %s." % self.HTTP_PROXY )
134 Message( "Not using any proxy." )
138 def ClearRebootFlag( self ):
139 os.system( "/bin/rm -rf %s" % REBOOT_FLAG )
142 def CheckForUpdates( self ):
144 Message( "\nRemoving any existing reboot flags" )
145 self.ClearRebootFlag()
147 if self.doReboot == 0:
148 Message( "\nIgnoring any reboot flags set by RPMs" );
150 Message( "\nUpdating PlanetLab group" )
151 os.system( "%s --sslcertdir=%s -y groupupdate \"PlanetLab\"" %
152 (YUM_PATH,SSL_CERT_DIR) )
154 Message( "\nUpdating rest of system" )
155 os.system( "%s --sslcertdir=%s -y update" %
156 (YUM_PATH,SSL_CERT_DIR) )
158 if os.access(REBOOT_FLAG, os.R_OK) and os.path.isfile(REBOOT_FLAG) and self.doReboot:
159 Message( "\nAt least one update requested the system be rebooted" )
160 self.ClearRebootFlag()
161 os.system( "/sbin/shutdown -r now" )
166 if __name__ == "__main__":
168 # if we are invoked with 'start', display the output. this
169 # is usefull for running something under cron and as a service
170 # (at startup), so the cron only outputs errors and doesn't
171 # generate mail when it works correctly
175 # if we hit an rpm that requests a reboot, do it if this is
176 # set to 1. can be turned off by adding noreboot to command line
181 if "start" in sys.argv:
184 if "noreboot" in sys.argv:
187 if "updatecron" in sys.argv:
188 # simply update the /etc/cron.d file for us, and exit
192 if "removecron" in sys.argv:
197 # see if we are already running by checking the existance
198 # of a PID file, and if it exists, attempting a test kill
199 # to see if the process really does exist. If both of these
202 if os.access(NODEUPDATE_PID_FILE, os.R_OK):
203 pid= string.strip(file(NODEUPDATE_PID_FILE).readline())
205 if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
206 print "It appears we are already running, exiting."
209 # write out our process id
210 pidfile= file( NODEUPDATE_PID_FILE, 'w' )
211 pidfile.write( "%d\n" % os.getpid() )
215 nodeupdate= NodeUpdate(doReboot)
217 print "Unable to initialize."
219 nodeupdate.CheckForUpdates()
221 # remove the PID file
222 os.unlink( NODEUPDATE_PID_FILE )