summaryrefslogtreecommitdiffstats
path: root/web/services.py
blob: 77f89a249d8b1952b6fed5dd6b629cf4d4764bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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()