feat: add unified decommission and database provisioning modules
This commit is contained in:
parent
171b2a63dd
commit
b0f97f80df
4 changed files with 114 additions and 2 deletions
42
infra_cli/database.py
Normal file
42
infra_cli/database.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from .ssh import SSHClient
|
||||
|
||||
class DatabaseManager:
|
||||
def __init__(self, config):
|
||||
# Database server details
|
||||
self.host = config.get('database.host', '10.32.70.54')
|
||||
self.user = config.get('database.user', 'root')
|
||||
self.ssh_key = config.get('proxmox.ssh_key_path')
|
||||
self.client = SSHClient(self.host, self.user, self.ssh_key)
|
||||
|
||||
def exec_sql(self, sql):
|
||||
# Runs SQL as postgres user via SSH
|
||||
res = self.client.run(f"su - postgres -c \"psql -c \\"{sql}\"\"")
|
||||
if res.returncode != 0:
|
||||
raise RuntimeError(f"PostgreSQL command failed: {res.stderr}")
|
||||
return res.stdout
|
||||
|
||||
def create_database(self, db_name, owner=None):
|
||||
sql = f"CREATE DATABASE {db_name}"
|
||||
if owner:
|
||||
sql += f" OWNER {owner}"
|
||||
return self.exec_sql(sql)
|
||||
|
||||
def create_user(self, username, password):
|
||||
sql = f"CREATE USER {username} WITH PASSWORD '{password}'"
|
||||
return self.exec_sql(sql)
|
||||
|
||||
def grant_privileges(self, db_name, username):
|
||||
sql = f"GRANT ALL PRIVILEGES ON DATABASE {db_name} TO {username}"
|
||||
return self.exec_sql(sql)
|
||||
|
||||
def list_databases(self):
|
||||
return self.exec_sql("\l")
|
||||
|
||||
def list_users(self):
|
||||
return self.exec_sql("\du")
|
||||
|
||||
def drop_database(self, db_name):
|
||||
return self.exec_sql(f"DROP DATABASE IF EXISTS {db_name}")
|
||||
|
||||
def drop_user(self, username):
|
||||
return self.exec_sql(f"DROP USER IF EXISTS {username}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue