Add move servers dir functionality

This commit is contained in:
amcmanu3 2023-01-30 15:23:10 -05:00
parent aaefcb913b
commit 863ded782a
4 changed files with 112 additions and 2 deletions

View File

@ -1005,5 +1005,55 @@ class Controller:
HelperUsers.clear_support_status()
def set_master_server_dir(self, server_dir):
# This method should only be used on a first run basis if the server dir is ""
self.helper.servers_dir = server_dir
HelpersManagement.set_master_server_dir(server_dir)
def update_master_server_dir(self, server_dir, user_id):
if self.management.get_master_server_dir() == server_dir:
logger.info(
"Admin tried to change server dir to current server dir. Canceling..."
)
return
if self.helper.is_subdir(server_dir, self.management.get_master_server_dir()):
logger.info(
"Admin tried to change server dir to be inside a sub directory of the"
" current server dir. This will result in a copy loop."
)
self.helper.servers_dir = server_dir
if not self.helper.ensure_dir_exists(os.path.join(server_dir, "servers")):
self.helper.websocket_helper.broadcast_user(
user_id,
"send_start_error",
{
"error": "Crafty failed to move server dir. "
"It seems Crafty lacks permission to write to "
"the new directory."
},
)
return
HelpersManagement.set_master_server_dir(server_dir)
servers = self.servers.get_all_defined_servers()
for server in servers:
server_path = server.get("path")
new_server_path = os.path.join(
server_dir, "servers", server.get("server_uuid")
)
if os.path.isdir(server_path):
self.file_helper.move_dir(
server_path,
new_server_path,
)
server_obj = self.servers.get_server_obj(server.get("server_id"))
server_obj.path = new_server_path
failed = False
for s in self.servers.failed_servers:
if int(s["server_id"]) == int(server.get("server_id")):
failed = True
if not failed:
self.servers.update_server(server_obj)
else:
self.servers.update_unloaded_server(server_obj)
self.servers.init_all_servers()

View File

@ -575,6 +575,25 @@ class AjaxHandler(BaseHandler):
self.controller.server_jars.manual_refresh_cache()
return
elif page == "update_server_dir":
for server in self.controller.servers.get_all_servers_stats():
if server["stats"]["running"]:
self.helper.websocket_helper.broadcast_user(
exec_user["user_id"],
"send_start_error",
{
"error": "You must stop all servers before "
"starting a storage migration."
},
)
return
if not superuser:
self.redirect("/panel/error?error=Not a super user")
return
new_dir = urllib.parse.unquote(self.get_argument("server_dir"))
self.controller.update_master_server_dir(new_dir, exec_user["user_id"])
return
@tornado.web.authenticated
def delete(self, page):
api_key, _, exec_user = self.current_user

View File

@ -841,6 +841,9 @@ class PanelHandler(BaseHandler):
page_data["auth-servers"] = auth_servers
page_data["role-servers"] = auth_role_servers
page_data["user-roles"] = user_roles
page_data[
"servers_dir"
] = self.controller.management.get_master_server_dir()
page_data["users"] = self.controller.users.user_query(exec_user["user_id"])
page_data["roles"] = self.controller.users.user_role_query(

View File

@ -50,7 +50,9 @@
<h4 class="card-title"><i class="fas fa-users"></i> {{ translate('panelConfig', 'users', data['lang'])
}}</h4>
{% if data['user_data']['hints'] %}
<span class="too_small" title="{{ translate('dashboard', 'cannotSee', data['lang']) }}" , data-content="{{ translate('dashboard', 'cannotSeeOnMobile2', data['lang']) }}" , data-placement="top"></span>
<span class="too_small" title="{{ translate('dashboard', 'cannotSee', data['lang']) }}" ,
data-content="{{ translate('dashboard', 'cannotSeeOnMobile2', data['lang']) }}" ,
data-placement="top"></span>
{% end %}
<!-- TODO: Translate the following -->
<div><a class="nav-link" href="/panel/add_user"><i class="fas fa-plus-circle"></i> &nbsp; {{
@ -148,7 +150,9 @@
<h4 class="card-title"><i class="fas fa-user-tag"></i> {{ translate('panelConfig', 'roles',
data['lang']) }}</h4>
{% if data['user_data']['hints'] %}
<span class="too_small2" title="{{ translate('dashboard', 'cannotSee', data['lang']) }}" , data-content="{{ translate('dashboard', 'cannotSeeOnMobile2', data['lang']) }}" , data-placement="top"></span>
<span class="too_small2" title="{{ translate('dashboard', 'cannotSee', data['lang']) }}" ,
data-content="{{ translate('dashboard', 'cannotSeeOnMobile2', data['lang']) }}" ,
data-placement="top"></span>
{% end %}
<div><a class="nav-link" href="/panel/add_role"><i class="fas fa-plus-circle"></i> &nbsp; {{
translate('panelConfig', 'newRole', data['lang']) }}</a></div>
@ -234,6 +238,16 @@
<h4 class="card-title"><i class="fas fa-user-tag"></i> {{ translate('panelConfig', 'adminControls',
data['lang']) }}</h4>
</div>
<br>
<form id="server-path">
<label for="global_server_path">Global Server Path <small class="text-muted ml-1"> - Where Crafty
stores all of your servers.</small>
</label>
<input type="text" id="global_server_path" class="form-control" name="global_server_path"
placeholder="/var/opt/servers" value="{{data['servers_dir']}}">
<button class="btn btn-success" type="submit">Submit</button>&nbsp;<span id="submit-status"></span>
</form>
</div>
</div>
</div>
@ -258,6 +272,30 @@
{% block js %}
<script>
$("#server-path").submit(function (e) {
var token = getCookie("_xsrf")
e.preventDefault();
$("#submit-status").html('<i class="fa fa-spinner fa-spin"></i>');
let path = $("#global_server_path").val();
let encoded = encodeURIComponent(path);
console.log(path)
$.ajax({
type: "POST",
headers: { 'X-XSRFToken': token },
dataType: "text",
url: '/ajax/update_server_dir',
data: {
"server_dir": encoded,
},
success: function (data) {
$("#submit-status").html('<i class="fa fa-check"></i>');
},
});
});
$(document).ready(function () {
$('[data-toggle="popover"]').popover();
if ($(window).width() < 1000) {