blob: e01cb08bb2b69b6e6e8c41c6d1b9954abc6642c7 [file] [log] [blame]
Yunlian Jiangd97422a2015-12-16 11:06:13 -08001#!/usr/bin/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
8import datetime
9import optparse
10import os
Han Shena58a8242014-04-23 11:05:22 -070011import re
Han Shene4b6f222013-11-22 10:02:38 -080012import sys
13
14from utils import command_executer
15from utils import constants
16from utils import misc
17
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."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023 chromeos_dirs = [os.path.join(s, x)
24 for x in os.listdir(s)
Han Shene4b6f222013-11-22 10:02:38 -080025 if misc.IsChromeOsTree(os.path.join(s, x))]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080026 ce = command_executer.GetCommandExecuter(log_level='none')
Han Shena58a8242014-04-23 11:05:22 -070027 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080028 for cd in chromeos_dirs:
Han Shena58a8242014-04-23 11:05:22 -070029 if misc.DeleteChromeOsTree(cd, dry_run=dry_run):
Yunlian Jiangd97422a2015-12-16 11:06:13 -080030 print('Successfully removed chromeos tree "{0}".'.format(cd))
Han Shena58a8242014-04-23 11:05:22 -070031 else:
32 all_succeeded = False
Yunlian Jiangd97422a2015-12-16 11:06:13 -080033 print('Failed to remove chromeos tree "{0}", please check.'.format(cd))
Han Shena58a8242014-04-23 11:05:22 -070034
Han Shen4375f762014-04-25 10:35:29 -070035 if not all_succeeded:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080036 print('Failed to delete at least one chromeos tree, please check.')
Han Shen4375f762014-04-25 10:35:29 -070037 return False
38
Han Shena58a8242014-04-23 11:05:22 -070039 ## Now delete the numbered dir Before forcibly removing the directory, just
Han Shen4375f762014-04-25 10:35:29 -070040 ## check 's' to make sure it is sane. A valid dir to be removed must be
41 ## '/usr/local/google/crostc/(SUN|MON|TUE...|SAT)'.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 valid_dir_pattern = (
43 '^' + constants.CROSTC_WORKSPACE + '/(' + '|'.join(DIR_BY_WEEKDAY) + ')')
Han Shen4375f762014-04-25 10:35:29 -070044 if not re.search(valid_dir_pattern, s):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080045 print('Trying to delete an invalid dir "{0}" (must match "{1}"), '
46 'please check.'.format(s, valid_dir_pattern))
Han Shena58a8242014-04-23 11:05:22 -070047 return False
48
Han Shene4b6f222013-11-22 10:02:38 -080049 cmd = 'rm -fr {0}'.format(s)
50 if dry_run:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080051 print(cmd)
Han Shene4b6f222013-11-22 10:02:38 -080052 else:
Luis Lozano036c9232015-12-10 10:47:01 -080053 if ce.RunCommand(cmd, print_to_console=False, terminated_timeout=480) == 0:
Yunlian Jiangd97422a2015-12-16 11:06:13 -080054 print('Successfully removed "{0}".'.format(s))
Han Shena58a8242014-04-23 11:05:22 -070055 else:
56 all_succeeded = False
Yunlian Jiangd97422a2015-12-16 11:06:13 -080057 print('Failed to remove "{0}", please check.'.format(s))
Han Shena58a8242014-04-23 11:05:22 -070058 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080059
60
61def CleanDatedDir(dated_dir, dry_run=False):
62 # List subdirs under dir
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 subdirs = [os.path.join(dated_dir, x)
64 for x in os.listdir(dated_dir)
Han Shene4b6f222013-11-22 10:02:38 -080065 if os.path.isdir(os.path.join(dated_dir, x))]
Han Shena58a8242014-04-23 11:05:22 -070066 all_succeeded = True
Han Shene4b6f222013-11-22 10:02:38 -080067 for s in subdirs:
Han Shena58a8242014-04-23 11:05:22 -070068 if not CleanNumberedDir(s, dry_run):
69 all_succeeded = False
70 return all_succeeded
Han Shene4b6f222013-11-22 10:02:38 -080071
72
73def ProcessArguments(argv):
74 """Process arguments."""
75 parser = optparse.OptionParser(
76 description='Automatically delete nightly test data directories.',
Han Shena58a8242014-04-23 11:05:22 -070077 usage='auto_delete_nightly_test_data.py options')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 parser.add_option('-d',
79 '--dry_run',
80 dest='dry_run',
81 default=False,
82 action='store_true',
Han Shene4b6f222013-11-22 10:02:38 -080083 help='Only print command line, do not execute anything.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080084 parser.add_option('--days_to_preserve',
85 dest='days_to_preserve',
86 default=3,
cmtice37828572015-02-04 17:37:43 -080087 help=('Specify the number of days (not including today), '
88 'test data generated on these days will *NOT* be '
89 'deleted. Defaults to 3.'))
Han Shene4b6f222013-11-22 10:02:38 -080090 options, _ = parser.parse_args(argv)
91 return options
92
93
Han Shenfdd8a5b2015-02-18 09:37:52 -080094def CleanChromeOsTmpAndImages():
95 """Delete temporaries, images under crostc/chromeos."""
96
Luis Lozanof2a3ef42015-12-15 13:49:30 -080097 chromeos_chroot_tmp = os.path.join(constants.CROSTC_WORKSPACE, 'chromeos',
98 'chroot', 'tmp')
Han Shenfdd8a5b2015-02-18 09:37:52 -080099
100 ce = command_executer.GetCommandExecuter()
101 # Clean chroot/tmp/test_that_* and chroot/tmp/tmpxxxxxx, that were last
102 # accessed more than 24 hours ago.
103 cmd = (r'find {0} -maxdepth 1 -type d '
104 r'\( -name "test_that_*" -o -regex "{0}/tmp......" \) '
105 r'-amin +1440 '
106 r'-exec bash -c "echo rm -fr {{}}" \; '
107 r'-exec bash -c "rm -fr {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800108 rv = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800109 if rv == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110 print('Successfully cleaned chromeos tree tmp directory '
111 '"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800112 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800113 print('Some directories were not removed under chromeos tree '
114 'tmp directory -"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800115
116 # Clean image tar files, which were last accessed 1 hour ago and clean image
117 # bin files that were last accessed more than 24 hours ago.
118 cmd = ('find {0}/*-release -type f '
119 r'\( -name "chromiumos_test_image.tar" -amin +60 -o '
120 r' -name "chromiumos_test_image.tar.xz" -amin +60 -o '
cmtice07eb87f2015-03-11 10:10:41 -0700121 r' -name "chromiumos_test_image.bin" -amin +1440 \) '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800122 r'-exec bash -c "echo rm -f {{}}" \; '
123 r'-exec bash -c "rm -f {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800124 rv2 = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800125 if rv2 == 0:
Yunlian Jiangd97422a2015-12-16 11:06:13 -0800126 print('Successfully cleaned chromeos images.')
Han Shenfdd8a5b2015-02-18 09:37:52 -0800127 else:
Yunlian Jiangd97422a2015-12-16 11:06:13 -0800128 print('Some chromeos images were not removed.')
Han Shenfdd8a5b2015-02-18 09:37:52 -0800129
130 return rv + rv2
131
132
Han Shene4b6f222013-11-22 10:02:38 -0800133def Main(argv):
Han Shenfdd8a5b2015-02-18 09:37:52 -0800134 """Delete nightly test data directories, tmps and test images."""
Han Shene4b6f222013-11-22 10:02:38 -0800135 options = ProcessArguments(argv)
136 # Function 'isoweekday' returns 1(Monday) - 7 (Sunday).
137 d = datetime.datetime.today().isoweekday()
138 # We go back 1 week, delete from that day till we are
139 # options.days_to_preserve away from today.
140 s = d - 7
cmtice798a8fa2014-05-12 13:56:42 -0700141 e = d - int(options.days_to_preserve)
Han Shena58a8242014-04-23 11:05:22 -0700142 rv = 0
Han Shene4b6f222013-11-22 10:02:38 -0800143 for i in range(s + 1, e):
144 if i <= 0:
145 ## Wrap around if index is negative. 6 is from i + 7 - 1, because
146 ## DIR_BY_WEEKDAY starts from 0, while isoweekday is from 1-7.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800147 dated_dir = DIR_BY_WEEKDAY[i + 6]
Han Shene4b6f222013-11-22 10:02:38 -0800148 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800149 dated_dir = DIR_BY_WEEKDAY[i - 1]
150 rv += 0 if CleanDatedDir(
151 os.path.join(constants.CROSTC_WORKSPACE,
152 dated_dir), options.dry_run) else 1
Han Shenfdd8a5b2015-02-18 09:37:52 -0800153
154 ## Finally clean temporaries, images under crostc/chromeos
155 rv2 = CleanChromeOsTmpAndImages()
156
157 return rv + rv2
Han Shene4b6f222013-11-22 10:02:38 -0800158
159
160if __name__ == '__main__':
161 retval = Main(sys.argv)
162 sys.exit(retval)