Example #1
0
    def setup_ftp(self, server_id=1):
        ftp_settings = None
        mc_settings = None
        try:
            ftp_settings = Ftp_Srv.get_by_id(1)
            mc_settings = MC_settings.get_by_id(server_id)

        except Exception as e:
            logging.exception("Error Loading FTP. Traceback:")
            self.last_error = e
            return False

        pemfile = os.path.join(helper.crafty_root, "app", 'web', 'certs',
                               'crafty.pem')

        if not helper.check_file_exists(pemfile):
            helper.create_ftp_pem()

        if ftp_settings is not None and mc_settings is not None:

            self.user = ftp_settings.user
            self.password = ftp_settings.password
            self.port = ftp_settings.port
            self.root_dir = mc_settings.server_path

            logger.info("FTP server is now setup - Port: {}, Dir: {}".format(
                self.port, self.root_dir))
 def detect_bungee_waterfall(self):
     bungee_waterfall_file = os.path.join(self.server_path.replace('"', ''),
                                          'config.yml')
     if helper.check_file_exists(bungee_waterfall_file):
         return True
     else:
         return False
    def setup_server_run_command(self):
        # configure the server
        server_path = self.settings.server_path
        server_jar = self.settings.server_jar
        server_max_mem = self.settings.memory_max
        server_min_mem = self.settings.memory_min
        server_args = self.settings.additional_args
        server_pre_args = self.settings.pre_args
        java_path = self.settings.java_path

        # set up execute path if we have spaces, we put quotes around it for windows
        if " " in server_path:
            exec_path = '"{}"'.format(server_path)
        else:
            exec_path = server_path

        # Wrap Java path in quotes if it contains spaces
        if " " in java_path:
            java_exec = '"{}"'.format(java_path)
        else:
            java_exec = java_path

        server_exec_path = os.path.join(exec_path, server_jar)
        if int(server_min_mem) >= 0:
            self.server_command = '{} -Xms{}M -Xmx{}M {} -jar {} nogui {}'.format(
                java_exec, server_min_mem, server_max_mem, server_pre_args,
                server_exec_path, server_args)
        else:
            self.server_command = '{} -Xmx{}M {} -jar {} nogui {}'.format(
                java_exec, server_max_mem, server_pre_args, server_exec_path,
                server_args)

        self.server_path = server_path
        self.jar_exists = helper.check_file_exists(
            os.path.join(server_path, server_jar))

        # Check if custom Java path is specified and if it exists
        if java_path == 'java':
            self.java_path_exists = True
        else:
            self.java_path_exists = helper.check_file_exists(java_path)
    def search_server_properties(self, regex='*'):

        # whats the file we are looking for?
        server_prop_file = os.path.join(self.server_path.replace('"', ''),
                                        'server.properties')
        bungee_waterfall_file = os.path.join(self.server_path.replace('"', ''),
                                             'config.yml')

        # re of what we are looking for
        # ignoring case - just in case someone used all caps
        pattern = re.compile(regex, re.IGNORECASE)

        # make sure it exists
        if helper.check_file_exists(server_prop_file):
            with open(server_prop_file, 'rt') as f:
                for line in f:
                    # if we find something
                    if pattern.search(line) is not None:
                        match_line = line.rstrip('\n').split("=", 2)

                        # if we have at least 2 items in the list (i.e. there was an = char
                        if len(match_line) == 2:
                            return match_line[1]

            # if we got here, we couldn't find it
            logger.warning(
                "Unable to find string using regex \"%s\" in server.properties file",
                regex)
            return False

        elif helper.check_file_exists(bungee_waterfall_file):
            return "Bungee/Waterfall Detected"

        # if we got here, we can't find server.properties (bigger issues)
        logger.warning("Unable to find server.properties file")
        return False
    def revert_updated_server_jar(self, with_console=True):

        self.reload_settings()

        self.updating = True

        logger.info("Starting Jar Revert Process")

        if with_console:
            console.info("Starting Jar Revert Process")

        backup_dir = os.path.join(self.settings.server_path,
                                  'crafty_jar_backups')
        backup_jar_name = os.path.join(backup_dir, 'old_server.jar')
        current_jar = os.path.join(self.settings.server_path,
                                   self.settings.server_jar)
        was_running = False

        # verify we have a backup
        if not helper.check_file_exists(backup_jar_name):
            logger.critical("Can't find server.jar backup! - can't continue")
            console.critical("Can't find server.jar backup! - can't continue")
            self.updating = False
            return False

        if self.check_running():
            was_running = True
            logger.info("Server was running, stopping server for jar revert")

            if with_console:
                console.info(
                    "Server was running, stopping server for jar revert")

            self.stop_threaded_server()

        # make sure the backup directory exists
        helper.ensure_dir_exists(backup_dir)

        # remove the current_server.jar
        if helper.check_file_exists(backup_jar_name):
            logger.info("Removing current server jar %s", backup_jar_name)

            if with_console:
                console.info(
                    "Removing current server jar: {}".format(backup_jar_name))

            os.remove(current_jar)

        logger.info("Copying old jar back")

        if with_console:
            console.info("Copying old jar back")

        helper.copy_file(backup_jar_name, current_jar)

        if was_running:
            logger.info(
                "Server was running, starting server backup after update")

            if with_console:
                console.info(
                    "Server was running, starting server backup after update")

            self.run_threaded_server()

        self.updating = False
        console.info(
            "Server Jar Revert Completed - press enter to get the prompt back")
    def update_server_jar(self, with_console=True):

        self.reload_settings()

        self.updating = True

        logger.info("Starting Jar Update Process")

        if with_console:
            console.info("Starting Jar Update Process")

        backup_dir = os.path.join(self.settings.server_path,
                                  'crafty_jar_backups')
        backup_jar_name = os.path.join(backup_dir, 'old_server.jar')
        current_jar = os.path.join(self.settings.server_path,
                                   self.settings.server_jar)
        was_running = False

        if self.check_running():
            was_running = True
            logger.info("Server was running, stopping server for jar update")

            if with_console:
                console.info(
                    "Server was running, stopping server for jar update")

            self.stop_threaded_server()

        # make sure the backup directory exists
        helper.ensure_dir_exists(backup_dir)

        # remove the old_server.jar
        if helper.check_file_exists(backup_jar_name):
            logger.info("Removing old backup jar %s", backup_jar_name)

            if with_console:
                console.info(
                    "Removing old backup jar {}".format(backup_jar_name))

            os.remove(backup_jar_name)

        logger.info("Starting Server Jar Download")

        if with_console:
            console.info("Starting Server Jar Download")

        # backup the server jar file
        logger.info("Backing up Current Jar")
        helper.copy_file(current_jar, backup_jar_name)

        # download the new server jar file
        download_complete = helper.download_file(self.settings.jar_url,
                                                 current_jar)

        if download_complete:
            logger.info("Server Jar Download Complete")

            if with_console:
                console.info("Server Jar Download Complete")
        else:
            if with_console:
                console.info("Server Jar Had An Error")

        if was_running:
            logger.info(
                "Server was running, starting server backup after update")

            if with_console:
                console.info(
                    "Server was running, starting server backup after update")

            self.run_threaded_server()

        self.updating = False
        console.info(
            "Server Jar Update Completed - press enter to get the prompt back")