Skip to content

Commit 125fecd

Browse files
committed
Add last start column.
1 parent f884778 commit 125fecd

3 files changed

Lines changed: 100 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Actually, followings commands are available (add `--help` to display all options
88
* `lmncli devices FILTER_STR` : List all devices which hostname, mac or room containing FILTER_STR. Gives all devices when FILTER_STR is emtpy.
99
* `lmncli linbo groups` : Display all available linbo groups.
1010
* `lmncli linbo images` : Display all available linbo images.
11-
* `lmncli linbo lastsync GROUP` : Display last synchronisation date for all devices or the selected GROUP, or all if GROUP is empty. Use `-w/--warning` or `-d/--danger` to only list the devices in the matching state (yellow, resp. red)
11+
* `lmncli linbo lastsync GROUP` : Display last synchronisation date for all devices or the selected GROUP, or all if GROUP is empty. Use `-w/--warning` or `-d/--danger` to only list the devices in the matching state (yellow, resp. red), and `-l/--last-start` to also show the last OS start of each device
1212
* `lmncli samba dns` : Display all current DNS enries.
1313
* `lmncli samba drives` : Display all configured drives in linuxmuster.net for the specified school.
1414
* `lmncli samba gpos` : Display all GPOS details on the system.

‎usr/lib/python3/dist-packages/linuxmusterCli/pytests/test_typers_linbo.py‎

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from datetime import datetime
23

34
from linuxmusterCli.typers import linbo
45
from linuxmusterCli.typers.state import state
@@ -171,7 +172,7 @@ def _devices(self):
171172
def test_groups_with_no_hosts_or_no_images_are_skipped(self, runner, monkeypatch):
172173
devices = self._devices()
173174
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
174-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
175+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
175176

176177
result = runner.invoke(linbo.app, ['lastsync'])
177178

@@ -182,7 +183,7 @@ def test_groups_with_no_hosts_or_no_images_are_skipped(self, runner, monkeypatch
182183
def test_never_synced_shows_no_date_found(self, runner, monkeypatch):
183184
devices = self._devices()
184185
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
185-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
186+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
186187

187188
result = runner.invoke(linbo.app, ['lastsync'])
188189

@@ -192,7 +193,7 @@ def test_never_synced_shows_no_date_found(self, runner, monkeypatch):
192193
def test_real_epoch_is_formatted_as_date(self, runner, monkeypatch):
193194
devices = self._devices()
194195
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
195-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
196+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
196197

197198
result = runner.invoke(linbo.app, ['lastsync'])
198199

@@ -207,7 +208,7 @@ def fake_list_workstations(groups=None, **kwargs):
207208
return self._devices()
208209

209210
monkeypatch.setattr(linbo, 'list_workstations', fake_list_workstations)
210-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
211+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
211212

212213
runner.invoke(linbo.app, ['lastsync', 'win10'])
213214

@@ -221,7 +222,7 @@ def fake_list_workstations(**kwargs):
221222
return self._devices()
222223

223224
monkeypatch.setattr(linbo, 'list_workstations', fake_list_workstations)
224-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
225+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
225226

226227
runner.invoke(linbo.app, ['lastsync'])
227228

@@ -236,7 +237,7 @@ def fake_list_workstations(**kwargs):
236237
return self._devices()
237238

238239
monkeypatch.setattr(linbo, 'list_workstations', fake_list_workstations)
239-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
240+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
240241

241242
result = runner.invoke(linbo.app, ['lastsync', 'win10', '--school', 'other-school'])
242243

@@ -246,7 +247,7 @@ def fake_list_workstations(**kwargs):
246247
def test_raw_format_exports_scalars_not_the_sync_dict(self, runner, monkeypatch):
247248
devices = self._devices()
248249
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
249-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
250+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
250251
state.format = True
251252
state.raw = True
252253

@@ -267,7 +268,7 @@ def test_raw_format_exports_scalars_not_the_sync_dict(self, runner, monkeypatch)
267268
def test_group_name_is_shown_as_table_title(self, runner, monkeypatch):
268269
devices = self._devices()
269270
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
270-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
271+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
271272

272273
result = runner.invoke(linbo.app, ['lastsync'])
273274

@@ -278,7 +279,7 @@ def test_group_name_is_shown_as_table_title(self, runner, monkeypatch):
278279
def test_group_is_appended_to_exported_rows(self, runner, monkeypatch):
279280
devices = self._devices()
280281
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
281-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
282+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
282283
state.format = True
283284
state.raw = True
284285

@@ -323,7 +324,7 @@ def _devices_with_version(self):
323324
def test_applied_image_version_is_shown_next_to_the_sync_date(self, runner, monkeypatch):
324325
devices = self._devices_with_version()
325326
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
326-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
327+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
327328

328329
result = runner.invoke(linbo.app, ['lastsync'])
329330

@@ -335,7 +336,7 @@ def test_applied_image_version_is_shown_next_to_the_sync_date(self, runner, monk
335336
def test_applied_image_version_is_exported_as_its_own_field(self, runner, monkeypatch):
336337
devices = self._devices_with_version()
337338
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
338-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
339+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
339340
state.format = True
340341
state.raw = True
341342

@@ -381,7 +382,7 @@ def _devices_by_status(self):
381382
def _patch_devices(self, monkeypatch):
382383
devices = self._devices_by_status()
383384
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
384-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
385+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
385386

386387
def test_without_status_filter_all_hosts_are_shown(self, runner, monkeypatch):
387388
self._patch_devices(monkeypatch)
@@ -447,11 +448,73 @@ def test_host_matching_on_one_image_keeps_all_its_images(self, runner, monkeypat
447448
},
448449
}
449450
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
450-
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices: None)
451+
monkeypatch.setattr(linbo, 'last_sync_all', lambda devices, **kwargs: None)
451452

452453
result = runner.invoke(linbo.app, ['lastsync', '--warning'])
453454

454455
assert result.exit_code == 0
455456
assert 'pc-mixed' in result.output
456457
assert 'win10.image' in result.output
457458
assert 'ubuntu.image' in result.output
459+
460+
def _patch_last_start(self, monkeypatch):
461+
devices = {
462+
'win10': {
463+
'hosts': [
464+
{
465+
'hostname': 'pc01', 'ip': '10.0.0.1',
466+
'image': [{'image': 'win10.image', 'date': 1700000000, 'status': 'success'}],
467+
},
468+
{
469+
'hostname': 'pc02', 'ip': '10.0.0.2',
470+
'image': [{'image': 'win10.image', 'date': 'Never', 'status': 'danger'}],
471+
},
472+
]
473+
},
474+
}
475+
calls = []
476+
477+
def fake_last_sync_all(devices, laststart=False):
478+
calls.append(laststart)
479+
if laststart:
480+
devices['win10']['hosts'][0]['lastStart'] = {'date': 1700000000, 'os': 'Windows 10'}
481+
devices['win10']['hosts'][1]['lastStart'] = 'Never'
482+
483+
monkeypatch.setattr(linbo, 'list_workstations', lambda **kw: devices)
484+
monkeypatch.setattr(linbo, 'last_sync_all', fake_last_sync_all)
485+
return calls
486+
487+
def test_last_start_is_not_read_by_default(self, runner, monkeypatch):
488+
calls = self._patch_last_start(monkeypatch)
489+
490+
result = runner.invoke(linbo.app, ['lastsync'])
491+
492+
assert result.exit_code == 0
493+
assert calls == [False]
494+
assert 'Last start' not in result.output
495+
496+
def test_last_start_option_shows_date_and_os(self, runner, monkeypatch):
497+
calls = self._patch_last_start(monkeypatch)
498+
499+
result = runner.invoke(linbo.app, ['lastsync', '--last-start'])
500+
501+
assert result.exit_code == 0
502+
assert calls == [True]
503+
output = ' '.join(result.output.split())
504+
assert '(Windows 10)' in output
505+
assert 'No start found' in output
506+
507+
def test_last_start_is_exported_after_the_group(self, runner, monkeypatch):
508+
self._patch_last_start(monkeypatch)
509+
state.format = True
510+
state.raw = True
511+
512+
result = runner.invoke(linbo.app, ['lastsync', '-l'])
513+
514+
assert result.exit_code == 0
515+
rows = [line.split('\t') for line in result.output.splitlines() if line.strip()]
516+
# The existing columns keep their position.
517+
assert rows[0][:2] == ['Hostname', 'IP']
518+
assert rows[0][-3:] == ['Group', 'Last start', 'Last started OS']
519+
assert rows[1][-2:] == [datetime.fromtimestamp(1700000000).strftime('%Y-%m-%d %H:%M'), 'Windows 10']
520+
assert rows[2][-2:] == ['Never', '']

‎usr/lib/python3/dist-packages/linuxmusterCli/typers/linbo.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def lastsync(
8181
school: Annotated[str, typer.Option("--school", "-s")] = 'default-school',
8282
warning: Annotated[bool, typer.Option("--warning", "-w", help="Only show devices with a warning status (yellow, not synchronised for more than 7 days).")] = False,
8383
danger: Annotated[bool, typer.Option("--danger", "-d", help="Only show devices with a danger status (red, never synchronised or not for more than 30 days).")] = False,
84+
last_start: Annotated[bool, typer.Option("--last-start", "-l", help="Also show the last OS start of each device, read from its linbo.log.")] = False,
8485
):
8586
# Temporary UGLY solution. The code in linuxmusterTools must be rewritten
8687

@@ -125,12 +126,23 @@ def format(sync_data):
125126
rendered += f' [dim](image {version})[/dim]'
126127
return rendered
127128

129+
def format_start(start):
130+
if start == 'Never':
131+
return '[dim]No start found[/dim]'
132+
date = datetime.fromtimestamp(start['date']).strftime('%c')
133+
return f"{date} [dim]({start['os'] or 'unknown OS'})[/dim]"
134+
135+
def start_fields(start):
136+
if start == 'Never':
137+
return ['Never', '']
138+
return [datetime.fromtimestamp(start['date']).strftime('%Y-%m-%d %H:%M'), start['os'] or '']
139+
128140
if group:
129141
devices = list_workstations(school=school, groups=[group])
130142
else:
131143
devices = list_workstations(school=school)
132144

133-
last_sync_all(devices)
145+
last_sync_all(devices, laststart=last_start)
134146

135147
for grp, hosts in devices.items():
136148
images = []
@@ -146,6 +158,8 @@ def format(sync_data):
146158
sync = Table(title=f"Group {grp} ({len(filtered_hosts)} device(s))")
147159
sync.add_column('Hostname', style="cyan")
148160
sync.add_column('IP', style="cyan")
161+
if last_start:
162+
sync.add_column('Last start')
149163
data = [[c.header for c in sync.columns]]
150164
for image in images:
151165
sync.add_column(f'Last synchronisation for {image}')
@@ -156,12 +170,18 @@ def format(sync_data):
156170
data[0].append(f'Applied version of {image}')
157171

158172
data[0].append('Group')
173+
if last_start:
174+
data[0].remove('Last start')
175+
data[0].append('Last start')
176+
data[0].append('Last started OS')
159177

160178
for host in filtered_hosts:
161179
sync_by_image = {d['image']: d for d in host['image']}
180+
start_cells = [format_start(host['lastStart'])] if last_start else []
162181
sync.add_row(
163182
host['hostname'],
164183
host['ip'],
184+
*start_cells,
165185
*[format(sync_by_image[image]) for image in images]
166186
)
167187
sync_fields = []
@@ -178,7 +198,8 @@ def format(sync_data):
178198
host['hostname'],
179199
host['ip'],
180200
*sync_fields,
181-
grp
201+
grp,
202+
*(start_fields(host['lastStart']) if last_start else [])
182203
])
183204

184205
if state.format:

0 commit comments

Comments
 (0)