1 | # |
---|
2 | # Copyright (C) 2022 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation; either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | |
---|
9 | """ |
---|
10 | Utility module for ogBrowser menu generation |
---|
11 | """ |
---|
12 | |
---|
13 | import os |
---|
14 | import socket |
---|
15 | |
---|
16 | from src.utils.net import getifaddr, getifhwaddr, ethtool |
---|
17 | |
---|
18 | MENU_TEMPLATE = """ |
---|
19 | <div align="center" style="font-family: Arial, Helvetica, sans-serif;"> |
---|
20 | <p style="color:#999999; font-size: 16px; margin: 2em;"> |
---|
21 | <table border="1" width="100%"> |
---|
22 | <tr> |
---|
23 | <td rowspan="2"><p align="left"><img border="0" src="../images/iconos/logoopengnsys.png"><p> </td> |
---|
24 | <td> Hostname </td> <td> IP </td> <td> MAC </td> <td> SPEED </td> </tr> |
---|
25 | <tr> <td>{hostname} </td> <td> {ip} </td> <td> {mac} </td> <td> {speed} </td> </tr> |
---|
26 | </table> |
---|
27 | </p> |
---|
28 | |
---|
29 | {boot_list} |
---|
30 | |
---|
31 | <p><a href="command:poweroff">Power Off</a></p> |
---|
32 | </div> |
---|
33 | """ |
---|
34 | |
---|
35 | MENU_OS_TEMPLATE = "<p><a href=\"command:bootOs {diskno} {partno}\">Boot {os} ({diskno}, {partno})</a></p>" |
---|
36 | |
---|
37 | def generate_menu(part_setup): |
---|
38 | """ |
---|
39 | Writes html menu to /opt/opengnsys/log/{ip}.info.html based on a partition |
---|
40 | setup |
---|
41 | """ |
---|
42 | device = os.getenv('DEVICE') |
---|
43 | if not device: |
---|
44 | return False |
---|
45 | |
---|
46 | ip = getifaddr(device) |
---|
47 | mac = getifhwaddr(device) |
---|
48 | speed = ethtool(device) |
---|
49 | hostname = socket.gethostname() |
---|
50 | menufile = f'/opt/opengnsys/log/{ip}.info.html' |
---|
51 | |
---|
52 | l = [MENU_OS_TEMPLATE.format(diskno=part['disk'], partno=part['partition'], os=part['os']) |
---|
53 | for part in part_setup if part['os']] |
---|
54 | boot_list = '\n'.join(l) |
---|
55 | |
---|
56 | with open(menufile, 'w') as f: |
---|
57 | f.write(MENU_TEMPLATE.format(hostname=hostname, ip=ip, mac=mac, speed=speed, boot_list=boot_list)) |
---|
58 | return True |
---|