|
| 1 | +import select |
| 2 | +import json |
| 3 | +import logging |
| 4 | + |
| 5 | +# Set up logging |
| 6 | +logging.basicConfig( |
| 7 | + level=logging.DEBUG, |
| 8 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 9 | + datefmt="%Y-%m-%d %H:%M:%S", |
| 10 | +) |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | +logger.setLevel(logging.DEBUG) |
| 13 | + |
| 14 | +try: |
| 15 | + import psycopg2 |
| 16 | +except ImportError as e: |
| 17 | + logger.error( |
| 18 | + "psycopg2 is not installed in the development environment. " |
| 19 | + "Please install it using `pip install psycopg2`" |
| 20 | + ) |
| 21 | + raise e |
| 22 | + |
| 23 | + |
| 24 | +class PostgresqlDatabase: |
| 25 | + """Class to interact with a PostgreSQL database. |
| 26 | + This class provides methods to connect to the database, insert data, |
| 27 | + retrieve data, and listen for notifications. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + dbname: str, |
| 33 | + user: str, |
| 34 | + password: str, |
| 35 | + host: str, |
| 36 | + port: int, |
| 37 | + table_name: str, |
| 38 | + ): |
| 39 | + """Initialize the database connection. |
| 40 | +
|
| 41 | + Args: |
| 42 | + dbname (str): The name of the database. |
| 43 | + user (str): The username to connect to the database. |
| 44 | + password (str): The password for the user. |
| 45 | + host (str): The host where the database is located. |
| 46 | + port (int): The port number for the database connection. |
| 47 | + table_name (str): The name of the table to interact with. |
| 48 | + """ |
| 49 | + self.dbname = dbname |
| 50 | + self.user = user |
| 51 | + self.password = password |
| 52 | + self.host = host |
| 53 | + self.port = port |
| 54 | + self.table_name = table_name |
| 55 | + |
| 56 | + # Connect to the PostgreSQL database |
| 57 | + self.conn = psycopg2.connect( |
| 58 | + dbname=dbname, |
| 59 | + user=user, |
| 60 | + password=password, |
| 61 | + host=host, |
| 62 | + port=port, |
| 63 | + ) |
| 64 | + |
| 65 | + # Set the isolation level to autocommit so that each SQL command is immediately executed |
| 66 | + self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) |
| 67 | + self.cursor = self.conn.cursor() |
| 68 | + |
| 69 | + def get_column_names(self, table_name=None): |
| 70 | + """Get the column names of a table. |
| 71 | +
|
| 72 | + Args: |
| 73 | + table_name (str): The name of the table. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + list: A list of column names. |
| 77 | + """ |
| 78 | + if table_name is None: |
| 79 | + table_name = self.table_name |
| 80 | + |
| 81 | + # Query to get the column names from the information schema |
| 82 | + self.cursor.execute( |
| 83 | + f"SELECT column_name FROM information_schema.columns WHERE table_name = '{table_name}'" |
| 84 | + ) |
| 85 | + return [row[0] for row in self.cursor.fetchall()] |
| 86 | + |
| 87 | + def insert_vm(self, hostname): |
| 88 | + """Insert a new row into the table. |
| 89 | +
|
| 90 | + Args: |
| 91 | + hostname (str): The hostname of the VM. |
| 92 | + """ |
| 93 | + column_names = self.get_column_names() |
| 94 | + |
| 95 | + values = [] |
| 96 | + |
| 97 | + for col in column_names: |
| 98 | + # Find the column that corresponds to the hostname and set its value |
| 99 | + if col == "hostname": |
| 100 | + values.append(hostname) |
| 101 | + elif col == "inuse": |
| 102 | + values.append(False) |
| 103 | + else: |
| 104 | + values.append(None) |
| 105 | + |
| 106 | + # Construct the SQL query |
| 107 | + columns = ", ".join(column_names) |
| 108 | + placeholders = ", ".join(["%s" for _ in column_names]) |
| 109 | + |
| 110 | + sql = f"INSERT INTO {self.table_name} ({columns}) VALUES ({placeholders});" |
| 111 | + self.cursor.execute(sql, values) |
| 112 | + self.conn.commit() |
| 113 | + logger.debug(f"Inserted data: {values}") |
| 114 | + |
| 115 | + def listen_for_notifications(self, channel, target_hostname): |
| 116 | + """Listen for notifications on a specific channel. |
| 117 | +
|
| 118 | + Args: |
| 119 | + channel (str): The name of the notification channel. |
| 120 | + target_hostname (str): The hostname of the VM to connect to. |
| 121 | + """ |
| 122 | + self.cursor.execute(f"LISTEN {channel};") |
| 123 | + logger.debug(f"Listening for notifications on '{channel}'...") |
| 124 | + |
| 125 | + # Infinite loop to wait for notifications |
| 126 | + try: |
| 127 | + while True: |
| 128 | + # Wait for notifications |
| 129 | + if select.select([self.conn], [], [], 10) == ([], [], []): |
| 130 | + continue |
| 131 | + else: |
| 132 | + self.conn.poll() # Process any pending notifications |
| 133 | + while self.conn.notifies: |
| 134 | + notify = self.conn.notifies.pop(0) |
| 135 | + logger.debug( |
| 136 | + f"Received notification: {notify.payload} from channel {notify.channel}" |
| 137 | + ) |
| 138 | + # Parse the JSON payload |
| 139 | + try: |
| 140 | + payload_data = json.loads(notify.payload) |
| 141 | + logger.debug(f"Payload data: {payload_data}") |
| 142 | + hostname = payload_data.get("HostName") |
| 143 | + pin = payload_data.get("Pin") |
| 144 | + command = payload_data.get("CrdCommand") |
| 145 | + |
| 146 | + if hostname is None or pin is None or command is None: |
| 147 | + logger.error( |
| 148 | + "Invalid payload data. Missing required fields." |
| 149 | + ) |
| 150 | + continue |
| 151 | + |
| 152 | + # Check if the hostname matches the current hostname |
| 153 | + if hostname != target_hostname: |
| 154 | + logger.debug( |
| 155 | + f"Hostname '{hostname}' does not match the current hostname '{target_hostname}'." |
| 156 | + ) |
| 157 | + continue |
| 158 | + |
| 159 | + logger.debug( |
| 160 | + "Chrome Remote Desktop connected successfully. Exiting listener loop." |
| 161 | + ) |
| 162 | + return { |
| 163 | + "status": "success", |
| 164 | + "pin": pin, |
| 165 | + "command": command, |
| 166 | + } |
| 167 | + |
| 168 | + except json.JSONDecodeError as e: |
| 169 | + logger.error(f"Error decoding JSON payload: {e}") |
| 170 | + continue |
| 171 | + except Exception as e: |
| 172 | + logger.error(f"Error processing notification: {e}") |
| 173 | + continue |
| 174 | + except KeyboardInterrupt: |
| 175 | + logger.debug("Exiting...") |
| 176 | + |
| 177 | + def get_crd_command(self, hostname): |
| 178 | + """Get the command assigned to a VM. |
| 179 | +
|
| 180 | + Args: |
| 181 | + hostname (str): The hostname of the VM. |
| 182 | +
|
| 183 | + Returns: |
| 184 | + str: The command assigned to the VM. |
| 185 | + """ |
| 186 | + if not self.vm_exists(hostname): |
| 187 | + logger.error(f"VM with hostname '{hostname}' does not exist.") |
| 188 | + return None |
| 189 | + |
| 190 | + query = f"SELECT crdcommand FROM {self.table_name} WHERE hostname = %s" |
| 191 | + self.cursor.execute(query, (hostname,)) |
| 192 | + return self.cursor.fetchone()[0] |
| 193 | + |
| 194 | + def get_unassigned_vms(self): |
| 195 | + """Get the VMs that are not assigned to any command. |
| 196 | +
|
| 197 | + Returns: |
| 198 | + list: A list of VMs that are not assigned to any command. |
| 199 | + """ |
| 200 | + query = f"SELECT hostname FROM {self.table_name} WHERE crdcommand IS NULL" |
| 201 | + try: |
| 202 | + self.cursor.execute(query) |
| 203 | + return [row[0] for row in self.cursor.fetchall()] |
| 204 | + except Exception as e: |
| 205 | + logger.error(f"Error retrieving unassigned VMs: {e}") |
| 206 | + return [] |
| 207 | + |
| 208 | + def vm_exists(self, hostname): |
| 209 | + """Check if a VM with the given hostname exists in the table. |
| 210 | +
|
| 211 | + Args: |
| 212 | + hostname (str): The hostname of the VM. |
| 213 | +
|
| 214 | + Returns: |
| 215 | + bool: True if the VM exists, False otherwise. |
| 216 | + """ |
| 217 | + query = f"SELECT EXISTS (SELECT 1 FROM {self.table_name} WHERE hostname = %s)" |
| 218 | + self.cursor.execute(query, (hostname,)) |
| 219 | + return self.cursor.fetchone()[0] |
| 220 | + |
| 221 | + def get_assigned_vms(self): |
| 222 | + """Get the VMs that are assigned to a command. |
| 223 | +
|
| 224 | + Returns: |
| 225 | + list: A list of VMs that are assigned to a command. |
| 226 | + """ |
| 227 | + query = f"SELECT hostname FROM {self.table_name} WHERE crdcommand IS NOT NULL" |
| 228 | + try: |
| 229 | + self.cursor.execute(query) |
| 230 | + return [row[0] for row in self.cursor.fetchall()] |
| 231 | + except Exception as e: |
| 232 | + logger.error(f"Error retrieving assigned VMs: {e}") |
| 233 | + |
| 234 | + @classmethod |
| 235 | + def load_database(cls, dbname, user, password, host, port, table_name): |
| 236 | + """Loads an existing database from PostgreSQL. |
| 237 | +
|
| 238 | + Args: |
| 239 | + dbname (str): The name of the database. |
| 240 | + user (str): The username to connect to the database. |
| 241 | + password (str): The password for the user. |
| 242 | + host (str): The host where the database is located. |
| 243 | + port (int): The port number for the database connection. |
| 244 | + table_name (str): The name of the table to interact with. |
| 245 | +
|
| 246 | + Returns: |
| 247 | + PostgresqlDtabase: An instance of the PostgresqlDtabase class. |
| 248 | + """ |
| 249 | + return cls(dbname, user, password, host, port, table_name) |
| 250 | + |
| 251 | + def __del__(self): |
| 252 | + """Close the database connection when the object is deleted.""" |
| 253 | + self.cursor.close() |
| 254 | + self.conn.close() |
| 255 | + logger.debug("Database connection closed.") |
0 commit comments