dodanie listy obrazów i poprawa buga

This commit is contained in:
Mateusz779 2023-04-19 14:10:31 +02:00
parent 8d2455724e
commit 26275a0674
7 changed files with 84 additions and 15 deletions

11
app.py
View File

@ -47,7 +47,16 @@ def logout():
return response
return render_template('login.html')
@app.route('/create/conf')
@app.route('/images')
def list_images():
auth_token = request.cookies.get('auth_token')
if auth_token != "" or auth_token is not None:
if db.get_user_bytoken(auth_token) is None:
return redirect("/login")
images_all = db.get_images()
return render_template("images.html", images=images_all)
@app.route('/create')
def create_conf():
auth_token = request.cookies.get('auth_token')
if auth_token != "" or auth_token is not None:

View File

@ -82,13 +82,11 @@ fi
mkdir /tmp/output/vpn/scripts
cat <<EOF> /tmp/output/vpn/scripts/starttap.sh
#!/bin/sh
ifconfig uvpnT2 $ip netmask 255.255.255.0 up
EOF
cat <<EOF> /tmp/output/vpn/scripts/arpinggw.sh
#!/bin/sh
ip -s -s neigh flush all dev uvpnT2
EOF

16
db.py
View File

@ -2,6 +2,7 @@ import psycopg2
import config
import utils
import machines
import images
def connect():
global cur, conn
@ -172,6 +173,21 @@ def get_machines():
return machinesall
except:
return None
def get_images():
connect()
with get_cur() as cur:
cur.execute("""
SELECT id, token, image_name FROM image""")
try:
images_all = images.ImageManager()
for row in cur.fetchall():
image = images.Image(id = row[0], token=row[1], name=row[2])
images_all.add_image(image)
return images_all
except:
return None
def get_image_allocation_all_id():
connect()

21
images.py Normal file
View File

@ -0,0 +1,21 @@
class Image:
def __init__(self, id, token, name):
self.id = id
self.name = name
self.token = token
class ImageManager:
def __init__(self):
self.images = []
def add_image(self, machine):
self.images.append(machine)
def remove_image(self, machine):
self.images.remove(machine)
def get_image_by_token(self, token):
for image in self.images:
if image.token == token:
return image
return None

View File

@ -7,11 +7,6 @@ class Machine:
self.iplocal = iplocal
self.username = username
self.password = password
def __str__(self):
return f"Name: {self.name}\nImage name: {self.image_name}\nStart time: {self.start_time}\nIP VPN: {self.ipvpn}\nIP local: {self.iplocal}\nUsername: {self.username}\nPassword: {self.password}"
class MachineManager:
def __init__(self):
self.machines = []
@ -27,9 +22,3 @@ class MachineManager:
if machine.name == name:
return machine
return None
def __str__(self):
result = ""
for machine in self.machines:
result += str(machine) + "\n\n"
return result

View File

@ -20,7 +20,10 @@
<a class="nav-link" href="/">Lista maszyn</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/create/conf">Tworzenie obrazu</a>
<a class="nav-link" href="/create">Tworzenie obrazu</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/images">Lista obrazów</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/logout">Wyloguj</a>

33
templates/images.html Normal file
View File

@ -0,0 +1,33 @@
{% extends "base.html" %}
{% block title %}Obrazy{% endblock %}
{% block style %}
{% endblock %}
{% block content %}
<div class="container">
<h1 class="my-4">Lista obrazów</h1>
<table class="table">
<thead>
<tr>
<th>Token</th>
<th>Nazwa</th>
<th>Usuń</th>
</tr>
</thead>
<tbody>
{% for image in images %}
<tr>
<td>{{ image.token }}</td>
<td>{{ image.name }}</td>
<td>
<form method="post" action="/api/delete_image/{{ image.id }}">
<button type="submit" class="btn btn-danger">Usuń</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}