dodanie kolumny z ip po sieci lokalnej

This commit is contained in:
Mateusz779 2023-04-19 12:01:53 +02:00
parent 8c87e36370
commit 407bb21168
5 changed files with 19 additions and 32 deletions

2
app.py
View File

@ -189,7 +189,7 @@ def add_ip():
ip = request.form['ip']
except:
return jsonify(message="400")
if db.update_image_allocation_ip(token, ip) is not None:
if db.update_image_allocation_ip_vpn(token, ip) is not None:
return jsonify(message="200")
else:
return jsonify(message="400")

17
db.py
View File

@ -50,7 +50,8 @@ def connect():
image_id INTEGER NOT NULL REFERENCES image(id),
allocation_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_access_time TIMESTAMP,
client_ip INET
client_ip_local INET,
client_ip_vpn INET
);""")
conn.commit()
@ -148,13 +149,13 @@ def get_machines():
connect()
with get_cur() as cur:
cur.execute("""
SELECT image_id, allocation_time, client_ip FROM image_allocation""")
SELECT image_id, allocation_time, client_ip_vpn, client_ip_local FROM image_allocation""")
try:
machinesall = machines.MachineManager()
for row in cur.fetchall():
token = get_one("SELECT token FROM image WHERE id = %s", row[0])
image_name = get_one("SELECT image_name FROM image WHERE id = %s", row[0])
machine = machines.Machine(token, image_name, start_time=row[1], ip=row[2], username="root", password="")
machine = machines.Machine(token, image_name, start_time=row[1], ipvpn=row[2], iplocal=row[3], username="root", password="")
machinesall.add_machine(machine)
return machinesall
except:
@ -213,8 +214,8 @@ def get_image_allocation_clientip(token):
return get_one("SELECT last_access_time FROM image_allocation WHERE id = %s", id_image)
def get_image_allocation_clientip_id(id):
return get_one("SELECT client_ip FROM image_allocation WHERE id = %s", id)
def get_image_allocation_clientip_id_vpn(id):
return get_one("SELECT client_ip_vpn FROM image_allocation WHERE id = %s", id)
def set_image_allocation(token, client_ip):
@ -225,7 +226,7 @@ def set_image_allocation(token, client_ip):
connect()
with get_cur() as cur:
cur.execute("""
INSERT INTO image_allocation (image_id, client_ip, last_access_time)
INSERT INTO image_allocation (image_id, client_ip_local, last_access_time)
VALUES (%s, %s, CURRENT_TIMESTAMP)
""", (id_image, client_ip,))
conn.commit()
@ -271,14 +272,14 @@ def update_image_allocation_time(id):
except:
return None
def update_image_allocation_ip(token, ip):
def update_image_allocation_ip_vpn(token, ip):
image_id = get_conf_id(token)
if image_id is None:
return None
connect()
with get_cur() as cur:
cur.execute("""
UPDATE image_allocation SET client_ip = %s WHERE image_id = %s
UPDATE image_allocation SET client_ip_vpn = %s WHERE image_id = %s
""", (ip, image_id,))
try:
conn.commit()

View File

@ -1,14 +1,16 @@
class Machine:
def __init__(self, name, image_name, start_time, ip, username, password):
def __init__(self, name, image_name, start_time, ipvpn, iplocal, username, password):
self.name = name
self.image_name = image_name
self.start_time = start_time
self.ip = ip
self.ipvpn = ipvpn
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: {self.ip}\nUsername: {self.username}\nPassword: {self.password}"
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):
@ -31,21 +33,3 @@ class MachineManager:
for machine in self.machines:
result += str(machine) + "\n\n"
return result
def to_html_table(self):
html = "<table>"
html += """<th>Nazwa maszyny</th>
<th>Obraz</th>
<th>Czas uruchomienia</th>
<th>IP uVPN</th>
<th></th>"""
for machine in self.machines:
html += f"""<tr>
<td>{ machine.name }</td>
<td>{ machine.image_name }</td>
<td>{ machine.start_time }</td>
<td>{ machine.ip }</td>
<td><button onclick="ssh('{ machine.ip, machine.username, machine.password }')">SSH</button></td>
</tr>"""
html += "</table>"
return html

View File

@ -67,6 +67,7 @@
<th>Obraz</th>
<th>Czas uruchomienia</th>
<th>IP uVPN</th>
<th>IP lokalne</th>
<th></th>
</tr>
</thead>
@ -76,7 +77,8 @@
<td>{{ machine.name }}</td>
<td>{{ machine.image_name }}</td>
<td>{{ machine.start_time }}</td>
<td>{{ machine.ip }}</td>
<td>{{ machine.ipvpn }}</td>
<td>{{ machine.iplocal }}</td>
<td><button onclick="ssh('{{ machine.ip }}', '{{ machine.username }}', '{{ machine.password }}')">SSH</button></td>
</tr>
{% endfor %}

View File

@ -42,7 +42,7 @@ def check_allocation_thread_function():
while True:
ids = db.get_image_allocation_all()
for x in ids:
ip = db.get_image_allocation_clientip_id(x[0])
ip = db.get_image_allocation_clientip_id_vpn(x[0])
ping_thread = PingThread(ip, x[0])
ping_thread.start()