Skip to content

Commit 44f51de

Browse files
committed
Various cleanup and improvements
Sorry! That is a terrible commit message, but I've been coding frantically and watching Topgun with the missus. She's never seen it. Watching vicariously I'm becoming increasingly certain that Topgun is a chickflick masquerading as a action movie.
1 parent ffbcc54 commit 44f51de

File tree

10 files changed

+244
-124
lines changed

10 files changed

+244
-124
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# snapper_systemd_boot
2+
3+
### TODO
4+
5+
* Look at user metadata to decide whether to clone vmlinuz and initramfs
6+
* Hook into snapper
7+
* Cleanup and commit
8+

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tool:pytest]
2+
addopts = -v
3+
testpaths = snapper_systemd_boot
4+
python_files = *.py
5+
log_cli = true

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
entry_points={
1414
"console_scripts": [
15-
"snapper-systemd-boot = snapper_systemd_boot.main:main",
15+
"snapper-systemd-boot = snapper_systemd_boot.cli:main",
1616
]
1717
}
1818
)

snapper_systemd_boot.conf.example

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[DEFAULT]
22

3-
IMAGES_SOURCE = /boot
3+
KERNEL_IMAGE_SOURCE = .bootbackup/pre/vmlinuz-linux
4+
INITRAMFS_IMAGE_SOURCE = .bootbackup/pre/initramfs-linux.img
5+
6+
IMAGES_SNAPSHOT_DIR = /boot/snapper
47
SYSTEMD_ENTRIES = /boot/loader/entries
8+
59
ENTRY_PREFIX = arch-auto-snapshot-
610
ENTRY_TEMPLATE =
7-
title Arch Linux (Snapshot {date} [{snapshot.num}])
11+
title Arch Linux (Snapshot {snapshot.iso_timestamp} [{snapshot.num}])
812
linux /vmlinuz-linux
913
initrd /initramfs-linux.img
10-
options \
11-
cryptdevice=UUID=d79c85d5-0ed6-4b92-b3dd-e7b6fc7dee9f:aeryn-root-crypt\
12-
root=/dev/mapper/aeryn-root-crypt quiet rw\
13-
rootflags=subvol=@/.snapshots/{snapshot.num}/snapshot
14+
options cryptdevice=UUID=d79c85d5-0ed6-4b92-b3dd-e7b6fc7dee9f:aeryn-root-crypt root=/dev/mapper/aeryn-root-crypt quiet rw rootflags=subvol=@/.snapshots/{snapshot.num}/snapshot

snapper_systemd_boot/cli.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
CLI for snapper_systemd_boot
4+
"""
5+
import json
6+
import logging
7+
import textwrap
8+
9+
import argh
10+
11+
from snapper_systemd_boot import context
12+
from snapper_systemd_boot.manager import SnapperSystemDBootManager
13+
14+
DEV_LOGGER = logging.getLogger(__name__)
15+
16+
17+
def update():
18+
"""
19+
Update systemd-boot entries based on snapper snapshots.
20+
"""
21+
DEV_LOGGER.info("Update entries.")
22+
inst = context.get_manager()
23+
inst.remove_boot_configs()
24+
inst.write_boot_entries()
25+
26+
27+
def remove():
28+
"""
29+
Remove all systemd-boot entries generated from snapper snapshots.
30+
"""
31+
DEV_LOGGER.info("Remove existing entries.")
32+
inst = context.get_manager()
33+
snapper = context.get_snapper()
34+
config = context.get_config()
35+
inst = SnapperSystemDBootManager(snapper, config)
36+
inst.remove_boot_configs()
37+
38+
39+
def view_config():
40+
"""
41+
Print config
42+
"""
43+
config = context.get_config()
44+
print(config)
45+
46+
47+
class SnapshotListWrapper:
48+
def __init__(self, snapshot):
49+
self.snapshot = snapshot
50+
51+
def __getattr__(self, name):
52+
return getattr(self.snapshot, name)
53+
54+
@property
55+
def userdata_compact_str(self):
56+
"""
57+
Outputs userdata in compact one line string.
58+
"""
59+
return textwrap.shorten(
60+
json.dumps(self.userdata),
61+
width=70,
62+
placeholder="...}")
63+
64+
@property
65+
def description(self):
66+
"""
67+
Shorten description for output.
68+
"""
69+
return textwrap.shorten(
70+
self.snapshot.description,
71+
width=80,
72+
placeholder="...")
73+
74+
def __str__(self):
75+
return textwrap.dedent("""\
76+
{s.num:04}: {s.description}
77+
created: {s.iso_timestamp}
78+
type: {s.type.name}
79+
user_data: {s.userdata_compact_str:.70}
80+
""".format(s=self))
81+
82+
83+
def list_snapshots():
84+
"""
85+
List snapshots that will be converted into entries.
86+
"""
87+
DEV_LOGGER.info("List applicable snapshots")
88+
inst = context.get_manager()
89+
snapper = context.get_snapper()
90+
config = context.get_config()
91+
inst = SnapperSystemDBootManager(snapper, config)
92+
93+
yield "Snapshots to make entries:"
94+
95+
for snapshot in map(SnapshotListWrapper, inst.get_snapshots_iter()):
96+
yield textwrap.indent(str(snapshot), " ")
97+
98+
99+
def list_generated():
100+
"""
101+
List the filenames of already generated entries.
102+
"""
103+
DEV_LOGGER.info("List generated snapshots")
104+
inst = context.get_manager()
105+
snapper = context.get_snapper()
106+
config = context.get_config()
107+
inst = SnapperSystemDBootManager(snapper, config)
108+
for p in inst.get_existing_entries():
109+
yield str(p)
110+
111+
112+
def list_entries():
113+
"""
114+
List entries that will be written.
115+
"""
116+
DEV_LOGGER.info("List generated entries.")
117+
inst = context.get_manager()
118+
snapper = context.get_snapper()
119+
config = context.get_config()
120+
inst = SnapperSystemDBootManager(snapper, config)
121+
for entry in inst.get_boot_entries():
122+
yield "Will write to path:"
123+
yield textwrap.indent(str(entry.get_entry_path()), " ")
124+
yield "\nWill write:"
125+
yield textwrap.indent(str(entry.get_contents()), " ")
126+
yield "=" * 80
127+
yield "\n"
128+
129+
130+
def main():
131+
parser = argh.ArghParser()
132+
parser.add_commands([
133+
update,
134+
remove,
135+
view_config,
136+
list_generated,
137+
list_snapshots,
138+
list_entries,
139+
])
140+
parser.add_argument(
141+
"--log-level",
142+
action="store",
143+
default="WARNING",
144+
help="Set the log level for the process.",
145+
)
146+
parser.set_default_command(update)
147+
148+
ns = parser.parse_args()
149+
logging.basicConfig(level=getattr(logging, ns.log_level))
150+
151+
parser.dispatch()
152+
153+
154+
if __name__ == "__main__":
155+
main()

snapper_systemd_boot/config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ class SnapperSystemDBootConfig:
88
def __init__(
99
self,
1010
*ignore,
11-
image_source_path,
1211
systemd_entries_path,
1312
entry_prefix,
1413
entry_template,
1514
):
1615
assert not ignore
1716

18-
self.image_source_path = Path(image_source_path)
19-
assert self.image_source_path.is_dir()
20-
2117
self.systemd_entries_path = Path(systemd_entries_path)
2218
assert self.systemd_entries_path.is_dir()
2319

@@ -30,13 +26,11 @@ def from_filename(cls, filename):
3026
config.read(filename)
3127
section = config["DEFAULT"]
3228
return cls(
33-
image_source_path=section["IMAGES_SOURCE"],
3429
systemd_entries_path=section["SYSTEMD_ENTRIES"],
3530
entry_prefix=section["ENTRY_PREFIX"],
3631
entry_template=section["ENTRY_TEMPLATE"])
3732

3833
__repr__ = GetattrRepr(
39-
image_source_path="image_source_path",
4034
systemd_entries_path="systemd_entries_path",
4135
entry_prefix="entry_prefix",
4236
entry_template="entry_template",

snapper_systemd_boot/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ def snapper():
1414

1515
@pytest.fixture
1616
def config(tmpdir):
17-
image_source_path = Path(tmpdir.mkdir("image_source"))
1817
systemd_entries_path = Path(tmpdir.mkdir("systemd_entries"))
1918
entry_template = dedent("""
20-
title Arch Linux (Snapshot {date} [{snapshot.num}])
19+
title Arch Linux (Snapshot {snapshot.iso_timestamp} [{snapshot.num}])
2120
linux /vmlinuz-linux
2221
initrd /initramfs-linux.img
2322
options \
@@ -27,7 +26,6 @@ def config(tmpdir):
2726
""")
2827

2928
yield SnapperSystemDBootConfig(
30-
image_source_path=image_source_path,
3129
systemd_entries_path=systemd_entries_path,
3230
entry_prefix="arch-auto-snapshot-",
3331
entry_template=entry_template)

snapper_systemd_boot/main.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)