ready for tagging
[nodeupdate.git] / NodeUpdate.py
1 #!/usr/bin/python2
2
3 import sys, os
4 from random import Random
5 import string
6
7 NODEUPDATE_PID_FILE= "/var/run/NodeUpdate.pid"
8
9 # variables for cron file creation
10 TARGET_SCRIPT = '(echo && date && echo && /usr/bin/NodeUpdate.py start) >>/var/log/NodeUpdate.log 2>&1'
11 TARGET_DESC = 'Update node RPMs periodically'
12 TARGET_USER = 'root'
13 TARGET_SHELL = '/bin/bash'
14 CRON_FILE = '/etc/cron.d/NodeUpdate.cron'
15
16 YUM_PATH = "/usr/bin/yum"
17
18 RPM_PATH = "/bin/rpm"
19
20 RPM_GPG_PATH = "/etc/pki/rpm-gpg"
21
22
23 # location of file containing http/https proxy info, if needed
24 PROXY_FILE = '/etc/planetlab/http_proxy'
25
26 # this is the flag that indicates an update needs to restart
27 # the system to take effect. it is created by the rpm that requested
28 # the reboot
29 REBOOT_FLAG = '/etc/planetlab/update-reboot'
30
31 # location of directory containing boot server ssl certs
32 SSL_CERT_DIR='/mnt/cdrom/bootme/cacert/'
33
34 # file containing list of extra groups to attempt to update,
35 # if necessary.
36 EXTRA_GROUPS_FILE= '/etc/planetlab/extra-node-groups'
37
38 # file containing a list of rpms that we should attempt to delete
39 # before we updating everything else. this list is not
40 # removed with 'yum remove', because that could accidently remove
41 # dependency rpms that were not intended to be deleted.
42 DELETE_RPM_LIST_FILE= '/etc/planetlab/delete-rpm-list'
43
44
45 # print out a message only if we are displaying output
46 def Message(Str):
47     if displayOutput:
48         print Str
49
50
51 # print out a message only if we are displaying output
52 def Error(Str):
53     print Str
54
55
56 # create an entry in /etc/cron.d so we run periodically.
57 # we will be run once a day at a 0-59 minute random offset
58 # into a 0-23 random hour
59 def UpdateCronFile():
60     try:
61         
62         randomMinute= Random().randrange( 0, 59, 1 );
63         randomHour= Random().randrange( 0, 11, 1 );
64         
65         f = open( CRON_FILE, 'w' );
66         f.write( "# %s\n" % (TARGET_DESC) );
67         f.write( "MAILTO=%s\n" % (TARGET_USER) );
68         f.write( "SHELL=%s\n" % (TARGET_SHELL) );
69         f.write( "%s %s,%s * * * %s %s\n\n" %
70                  (randomMinute, randomHour, randomHour + 12, TARGET_USER, TARGET_SCRIPT) );
71         f.close()
72     
73         print( "Created new cron.d entry." )
74     except:
75         print( "Unable to create cron.d entry." )
76
77
78 # simply remove the cron file we created
79 def RemoveCronFile():
80     try:
81         os.unlink( CRON_FILE )
82         print( "Deleted cron.d entry." )
83     except:
84         print( "Unable to delete cron.d entry." )
85         
86
87
88 class NodeUpdate:
89
90     def __init__( self, doReboot ):
91         if self.CheckProxy():
92             os.environ['http_proxy']= self.HTTP_PROXY
93             os.environ['HTTP_PROXY']= self.HTTP_PROXY
94             
95         self.doReboot= doReboot
96
97     
98
99     def CheckProxy( self ):
100         Message( "Checking existance of proxy config file..." )
101         
102         if os.access(PROXY_FILE, os.R_OK) and os.path.isfile(PROXY_FILE):
103             self.HTTP_PROXY= string.strip(file(PROXY_FILE,'r').readline())
104             Message( "Using proxy %s." % self.HTTP_PROXY )
105             return 1
106         else:
107             Message( "Not using any proxy." )
108             return 0
109
110
111     def InstallKeys( self ):
112         Message( "\nRemoving any existing GPG signing keys from the RPM database" )
113         os.system( "%s --allmatches -e gpg-pubkey" % RPM_PATH )
114
115         Message( "\nInstalling all GPG signing keys in %s" % RPM_GPG_PATH )
116         os.system( "%s --import %s/*" % (RPM_PATH, RPM_GPG_PATH) )
117
118
119     def ClearRebootFlag( self ):
120         os.system( "/bin/rm -rf %s" % REBOOT_FLAG )
121
122
123     def CheckForUpdates( self ):
124
125         Message( "\nRemoving any existing reboot flags" )
126         self.ClearRebootFlag()
127
128         if self.doReboot == 0:
129             Message( "\nIgnoring any reboot flags set by RPMs" );
130
131         Message( "\nChecking if yum supports SSL certificate checks" )
132         if os.system( "%s --help | grep -q sslcertdir" % YUM_PATH ) == 0:
133             Message( "Yes, using --sslcertdir option" )
134             sslcertdir = "--sslcertdir=" + SSL_CERT_DIR
135         else:
136             Message( "No, not using --sslcertdir option" )
137             sslcertdir = ""
138                     
139         Message( "\nUpdating PlanetLab group" )
140         os.system( "%s %s -y groupupdate \"PlanetLab\"" %
141                    (YUM_PATH, sslcertdir) )
142
143         Message( "\nUpdating rest of system" )
144         os.system( "%s %s -y update" %
145                    (YUM_PATH, sslcertdir) )
146
147         Message( "\nChecking for extra groups to update" )
148         if os.access(EXTRA_GROUPS_FILE, os.R_OK) and \
149            os.path.isfile(EXTRA_GROUPS_FILE):
150             extra_groups_contents= file(EXTRA_GROUPS_FILE).read()
151             extra_groups_contents= string.strip(extra_groups_contents)
152             if extra_groups_contents == "":
153                 Message( "No extra groups found in file." )
154             else:
155                 for group in string.split(extra_groups_contents,"\n"):
156                     Message( "\nUpdating %s group" % group )
157                     os.system( "%s %s -y groupupdate \"%s\"" %
158                                (YUM_PATH, sslcertdir, group) )
159         else:
160             Message( "No extra groups file found" )
161             
162         if os.access(REBOOT_FLAG, os.R_OK) and os.path.isfile(REBOOT_FLAG) and self.doReboot:
163             Message( "\nAt least one update requested the system be rebooted" )
164             self.ClearRebootFlag()
165             os.system( "/sbin/shutdown -r now" )
166
167     def RebuildRPMdb( self ):
168         Message( "\nRebuilding RPM Database." )
169         try: os.system( "rm /var/lib/rpm/__db.*" )
170         except Exception, err: print "RebuildRPMdb: %s" % err
171         try: os.system( "%s --rebuilddb" % RPM_PATH )
172         except Exception, err: print "RebuildRPMdb: %s" % err
173
174     def RemoveRPMS( self ):
175
176         Message( "\nLooking for RPMs to be deleted." )
177         if os.access(DELETE_RPM_LIST_FILE, os.R_OK) and \
178            os.path.isfile(DELETE_RPM_LIST_FILE):
179             rpm_list_contents= file(DELETE_RPM_LIST_FILE).read()
180             rpm_list_contents= string.strip(rpm_list_contents)
181
182             if rpm_list_contents == "":
183                 Message( "No RPMs listed in file to delete." )
184                 return
185
186             rpm_list= string.join(string.split(rpm_list_contents))
187             
188             Message( "Deleting these RPMs:" )
189             Message( rpm_list_contents )
190             
191             rc= os.system( "%s -ev %s" % (RPM_PATH, rpm_list) )
192
193             if rc != 0:
194                 Error( "Unable to delete RPMs, continuing. rc=%d" % rc )
195             else:
196                 Message( "RPMs deleted successfully." )
197             
198         else:
199             Message( "No RPMs list file found." )
200
201
202
203 if __name__ == "__main__":
204
205     # if we are invoked with 'start', display the output. this
206     # is usefull for running something under cron and as a service
207     # (at startup), so the cron only outputs errors and doesn't
208     # generate mail when it works correctly
209
210     displayOutput= 0
211
212     # if we hit an rpm that requests a reboot, do it if this is
213     # set to 1. can be turned off by adding noreboot to command line
214     # option
215     
216     doReboot= 1
217
218     if "start" in sys.argv:
219         displayOutput= 1
220
221     if "noreboot" in sys.argv:
222         doReboot= 0
223
224     if "updatecron" in sys.argv:
225         # simply update the /etc/cron.d file for us, and exit
226         UpdateCronFile()
227         sys.exit(0)
228
229     if "removecron" in sys.argv:
230         RemoveCronFile()
231         sys.exit(0)     
232
233             
234     # see if we are already running by checking the existance
235     # of a PID file, and if it exists, attempting a test kill
236     # to see if the process really does exist. If both of these
237     # tests pass, exit.
238     
239     if os.access(NODEUPDATE_PID_FILE, os.R_OK):
240         pid= string.strip(file(NODEUPDATE_PID_FILE).readline())
241         if pid <> "":
242             if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
243                 Message( "It appears we are already running, exiting." )
244                 sys.exit(1)
245                     
246     # write out our process id
247     pidfile= file( NODEUPDATE_PID_FILE, 'w' )
248     pidfile.write( "%d\n" % os.getpid() )
249     pidfile.close()
250
251     
252     nodeupdate= NodeUpdate(doReboot)
253     if not nodeupdate:
254         Error( "Unable to initialize." )
255     else:
256         nodeupdate.RebuildRPMdb()
257         nodeupdate.RemoveRPMS()
258         nodeupdate.InstallKeys()
259         nodeupdate.CheckForUpdates()
260         Message( "\nUpdate complete." )
261
262     # remove the PID file
263     os.unlink( NODEUPDATE_PID_FILE )
264