2020-08-12 00:36:09 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
import logging.config
|
|
|
|
|
|
|
|
""" Our custom classes / pip packages """
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.shared.helpers import helper
|
2020-08-13 01:33:36 +00:00
|
|
|
from app.classes.shared.models import installer
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2021-03-06 20:48:02 +00:00
|
|
|
from app.classes.shared.tasks import TasksManager
|
2021-03-22 04:02:18 +00:00
|
|
|
from app.classes.shared.controller import Controller
|
2021-03-22 04:32:48 +00:00
|
|
|
|
2020-08-27 22:53:04 +00:00
|
|
|
from app.classes.shared.cmd import MainPrompt
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
def do_intro():
|
2020-08-17 02:47:53 +00:00
|
|
|
logger.info("***** Crafty Controller Started *****")
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
version = helper.get_version_string()
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
intro = """
|
|
|
|
{lines}
|
|
|
|
#\t\tWelcome to Crafty Controller - v.{version}\t\t #
|
|
|
|
{lines}
|
|
|
|
# \tServer Manager / Web Portal for your Minecraft server \t #
|
|
|
|
# \t\tHomepage: www.craftycontrol.com\t\t\t #
|
|
|
|
{lines}
|
|
|
|
""".format(lines="/" * 75, version=version)
|
|
|
|
|
|
|
|
console.magenta(intro)
|
|
|
|
|
|
|
|
|
2021-03-09 03:01:42 +00:00
|
|
|
def setup_logging(debug=True):
|
2020-08-12 00:36:09 +00:00
|
|
|
logging_config_file = os.path.join(os.path.curdir,
|
|
|
|
'app',
|
|
|
|
'config',
|
|
|
|
'logging.json'
|
|
|
|
)
|
|
|
|
|
|
|
|
if os.path.exists(logging_config_file):
|
|
|
|
# open our logging config file
|
|
|
|
with open(logging_config_file, 'rt') as f:
|
|
|
|
logging_config = json.load(f)
|
|
|
|
if debug:
|
|
|
|
logging_config['loggers']['']['level'] = 'DEBUG'
|
2021-01-19 13:56:00 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
logging.config.dictConfig(logging_config)
|
2021-01-19 13:56:00 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
else:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logging.warning("Unable to read logging config from {}".format(logging_config_file))
|
|
|
|
console.critical("Unable to read logging config from {}".format(logging_config_file))
|
|
|
|
|
|
|
|
|
|
|
|
""" Our Main Starter """
|
|
|
|
if __name__ == '__main__':
|
2020-08-31 17:46:25 +00:00
|
|
|
parser = argparse.ArgumentParser("Crafty Controller - A Server Management System")
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
parser.add_argument('-i', '--ignore',
|
|
|
|
action='store_true',
|
2020-08-19 01:04:43 +00:00
|
|
|
help="Ignore session.lock files"
|
2020-08-12 00:36:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument('-v', '--verbose',
|
|
|
|
action='store_true',
|
|
|
|
help="Sets logging level to debug."
|
|
|
|
)
|
|
|
|
|
2021-03-06 20:48:02 +00:00
|
|
|
parser.add_argument('-d', '--daemon',
|
|
|
|
action='store_true',
|
|
|
|
help="Runs Crafty in daemon mode (no prompt)"
|
|
|
|
)
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
helper.ensure_logging_setup()
|
|
|
|
|
|
|
|
setup_logging(debug=args.verbose)
|
|
|
|
|
|
|
|
# setting up the logger object
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-01-19 13:56:00 +00:00
|
|
|
print("Logging set to: {} ".format(logger.level))
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
# print our pretty start message
|
|
|
|
do_intro()
|
|
|
|
|
2020-08-31 17:46:25 +00:00
|
|
|
# our session file, helps prevent multiple controller agents on the same machine.
|
2020-08-12 00:36:09 +00:00
|
|
|
helper.create_session_file(ignore=args.ignore)
|
|
|
|
|
2020-08-31 17:46:25 +00:00
|
|
|
# do our installer stuff
|
2020-09-22 19:00:05 +00:00
|
|
|
fresh_install = installer.is_fresh_install()
|
|
|
|
|
|
|
|
if fresh_install:
|
2021-03-22 04:02:18 +00:00
|
|
|
console.debug("Fresh install detected")
|
2020-08-31 17:46:25 +00:00
|
|
|
installer.create_tables()
|
|
|
|
installer.default_settings()
|
2021-03-22 04:02:18 +00:00
|
|
|
else:
|
|
|
|
console.debug("Existing install detected")
|
|
|
|
installer.check_schema_version()
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
# now the tables are created, we can load the tasks_manger and server controller
|
|
|
|
controller = Controller()
|
|
|
|
tasks_manager = TasksManager(controller)
|
2020-08-12 00:36:09 +00:00
|
|
|
tasks_manager.start_webserver()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
# slowing down reporting just for a 1/2 second so messages look cleaner
|
|
|
|
time.sleep(.5)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2020-08-24 23:16:33 +00:00
|
|
|
# init servers
|
|
|
|
logger.info("Initializing all servers defined")
|
|
|
|
console.info("Initializing all servers defined")
|
|
|
|
controller.init_all_servers()
|
|
|
|
servers = controller.list_defined_servers()
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
# start stats logging
|
|
|
|
tasks_manager.start_stats_recording()
|
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
# once the controller is up and stats are logging, we can kick off the scheduler officially
|
|
|
|
tasks_manager.start_scheduler()
|
|
|
|
|
2020-08-23 22:43:28 +00:00
|
|
|
# refresh our cache and schedule for every 12 hoursour cache refresh for serverjars.com
|
|
|
|
tasks_manager.serverjar_cache_refresher()
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
# this should always be last
|
|
|
|
tasks_manager.start_main_kill_switch_watcher()
|
|
|
|
|
2021-03-06 20:48:02 +00:00
|
|
|
Crafty = MainPrompt(tasks_manager)
|
|
|
|
if not args.daemon:
|
|
|
|
Crafty.cmdloop()
|
|
|
|
else:
|
|
|
|
print("Crafty started in daemon mode, no shell will be printed")
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if tasks_manager.get_main_thread_run_status():
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logger.info("Recieved SIGTERM, stopping Crafty")
|
|
|
|
break
|
|
|
|
|
|
|
|
logger.info("Stopping all server daemons / threads")
|
|
|
|
console.info("Stopping all server daemons / threads - This may take a few seconds")
|
|
|
|
Crafty._clean_shutdown()
|
|
|
|
while True:
|
|
|
|
if tasks_manager.get_main_thread_run_status():
|
|
|
|
sys.exit(0)
|
|
|
|
time.sleep(1)
|