forked from PeterJCLaw/srcomp-kiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macs-to-names.py
executable file
·103 lines (82 loc) · 2.62 KB
/
macs-to-names.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
from __future__ import print_function
import os.path
import textwrap
LIVESTREAM_URL = 'https://www.youtube-nocookie.com/embed/M3stcLPbHOU'
FILE_NAME = 'pi_macs'
NAME_TEMPLATE = 'pi-{page}-{qual}.srobo'
LOCAL_NAME_SUFFIX = '.sr.lan'
PAGE_TEMPLATE = 'http://%{{hiera(\'compbox_hostname\')}}/{page}.html{query}'
CONTENT_TEMPLATE = '''# Student Robotics Pi {ident}
---
url: {url}
hostname: {name}
remote_ssh_port: {remote_ssh_port}
compbox_hostname: "%{{hiera('{compbox}_compbox_hostname')}}"
'''
def tidy(lines):
output_lines = []
for line in lines:
hash_idx = line.find('#')
if hash_idx > -1:
line = line[:hash_idx]
line = line.strip()
if line:
output_lines.append(line)
return output_lines
def build_url(page):
if page == 'livestream':
return LIVESTREAM_URL
parts = page.split('?')
if len(parts) == 1:
return PAGE_TEMPLATE.format(page=page, query='')
else:
query = '?' + parts[1]
return PAGE_TEMPLATE.format(page=parts[0], query=query)
def build_name(ident, page):
parts = page.split('?')
if len(parts) == 1:
return NAME_TEMPLATE.format(page=page, qual=ident)
else:
qual = parts[1].replace(',', '')
return NAME_TEMPLATE.format(page=parts[0], qual=qual)
def build_filename(mac):
return os.path.join('hieradata', 'node', mac + '.yaml')
def build_port(mac):
left, right = mac.split(':')[-2:]
upper, lower = int(left, 16), int(right, 16)
port = upper * 0xff + lower
return port
with open(FILE_NAME, 'r') as fh:
lines = tidy(fh.readlines())
port_to_name = {}
for line in lines:
ident, mac, compbox, page = line.split()
name = build_name(ident, page)
url = build_url(page)
remote_ssh_port = build_port(mac)
assert remote_ssh_port not in port_to_name
port_to_name[remote_ssh_port] = name
fn = build_filename(mac)
with open(fn, 'w+') as fh:
fh.write(CONTENT_TEMPLATE.format(
name=name,
ident=ident,
url=url,
remote_ssh_port=remote_ssh_port,
compbox=compbox,
))
with open('pi-ssh-config', mode='w') as f:
for port, name in port_to_name.items():
f.write(textwrap.dedent(f'''
Host {name}.remote
HostName localhost
Port {port}
ProxyJump srcomp.studentrobotics.org
User pi
Host {name}
HostName {name}{LOCAL_NAME_SUFFIX}
User pi
'''))
with open('pi-names', mode='w') as f:
print('\n'.join(port_to_name.values()), file=f)