| 147 | |
| 148 | |
| 149 | class ServerNodeDB: |
| 150 | _DB_FILE = public.get_panel_path() + "/data/db/node.db" |
| 151 | _DB_INIT_FILE = os.path.dirname(__file__) + "/node.sql" |
| 152 | |
| 153 | def __init__(self): |
| 154 | sql = db.Sql() |
| 155 | sql._Sql__DB_FILE = self._DB_FILE |
| 156 | self.db = sql |
| 157 | |
| 158 | def init_db(self): |
| 159 | sql_data = public.readFile(self._DB_INIT_FILE) |
| 160 | import sqlite3 |
| 161 | conn = sqlite3.connect(self._DB_FILE) |
| 162 | cur = conn.cursor() |
| 163 | cur.executescript(sql_data) |
| 164 | cur.execute("PRAGMA table_info(node)") |
| 165 | existing_cols = [row[1] for row in cur.fetchall()] |
| 166 | if "ssh_test" in existing_cols: |
| 167 | pass |
| 168 | # print("字段 ssh_test 已存在") |
| 169 | else: |
| 170 | cur.execute("ALTER TABLE node ADD COLUMN ssh_test INTEGER DEFAULT (0)") |
| 171 | conn.commit() |
| 172 | conn.close() |
| 173 | |
| 174 | def close(self): |
| 175 | self.db.close() |
| 176 | |
| 177 | def __enter__(self): |
| 178 | return self |
| 179 | |
| 180 | def __exit__(self, exc_type, exc_value, exc_trackback): |
| 181 | self.close() |
| 182 | |
| 183 | def __del__(self): |
| 184 | self.close() |
| 185 | |
| 186 | def is_local_node(self, node_id: int): |
| 187 | return self.db.table('node').where("id=? AND app_key = 'local' AND api_key = 'local'", (node_id,)).count() > 0 |
| 188 | |
| 189 | def get_local_node(self): |
| 190 | data = self.db.table('node').where("app_key = 'local' AND api_key = 'local'", ()).find() |
| 191 | if isinstance(data, dict): |
| 192 | return data |
| 193 | return { |
| 194 | "id": 0, |
| 195 | "address": "", |
| 196 | "category_id": 0, |
| 197 | "remarks": "本机节点", |
| 198 | "api_key": "local", |
| 199 | "create_time": time.strftime('%Y-%m-%d %H:%M:%S'), |
| 200 | "server_ip": "127.0.0.1", |
| 201 | "status": 0, |
| 202 | "error": 0, |
| 203 | "error_num": 0, |
| 204 | "app_key": "local", |
| 205 | "ssh_conf": "{}", |
| 206 | "lpver": "", |
no test coverage detected