summaryrefslogtreecommitdiffstats
path: root/web/services.py
diff options
context:
space:
mode:
Diffstat (limited to 'web/services.py')
-rwxr-xr-xweb/services.py156
1 files changed, 156 insertions, 0 deletions
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('<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 + ' --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 = '<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 + '&amp;command=stop"><button type="button" class="btn btn-warning">stop</button></a> ' +
+ '</div> '+
+ '<a href="' + Url + '?name=' + name + '&amp;command=kill"><button type="button" class="btn btn-danger">kill</button></a> '
+ )
+ else:
+ print(
+ '<a href="' + Url + '?name=' + name + '&amp;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/services.bottom.html.in", host_name)
+ print_html('html/footer.html', host_name)
+
+ exit(0)
+
+if __name__ == '__main__':
+ main()