Setting tag nodeupdate-0.5-12
[nodeupdate.git] / NodeUpdate.py
1 #!/usr/bin/python2
2
3 import sys, os
4 from random import Random
5 import string
6 from types import StringTypes
7
8 from time import strftime
9 TIMEFORMAT="%Y-%m-%d %H:%M:%S"
10
11 NODEUPDATE_PID_FILE= "/var/run/NodeUpdate.pid"
12
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'
16 TARGET_USER = 'root'
17 TARGET_SHELL = '/bin/bash'
18 CRON_FILE = '/etc/cron.d/NodeUpdate.cron'
19
20 YUM_PATH = "/usr/bin/yum"
21
22 RPM_PATH = "/bin/rpm"
23
24 RPM_GPG_PATH = "/etc/pki/rpm-gpg"
25
26 # location of file containing http/https proxy info, if needed
27 PROXY_FILE = '/etc/planetlab/http_proxy'
28
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
31 # the reboot
32 REBOOT_FLAG = '/etc/planetlab/update-reboot'
33
34 # location of directory containing boot server ssl certs
35 SSL_CERT_DIR='/mnt/cdrom/bootme/cacert/'
36
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'
42
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'
48
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 
54 # this one is builtin
55 CRUCIAL_PACKAGES_BUILTIN=[ 'NodeUpdate' , 'nodemanager-lib', 'nodemanager-lxc', 'nodemanager-vs' ]
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_PATHS=[
59     # this is a legacy name, please avoid using this one
60     '/etc/planetlab/NodeUpdate.packages'
61     # file to use with a hand-written conf_file
62     '/etc/planetlab/crucial-rpm-list'
63     # this one could some day be maintained by a predefined config_file
64     '/etc/planetlab/sliceimage-rpm-list'
65 ]
66
67 # print out a message only if we are displaying output
68 def Message(message):
69     if displayOutput:
70         if isinstance(message,StringTypes) and len(message) >=2 and message[0]=="\n":
71             print "\n",
72             message=message[1:]
73         print strftime(TIMEFORMAT),
74         print message
75
76 # always print errors
77 def Error(Str):
78     print strftime(TIMEFORMAT),
79     print Str
80
81
82 # create an entry in /etc/cron.d so we run periodically.
83 # we will be run once a day at a 0-59 minute random offset
84 # into a 0-23 random hour
85 def UpdateCronFile():
86     try:
87         
88         randomMinute= Random().randrange( 0, 59, 1 );
89         randomHour= Random().randrange( 0, 11, 1 );
90         
91         f = open( CRON_FILE, 'w' );
92         f.write( "# %s\n" % (TARGET_DESC) );
93         ### xxx is root aliased to the support mailing list ?
94         f.write( "MAILTO=%s\n" % (TARGET_USER) );
95         f.write( "SHELL=%s\n" % (TARGET_SHELL) );
96         f.write( "%s %s,%s * * * %s %s\n\n" %
97                  (randomMinute, randomHour, randomHour + 12, TARGET_USER, TARGET_SCRIPT) );
98         f.close()
99     
100         print( "Created new cron.d entry." )
101     except:
102         print( "Unable to create cron.d entry." )
103
104
105 # simply remove the cron file we created
106 def RemoveCronFile():
107     try:
108         os.unlink( CRON_FILE )
109         print( "Deleted cron.d entry." )
110     except:
111         print( "Unable to delete cron.d entry." )
112         
113
114
115 class NodeUpdate:
116
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
121             
122         self.doReboot= doReboot
123
124     
125
126     def CheckProxy( self ):
127         Message( "Checking existence of proxy config file..." )
128         
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 )
132             return 1
133         else:
134             Message( "Not using any proxy." )
135             return 0
136
137
138     def InstallKeys( self ):
139         Message( "\nRemoving any existing GPG signing keys from the RPM database" )
140         os.system( "%s --allmatches -e gpg-pubkey" % RPM_PATH )
141
142         Message( "\nInstalling all GPG signing keys in %s" % RPM_GPG_PATH )
143         os.system( "%s --import %s/*" % (RPM_PATH, RPM_GPG_PATH) )
144
145
146     def ClearRebootFlag( self ):
147         os.system( "/bin/rm -rf %s" % REBOOT_FLAG )
148
149
150     def CheckForUpdates( self ):
151
152         Message( "\nRemoving any existing reboot flags" )
153         self.ClearRebootFlag()
154
155         if self.doReboot == 0:
156             Message( "\nIgnoring any reboot flags set by RPMs" );
157
158         Message( "\nChecking if yum supports SSL certificate checks" )
159         if os.system( "%s --help | grep -q sslcertdir" % YUM_PATH ) == 0:
160             Message( "It does, using --sslcertdir option" )
161             sslcertdir = "--sslcertdir=" + SSL_CERT_DIR
162         else:
163             Message( "Unsupported, not using --sslcertdir option" )
164             sslcertdir = ""
165                     
166         yum_options=""
167         Message( "\nChecking if yum supports --verbose" )
168         if os.system( "%s --help | grep -q verbose" % YUM_PATH ) == 0:
169             Message( "It does, using --verbose option" )
170             yum_options += " --verbose"
171         else:
172             Message( "Unsupported, not using --verbose option" )
173         
174         # a configurable list of packages to try and update independently
175         # cautious..
176         try:
177             crucial_packages = []
178             for package in CRUCIAL_PACKAGES_BUILTIN: crucial_packages.append(package)
179             for path in CRUCIAL_PACKAGES_OPTIONAL_PATHS:
180                 try: crucial_packages += file(CRUCIAL_PACKAGES_OPTIONAL_PATH1).read().split()
181                 except: pass
182             for package in crucial_packages:
183                 # if package is not yet installed, like e.g. slice images, 
184                 # need to yum install, not yum update
185                 if os.system("rpm -q %s > /dev/null"%package)==0:
186                     Message( "\nUpdating crucial package %s" % package)
187                     os.system( "%s %s -y update %s" %(YUM_PATH, yum_options, package))
188                 else:
189                     Message( "\Installing crucial package %s" % package)
190                     os.system( "%s %s -y install %s" %(YUM_PATH, yum_options, package))
191         except:
192             pass
193
194         Message( "\nUpdating PlanetLab group" )
195         os.system( "%s %s %s -y groupinstall \"PlanetLab\"" %
196                    (YUM_PATH, yum_options, sslcertdir) )
197
198         Message( "\nUpdating rest of system" )
199         os.system( "%s %s %s -y update" % (YUM_PATH, yum_options, sslcertdir) )
200
201         Message( "\nChecking for extra groups (extensions) to update" )
202         if os.access(EXTENSIONS_FILE, os.R_OK) and \
203            os.path.isfile(EXTENSIONS_FILE):
204             extensions_contents= file(EXTENSIONS_FILE).read()
205             extensions_contents= string.strip(extensions_contents)
206             if extensions_contents == "":
207                 Message( "No extra groups found in file." )
208             else:
209                 extensions_contents.strip()
210                 for extension in extensions_contents.split():
211                     group = "extension%s" % extension
212                     Message( "\nUpdating %s group" % group )
213                     os.system( "%s %s %s -y groupinstall \"%s\"" %
214                                (YUM_PATH, yum_options, sslcertdir, group) )
215         else:
216             Message( "No extensions file found" )
217             
218         if os.access(REBOOT_FLAG, os.R_OK) and os.path.isfile(REBOOT_FLAG) and self.doReboot:
219             Message( "\nAt least one update requested the system be rebooted" )
220             self.ClearRebootFlag()
221             os.system( "/sbin/shutdown -r now" )
222
223     def RebuildRPMdb( self ):
224         Message( "\nRebuilding RPM Database." )
225         try: os.system( "rm /var/lib/rpm/__db.*" )
226         except Exception, err: print "RebuildRPMdb: %s" % err
227         try: os.system( "%s --rebuilddb" % RPM_PATH )
228         except Exception, err: print "RebuildRPMdb: %s" % err
229
230     def YumCleanAll ( self ):
231         Message ("\nCleaning all yum cache (yum clean all)")
232         try:
233             os.system( "yum clean all")
234         except:
235             pass
236
237     def RemoveRPMS( self ):
238
239         Message( "\nLooking for RPMs to be deleted." )
240         if os.access(DELETE_RPM_LIST_FILE, os.R_OK) and \
241            os.path.isfile(DELETE_RPM_LIST_FILE):
242             rpm_list_contents= file(DELETE_RPM_LIST_FILE).read().strip()
243
244             if rpm_list_contents == "":
245                 Message( "No RPMs listed in file to delete." )
246                 return
247
248             rpm_list= string.split(rpm_list_contents)
249             
250             Message( "Deleting RPMs from %s: %s" %(DELETE_RPM_LIST_FILE," ".join(rpm_list)))
251
252             # invoke them separately as otherwise one faulty (e.g. already uninstalled)
253             # would prevent the other ones from uninstalling
254             for rpm in rpm_list:
255                 # is it installed
256                 is_installed = os.system ("%s -q %s"%(RPM_PATH,rpm))==0
257                 if not is_installed:
258                     Message ("Ignoring rpm %s marked to delete, already uninstalled"%rpm)
259                     continue
260                 uninstalled = os.system( "%s -ev %s" % (RPM_PATH, rpm) )==0
261                 if uninstalled:
262                     Message ("Successfully removed RPM %s"%rpm)
263                     continue
264                 else:
265                     Error( "Unable to delete RPM %s, continuing. rc=%d" % (rpm,uninstalled))
266             
267         else:
268             Message( "No RPMs list file found." )
269
270
271
272 if __name__ == "__main__":
273
274     # if we are invoked with 'start', display the output. 
275     # this is useful for running something silently 
276     # under cron and as a service (at startup), 
277     # so the cron only outputs errors and doesn't
278     # generate mail when it works correctly
279
280     displayOutput= 0
281
282     # if we hit an rpm that requests a reboot, do it if this is
283     # set to 1. can be turned off by adding noreboot to command line
284     # option
285     
286     doReboot= 1
287
288     if "start" in sys.argv or "display" in sys.argv:
289         displayOutput= 1
290         Message ("\nTurning on messages")
291
292     if "noreboot" in sys.argv:
293         doReboot= 0
294
295     if "updatecron" in sys.argv:
296         # simply update the /etc/cron.d file for us, and exit
297         UpdateCronFile()
298         sys.exit(0)
299
300     if "removecron" in sys.argv:
301         RemoveCronFile()
302         sys.exit(0)     
303
304             
305     # see if we are already running by checking the existance
306     # of a PID file, and if it exists, attempting a test kill
307     # to see if the process really does exist. If both of these
308     # tests pass, exit.
309     
310     if os.access(NODEUPDATE_PID_FILE, os.R_OK):
311         pid= string.strip(file(NODEUPDATE_PID_FILE).readline())
312         if pid <> "":
313             if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
314                 Message( "It appears we are already running, exiting." )
315                 sys.exit(1)
316                     
317     # write out our process id
318     pidfile= file( NODEUPDATE_PID_FILE, 'w' )
319     pidfile.write( "%d\n" % os.getpid() )
320     pidfile.close()
321
322     
323     nodeupdate= NodeUpdate(doReboot)
324     if not nodeupdate:
325         Error( "Unable to initialize." )
326     else:
327         nodeupdate.RebuildRPMdb()
328         nodeupdate.RemoveRPMS()
329         nodeupdate.InstallKeys()
330         nodeupdate.YumCleanAll()
331         nodeupdate.CheckForUpdates()
332         Message( "\nUpdate complete." )
333
334     # remove the PID file
335     os.unlink( NODEUPDATE_PID_FILE )
336