From f73f2356820468344757dbb9d7f3ec73ece7bf66 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 13 Feb 2023 20:43:51 +0100 Subject: Updating. Signed-off-by: Daniel Baumann --- web/services.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100755 web/services.py (limited to 'web/services.py') diff --git a/web/services.py b/web/services.py new file mode 100755 index 0000000..77f89a2 --- /dev/null +++ b/web/services.py @@ -0,0 +1,156 @@ +#!/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: + 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) + + print('Location: ' + Url) + print + + print('Content-Type: text/html\n') + print_html('html/header.html.in', host_name) + print_html('html/services.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('') + print('') + print('') + print('') + print('') + + 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 + ' --status' + command_line = command.split() + cnt = Popen(command_line, stdout=PIPE) + status = str(cnt.stdout.readline().decode('utf-8').strip()) + + command = 'env CONTAINER_USER="' + User + '" sudo --preserve-env=CONTAINER_USER cnt info -n ' + name + ' --os' + command_line = command.split() + cnt = Popen(command_line, stdout=PIPE) + os = str(cnt.stdout.readline().decode('utf-8').strip()) + + command = 'env CONTAINER_USER="' + User + '" sudo --preserve-env=CONTAINER_USER cnt info -n ' + name + ' --ip' + command_line = command.split() + cnt = Popen(command_line, stdout=PIPE) + ip = str(cnt.stdout.readline().decode('utf-8').strip()) + + if status == 'started': + index_button = '' + str(index) + '' + elif status == 'stopped': + index_button = '' + str(index) + '' + else: + index_button = '' + str(index) + '' + + # FIXME: maybe move out in favour of: cnt info -n foo --support + if os == 'Debian 11 (bullseye)' or os == 'Debian 10 (buster)': + os_button = '' + os + '' + elif os == 'Debian 9 (stretch)': + os_button = '' + os + '' + elif os == 'Debian 8 (jessie)' or os == 'Debian 7 (wheezy)' or os == 'Debian 6 (squeeze)': + os_button = '' + os + '' + else: + os_button = '' + os + '' + + if not ip: + ip = 'none' + + print( + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + '') + + print('') + print('
# System OS IP Actions
' + index_button + '' + name + '' + os_button + '' + ip + ' ' + + '
' + ) + + if status == 'started': + print( + ' ' + + ' ' + + '
'+ + ' ' + ) + else: + print( + ' ' + + ' ' + + ' '+ + ' ' + ) + + print( + '
') + + print_html("html/services.bottom.html.in", host_name) + print_html('html/footer.html', host_name) + + exit(0) + +if __name__ == '__main__': + main() -- cgit v1.2.3