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
1=== modified file 'meph2/commands/meph2_util.py'
2--- meph2/commands/meph2_util.py 2016-04-18 22:40:10 +0000
3+++ meph2/commands/meph2_util.py 2016-06-02 20:45:30 +0000
4@@ -11,7 +11,7 @@
5 import subprocess
6 import yaml
7
8-from meph2 import util
9+from meph2 import util, ubuntu_info
10 from meph2.url_helper import geturl_text
11
12 from simplestreams import (
13@@ -102,6 +102,16 @@
14 ('filters', {'nargs': '*', 'default': []}),
15 ]
16 },
17+ 'clean-unsupported': {
18+ 'help': 'clean streams metadata only to keep supported items',
19+ 'opts': [
20+ COMMON_FLAGS['dry-run'], COMMON_FLAGS['no-sign'],
21+ COMMON_FLAGS['keyring'],
22+ ('target', {}),
23+ ('--removed-stream',
24+ {'help': 'create stream of removed products'}),
25+ ]
26+ },
27 'find-orphans': {
28 'help': 'find files in data_d not referenced in a "path"',
29 'opts': [
30@@ -324,6 +334,61 @@
31 self.removed_versions.append((self.tcontent_id, pedigree,))
32
33
34+class UbuntuUnsupportedMirrorWriter(BareMirrorWriter):
35+
36+ def filter_product(self, data, src, target, pedigree):
37+ data = sutil.products_exdata(src, pedigree)
38+ is_ubuntu_os = (
39+ 'os' not in data or data['os'] == 'ubuntu')
40+ if is_ubuntu_os and data['release'] not in ubuntu_info.SUPPORTED:
41+ return True
42+ return False
43+
44+
45+class UbuntuCleanUnsupportedMirrorWriter(BareMirrorWriter):
46+
47+ def __init__(self, config, objectstore, remove):
48+ super(UbuntuCleanUnsupportedMirrorWriter, self).__init__(
49+ config, objectstore)
50+ self.remove = remove
51+
52+ def sync_products(self, reader, path=None, src=None, content=None):
53+ (src, content) = mirrors._get_data_content(path, src, content, reader)
54+
55+ sutil.expand_tree(src)
56+
57+ mirrors.check_tree_paths(src)
58+
59+ content_id = src['content_id']
60+ target = self.load_products(path, content_id)
61+ if not target:
62+ target = sutil.stringitems(src)
63+
64+ sutil.expand_tree(target)
65+
66+ stree = src.get('products', {})
67+ if 'products' not in target:
68+ target['products'] = {}
69+
70+ for prodname, product in stree.items():
71+ data = sutil.products_exdata(src, (prodname,))
72+ is_ubuntu_os = (
73+ 'os' not in data or data['os'] == 'ubuntu')
74+ if is_ubuntu_os and data['release'] not in ubuntu_info.SUPPORTED:
75+ self.removed_versions.append((prodname,))
76+ if self.remove:
77+ self.remove_product(product, src, target, (prodname,))
78+
79+ self.insert_products(path, target, content)
80+
81+ def remove_product(self, data, src, target, pedigree):
82+ # Delete all items in all versions.
83+ for vername, version in data.get('versions', {}).items():
84+ for itemname, item in version.get('items', {}).items():
85+ if 'path' in item:
86+ self.store.remove(item['path'])
87+
88+
89 def get_sha256_meta_images(url):
90 """ Given a URL to a SHA256SUM file return a dictionary of filenames and
91 SHA256 checksums keyed off the file version found as a date string in
92@@ -635,6 +700,52 @@
93 return 0
94
95
96+def main_clean_unsupported(args):
97+ (target_url, target_path) = sutil.path_from_mirror_url(args.target, None)
98+ policy = partial(util.endswith_policy, target_path, args.keyring)
99+
100+ if args.dry_run:
101+ smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
102+ tstore = objectstores.FileStore(target_url)
103+ tmirror = UbuntuCleanUnsupportedMirrorWriter(
104+ config={}, objectstore=tstore, remove=False)
105+ tmirror.sync(smirror, target_path)
106+ for pedigree in tmirror.removed_versions:
107+ sys.stderr.write("remove " + '/'.join(pedigree) + "\n")
108+ return 0
109+
110+ if args.removed_stream:
111+ # Copy unsupported releases to removed.
112+ (removed_url, removed_path) = sutil.path_from_mirror_url(
113+ args.removed_stream, None)
114+ smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
115+ tstore = objectstores.FileStore(removed_url)
116+ tmirror = UbuntuUnsupportedMirrorWriter(
117+ config={'max_items': 1}, objectstore=tstore)
118+ tmirror.sync(smirror, target_path)
119+ gen_index_and_sign(removed_url, not args.no_sign)
120+
121+ # Remove the unsupported releases from target.
122+ smirror = mirrors.UrlMirrorReader(target_url, policy=policy)
123+ tstore = objectstores.FileStore(target_url)
124+ tmirror = UbuntuCleanUnsupportedMirrorWriter(
125+ config={}, objectstore=tstore, remove=True)
126+ tmirror.sync(smirror, target_path)
127+ gen_index_and_sign(target_url, not args.no_sign)
128+
129+ # Remove any directories that are now empty.
130+ for dir, _, _ in os.walk(target_url, topdown=False):
131+ if dir == target_url:
132+ break
133+ else:
134+ try:
135+ os.rmdir(dir)
136+ except OSError:
137+ pass
138+
139+ return 0
140+
141+
142 def main_find_orphans(args):
143 data_d = args.data_d
144 streams_d = args.streams_dirs
145
146=== modified file 'tox.ini'
147--- tox.ini 2016-02-25 18:25:34 +0000
148+++ tox.ini 2016-06-02 20:45:30 +0000
149@@ -15,6 +15,9 @@
150 [testenv:jerff]
151 # the 'jerff' environment simulates what is on the build system
152 # it has very few dependencies, but must be able to run meph2-util
153+deps =
154+ {[testenv]deps}
155+ distro_info
156 commands = {toxinidir}/bin/meph2-util --help
157
158 [testenv:flake8]

Subscribers

People subscribed via source and target branches