blob: ea6ea7cc8475d27f8567c21c7b6b9032bb29dc84 [file] [log] [blame]
Han Shene4b6f222013-11-22 10:02:38 -08001#!/usr/bin/python
Han Shene4b6f222013-11-22 10:02:38 -08002"""A crontab script to delete night test data."""
3__author__ = 'shenhan@google.com (Han Shen)'
4
5import datetime
6import optparse
7import os
Han Shena58a8242014-04-23 11:05:22 -07008import re
Han Shene4b6f222013-11-22 10:02:38 -08009import sys
10
11from utils import command_executer
12from utils import constants
13from utils import misc
14
15DIR_BY_WEEKDAY = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
16
17
18def CleanNumberedDir(s, dry_run=False):
19 """Deleted directories under each dated_dir."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080020 chromeos_dirs = [os.path.join(s, x)
21 for x in os.listdir(s)
Han Shene4b6f222013-11-22 10:02:38 -080022 if misc.IsChromeOsTree(os.path.join(s, x))]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023 ce = command_executer.GetCommandExecuter(log_level='none')
Han Shena58a8242014-04-23 11:05:22 -070024 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080025 for cd in chromeos_dirs:
Han Shena58a8242014-04-23 11:05:22 -070026 if misc.DeleteChromeOsTree(cd, dry_run=dry_run):
27 print 'Successfully removed chromeos tree "{0}".'.format(cd)
28 else:
29 all_succeeded = False
30 print 'Failed to remove chromeos tree "{0}", please check.'.format(cd)
31
Han Shen4375f762014-04-25 10:35:29 -070032 if not all_succeeded:
33 print 'Failed to delete at least one chromeos tree, please check.'
34 return False
35
Han Shena58a8242014-04-23 11:05:22 -070036 ## Now delete the numbered dir Before forcibly removing the directory, just
Han Shen4375f762014-04-25 10:35:29 -070037 ## check 's' to make sure it is sane. A valid dir to be removed must be
38 ## '/usr/local/google/crostc/(SUN|MON|TUE...|SAT)'.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080039 valid_dir_pattern = (
40 '^' + constants.CROSTC_WORKSPACE + '/(' + '|'.join(DIR_BY_WEEKDAY) + ')')
Han Shen4375f762014-04-25 10:35:29 -070041 if not re.search(valid_dir_pattern, s):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 print('Trying to delete an invalid dir "{0}" (must match "{1}"), '
43 'please check.'.format(s, valid_dir_pattern))
Han Shena58a8242014-04-23 11:05:22 -070044 return False
45
Han Shene4b6f222013-11-22 10:02:38 -080046 cmd = 'rm -fr {0}'.format(s)
47 if dry_run:
48 print cmd
49 else:
Luis Lozano036c9232015-12-10 10:47:01 -080050 if ce.RunCommand(cmd, print_to_console=False, terminated_timeout=480) == 0:
Han Shena58a8242014-04-23 11:05:22 -070051 print 'Successfully removed "{0}".'.format(s)
52 else:
53 all_succeeded = False
54 print 'Failed to remove "{0}", please check.'.format(s)
55 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080056
57
58def CleanDatedDir(dated_dir, dry_run=False):
59 # List subdirs under dir
Luis Lozanof2a3ef42015-12-15 13:49:30 -080060 subdirs = [os.path.join(dated_dir, x)
61 for x in os.listdir(dated_dir)
Han Shene4b6f222013-11-22 10:02:38 -080062 if os.path.isdir(os.path.join(dated_dir, x))]
Han Shena58a8242014-04-23 11:05:22 -070063 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080064 for s in subdirs:
Han Shena58a8242014-04-23 11:05:22 -070065 if not CleanNumberedDir(s, dry_run):
66 all_succeeded = False
67 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080068
69
70def ProcessArguments(argv):
71 """Process arguments."""
72 parser = optparse.OptionParser(
73 description='Automatically delete nightly test data directories.',
Han Shena58a8242014-04-23 11:05:22 -070074 usage='auto_delete_nightly_test_data.py options')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080075 parser.add_option('-d',
76 '--dry_run',
77 dest='dry_run',
78 default=False,
79 action='store_true',
Han Shene4b6f222013-11-22 10:02:38 -080080 help='Only print command line, do not execute anything.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 parser.add_option('--days_to_preserve',
82 dest='days_to_preserve',
83 default=3,
cmtice37828572015-02-04 17:37:43 -080084 help=('Specify the number of days (not including today), '
85 'test data generated on these days will *NOT* be '
86 'deleted. Defaults to 3.'))
Han Shene4b6f222013-11-22 10:02:38 -080087 options, _ = parser.parse_args(argv)
88 return options
89
90
Han Shenfdd8a5b2015-02-18 09:37:52 -080091def CleanChromeOsTmpAndImages():
92 """Delete temporaries, images under crostc/chromeos."""
93
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 chromeos_chroot_tmp = os.path.join(constants.CROSTC_WORKSPACE, 'chromeos',
95 'chroot', 'tmp')
Han Shenfdd8a5b2015-02-18 09:37:52 -080096
97 ce = command_executer.GetCommandExecuter()
98 # Clean chroot/tmp/test_that_* and chroot/tmp/tmpxxxxxx, that were last
99 # accessed more than 24 hours ago.
100 cmd = (r'find {0} -maxdepth 1 -type d '
101 r'\( -name "test_that_*" -o -regex "{0}/tmp......" \) '
102 r'-amin +1440 '
103 r'-exec bash -c "echo rm -fr {{}}" \; '
104 r'-exec bash -c "rm -fr {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800105 rv = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800106 if rv == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800107 print('Successfully cleaned chromeos tree tmp directory '
108 '"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800109 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110 print('Some directories were not removed under chromeos tree '
111 'tmp directory -"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800112
113 # Clean image tar files, which were last accessed 1 hour ago and clean image
114 # bin files that were last accessed more than 24 hours ago.
115 cmd = ('find {0}/*-release -type f '
116 r'\( -name "chromiumos_test_image.tar" -amin +60 -o '
117 r' -name "chromiumos_test_image.tar.xz" -amin +60 -o '
cmtice07eb87f2015-03-11 10:10:41 -0700118 r' -name "chromiumos_test_image.bin" -amin +1440 \) '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800119 r'-exec bash -c "echo rm -f {{}}" \; '
120 r'-exec bash -c "rm -f {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800121 rv2 = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800122 if rv2 == 0:
Luis Lozano1af92942015-11-04 17:48:32 -0800123 print 'Successfully cleaned chromeos images.'
Han Shenfdd8a5b2015-02-18 09:37:52 -0800124 else:
Luis Lozano1af92942015-11-04 17:48:32 -0800125 print 'Some chromeos images were not removed.'
Han Shenfdd8a5b2015-02-18 09:37:52 -0800126
127 return rv + rv2
128
129
Han Shene4b6f222013-11-22 10:02:38 -0800130def Main(argv):
Han Shenfdd8a5b2015-02-18 09:37:52 -0800131 """Delete nightly test data directories, tmps and test images."""
Han Shene4b6f222013-11-22 10:02:38 -0800132 options = ProcessArguments(argv)
133 # Function 'isoweekday' returns 1(Monday) - 7 (Sunday).
134 d = datetime.datetime.today().isoweekday()
135 # We go back 1 week, delete from that day till we are
136 # options.days_to_preserve away from today.
137 s = d - 7
cmtice798a8fa2014-05-12 13:56:42 -0700138 e = d - int(options.days_to_preserve)
Han Shena58a8242014-04-23 11:05:22 -0700139 rv = 0
Han Shene4b6f222013-11-22 10:02:38 -0800140 for i in range(s + 1, e):
141 if i <= 0:
142 ## Wrap around if index is negative. 6 is from i + 7 - 1, because
143 ## DIR_BY_WEEKDAY starts from 0, while isoweekday is from 1-7.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800144 dated_dir = DIR_BY_WEEKDAY[i + 6]
Han Shene4b6f222013-11-22 10:02:38 -0800145 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 dated_dir = DIR_BY_WEEKDAY[i - 1]
147 rv += 0 if CleanDatedDir(
148 os.path.join(constants.CROSTC_WORKSPACE,
149 dated_dir), options.dry_run) else 1
Han Shenfdd8a5b2015-02-18 09:37:52 -0800150
151 ## Finally clean temporaries, images under crostc/chromeos
152 rv2 = CleanChromeOsTmpAndImages()
153
154 return rv + rv2
Han Shene4b6f222013-11-22 10:02:38 -0800155
156
157if __name__ == '__main__':
158 retval = Main(sys.argv)
159 sys.exit(retval)