#!/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('