2020-08-27 22:53:04 +00:00
|
|
|
import sys
|
|
|
|
import cmd
|
|
|
|
import time
|
2021-11-20 00:14:32 +00:00
|
|
|
import threading
|
2020-08-27 22:53:04 +00:00
|
|
|
import logging
|
2022-05-16 19:39:10 +00:00
|
|
|
import getpass
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.shared.import3 import Import3
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-08 04:40:44 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class MainPrompt(cmd.Cmd):
|
2022-05-16 19:39:10 +00:00
|
|
|
def __init__(self, helper, tasks_manager, migration_manager, main_controller):
|
2021-03-22 04:02:18 +00:00
|
|
|
super().__init__()
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper = helper
|
2021-03-22 04:02:18 +00:00
|
|
|
self.tasks_manager = tasks_manager
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager = migration_manager
|
2022-05-16 19:39:10 +00:00
|
|
|
self.controller = main_controller
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
# overrides the default Prompt
|
|
|
|
self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > "
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-05-20 23:04:03 +00:00
|
|
|
def emptyline(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
pass
|
|
|
|
|
2022-04-02 01:49:48 +00:00
|
|
|
def do_exit(self, _line):
|
2021-12-09 23:35:00 +00:00
|
|
|
self.tasks_manager._main_graceful_exit()
|
2021-04-17 21:24:54 +00:00
|
|
|
self.universal_exit()
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
def do_migrations(self, line):
|
2022-03-23 02:50:12 +00:00
|
|
|
if line == "up":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.up()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "down":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.down()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "done":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.done)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "todo":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.todo)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "diff":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.diff)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "info":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Done: {self.migration_manager.done}")
|
|
|
|
Console.info(f"FS: {self.migration_manager.todo}")
|
|
|
|
Console.info(f"Todo: {self.migration_manager.diff}")
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line.startswith("add "):
|
|
|
|
migration_name = line[len("add ") :]
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.create(migration_name, False)
|
|
|
|
else:
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Unknown migration command")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-05-16 19:39:10 +00:00
|
|
|
def do_set_passwd(self, line):
|
|
|
|
|
|
|
|
try:
|
|
|
|
username = str(line).lower()
|
2022-05-18 21:37:07 +00:00
|
|
|
# If no user is found it returns None
|
2022-05-16 19:39:10 +00:00
|
|
|
user_id = self.controller.users.get_id_by_name(username)
|
2022-05-18 21:37:07 +00:00
|
|
|
if not username:
|
|
|
|
Console.error("You must enter a username. Ex: `set_passwd admin'")
|
|
|
|
return False
|
|
|
|
if not user_id:
|
|
|
|
Console.error(f"No user found by the name of {username}")
|
|
|
|
return False
|
2022-05-17 20:01:37 +00:00
|
|
|
except:
|
2022-05-16 19:39:10 +00:00
|
|
|
Console.error(f"User: {line} Not Found")
|
|
|
|
return False
|
|
|
|
new_pass = getpass.getpass(prompt=f"NEW password for: {username} > ")
|
|
|
|
new_pass_conf = getpass.getpass(prompt="Re-enter your password: > ")
|
|
|
|
|
|
|
|
if new_pass != new_pass_conf:
|
|
|
|
Console.error("Passwords do not match. Please try again.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
if len(new_pass) > 512:
|
2022-05-18 21:22:39 +00:00
|
|
|
Console.warning("Passwords must be greater than 6char long and under 512")
|
2022-05-16 19:39:10 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if len(new_pass) < 6:
|
2022-05-18 21:22:39 +00:00
|
|
|
Console.warning("Passwords must be greater than 6char long and under 512")
|
2022-05-16 19:39:10 +00:00
|
|
|
return False
|
|
|
|
self.controller.users.update_user(user_id, {"password": new_pass})
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def do_threads(_line):
|
2021-11-20 00:14:32 +00:00
|
|
|
for thread in threading.enumerate():
|
2022-01-15 00:23:50 +00:00
|
|
|
if sys.version_info >= (3, 8):
|
2022-03-23 02:50:12 +00:00
|
|
|
print(
|
2022-05-20 22:57:40 +00:00
|
|
|
f"Name: {thread.name}\tIdentifier: "
|
|
|
|
f"{thread.ident}\tTID/PID: {thread.native_id}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-05-20 22:57:40 +00:00
|
|
|
print(f"Name: {thread.name}\tIdentifier: {thread.ident}")
|
2021-11-20 00:14:32 +00:00
|
|
|
|
2022-05-20 22:53:17 +00:00
|
|
|
def print_prompt(self):
|
|
|
|
self.stdout.write(self.prompt)
|
|
|
|
self.stdout.flush()
|
|
|
|
|
2022-03-19 03:59:10 +00:00
|
|
|
def do_import3(self, _line):
|
2022-04-14 02:10:25 +00:00
|
|
|
Import3.start_import()
|
2022-03-19 02:55:39 +00:00
|
|
|
|
2021-04-17 21:24:54 +00:00
|
|
|
def universal_exit(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
logger.info("Stopping all server daemons / threads")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 02:50:12 +00:00
|
|
|
"Stopping all server daemons / threads - This may take a few seconds"
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.websocket_helper.disconnect_all()
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Waiting for main thread to stop")
|
2020-08-27 22:53:04 +00:00
|
|
|
while True:
|
2021-03-06 20:48:02 +00:00
|
|
|
if self.tasks_manager.get_main_thread_run_status():
|
2020-08-27 22:53:04 +00:00
|
|
|
sys.exit(0)
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_exit(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Stops the server if running, Exits the program")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_migrations(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Only for advanced users. Use with caution")
|
2022-03-20 17:14:51 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_import3(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Import users and servers from Crafty 3")
|