/usr/lib/python3/dist-packages/provisioningserver/import_images/cleanup.py is in python3-maas-provisioningserver 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | # Copyright 2014-2016 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Clean up old snapshot directories."""
__all__ = [
'cleanup_snapshots_and_cache',
]
import os
import shutil
def list_old_snapshots(storage):
"""List of snapshot directories that are no longer in use."""
current_dir = os.path.join(storage, 'current')
if os.path.exists(current_dir):
current_snapshot = os.path.basename(os.readlink(current_dir))
else:
current_snapshot = None
return [
os.path.join(storage, directory)
for directory in os.listdir(storage)
if directory.startswith('snapshot-') and directory != current_snapshot
]
def cleanup_snapshots(storage):
"""Remove old snapshot directories."""
old_snapshots = list_old_snapshots(storage)
for snapshot in old_snapshots:
shutil.rmtree(snapshot)
def list_unused_cache_files(storage):
"""List of cache files that are no longer being referenced by snapshots."""
cache_dir = os.path.join(storage, 'cache')
if os.path.exists(cache_dir):
cache_files = [
os.path.join(cache_dir, filename)
for filename in os.listdir(cache_dir)
if os.path.isfile(os.path.join(cache_dir, filename))
]
else:
cache_files = []
return [
cache_file
for cache_file in cache_files
if os.stat(cache_file).st_nlink == 1
]
def cleanup_cache(storage):
"""Remove files that are no longer being referenced by snapshots."""
cache_files = list_unused_cache_files(storage)
for cache_file in cache_files:
os.remove(cache_file)
def cleanup_snapshots_and_cache(storage):
"""Remove old snapshot directories and old cache files."""
cleanup_snapshots(storage)
cleanup_cache(storage)
|