Merge lp:~blake-rouse/maas-images/clean-unsupported into lp:maas-images

Proposed by Blake Rouse
Status: Merged
Merged at revision: 301
Proposed branch: lp:~blake-rouse/maas-images/clean-unsupported
Merge into: lp:maas-images
Diff against target: 158 lines (+115/-1)
2 files modified
meph2/commands/meph2_util.py (+112/-1)
tox.ini (+3/-0)
To merge this branch: bzr merge lp:~blake-rouse/maas-images/clean-unsupported
Reviewer Review Type Date Requested Status
Lee Trager (community) Approve
Review via email: mp+296365@code.launchpad.net

Commit message

Add clean-unsupported command to meph2-utils.

Removes old Ubuntu releases from the stream and optionally creates a stream for the items it has removed.

Description of the change

Can be used to remove old releases from a stream. In this case to remove old releases from the releases stream and add to an old-releases stream.

meph2-util clean-unsupported releases/ --removed-stream releases-removed/
meph2-util merge releases-removed/ old-releases/
rm -rf releases-removed

For daily we should not keep any copies so its simply:

meph2-util clean-unsupported daily/

To post a comment you must log in.
Revision history for this message
Lee Trager (ltrager) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'meph2/commands/meph2_util.py'
--- meph2/commands/meph2_util.py 2016-04-18 22:40:10 +0000
+++ meph2/commands/meph2_util.py 2016-06-02 20:45:30 +0000
@@ -11,7 +11,7 @@
11import subprocess11import subprocess
12import yaml12import yaml
1313
14from meph2 import util14from meph2 import util, ubuntu_info
15from meph2.url_helper import geturl_text15from meph2.url_helper import geturl_text
1616
17from simplestreams import (17from simplestreams import (
@@ -102,6 +102,16 @@
102 ('filters', {'nargs': '*', 'default': []}),102 ('filters', {'nargs': '*', 'default': []}),
103 ]103 ]
104 },104 },
105 'clean-unsupported': {
106 'help': 'clean streams metadata only to keep supported items',
107 'opts': [
108 COMMON_FLAGS['dry-run'], COMMON_FLAGS['no-sign'],
109 COMMON_FLAGS['keyring'],
110 ('target', {}),
111 ('--removed-stream',
112 {'help': 'create stream of removed products'}),
113 ]
114 },
105 'find-orphans': {115 'find-orphans': {
106 'help': 'find files in data_d not referenced in a "path"',116 'help': 'find files in data_d not referenced in a "path"',
107 'opts': [117 'opts': [
@@ -324,6 +334,61 @@
324 self.removed_versions.append((self.tcontent_id, pedigree,))334 self.removed_versions.append((self.tcontent_id, pedigree,))
325335
326336
337class UbuntuUnsupportedMirrorWriter(BareMirrorWriter):
338
339 def filter_product(self, data, src, target, pedigree):
340 data = sutil.products_exdata(src, pedigree)
341 is_ubuntu_os = (
342 'os' not in data or data['os'] == 'ubuntu')
343 if is_ubuntu_os and data['release'] not in ubuntu_info.SUPPORTED:
344 return True
345 return False
346
347
348class UbuntuCleanUnsupportedMirrorWriter(BareMirrorWriter):
349
350 def __init__(self, config, objectstore, remove):
351 super(UbuntuCleanUnsupportedMirrorWriter, self).__init__(
352 config, objectstore)
353 self.remove = remove
354
355 def sync_products(self, reader, path=None, src=None, content=None):
356 (src, content) = mirrors._get_data_content(path, src, content, reader)
357
358 sutil.expand_tree(src)
359
360 mirrors.check_tree_paths(src)
361
362 content_id = src['content_id']
363 target = self.load_products(path, content_id)
364 if not target:
365 target = sutil.stringitems(src)
366
367 sutil.expand_tree(target)
368
369 stree = src.get('products', {})
370 if 'products' not in target:
371 target['products'] = {}
372
373 for prodname, product in stree.items():
374 data = sutil.products_exdata(src, (prodname,))
375 is_ubuntu_os = (
376 'os' not in data or data['os'] == 'ubuntu')
377 if is_ubuntu_os and data['release'] not in ubuntu_info.SUPPORTED:
378 self.removed_versions.append((prodname,))
379 if self.remove:
380 self.remove_product(product, src, target, (prodname,))
381
382 self.insert_products(path, target, content)
383
384 def remove_product(self, data, src, target, pedigree):
385 # Delete all items in all versions.
386 for vername, version in data.get('versions', {}).items():
387 for itemname, item in version.get('items', {}).items():
388 if 'path' in item:
389 self.store.remove(item['path'])
390
391
327def get_sha256_meta_images(url):392def get_sha256_meta_images(url):
328 """ Given a URL to a SHA256SUM file return a dictionary of filenames and393 """ Given a URL to a SHA256SUM file return a dictionary of filenames and
329 SHA256 checksums keyed off the file version found as a date string in394 SHA256 checksums keyed off the file version found as a date string in
@@ -635,6 +700,52 @@
635 return 0700 return 0
636701
637702
703def main_clean_unsupported(args):
704 (target_url, target_path) = sutil.path_from_mirror_url(args.target, None)
705 policy = partial(util.endswith_policy, target_path, args.keyring)
706
707 if args.dry_run:
708 smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
709 tstore = objectstores.FileStore(target_url)
710 tmirror = UbuntuCleanUnsupportedMirrorWriter(
711 config={}, objectstore=tstore, remove=False)
712 tmirror.sync(smirror, target_path)
713 for pedigree in tmirror.removed_versions:
714 sys.stderr.write("remove " + '/'.join(pedigree) + "\n")
715 return 0
716
717 if args.removed_stream:
718 # Copy unsupported releases to removed.
719 (removed_url, removed_path) = sutil.path_from_mirror_url(
720 args.removed_stream, None)
721 smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
722 tstore = objectstores.FileStore(removed_url)
723 tmirror = UbuntuUnsupportedMirrorWriter(
724 config={'max_items': 1}, objectstore=tstore)
725 tmirror.sync(smirror, target_path)
726 gen_index_and_sign(removed_url, not args.no_sign)
727
728 # Remove the unsupported releases from target.
729 smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
730 tstore = objectstores.FileStore(target_url)
731 tmirror = UbuntuCleanUnsupportedMirrorWriter(
732 config={}, objectstore=tstore, remove=True)
733 tmirror.sync(smirror, target_path)
734 gen_index_and_sign(target_url, not args.no_sign)
735
736 # Remove any directories that are now empty.
737 for dir, _, _ in os.walk(target_url, topdown=False):
738 if dir == target_url:
739 break
740 else:
741 try:
742 os.rmdir(dir)
743 except OSError:
744 pass
745
746 return 0
747
748
638def main_find_orphans(args):749def main_find_orphans(args):
639 data_d = args.data_d750 data_d = args.data_d
640 streams_d = args.streams_dirs751 streams_d = args.streams_dirs
641752
=== modified file 'tox.ini'
--- tox.ini 2016-02-25 18:25:34 +0000
+++ tox.ini 2016-06-02 20:45:30 +0000
@@ -15,6 +15,9 @@
15[testenv:jerff]15[testenv:jerff]
16# the 'jerff' environment simulates what is on the build system16# the 'jerff' environment simulates what is on the build system
17# it has very few dependencies, but must be able to run meph2-util17# it has very few dependencies, but must be able to run meph2-util
18deps =
19 {[testenv]deps}
20 distro_info
18commands = {toxinidir}/bin/meph2-util --help21commands = {toxinidir}/bin/meph2-util --help
1922
20[testenv:flake8]23[testenv:flake8]

Subscribers

People subscribed via source and target branches