Merge branch '3.0-import' into 'dev'

Add ability to import config from 3.x

See merge request crafty-controller/crafty-commander!216
This commit is contained in:
Iain Powrie 2022-03-22 11:27:06 +00:00
commit f56b86308c
4 changed files with 84 additions and 2 deletions

View File

@ -96,9 +96,13 @@ class Users_Controller:
users_helper.update_user(user_id, up_data) users_helper.update_user(user_id, up_data)
@staticmethod @staticmethod
def add_user(username, password=None, email="default@example.com", enabled: bool = True, superuser: bool = False): def add_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False):
return users_helper.add_user(username, password=password, email=email, enabled=enabled, superuser=superuser) return users_helper.add_user(username, password=password, email=email, enabled=enabled, superuser=superuser)
@staticmethod
def add_rawpass_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False):
return users_helper.add_rawpass_user(username, password=password, email=email, enabled=enabled, superuser=superuser)
@staticmethod @staticmethod
def remove_user(user_id): def remove_user(user_id):
return users_helper.remove_user(user_id) return users_helper.remove_user(user_id)

View File

@ -147,7 +147,7 @@ class helper_users:
return user return user
@staticmethod @staticmethod
def add_user(username: str, password: Optional[str] = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str: def add_user(username: str, password: str = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str:
if password is not None: if password is not None:
pw_enc = helper.encode_pass(password) pw_enc = helper.encode_pass(password)
else: else:
@ -162,6 +162,18 @@ class helper_users:
}).execute() }).execute()
return user_id return user_id
@staticmethod
def add_rawpass_user(username: str, password: str = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str:
user_id = Users.insert({
Users.username: username.lower(),
Users.password: password,
Users.email: email,
Users.enabled: enabled,
Users.superuser: superuser,
Users.created: helper.get_time_as_string()
}).execute()
return user_id
@staticmethod @staticmethod
def update_user(user_id, up_data=None): def update_user(user_id, up_data=None):
if up_data is None: if up_data is None:

View File

@ -6,6 +6,7 @@ import logging
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
from app.classes.shared.import3 import import3
from app.classes.web.websocket_helper import websocket_helper from app.classes.web.websocket_helper import websocket_helper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -58,6 +59,9 @@ class MainPrompt(cmd.Cmd):
else: else:
print(f'Name: {thread.name} Identifier: {thread.ident}') print(f'Name: {thread.name} Identifier: {thread.ident}')
def do_import3(self, _line):
import3.start_import()
def universal_exit(self): def universal_exit(self):
logger.info("Stopping all server daemons / threads") logger.info("Stopping all server daemons / threads")
console.info("Stopping all server daemons / threads - This may take a few seconds") console.info("Stopping all server daemons / threads - This may take a few seconds")
@ -75,3 +79,7 @@ class MainPrompt(cmd.Cmd):
@staticmethod @staticmethod
def help_migrations(): def help_migrations():
console.help("Only for advanced users. Use with caution") console.help("Only for advanced users. Use with caution")
@staticmethod
def help_import3():
console.help("Import users and servers from Crafty 3")

View File

@ -0,0 +1,58 @@
import json
import os
import logging
from app.classes.controllers.users_controller import users_helper
from app.classes.shared.main_controller import Controller
from app.classes.shared.console import console
logger = logging.getLogger(__name__)
class import3:
def __init__(self):
self.controller = Controller()
def start_import(self):
folder = os.path.normpath(input("Please input the path to the migrations folder in your installation of Crafty 3: "))
if not os.path.exists(folder):
console.info("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?")
console.info("Please run the import3 command again and enter a valid path.")
else:
with open (os.path.join(folder, "users.json"), encoding="utf-8") as f:
user_json = json.loads(f.read())
with open (os.path.join(folder, "mc_settings.json"), encoding="utf-8") as f:
servers_json = json.loads(f.read())
self.import_users(user_json)
self.import_servers(servers_json, self.controller)
@staticmethod
def import_users(json_data):
# If there is only one user to import json needs to call the data differently
if isinstance(json_data, list):
for user in json_data:
users_helper.add_rawpass_user(user['username'], user['password'])
console.info(f"Imported user {user['username']} from Crafty 3")
logger.info(f"Imported user {user['username']} from Crafty 3")
else:
console.info("There is only one user detected. Cannot create duplicate Admin account.")
logger.info("There is only one user detected. Cannot create duplicate Admin account.")
@staticmethod
def import_servers(json_data, controller):
# If there is only one server to import json needs to call the data differently
if isinstance(json_data, list):
for server in json_data:
new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'],
server_jar=server['server_jar'], min_mem=(int(server['memory_min'])/1000),
max_mem=(int(server['memory_max'])/1000), port=server['server_port'])
console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}")
logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}")
else:
new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'],
server_jar=json_data['server_jar'], min_mem=(int(json_data['memory_min'])/1000),
max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port'])
console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}")
logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}")
import3 = import3()