Merge ~mwhudson/curtin:action-objects into curtin:master

Proposed by Michael Hudson-Doyle
Status: Merged
Approved by: Michael Hudson-Doyle
Approved revision: 47ad7b62b9489f0feaf7d0b979e035f9c7dca229
Merge reported by: Server Team CI bot
Merged at revision: not available
Proposed branch: ~mwhudson/curtin:action-objects
Merge into: curtin:master
Diff against target: 109 lines (+56/-8)
2 files modified
curtin/commands/block_meta.py (+23/-8)
curtin/storage_actions.py (+33/-0)
Reviewer Review Type Date Requested Status
Dan Bungert Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+448042@code.launchpad.net

Commit message

block_meta: start to turn dictionaries into objects

and start with the most insignificant handlers

Description of the change

Please admire this exercise in creative procrastination.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:5e680ba9c7b75e199cfb46a302ad8d07c793e22a

No commit message was specified in the merge proposal. Click on the following link and set the commit message (if you want jenkins to rebuild you need to trigger it yourself):
https://code.launchpad.net/~mwhudson/curtin/+git/curtin/+merge/448042/+edit-commit-message

https://jenkins.canonical.com/server-team/job/curtin-ci/159/
Executed test runs:
    FAILURE: https://jenkins.canonical.com/server-team/job/curtin-ci/nodes=metal-amd64/159/
    FAILURE: https://jenkins.canonical.com/server-team/job/curtin-ci/nodes=metal-arm64/159/
    FAILURE: https://jenkins.canonical.com/server-team/job/curtin-ci/nodes=metal-ppc64el/159/
    FAILURE: https://jenkins.canonical.com/server-team/job/curtin-ci/nodes=metal-s390x/159/

Click here to trigger a rebuild:
https://jenkins.canonical.com/server-team/job/curtin-ci/159//rebuild

review: Needs Fixing (continuous-integration)
~mwhudson/curtin:action-objects updated
e46db65... by Michael Hudson-Doyle

oops

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Needs Fixing (continuous-integration)
~mwhudson/curtin:action-objects updated
47ad7b6... by Michael Hudson-Doyle

sigh, need to stick to attr for now

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Dan Bungert (dbungert) wrote :

Let it begin

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/curtin/commands/block_meta.py b/curtin/commands/block_meta.py
2index 421b048..11972b9 100644
3--- a/curtin/commands/block_meta.py
4+++ b/curtin/commands/block_meta.py
5@@ -1,7 +1,7 @@
6 # This file is part of curtin. See LICENSE file for copyright and license info.
7
8 from collections import OrderedDict, namedtuple
9-from curtin import (block, compat, config, paths, util)
10+from curtin import (block, compat, config, paths, storage_actions, util)
11 from curtin.block import schemas
12 from curtin.block import (bcache, clear_holders, dasd, iscsi, lvm, mdadm, mkfs,
13 multipath, zfs)
14@@ -659,10 +659,24 @@ def get_path_to_storage_volume(volume, storage_config):
15 DEVS = set()
16
17
18+@storage_actions.define("image")
19+class Image:
20+ path: str
21+ size: int = storage_actions.size()
22+ preserve: bool = False
23+ sector_size: int = storage_actions.size(default=512)
24+
25+
26+@storage_actions.define("device")
27+class Device:
28+ path: str
29+
30+
31 def image_handler(info, storage_config, context):
32- path = info['path']
33- size = int(util.human2bytes(info['size']))
34- if info.get('preserve', False):
35+ image: Image = storage_actions.asobject(info)
36+ path = image.path
37+ size = image.size
38+ if image.preserve:
39 actual_size = os.stat(path).st_size
40 if size != actual_size:
41 raise RuntimeError(
42@@ -680,23 +694,24 @@ def image_handler(info, storage_config, context):
43 raise
44
45 cmd = ['losetup', '--show', '--find', path]
46- sector_size = int(util.human2bytes(info.get('sector_size', 512)))
47+ sector_size = image.sector_size
48 if sector_size != 512:
49 compat.supports_large_sectors(fatal=True)
50 cmd.extend(('--sector-size', str(sector_size)))
51 try:
52 dev = util.subp(cmd, capture=True)[0].strip()
53 except BaseException:
54- if os.path.exists(path) and not info.get('preserve'):
55+ if os.path.exists(path) and not image.preserve:
56 os.unlink(path)
57 raise
58- context.id_to_device[info['id']] = info['dev'] = dev
59+ context.id_to_device[image.id] = info['dev'] = dev
60 DEVS.add(dev)
61 context.handlers['disk'](info, storage_config, context)
62
63
64 def device_handler(info, storage_config, context):
65- context.id_to_device[info['id']] = info['path']
66+ device: Device = storage_actions.asobject(info)
67+ context.id_to_device[device.id] = device.path
68 context.handlers['disk'](info, storage_config, context)
69
70
71diff --git a/curtin/storage_actions.py b/curtin/storage_actions.py
72new file mode 100644
73index 0000000..b2a6fd4
74--- /dev/null
75+++ b/curtin/storage_actions.py
76@@ -0,0 +1,33 @@
77+import attr
78+
79+from curtin import config, util
80+
81+_type_to_cls = {}
82+
83+
84+def define(typ):
85+ def wrapper(c):
86+ c.type = attr.ib(default=typ)
87+ c.id = attr.ib()
88+ c.__annotations__["id"] = str
89+ c.__annotations__["type"] = str
90+ c = attr.s(auto_attribs=True, kw_only=True)(c)
91+ _type_to_cls[typ] = c
92+ return c
93+
94+ return wrapper
95+
96+
97+def _convert_size(s):
98+ if isinstance(s, str):
99+ return int(util.human2bytes(s))
100+ return s
101+
102+
103+def asobject(obj):
104+ cls = _type_to_cls[obj["type"]]
105+ return config.fromdict(cls, obj)
106+
107+
108+def size(*, default=attr.NOTHING):
109+ return attr.ib(converter=_convert_size, default=default)

Subscribers

People subscribed via source and target branches