4 from random import Random
6 from types import StringTypes
8 from time import strftime
9 TIMEFORMAT="%Y-%m-%d %H:%M:%S"
11 NODEUPDATE_PID_FILE= "/var/run/NodeUpdate.pid"
13 # variables for cron file creation
14 TARGET_SCRIPT = '(echo && date && echo && /usr/bin/NodeUpdate.py start) >>/var/log/NodeUpdate.log 2>&1'
15 TARGET_DESC = 'Update node RPMs periodically'
17 TARGET_SHELL = '/bin/bash'
18 CRON_FILE = '/etc/cron.d/NodeUpdate.cron'
20 YUM_PATH = "/usr/bin/yum"
24 RPM_GPG_PATH = "/etc/pki/rpm-gpg"
27 # location of file containing http/https proxy info, if needed
28 PROXY_FILE = '/etc/planetlab/http_proxy'
30 # this is the flag that indicates an update needs to restart
31 # the system to take effect. it is created by the rpm that requested
33 REBOOT_FLAG = '/etc/planetlab/update-reboot'
35 # location of directory containing boot server ssl certs
36 SSL_CERT_DIR='/mnt/cdrom/bootme/cacert/'
38 # file containing the list of extensions this node has, each
39 # correspond to a package group in yum repository.
40 EXTENSIONS_FILE='/etc/planetlab/extensions'
42 # file containing a list of rpms that we should attempt to delete
43 # before updating everything else. This list is not removed with
44 # 'yum remove', because that could accidently remove dependency rpms
45 # that were not intended to be deleted.
46 DELETE_RPM_LIST_FILE= '/etc/planetlab/delete-rpm-list'
49 # print out a message only if we are displaying output
52 if isinstance(message,StringTypes) and len(message) >=2 and message[0]=="\n":
55 print strftime(TIMEFORMAT),
60 print strftime(TIMEFORMAT),
64 # create an entry in /etc/cron.d so we run periodically.
65 # we will be run once a day at a 0-59 minute random offset
66 # into a 0-23 random hour
70 randomMinute= Random().randrange( 0, 59, 1 );
71 randomHour= Random().randrange( 0, 11, 1 );
73 f = open( CRON_FILE, 'w' );
74 f.write( "# %s\n" % (TARGET_DESC) );
75 ### xxx is root aliased to the support mailing list ?
76 f.write( "MAILTO=%s\n" % (TARGET_USER) );
77 f.write( "SHELL=%s\n" % (TARGET_SHELL) );
78 f.write( "%s %s,%s * * * %s %s\n\n" %
79 (randomMinute, randomHour, randomHour + 12, TARGET_USER, TARGET_SCRIPT) );
82 print( "Created new cron.d entry." )
84 print( "Unable to create cron.d entry." )
87 # simply remove the cron file we created
90 os.unlink( CRON_FILE )
91 print( "Deleted cron.d entry." )
93 print( "Unable to delete cron.d entry." )
99 def __init__( self, doReboot ):
100 if self.CheckProxy():
101 os.environ['http_proxy']= self.HTTP_PROXY
102 os.environ['HTTP_PROXY']= self.HTTP_PROXY
104 self.doReboot= doReboot
108 def CheckProxy( self ):
109 Message( "Checking existence of proxy config file..." )
111 if os.access(PROXY_FILE, os.R_OK) and os.path.isfile(PROXY_FILE):
112 self.HTTP_PROXY= string.strip(file(PROXY_FILE,'r').readline())
113 Message( "Using proxy %s." % self.HTTP_PROXY )
116 Message( "Not using any proxy." )
120 def InstallKeys( self ):
121 Message( "\nRemoving any existing GPG signing keys from the RPM database" )
122 os.system( "%s --allmatches -e gpg-pubkey" % RPM_PATH )
124 Message( "\nInstalling all GPG signing keys in %s" % RPM_GPG_PATH )
125 os.system( "%s --import %s/*" % (RPM_PATH, RPM_GPG_PATH) )
128 def ClearRebootFlag( self ):
129 os.system( "/bin/rm -rf %s" % REBOOT_FLAG )
132 def CheckForUpdates( self ):
134 Message( "\nRemoving any existing reboot flags" )
135 self.ClearRebootFlag()
137 if self.doReboot == 0:
138 Message( "\nIgnoring any reboot flags set by RPMs" );
140 Message( "\nChecking if yum supports SSL certificate checks" )
141 if os.system( "%s --help | grep -q sslcertdir" % YUM_PATH ) == 0:
142 Message( "It does, using --sslcertdir option" )
143 sslcertdir = "--sslcertdir=" + SSL_CERT_DIR
145 Message( "Unsupported, not using --sslcertdir option" )
149 Message( "\nChecking if yum supports --verbose" )
150 if os.system( "%s --help | grep -q verbose" % YUM_PATH ) == 0:
151 Message( "It does, using --verbose option" )
152 yum_options += " --verbose"
154 Message( "Unsupported, not using --verbose option" )
156 Message( "\nUpdating PlanetLab group" )
157 os.system( "%s %s %s -y groupupdate \"PlanetLab\"" %
158 (YUM_PATH, yum_options, sslcertdir) )
160 Message( "\nUpdating rest of system" )
161 os.system( "%s %s %s -y update" %
162 (YUM_PATH, yum_options, sslcertdir) )
164 Message( "\nChecking for extra groups (extensions) to update" )
165 if os.access(EXTENSIONS_FILE, os.R_OK) and \
166 os.path.isfile(EXTENSIONS_FILE):
167 extensions_contents= file(EXTENSIONS_FILE).read()
168 extensions_contents= string.strip(extensions_contents)
169 if extensions_contents == "":
170 Message( "No extra groups found in file." )
172 extensions_contents.strip()
173 for extension in extensions_contents.split():
174 group = "extension%s" % extension
175 Message( "\nUpdating %s group" % group )
176 os.system( "%s %s %s -y groupupdate \"%s\"" %
177 (YUM_PATH, yum_options, sslcertdir, group) )
179 Message( "No extensions file found" )
181 if os.access(REBOOT_FLAG, os.R_OK) and os.path.isfile(REBOOT_FLAG) and self.doReboot:
182 Message( "\nAt least one update requested the system be rebooted" )
183 self.ClearRebootFlag()
184 os.system( "/sbin/shutdown -r now" )
186 def RebuildRPMdb( self ):
187 Message( "\nRebuilding RPM Database." )
188 try: os.system( "rm /var/lib/rpm/__db.*" )
189 except Exception, err: print "RebuildRPMdb: %s" % err
190 try: os.system( "%s --rebuilddb" % RPM_PATH )
191 except Exception, err: print "RebuildRPMdb: %s" % err
193 def YumCleanAll ( self ):
194 Message ("\nCleaning all yum cache (yum clean all)")
196 os.system( "yum clean all")
200 def RemoveRPMS( self ):
202 Message( "\nLooking for RPMs to be deleted." )
203 if os.access(DELETE_RPM_LIST_FILE, os.R_OK) and \
204 os.path.isfile(DELETE_RPM_LIST_FILE):
205 rpm_list_contents= file(DELETE_RPM_LIST_FILE).read()
206 rpm_list_contents= string.strip(rpm_list_contents)
208 if rpm_list_contents == "":
209 Message( "No RPMs listed in file to delete." )
212 rpm_list= string.join(string.split(rpm_list_contents))
214 Message( "Deleting these RPMs:" )
215 Message( rpm_list_contents )
217 rc= os.system( "%s -ev %s" % (RPM_PATH, rpm_list) )
220 Error( "Unable to delete RPMs, continuing. rc=%d" % rc )
222 Message( "RPMs deleted successfully." )
225 Message( "No RPMs list file found." )
229 if __name__ == "__main__":
231 # if we are invoked with 'start', display the output.
232 # this is useful for running something silently
233 # under cron and as a service (at startup),
234 # so the cron only outputs errors and doesn't
235 # generate mail when it works correctly
239 # if we hit an rpm that requests a reboot, do it if this is
240 # set to 1. can be turned off by adding noreboot to command line
245 if "start" in sys.argv or "display" in sys.argv:
247 Message ("\nTurning on messages")
249 if "noreboot" in sys.argv:
252 if "updatecron" in sys.argv:
253 # simply update the /etc/cron.d file for us, and exit
257 if "removecron" in sys.argv:
262 # see if we are already running by checking the existance
263 # of a PID file, and if it exists, attempting a test kill
264 # to see if the process really does exist. If both of these
267 if os.access(NODEUPDATE_PID_FILE, os.R_OK):
268 pid= string.strip(file(NODEUPDATE_PID_FILE).readline())
270 if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
271 Message( "It appears we are already running, exiting." )
274 # write out our process id
275 pidfile= file( NODEUPDATE_PID_FILE, 'w' )
276 pidfile.write( "%d\n" % os.getpid() )
280 nodeupdate= NodeUpdate(doReboot)
282 Error( "Unable to initialize." )
284 nodeupdate.RebuildRPMdb()
285 nodeupdate.RemoveRPMS()
286 nodeupdate.InstallKeys()
287 nodeupdate.YumCleanAll()
288 nodeupdate.CheckForUpdates()
289 Message( "\nUpdate complete." )
291 # remove the PID file
292 os.unlink( NODEUPDATE_PID_FILE )