blob: 3652b270e6664982a1ca8dfc93a5e082ef052e60 [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
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."""
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."""
Caroline Tice88272d42016-01-13 09:48:29 -080075 parser = argparse.ArgumentParser(
Han Shene4b6f222013-11-22 10:02:38 -080076 description='Automatically delete nightly test data directories.',
Han Shena58a8242014-04-23 11:05:22 -070077 usage='auto_delete_nightly_test_data.py options')
Caroline Tice88272d42016-01-13 09:48:29 -080078 parser.add_argument('-d',
79 '--dry_run',
80 dest='dry_run',
81 default=False,
82 action='store_true',
83 help='Only print command line, do not execute anything.')
84 parser.add_argument('--days_to_preserve',
85 dest='days_to_preserve',
86 default=3,
87 help=('Specify the number of days (not including today),'
88 ' test data generated on these days will *NOT* be '
89 'deleted. Defaults to 3.'))
90 options = parser.parse_args(argv)
Han Shene4b6f222013-11-22 10:02:38 -080091 return options
92
93
Han Shenfdd8a5b2015-02-18 09:37:52 -080094def CleanChromeOsTmpAndImages():
95 """Delete temporaries, images under crostc/chromeos."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080096 chromeos_chroot_tmp = os.path.join(constants.CROSTC_WORKSPACE, 'chromeos',
97 'chroot', 'tmp')
Han Shenfdd8a5b2015-02-18 09:37:52 -080098
99 ce = command_executer.GetCommandExecuter()
100 # Clean chroot/tmp/test_that_* and chroot/tmp/tmpxxxxxx, that were last
101 # accessed more than 24 hours ago.
102 cmd = (r'find {0} -maxdepth 1 -type d '
103 r'\( -name "test_that_*" -o -regex "{0}/tmp......" \) '
104 r'-amin +1440 '
105 r'-exec bash -c "echo rm -fr {{}}" \; '
106 r'-exec bash -c "rm -fr {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800107 rv = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800108 if rv == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800109 print('Successfully cleaned chromeos tree tmp directory '
110 '"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800111 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 print('Some directories were not removed under chromeos tree '
113 'tmp directory -"{0}".'.format(chromeos_chroot_tmp))
Han Shenfdd8a5b2015-02-18 09:37:52 -0800114
115 # Clean image tar files, which were last accessed 1 hour ago and clean image
116 # bin files that were last accessed more than 24 hours ago.
117 cmd = ('find {0}/*-release -type f '
118 r'\( -name "chromiumos_test_image.tar" -amin +60 -o '
119 r' -name "chromiumos_test_image.tar.xz" -amin +60 -o '
cmtice07eb87f2015-03-11 10:10:41 -0700120 r' -name "chromiumos_test_image.bin" -amin +1440 \) '
Han Shenfdd8a5b2015-02-18 09:37:52 -0800121 r'-exec bash -c "echo rm -f {{}}" \; '
122 r'-exec bash -c "rm -f {{}}" \;').format(chromeos_chroot_tmp)
Luis Lozano036c9232015-12-10 10:47:01 -0800123 rv2 = ce.RunCommand(cmd, print_to_console=False)
Han Shenfdd8a5b2015-02-18 09:37:52 -0800124 if rv2 == 0:
Yunlian Jiangd97422a2015-12-16 11:06:13 -0800125 print('Successfully cleaned chromeos images.')
Han Shenfdd8a5b2015-02-18 09:37:52 -0800126 else:
Yunlian Jiangd97422a2015-12-16 11:06:13 -0800127 print('Some chromeos images were not removed.')
Han Shenfdd8a5b2015-02-18 09:37:52 -0800128
129 return rv + rv2
130
131
Han Shene4b6f222013-11-22 10:02:38 -0800132def Main(argv):
Han Shenfdd8a5b2015-02-18 09:37:52 -0800133 """Delete nightly test data directories, tmps and test images."""
Han Shene4b6f222013-11-22 10:02:38 -0800134 options = ProcessArguments(argv)
135 # Function 'isoweekday' returns 1(Monday) - 7 (Sunday).
136 d = datetime.datetime.today().isoweekday()
137 # We go back 1 week, delete from that day till we are
138 # options.days_to_preserve away from today.
139 s = d - 7
cmtice798a8fa2014-05-12 13:56:42 -0700140 e = d - int(options.days_to_preserve)
Han Shena58a8242014-04-23 11:05:22 -0700141 rv = 0
Han Shene4b6f222013-11-22 10:02:38 -0800142 for i in range(s + 1, e):
143 if i <= 0:
144 ## Wrap around if index is negative. 6 is from i + 7 - 1, because
145 ## DIR_BY_WEEKDAY starts from 0, while isoweekday is from 1-7.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 dated_dir = DIR_BY_WEEKDAY[i + 6]
Han Shene4b6f222013-11-22 10:02:38 -0800147 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800148 dated_dir = DIR_BY_WEEKDAY[i - 1]
149 rv += 0 if CleanDatedDir(
150 os.path.join(constants.CROSTC_WORKSPACE,
151 dated_dir), options.dry_run) else 1
Han Shenfdd8a5b2015-02-18 09:37:52 -0800152
153 ## Finally clean temporaries, images under crostc/chromeos
154 rv2 = CleanChromeOsTmpAndImages()
155
156 return rv + rv2
Han Shene4b6f222013-11-22 10:02:38 -0800157
158
159if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800160 retval = Main(sys.argv[1:])
Han Shene4b6f222013-11-22 10:02:38 -0800161 sys.exit(retval)