init(app): initial version

This commit is contained in:
Aleksandr Tcitlionok
2024-12-04 08:30:25 +00:00
parent 79c3766d06
commit 209bf98fb4
17 changed files with 380 additions and 0 deletions

79
app/database.py Normal file
View File

@@ -0,0 +1,79 @@
import sqlite3
import json
from contextlib import closing
DB_PATH = "resources.db"
# Basic schema for the database
def init_db():
with closing(sqlite3.connect(DB_PATH)) as conn:
with conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS metal_nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
location TEXT,
vendor TEXT,
cpu INTEGER,
memory TEXT,
storage TEXT,
time_on_duty INTEGER,
initial_cost REAL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS virtual_machines (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
location TEXT,
cpu INTEGER,
memory TEXT,
storage TEXT,
type TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS kubernetes_nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
cluster_name TEXT NOT NULL,
node_name TEXT NOT NULL,
cpu INTEGER,
memory TEXT,
storage TEXT,
type TEXT,
namespaces TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
def insert_metal_node(name, location, vendor, cpu, memory, storage, time_on_duty, initial_cost):
with closing(sqlite3.connect(DB_PATH)) as conn:
with conn:
conn.execute("""
INSERT INTO metal_nodes (name, location, vendor, cpu, memory, storage, time_on_duty, initial_cost)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (name, location, vendor, cpu, memory, storage, time_on_duty, initial_cost))
def insert_virtual_machine(name, location, cpu, memory, storage, vm_type):
with closing(sqlite3.connect(DB_PATH)) as conn:
with conn:
conn.execute("""
INSERT INTO virtual_machines (name, location, cpu, memory, storage, type)
VALUES (?, ?, ?, ?, ?, ?)
""", (name, location, cpu, memory, storage, vm_type))
def insert_kubernetes_node(cluster_name, node_name, cpu, memory, storage, node_type, namespaces):
with closing(sqlite3.connect(DB_PATH)) as conn:
with conn:
conn.execute("""
INSERT INTO kubernetes_nodes (cluster_name, node_name, cpu, memory, storage, type, namespaces)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (cluster_name, node_name, cpu, memory, storage, node_type, json.dumps(namespaces)))
def fetch_all(table):
with closing(sqlite3.connect(DB_PATH)) as conn:
with conn:
cursor = conn.execute(f"SELECT * FROM {table}")
return cursor.fetchall()