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"
26 # location of file containing http/https proxy info, if needed
27 PROXY_FILE = '/etc/planetlab/http_proxy'
29 # this is the flag that indicates an update needs to restart
30 # the system to take effect. it is created by the rpm that requested
32 REBOOT_FLAG = '/etc/planetlab/update-reboot'
34 # location of directory containing boot server ssl certs
35 SSL_CERT_DIR='/mnt/cdrom/bootme/cacert/'
37 # file containing the list of extensions this node has, each
38 # correspond to a package group in yum repository.
39 # this is expected to be updated from the 'extensions tag'
40 # through the 'extensions.php' nodeconfig script
41 EXTENSIONS_FILE='/etc/planetlab/extensions'
43 # file containing a list of rpms that we should attempt to delete
44 # before updating everything else. This list is not removed with
45 # 'yum remove', because that could accidently remove dependency rpms
46 # that were not intended to be deleted.
47 DELETE_RPM_LIST_FILE= '/etc/planetlab/delete-rpm-list'
49 # ok, so the logic should be simple, just yum update the world
50 # however there are many cases in the real life where this
51 # just does not work, because of a glitch somewhere
52 # so, we force the update of crucial pkgs independently, as
53 # the whole group is sometimes too much to swallow
55 CRUCIAL_PACKAGES_BUILTIN=[ 'NodeUpdate' , 'NodeManager' ]
56 # and operations can also try to push a list through a conf_file
57 # should use the second one for consistency, try the first one as well for legacy
58 CRUCIAL_PACKAGES_OPTIONAL_PATH1='/etc/planetlab/NodeUpdate.packages'
59 CRUCIAL_PACKAGES_OPTIONAL_PATH2='/etc/planetlab/crucial-rpm-list'
62 # print out a message only if we are displaying output
65 if isinstance(message,StringTypes) and len(message) >=2 and message[0]=="\n":
68 print strftime(TIMEFORMAT),
73 print strftime(TIMEFORMAT),
77 # create an entry in /etc/cron.d so we run periodically.
78 # we will be run once a day at a 0-59 minute random offset
79 # into a 0-23 random hour
83 randomMinute= Random().randrange( 0, 59, 1 );
84 randomHour= Random().randrange( 0, 11, 1 );
86 f = open( CRON_FILE, 'w' );
87 f.write( "# %s\n" % (TARGET_DESC) );
88 ### xxx is root aliased to the support mailing list ?
89 f.write( "MAILTO=%s\n" % (TARGET_USER) );
90 f.write( "SHELL=%s\n" % (TARGET_SHELL) );
91 f.write( "%s %s,%s * * * %s %s\n\n" %
92 (randomMinute, randomHour, randomHour + 12, TARGET_USER, TARGET_SCRIPT) );
95 print( "Created new cron.d entry." )
97 print( "Unable to create cron.d entry." )
100 # simply remove the cron file we created
101 def RemoveCronFile():
103 os.unlink( CRON_FILE )
104 print( "Deleted cron.d entry." )
106 print( "Unable to delete cron.d entry." )
112 def __init__( self, doReboot ):
113 if self.CheckProxy():
114 os.environ['http_proxy']= self.HTTP_PROXY
115 os.environ['HTTP_PROXY']= self.HTTP_PROXY
117 self.doReboot= doReboot
121 def CheckProxy( self ):
122 Message( "Checking existence of proxy config file..." )
124 if os.access(PROXY_FILE, os.R_OK) and os.path.isfile(PROXY_FILE):
125 self.HTTP_PROXY= string.strip(file(PROXY_FILE,'r').readline())
126 Message( "Using proxy %s." % self.HTTP_PROXY )
129 Message( "Not using any proxy." )
133 def InstallKeys( self ):
134 Message( "\nRemoving any existing GPG signing keys from the RPM database" )
135 os.system( "%s --allmatches -e gpg-pubkey" % RPM_PATH )
137 Message( "\nInstalling all GPG signing keys in %s" % RPM_GPG_PATH )
138 os.system( "%s --import %s/*" % (RPM_PATH, RPM_GPG_PATH) )
141 def ClearRebootFlag( self ):
142 os.system( "/bin/rm -rf %s" % REBOOT_FLAG )
145 def CheckForUpdates( self ):
147 Message( "\nRemoving any existing reboot flags" )
148 self.ClearRebootFlag()
150 if self.doReboot == 0:
151 Message( "\nIgnoring any reboot flags set by RPMs" );
153 Message( "\nChecking if yum supports SSL certificate checks" )
154 if os.system( "%s --help | grep -q sslcertdir" % YUM_PATH ) == 0:
155 Message( "It does, using --sslcertdir option" )
156 sslcertdir = "--sslcertdir=" + SSL_CERT_DIR
158 Message( "Unsupported, not using --sslcertdir option" )
162 Message( "\nChecking if yum supports --verbose" )
163 if os.system( "%s --help | grep -q verbose" % YUM_PATH ) == 0:
164 Message( "It does, using --verbose option" )
165 yum_options += " --verbose"
167 Message( "Unsupported, not using --verbose option" )
169 # a configurable list of packages to try and update independently
172 crucial_packages = []
173 for package in CRUCIAL_PACKAGES_BUILTIN: crucial_packages.append(package)
174 try: crucial_packages += file(CRUCIAL_PACKAGES_OPTIONAL_PATH1).read().split()
176 try: crucial_packages += file(CRUCIAL_PACKAGES_OPTIONAL_PATH2).read().split()
178 for package in crucial_packages:
179 Message( "\nUpdating crucial package %s" % package)
180 os.system( "%s %s -y update %s" %(YUM_PATH, yum_options, package))
184 Message( "\nUpdating PlanetLab group" )
185 os.system( "%s %s %s -y groupinstall \"PlanetLab\"" %
186 (YUM_PATH, yum_options, sslcertdir) )
188 Message( "\nUpdating rest of system" )
189 os.system( "%s %s %s -y update" % (YUM_PATH, yum_options, sslcertdir) )
191 Message( "\nChecking for extra groups (extensions) to update" )
192 if os.access(EXTENSIONS_FILE, os.R_OK) and \
193 os.path.isfile(EXTENSIONS_FILE):
194 extensions_contents= file(EXTENSIONS_FILE).read()
195 extensions_contents= string.strip(extensions_contents)
196 if extensions_contents == "":
197 Message( "No extra groups found in file." )
199 extensions_contents.strip()
200 for extension in extensions_contents.split():
201 group = "extension%s" % extension
202 Message( "\nUpdating %s group" % group )
203 os.system( "%s %s %s -y groupinstall \"%s\"" %
204 (YUM_PATH, yum_options, sslcertdir, group) )
206 Message( "No extensions file found" )
208 if os.access(REBOOT_FLAG, os.R_OK) and os.path.isfile(REBOOT_FLAG) and self.doReboot:
209 Message( "\nAt least one update requested the system be rebooted" )
210 self.ClearRebootFlag()
211 os.system( "/sbin/shutdown -r now" )
213 def RebuildRPMdb( self ):
214 Message( "\nRebuilding RPM Database." )
215 try: os.system( "rm /var/lib/rpm/__db.*" )
216 except Exception, err: print "RebuildRPMdb: %s" % err
217 try: os.system( "%s --rebuilddb" % RPM_PATH )
218 except Exception, err: print "RebuildRPMdb: %s" % err
220 def YumCleanAll ( self ):
221 Message ("\nCleaning all yum cache (yum clean all)")
223 os.system( "yum clean all")
227 def RemoveRPMS( self ):
229 Message( "\nLooking for RPMs to be deleted." )
230 if os.access(DELETE_RPM_LIST_FILE, os.R_OK) and \
231 os.path.isfile(DELETE_RPM_LIST_FILE):
232 rpm_list_contents= file(DELETE_RPM_LIST_FILE).read().strip()
234 if rpm_list_contents == "":
235 Message( "No RPMs listed in file to delete." )
238 rpm_list= string.split(rpm_list_contents)
240 Message( "Deleting RPMs from %s: %s" %(DELETE_RPM_LIST_FILE," ".join(rpm_list)))
242 # invoke them separately as otherwise one faulty (e.g. already uninstalled)
243 # would prevent the other ones from uninstalling
246 is_installed = os.system ("%s -q %s"%(RPM_PATH,rpm))==0
248 Message ("Ignoring rpm %s marked to delete, already uninstalled"%rpm)
250 uninstalled = os.system( "%s -ev %s" % (RPM_PATH, rpm) )==0
252 Message ("Successfully removed RPM %s"%rpm)
255 Error( "Unable to delete RPM %s, continuing. rc=%d" % (rpm,rc ))
258 Message( "No RPMs list file found." )
262 if __name__ == "__main__":
264 # if we are invoked with 'start', display the output.
265 # this is useful for running something silently
266 # under cron and as a service (at startup),
267 # so the cron only outputs errors and doesn't
268 # generate mail when it works correctly
272 # if we hit an rpm that requests a reboot, do it if this is
273 # set to 1. can be turned off by adding noreboot to command line
278 if "start" in sys.argv or "display" in sys.argv:
280 Message ("\nTurning on messages")
282 if "noreboot" in sys.argv:
285 if "updatecron" in sys.argv:
286 # simply update the /etc/cron.d file for us, and exit
290 if "removecron" in sys.argv:
295 # see if we are already running by checking the existance
296 # of a PID file, and if it exists, attempting a test kill
297 # to see if the process really does exist. If both of these
300 if os.access(NODEUPDATE_PID_FILE, os.R_OK):
301 pid= string.strip(file(NODEUPDATE_PID_FILE).readline())
303 if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
304 Message( "It appears we are already running, exiting." )
307 # write out our process id
308 pidfile= file( NODEUPDATE_PID_FILE, 'w' )
309 pidfile.write( "%d\n" % os.getpid() )
313 nodeupdate= NodeUpdate(doReboot)
315 Error( "Unable to initialize." )
317 nodeupdate.RebuildRPMdb()
318 nodeupdate.RemoveRPMS()
319 nodeupdate.InstallKeys()
320 nodeupdate.YumCleanAll()
321 nodeupdate.CheckForUpdates()
322 Message( "\nUpdate complete." )
324 # remove the PID file
325 os.unlink( NODEUPDATE_PID_FILE )