Please note that the CVS and issue trackers have moved to GitHub. These Trac pages are no longer kept up-to-date.

root/seattle/trunk/deploymentscripts/cleanup_deploy.py@5637

Revision 2520, 6.0 KB (checked in by konp, 10 years ago)

Committed the deployment scripts, just missing complete readme. Moved old files into /old before deletion.

Line 
1"""
2<Program Name>
3  cleanup_deploy.py
4
5<Started>
6  May 2009
7
8<Author>
9  n2k8000@u.washington.edu
10  Konstantin Pik
11
12<Purpose>
13  Executes on each test machine and prepares it for runlocaltest.py by
14  cleaning old log files (if any) and old directory locks (if any).
15
16<Usage>
17  Note: Not indended to run as a standalone.
18 
19  python cleanup_deploy.py myhostname
20
21  myhostname:   Required. The hostname of this computer. Used for some path
22                  information (logs, etc).
23"""
24
25import os
26import sys
27import subprocess
28
29# for logging purposes we keep this global
30my_host_name = ''
31
32
33
34def format_print(data):
35  """
36  <Purpose>
37    Wrapper for printing stuff to stdout. Just appends the name of this script
38    and my ip.
39
40  <Arguments>
41    data: the data to print to console.
42
43  <Exceptions>
44    None.
45
46  <Side Effects>
47    None.
48
49  <Returns>
50    None.
51  """
52
53  global my_host_name
54
55  # prints and puts a 'description string' identifying the msg from this
56  # script. so long as string isn't empty
57  if data.strip('\n '):
58    print my_host_name+': Cleanup & Setup Script: '+data
59
60   
61   
62def shellexec(cmd_str):
63  """
64  <Purpose>
65    Uses subprocess to execute the command string in the shell.
66     
67  <Arguments>
68    cmd_str:  The string to be treated as a command (or set of commands,
69                separated by ;).
70   
71  <Exceptions>
72    None.
73
74  <Side Effects>
75    None.
76
77  <Returns>
78    A tuple containing (stdout, strerr)
79
80    Detailed:
81    stdout: stdout printed to console during command execution.
82    strerr: error (note: some programs print to strerr instead of stdout)
83  """
84  handle = subprocess.Popen(cmd_str, shell=True, 
85      stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
86  stdoutdata, strerrdata = handle.communicate("")
87
88  return stdoutdata, strerrdata
89
90 
91 
92def find_seattle_installs():
93  """
94  <Purpose>
95    Finds all seattle install directories. (cares only about the first
96      one found)
97
98    Priority:
99      1. ./seattle_repy folder (exit after find)
100      2. ./[remotehost]/seattle_repy folder (exit after find)
101      3. search ALL folder with search depth 1 for a seattle_repy folder
102
103    Also assumes that seattle_repy is the name of the install directory.
104     
105  <Arguments>
106    None.
107   
108  <Exceptions>
109    None.
110
111  <Side Effects>
112    None.
113
114  <Returns>
115    A list containing the path to the folder containing seattle_repy install
116  """
117  global my_host_name
118
119  # This will keep track of which folders have a seattle installation
120  valid_folder = []
121  # Check for seattle_repy dir from where we are now..
122  if os.path.isdir("./"+my_host_name+"/seattle_repy"):
123    valid_folder.append("./"+my_host_name+"/seattle_repy")
124  elif os.path.isdir("./seattle_repy"):
125    valid_folder.append("./seattle_repy")
126  else:
127    # search each folder in current directory
128    # otherwise check all folders with depth one.
129    for folder in os.listdir('.'):
130      # if inside that folder we have a seattle install, add it to our list
131      if os.path.isdir("./"+folder+"/seattle_repy"):
132        valid_folder.append("./"+folder+"/seattle_repy")
133        return valid_folder
134 
135  # return list (with one element, the path) folder with a seattle_install
136  return valid_folder
137
138 
139 
140def clean_locks(paths):
141  # OBSOLETE
142  """
143  <Purpose>
144    Cleans DIR.lock files (possibly) left behind by previous runlocaltests.py
145    script so that this run will be a clean one. runlocaltests.py will check
146    for a DIR.lock file and if one is found it will skip over it - we don't
147    want that.
148     
149  <Arguments>
150    paths:  Path's discovered to have seattle_repy directories (same method as
151            runlocaltests.py). TODO: share common method across files?
152   
153  <Exceptions>
154    None.
155
156  <Side Effects>
157    None.
158
159  <Returns>
160    None.
161  """
162  return
163  for path in paths:
164    out, err = shellexec('rm -rf '+path+'/DIR.lock')
165    format_and_print(out, err)
166
167   
168   
169def clean_logs(paths):
170  """
171  <Purpose>
172    Cleans possibly left behind log files from previous runs.
173     
174  <Arguments>
175    paths:  Path's discovered to have seattle_repy directories (same method as
176            runlocaltests.py).
177   
178  <Exceptions>
179    None.
180
181  <Side Effects>
182    None.
183
184  <Returns>
185    None.
186  """
187  global my_host_name
188  # i clean the dirs for now so we cleanup all the stuff i might've left on
189  # some computers before i wrote this script.. but this will eventually be
190  # phased out.
191  for path in paths:
192    out, err = shellexec('rm -rf '+path+'/deploy.logs/')
193    format_and_print(out, err)
194  out, err = shellexec('rm -rf ./deploy.logs/')
195  format_and_print(out, err)
196 
197  # clean logfile
198  #out, err =  shellexec('rm -rf *.deployrun.log')
199  #format_and_print(out, err)
200
201  # clean errlog
202  #out, err =  shellexec('rm -rf *.deployrun.err.log')
203  #format_and_print(out, err)
204 
205  # clean tar files
206  #out, err =  shellexec('rm -rf '+my_host_name+'.tgz')
207  #format_and_print(out, err)
208
209 
210 
211def format_and_print(out, err):
212  """
213  <Purpose>
214    Helper method to format and print out from shellexec which returns two stdout
215    and err
216     
217  <Arguments>
218    out:  first string to format and print. typically stdout.
219    err:  second string to format and print. typically err.
220   
221  <Exceptions>
222    None.
223
224  <Side Effects>
225    None.
226
227  <Returns>
228    None.
229  """
230  out = out.strip('\n ')
231  err = out.strip('\n ')
232  format_print(out)
233  format_print(err)
234
235 
236
237def main():
238  """
239  <Purpose>
240    Launches the program.
241   
242    - Finds installs
243    - Cleans old log files
244    - Cleans old lock files
245     
246    Error on no seattle installations found.
247
248  <Arguments>
249    None.
250   
251  <Exceptions>
252    None.
253
254  <Side Effects>
255    None.
256
257  <Returns>
258    None.
259  """
260  global my_host_name
261
262  # assume this is safe for now because only deploy.py should be calling this script.
263  my_host_name = sys.argv[1]
264
265  # get the install paths
266  install_paths = find_seattle_installs()
267 
268  # empty!?
269  if not install_paths:
270    format_print('No valid installations of seattle found.')
271    return
272 
273  clean_locks(install_paths)
274  clean_logs(install_paths)
275
276if __name__ == '__main__':
277  main()
Note: See TracBrowser for help on using the browser.