Create clone servers button/action

Can't copy while running
This commit is contained in:
LukasDoesDev 2021-01-28 20:46:13 +02:00
parent 509c801436
commit ed0208ab0e
2 changed files with 71 additions and 0 deletions

View File

@ -1,6 +1,8 @@
import sys import sys
import json import json
import logging import logging
import os
import shutil
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.web.base_handler import BaseHandler from app.classes.web.base_handler import BaseHandler
@ -75,6 +77,64 @@ class ServerHandler(BaseHandler):
command = bleach.clean(self.get_argument("command", None)) command = bleach.clean(self.get_argument("command", None))
if server_id is not None: if server_id is not None:
if command == "clone_server":
def is_name_used(name):
for server in db_helper.get_all_defined_servers():
if server['server_name'] == name:
return True
return False
server_data = db_helper.get_server_data_by_id(server_id)
server_uuid = server_data.get('server_uuid')
new_server_name = server_data.get('server_name') + " (Copy)"
name_counter = 1
while is_name_used(new_server_name):
name_counter += 1
new_server_name = server_data.get('server_name') + " (Copy {})".format(name_counter)
console.debug('new_server_name: "{}"'.format(new_server_name))
new_server_uuid = helper.create_uuid()
while os.path.exists(os.path.join(helper.servers_dir, new_server_uuid)):
new_server_uuid = helper.create_uuid()
new_server_path = os.path.join(helper.servers_dir, new_server_uuid)
# copy the old server
shutil.copytree(server_data.get('path'), new_server_path)
# TODO get old server DB data to individual variables
stop_command = server_data.get('stop_command')
new_server_command = str(server_data.get('execution_command')).replace(server_uuid, new_server_uuid)
new_executable = server_data.get('executable')
new_server_log_file = str(server_data.get('log_path')).replace(server_uuid, new_server_uuid)
auto_start = server_data.get('auto_start')
auto_start_delay = server_data.get('auto_start_delay')
crash_detection = server_data.get('crash_detection')
server_port = server_data.get('server_port')
# TODO create the server on the DB side
Servers.insert({
Servers.server_name: new_server_name,
Servers.server_uuid: new_server_uuid,
Servers.path: new_server_path,
Servers.executable: new_executable,
Servers.execution_command: new_server_command,
Servers.auto_start: auto_start,
Servers.auto_start_delay: auto_start_delay,
Servers.crash_detection: crash_detection,
Servers.log_path: new_server_log_file,
Servers.server_port: server_port,
Servers.stop_command: stop_command
}).execute()
controller.init_all_servers()
console.debug('initted all servers')
return
db_helper.send_command(user_data['user_id'], server_id, self.get_remote_ip(), command) db_helper.send_command(user_data['user_id'], server_id, self.get_remote_ip(), command)
if page == "step1": if page == "step1":

View File

@ -131,6 +131,7 @@
<a class="restart_button" data-id="{{server['server_data']['server_id']}}"> <i class="fas fa-sync"></i></a> &nbsp; <a class="restart_button" data-id="{{server['server_data']['server_id']}}"> <i class="fas fa-sync"></i></a> &nbsp;
{% else %} {% else %}
<a data-id="{{server['server_data']['server_id']}}" class="play_button"><i class="fas fa-play"></i></a> &nbsp; <a data-id="{{server['server_data']['server_id']}}" class="play_button"><i class="fas fa-play"></i></a> &nbsp;
<a data-id="{{server['server_data']['server_id']}}" class="clone_button"> <i class="fas fa-clone"></i></a> &nbsp;
{% end %} {% end %}
</td> </td>
@ -268,6 +269,16 @@ $( document ).ready(function() {
}); });
}); });
$( ".clone_button" ).click(function() {
server_id = $(this).attr("data-id");
send_command(server_id, 'clone_server');
bootbox.alert({
backdrop: true,
title: "Sending your command",
message: '<div align="center"><i class="fas fa-spin fa-spinner"></i> &nbsp; Please be patient while we clone the server<br /> This screen will refresh in a moment </div>'
});
});
}); });
</script> </script>