blob: 1d9853af08ddfc9e55577c591132b3f3f4cd0ab4 [file] [log] [blame]
Manoj Guptaa8fd4f72016-12-06 11:40:58 -08001#!/usr/bin/env python2
Han Shene4b6f222013-11-22 10:02:38 -08002"""A crontab script to delete night test data."""
Yunlian Jiangd97422a2015-12-16 11:06:13 -08003
4from __future__ import print_function
5
Han Shene4b6f222013-11-22 10:02:38 -08006__author__ = 'shenhan@google.com (Han Shen)'
7
Caroline Tice88272d42016-01-13 09:48:29 -08008import argparse
Han Shene4b6f222013-11-22 10:02:38 -08009import datetime
Han Shene4b6f222013-11-22 10:02:38 -080010import os
Han Shena58a8242014-04-23 11:05:22 -070011import re
Han Shene4b6f222013-11-22 10:02:38 -080012import sys
13
Caroline Tice88272d42016-01-13 09:48:29 -080014from cros_utils import command_executer
15from cros_utils import constants
16from cros_utils import misc
Han Shene4b6f222013-11-22 10:02:38 -080017
18DIR_BY_WEEKDAY = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
19
20
21def CleanNumberedDir(s, dry_run=False):
22 """Deleted directories under each dated_dir."""
Manoj Guptaa8fd4f72016-12-06 11:40:58 -080023 chromeos_dirs = [
24 os.path.join(s, x) for x in os.listdir(s)
25 if misc.IsChromeOsTree(os.path.join(s, x))
26 ]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027 ce = command_executer.GetCommandExecuter(log_level='none')
Han Shena58a8242014-04-23 11:05:22 -070028 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080029 for cd in chromeos_dirs:
Han Shena58a8242014-04-23 11:05:22 -070030 if misc.DeleteChromeOsTree(cd, dry_run=dry_run):
Yunlian Jiangd97422a2015-12-16 11:06:13 -080031 print('Successfully removed chromeos tree "{0}".'.format(cd))
Han Shena58a8242014-04-23 11:05:22 -070032 else:
33 all_succeeded = False
Yunlian Jiangd97422a2015-12-16 11:06:13 -080034 print('Failed to remove chromeos tree "{0}", please check.'.format(cd))
Han Shena58a8242014-04-23 11:05:22 -070035
Han Shen4375f762014-04-25 10:35:29 -070036 if not all_succeeded:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080037 print('Failed to delete at least one chromeos tree, please check.')
Han Shen4375f762014-04-25 10:35:29 -070038 return False
39
Han Shena58a8242014-04-23 11:05:22 -070040 ## Now delete the numbered dir Before forcibly removing the directory, just
Han Shen4375f762014-04-25 10:35:29 -070041 ## check 's' to make sure it is sane. A valid dir to be removed must be
42 ## '/usr/local/google/crostc/(SUN|MON|TUE...|SAT)'.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 valid_dir_pattern = (
44 '^' + constants.CROSTC_WORKSPACE + '/(' + '|'.join(DIR_BY_WEEKDAY) + ')')
Han Shen4375f762014-04-25 10:35:29 -070045 if not re.search(valid_dir_pattern, s):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 print('Trying to delete an invalid dir "{0}" (must match "{1}"), '
47 'please check.'.format(s, valid_dir_pattern))
Han Shena58a8242014-04-23 11:05:22 -070048 return False
49
Han Shene4b6f222013-11-22 10:02:38 -080050 cmd = 'rm -fr {0}'.format(s)
51 if dry_run:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080052 print(cmd)
Han Shene4b6f222013-11-22 10:02:38 -080053 else:
Luis Lozano036c9232015-12-10 10:47:01 -080054 if ce.RunCommand(cmd, print_to_console=False, terminated_timeout=480) == 0:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080055 print('Successfully removed "{0}".'.format(s))
Han Shena58a8242014-04-23 11:05:22 -070056 else:
57 all_succeeded = False
Yunlian Jiangd97422a2015-12-16 11:06:13 -080058 print('Failed to remove "{0}", please check.'.format(s))
Han Shena58a8242014-04-23 11:05:22 -070059 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080060
61
62def CleanDatedDir(dated_dir, dry_run=False):
63 # List subdirs under dir
Manoj Guptaa8fd4f72016-12-06 11:40:58 -080064 subdirs = [
65 os.path.join(dated_dir, x) for x in os.listdir(dated_dir)
66 if os.path.isdir(os.path.join(dated_dir, x))
67 ]
Han Shena58a8242014-04-23 11:05:22 -070068 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080069 for s in subdirs:
Han Shena58a8242014-04-23 11:05:22 -070070 if not CleanNumberedDir(s, dry_run):
71 all_succeeded = False
72 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080073
74
75def ProcessArguments(argv):
76 """Process arguments."""
Caroline Tice88272d42016-01-13 09:48:29 -080077 parser = argparse.ArgumentParser(
Han Shene4b6f222013-11-22 10:02:38 -080078 description='Automatically delete nightly test data directories.',
Han Shena58a8242014-04-23 11:05:22 -070079 usage='auto_delete_nightly_test_data.py options')
Manoj Guptaa8fd4f72016-12-06 11:40:58 -080080 parser.add_argument(
81 '-d',
82 '--dry_run',
83 dest='dry_run',
84 default=False,
85 action='store_true',
86 help='Only print command line, do not execute anything.')
87 parser.add_argument(
88 '--days_to_preserve',
89 dest='days_to_preserve',
90 default=3,
91 help=('Specify the number of days (not including today),'
92 ' test data generated on these days will *NOT* be '
93 'deleted. Defaults to 3.'))
Caroline Tice88272d42016-01-13 09:48:29 -080094 options = parser.parse_args(argv)
Han Shene4b6f222013-11-22 10:02:38 -080095 return options
96
97
Manoj Gupta4762fbb2017-01-23 12:56:45 -080098def CleanChromeOsTmpFiles(chroot_tmp, days_to_preserve, dry_run):
Manoj Guptaa8fd4f72016-12-06 11:40:58 -080099 rv = 0
Han Shenfdd8a5b2015-02-18 09:37:52 -0800100 ce = command_executer.GetCommandExecuter()
101 # Clean chroot/tmp/test_that_* and chroot/tmp/tmpxxxxxx, that were last
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800102 # accessed more than specified time.
103 minutes = 1440 * days_to_preserve
Han Shenfdd8a5b2015-02-18 09:37:52 -0800104 cmd = (r'find {0} -maxdepth 1 -type d '
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800105 r'\( -name "test_that_*" -amin +{1} -o '
106 r' -name "cros-update*" -amin +{1} -o '
107 r' -regex "{0}/tmp......" -amin +{1} \) '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800108 r'-exec bash -c "echo rm -fr {{}}" \; '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800109 r'-exec bash -c "rm -fr {{}}" \;').format(chroot_tmp, minutes)
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800110 if dry_run:
111 print('Going to execute:\n%s' % cmd)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800112 else:
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800113 rv = ce.RunCommand(cmd, print_to_console=False)
114 if rv == 0:
115 print('Successfully cleaned chromeos tree tmp directory '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800116 '"{0}".'.format(chroot_tmp))
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800117 else:
118 print('Some directories were not removed under chromeos tree '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800119 'tmp directory -"{0}".'.format(chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800120
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800121 return rv
122
123
124def CleanChromeOsImageFiles(chroot_tmp, subdir_suffix, days_to_preserve,
125 dry_run):
126 rv = 0
127 rv2 = 0
128 ce = command_executer.GetCommandExecuter()
129 minutes = 1440 * days_to_preserve
Han Shenfdd8a5b2015-02-18 09:37:52 -0800130 # Clean image tar files, which were last accessed 1 hour ago and clean image
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800131 # bin files that were last accessed more than specified time.
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800132 cmd = ('find {0}/*{1} -type f '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800133 r'\( -name "chromiumos_test_image.tar" -amin +60 -o '
134 r' -name "chromiumos_test_image.tar.xz" -amin +60 -o '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800135 r' -name "chromiumos_test_image.bin" -amin +{2} \) '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800136 r'-exec bash -c "echo rm -f {{}}" \; '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800137 r'-exec bash -c "rm -f {{}}" \;').format(chroot_tmp, subdir_suffix,
138 minutes)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800139
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800140 if dry_run:
141 print('Going to execute:\n%s' % cmd)
142 else:
143 rv2 = ce.RunCommand(cmd, print_to_console=False)
144 if rv2 == 0:
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800145 print('Successfully cleaned chromeos images from '
146 '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800147 else:
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800148 print('Some chromeos images were not removed from '
149 '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800150
151 rv += rv2
152
153 # Clean autotest files that were last accessed more than specified time.
154 rv2 = 0
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800155 cmd = (r'find {0}/*{1} -maxdepth 2 -type d '
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800156 r'\( -name "autotest_files" \) '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800157 r'-amin +{2} '
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800158 r'-exec bash -c "echo rm -fr {{}}" \; '
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800159 r'-exec bash -c "rm -fr {{}}" \;').format(chroot_tmp, subdir_suffix,
160 minutes)
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800161 if dry_run:
162 print('Going to execute:\n%s' % cmd)
163 else:
164 rv2 = ce.RunCommand(cmd, print_to_console=False)
165 if rv2 == 0:
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800166 print('Successfully cleaned chromeos image autotest directories from '
167 '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800168 else:
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800169 print('Some image autotest directories were not removed from '
170 '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800171
172 rv += rv2
173 return rv
Han Shenfdd8a5b2015-02-18 09:37:52 -0800174
175
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800176def CleanChromeOsTmpAndImages(days_to_preserve=1, dry_run=False):
177 """Delete temporaries, images under crostc/chromeos."""
178 chromeos_chroot_tmp = os.path.join(constants.CROSTC_WORKSPACE, 'chromeos',
179 'chroot', 'tmp')
180 # Clean files in tmp directory
181 rv = CleanChromeOsTmpFiles(chromeos_chroot_tmp, days_to_preserve, dry_run)
182 # Clean image files in *-release directories
183 rv += CleanChromeOsImageFiles(chromeos_chroot_tmp, '-release',
184 days_to_preserve, dry_run)
185 # Clean image files in *-pfq directories
186 rv += CleanChromeOsImageFiles(chromeos_chroot_tmp, '-pfq', days_to_preserve,
187 dry_run)
188
189 return rv
190
191
Han Shene4b6f222013-11-22 10:02:38 -0800192def Main(argv):
Han Shenfdd8a5b2015-02-18 09:37:52 -0800193 """Delete nightly test data directories, tmps and test images."""
Han Shene4b6f222013-11-22 10:02:38 -0800194 options = ProcessArguments(argv)
195 # Function 'isoweekday' returns 1(Monday) - 7 (Sunday).
196 d = datetime.datetime.today().isoweekday()
197 # We go back 1 week, delete from that day till we are
198 # options.days_to_preserve away from today.
199 s = d - 7
cmtice798a8fa2014-05-12 13:56:42 -0700200 e = d - int(options.days_to_preserve)
Han Shena58a8242014-04-23 11:05:22 -0700201 rv = 0
Han Shene4b6f222013-11-22 10:02:38 -0800202 for i in range(s + 1, e):
203 if i <= 0:
204 ## Wrap around if index is negative. 6 is from i + 7 - 1, because
205 ## DIR_BY_WEEKDAY starts from 0, while isoweekday is from 1-7.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800206 dated_dir = DIR_BY_WEEKDAY[i + 6]
Han Shene4b6f222013-11-22 10:02:38 -0800207 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800208 dated_dir = DIR_BY_WEEKDAY[i - 1]
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800209
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800210 rv += 0 if CleanDatedDir(
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800211 os.path.join(constants.CROSTC_WORKSPACE, dated_dir),
212 options.dry_run) else 1
Han Shenfdd8a5b2015-02-18 09:37:52 -0800213
Manoj Gupta4762fbb2017-01-23 12:56:45 -0800214## Finally clean temporaries, images under crostc/chromeos
Manoj Guptaa8fd4f72016-12-06 11:40:58 -0800215 rv2 = CleanChromeOsTmpAndImages(
216 int(options.days_to_preserve), options.dry_run)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800217
218 return rv + rv2
Han Shene4b6f222013-11-22 10:02:38 -0800219
Han Shene4b6f222013-11-22 10:02:38 -0800220if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800221 retval = Main(sys.argv[1:])
Han Shene4b6f222013-11-22 10:02:38 -0800222 sys.exit(retval)