Use API v2 for jar creation and jar import

This commit is contained in:
amcmanu3 2023-08-17 15:11:29 -04:00
parent 4358a78e93
commit 470ab04bb2
2 changed files with 178 additions and 22 deletions

View File

@ -348,12 +348,14 @@ class Controller:
server_file = f"{create_data['type']}-{create_data['version']}.jar" server_file = f"{create_data['type']}-{create_data['version']}.jar"
# Create an EULA file # Create an EULA file
with open( if "agree_to_eula" in create_data:
os.path.join(new_server_path, "eula.txt"), "w", encoding="utf-8" with open(
) as file: os.path.join(new_server_path, "eula.txt"), "w", encoding="utf-8"
file.write( ) as file:
"eula=" + ("true" if create_data["agree_to_eula"] else "false") file.write(
) "eula="
+ ("true" if create_data["agree_to_eula"] else "false")
)
elif root_create_data["create_type"] == "import_server": elif root_create_data["create_type"] == "import_server":
_copy_import_dir_files(create_data["existing_server_path"]) _copy_import_dir_files(create_data["existing_server_path"])
server_file = create_data["jarfile"] server_file = create_data["jarfile"]
@ -376,11 +378,41 @@ class Controller:
def _wrap_jar_if_windows(): def _wrap_jar_if_windows():
return f'"{server_file}"' if Helpers.is_os_windows() else server_file return f'"{server_file}"' if Helpers.is_os_windows() else server_file
server_command = ( if root_create_data["create_type"] == "download_jar":
f"java -Xms{_gibs_to_mibs(min_mem)}M " if Helpers.is_os_windows():
f"-Xmx{_gibs_to_mibs(max_mem)}M " # Let's check for and setup for install server commands
f"-jar {_wrap_jar_if_windows()} nogui" if create_data["type"] == "forge":
) server_command = (
f"java -Xms{Helpers.float_to_string(min_mem)}M "
f"-Xmx{Helpers.float_to_string(max_mem)}M "
f'-jar "{server_file}" --installServer'
)
else:
server_command = (
f"java -Xms{Helpers.float_to_string(min_mem)}M "
f"-Xmx{Helpers.float_to_string(max_mem)}M "
f'-jar "{server_file}" nogui'
)
else:
if create_data["type"] == "forge":
server_command = (
f"java -Xms{Helpers.float_to_string(min_mem)}M "
f"-Xmx{Helpers.float_to_string(max_mem)}M "
f"-jar {server_file} --installServer"
)
else:
server_command = (
f"java -Xms{Helpers.float_to_string(min_mem)}M "
f"-Xmx{Helpers.float_to_string(max_mem)}M "
f"-jar {server_file} nogui"
)
else:
server_command = (
f"java -Xms{_gibs_to_mibs(min_mem)}M "
f"-Xmx{_gibs_to_mibs(max_mem)}M "
f"-jar {_wrap_jar_if_windows()} nogui"
)
elif data["create_type"] == "minecraft_bedrock": elif data["create_type"] == "minecraft_bedrock":
if root_create_data["create_type"] == "import_server": if root_create_data["create_type"] == "import_server":
existing_server_path = Helpers.get_os_understandable_path( existing_server_path = Helpers.get_os_understandable_path(

View File

@ -29,7 +29,7 @@
<br /> <br />
<p class="card-description"> <p class="card-description">
<form method="post" class="server-wizard" onSubmit="wait_msg()"> <form method="post" id="download_jar">
{% if data["server_api"] and data["online"] %} {% if data["server_api"] and data["online"] %}
<fieldset> <fieldset>
{% else %} {% else %}
@ -89,7 +89,7 @@
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label> <label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
<select required class="form-control form-control-lg select-css" id="server_type" name="server_type" onchange="serverTypeChange(this)"> <select required class="form-control form-control-lg select-css" id="server_type" name="type" onchange="serverTypeChange(this)">
<option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option> <option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option>
</select> </select>
</div> </div>
@ -98,7 +98,7 @@
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label> <label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label>
<select required class="form-control form-control-lg select-css" id="server" name="server"> <select required class="form-control form-control-lg select-css" id="server" name="version">
<option value="">{{ translate('serverWizard', 'selectVersion', data['lang']) }}</option> <option value="">{{ translate('serverWizard', 'selectVersion', data['lang']) }}</option>
</select> </select>
</div> </div>
@ -107,7 +107,7 @@
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label> <label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label>
<input type="text" class="form-control" id="server_name" name="server_name" placeholder="{{ translate('serverWizard', 'myNewServer', data['lang']) }}" required> <input type="text" class="form-control" id="server_name" name="name" placeholder="{{ translate('serverWizard', 'myNewServer', data['lang']) }}" required>
</div> </div>
</div> </div>
@ -122,7 +122,7 @@
<div class="form-group"> <div class="form-group">
<label for="min_memory1">{{ translate('serverWizard', 'minMem', data['lang']) }} <small> - {{ <label for="min_memory1">{{ translate('serverWizard', 'minMem', data['lang']) }} <small> - {{
translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label> translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label>
<input type="number" class="form-control" id="min_memory1" name="min_memory" value="1" step="0.5" min="0.5" required> <input type="number" class="form-control" id="min_memory1" name="mem_min" value="1" step="0.5" min="0.5" required>
</div> </div>
</div> </div>
@ -130,7 +130,7 @@
<div class="form-group"> <div class="form-group">
<label for="max_memory1">{{ translate('serverWizard', 'maxMem', data['lang']) }} <small> - {{ <label for="max_memory1">{{ translate('serverWizard', 'maxMem', data['lang']) }} <small> - {{
translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label> translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label>
<input type="number" class="form-control" id="max_memory1" name="max_memory" value="2" step="0.5" min="0.5" required> <input type="number" class="form-control" id="max_memory1" name="mem_max" value="2" step="0.5" min="0.5" required>
</div> </div>
</div> </div>
@ -197,7 +197,7 @@
<br /> <br />
<p class="card-description"> <p class="card-description">
<form method="post" class="server-wizard" onSubmit="wait_msg(true)"> <form class="server-wizard" id="jar-import">
{% raw xsrf_form_html() %} {% raw xsrf_form_html() %}
<input type="hidden" value="import_jar" name="create_type"> <input type="hidden" value="import_jar" name="create_type">
<div class="row"> <div class="row">
@ -205,7 +205,7 @@
<div class="col-sm-12"> <div class="col-sm-12">
<div class="form-group"> <div class="form-group">
<label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label> <label for="server_name">{{ translate('serverWizard', 'serverName', data['lang']) }}</label>
<input type="text" class="form-control" id="server_name" name="server_name" value="" placeholder="{{ translate('serverWizard', 'myNewServer', data['lang']) }}" required> <input type="text" class="form-control" id="server_name" name="name" value="" placeholder="{{ translate('serverWizard', 'myNewServer', data['lang']) }}" required>
</div> </div>
</div> </div>
@ -237,7 +237,7 @@
<div class="form-group"> <div class="form-group">
<label for="min_memory2">{{ translate('serverWizard', 'minMem', data['lang']) }} <small> - {{ <label for="min_memory2">{{ translate('serverWizard', 'minMem', data['lang']) }} <small> - {{
translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label> translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label>
<input type="number" class="form-control" id="min_memory2" name="min_memory" value="1" step="0.5" min="0.5" required> <input type="number" class="form-control" id="min_memory2" name="mem_min" value="1" step="0.5" min="0.5" required>
</div> </div>
</div> </div>
@ -245,7 +245,7 @@
<div class="form-group"> <div class="form-group">
<label for="max_memory2">{{ translate('serverWizard', 'maxMem', data['lang']) }} <small> - {{ <label for="max_memory2">{{ translate('serverWizard', 'maxMem', data['lang']) }} <small> - {{
translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label> translate('serverWizard', 'sizeInGB', data['lang']) }}</small></label>
<input type="number" class="form-control" id="max_memory2" name="max_memory" value="2" step="0.5" min="0.5" required> <input type="number" class="form-control" id="max_memory2" name="mem_max" value="2" step="0.5" min="0.5" required>
</div> </div>
</div> </div>
@ -759,7 +759,7 @@
{% end %} {% end %}
{% block js%} {% block js %}
<script> <script>
document.getElementById("root_files_button").addEventListener("click", function () { document.getElementById("root_files_button").addEventListener("click", function () {
@ -893,6 +893,130 @@
}); });
</script> </script>
<script>
function replacer(key, value) {
if (key != "ignored_exits") {
if (typeof value == "boolean" || key === "host" || key === "version") {
return value
} else {
return (isNaN(value) ? value : +value);
}
} else {
return value;
}
}
$(document).ready(function () {
$("#download_jar").on("submit", async function (e) {
wait_msg();
e.preventDefault();
var token = getCookie("_xsrf")
let jarForm = document.getElementById("download_jar");
let formData = new FormData(jarForm);
//Create an object from the form data entries
let formDataObject = Object.fromEntries(formData.entries());
console.log(formDataObject);
let send_data = {
"name": formDataObject.name,
"monitoring_type": "minecraft_java",
"minecraft_java_monitoring_data": {
"host": "127.0.0.1",
"port": formDataObject.port
},
"create_type": "minecraft_java",
"minecraft_java_create_data": {
"create_type": "download_jar",
"download_jar_create_data": {
"category": formDataObject.version.split("|")[0],
"type": formDataObject.version.split("|")[1],
"version": formDataObject.version.split("|")[2],
"mem_min": formDataObject.mem_min,
"mem_max": formDataObject.mem_max,
"server_properties_port": formDataObject.port
}
}
}
console.log(send_data);
// Format the plain form data as JSON
let formDataJsonString = JSON.stringify(send_data, replacer);
console.log(formDataJsonString);
let res = await fetch(`/api/v2/servers/`, {
method: 'POST',
headers: {
'X-XSRFToken': token
},
body: formDataJsonString,
});
let responseData = await res.json();
if (responseData.status === "ok") {
window.location.href = '/panel/dashboard';
} else {
bootbox.alert({
title: responseData.error,
message: responseData.error_data
});
}
});
$("#import-jar").on("submit", async function (e) {
wait_msg();
e.preventDefault();
var token = getCookie("_xsrf")
let jarForm = document.getElementById("import-jar");
let formData = new FormData(jarForm);
//Create an object from the form data entries
let formDataObject = Object.fromEntries(formData.entries());
console.log(formDataObject);
let send_data = {
"name": formDataObject.name,
"monitoring_type": "minecraft_java",
"minecraft_java_monitoring_data": {
"host": "127.0.0.1",
"port": formDataObject.port
},
"create_type": "minecraft_java",
"minecraft_java_create_data": {
"create_type": "import_server",
"import_server_create_data": {
"existing_server_path": formDataObject.server_path,
"jarfile": formDataObject.server_jar,
"mem_min": formDataObject.mem_min,
"mem_max": formDataObject.mem_max,
"server_properties_port": formDataObject.port
}
}
}
console.log(send_data);
// Format the plain form data as JSON
let formDataJsonString = JSON.stringify(send_data, replacer);
let res = await fetch(`/api/v2/servers/`, {
method: 'POST',
headers: {
'X-XSRFToken': token
},
body: formDataJsonString,
});
let responseData = await res.json();
if (responseData.status === "ok") {
window.location.href = '/panel/dashboard';
} else {
bootbox.alert({
title: responseData.error,
message: responseData.error_data
});
}
});
});
</script>
<script> <script>
$(".tree-reset").on("click", function () { $(".tree-reset").on("click", function () {
location.href = "/server/step1"; location.href = "/server/step1";