diff options
Diffstat (limited to '')
-rwxr-xr-x | web/systems.py | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/web/systems.py b/web/systems.py new file mode 100755 index 0000000..8d8b31a --- /dev/null +++ b/web/systems.py @@ -0,0 +1,152 @@ +#!/usr/bin/python3.10 + +import platform + +from cgi import FieldStorage +from os import getenv +from subprocess import Popen, PIPE +from sys import exit + + + +def print_html(htmlfile, host_name): + with open(htmlfile, 'r') as f: + html = f.read() + + Host = getenv("HTTP_HOST", "n/a") + Protocol = getenv("REQUEST_SCHEME", "n/a") + Script = getenv("SCRIPT_NAME", "n/a") + + Url = Protocol + '://' + Host + Script + + print(html.format(**locals())) + +def get_host_name(): + host_name = platform.node() + + return host_name + +def main(): + host_name = get_host_name() + + Host = getenv("HTTP_HOST", "n/a") + Protocol = getenv("REQUEST_SCHEME", "n/a") + Script = getenv("SCRIPT_NAME", "n/a") + User = getenv("REMOTE_USER", "n/a") + + Url = Protocol + '://' + Host + Script + + form = FieldStorage() + + if form.getvalue("command"): + Command = form.getvalue("command").split()[0] + + if Command == 'start' or Command == 'stop': + Options = '-f' + elif Command == 'kill': + Options = '-k' + else: + Options = ' ' + + if "name" in form: + Name = form.getvalue("name").split()[0] + + if Command and Name: + # run command + command = 'env CONTAINER_USER="' + User + '" sudo --preserve-env=CONTAINER_USER cnt ' + Command + ' -n ' + Name + ' ' + Options + command_line = command.split() + cnt = Popen(command_line, stdout=PIPE) + + # return to page + print('Location: ' + Url) + print + + print('Content-Type: text/html\n') + print_html('html/header.html.in', host_name) + print_html('html/systems.top.html.in', host_name) + + command = 'env CONTAINER_USER="' + User + '" sudo --preserve-env=CONTAINER_USER cnt list -f sh' + command_line = command.split() + cnt_list = Popen(command_line, stdout=PIPE) + containers = cnt_list.stdout.readlines() + + print('<table class="table table-sm" style="width: 100%;">') + print('<thead class="thead-dark">') + print('<tr> <th>#</th> <th>System</th> <th>OS</th> <th>IP</th> <th>Actions</th> </tr>') + print('</thead>') + print('<tbody>') + + index = 0 + + for container in containers: + index = index + 1 + name = container.rstrip().decode('utf-8') + + command = 'env CONTAINER_USER="' + User + '" sudo --preserve-env=CONTAINER_USER cnt info -n ' + name + command_line = command.split() + cnt_info = Popen(command_line, stdout=PIPE) + info = cnt_info.stdout.readlines() + + status = info[0].decode('utf-8').strip() + os = info[1].decode('utf-8').strip() + ip = info[2].decode('utf-8').strip().replace(' ', '<br />') + + if status == 'started': + index_button = '<span class="badge bg-success">' + str(index) + '</span>' + elif status == 'stopped': + index_button = '<span class="badge bg-danger">' + str(index) + '</span>' + else: + index_button = '<span class="badge bg-info">' + str(index) + '</span>' + + # FIXME: maybe move out in favour of: cnt info -n foo --support + if os == 'Debian 11 (bullseye)' or os == 'Debian 10 (buster)': + os_button = '<span class="badge bg-success">' + os + '</span>' + elif os == 'Debian 9 (stretch)': + os_button = '<span class="badge bg-warning">' + os + '</span>' + elif os == 'Debian 8 (jessie)' or os == 'Debian 7 (wheezy)' or os == 'Debian 6 (squeeze)': + os_button = '<span class="badge bg-danger">' + os + '</span>' + else: + os_button = '<span class="badge bg-info">' + os + '</span>' + + if not ip: + ip = 'none' + + print( + '<tr> ' + + '<td>' + index_button + '</td> ' + + '<td>' + name + '</td> ' + + '<td>' + os_button + '</td> ' + + '<td>' + ip + '</td> ' + + '<td> ' + + '<div class="btn-group" role="group"> ' + ) + + if status == 'started': + print( + '<button type="button" class="btn btn-success" disabled>start</button> ' + + '<a href="' + Url + '?name=' + name + '&command=stop"><button type="button" class="btn btn-warning">stop</button></a> ' + + '</div> '+ + '<a href="' + Url + '?name=' + name + '&command=kill"><button type="button" class="btn btn-danger">kill</button></a> ' + ) + else: + print( + '<a href="' + Url + '?name=' + name + '&command=start"><button type="button" class="btn btn-success">start</button></a> ' + + '<button type="button" class="btn btn-warning" disabled>stop</button> ' + + '</div> '+ + '<button type="button" class="btn btn-danger" disabled>kill</button> ' + ) + + print( + '</td> ' + + '</tr>') + + print('</tbody>') + print('</table>') + + print_html("html/systems.bottom.html.in", host_name) + print_html('html/footer.html', host_name) + + exit(0) + +if __name__ == '__main__': + main() |