Merge branch 'feature/java-selection' into 'dev'

Java path dropdown select

See merge request crafty-controller/crafty-4!375
This commit is contained in:
Iain Powrie 2022-06-21 03:39:30 +00:00
commit b5331501ec
5 changed files with 91 additions and 6 deletions

View File

@ -1,9 +1,10 @@
# Changelog # Changelog
## --- [4.0.4] - 2022/TBD ## --- [4.0.4] - 2022/06/21
### New features ### New features
- Add shutdown on backup feature ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/373)) - Add shutdown on backup feature ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/373))
- Add detection and dropdown of java versions ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/375))
### Bug fixes ### Bug fixes
- Backup/Config.json rework for API key hardening ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/369)) - Backup/Config.json rework for API key hardening ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/369))
- Fix stack on ping result being falsy ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/371)) - Fix stack on ping result being falsy ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/371))

View File

@ -15,6 +15,8 @@ import html
import zipfile import zipfile
import pathlib import pathlib
import ctypes import ctypes
import subprocess
import itertools
from datetime import datetime from datetime import datetime
from socket import gethostname from socket import gethostname
from contextlib import redirect_stderr, suppress from contextlib import redirect_stderr, suppress
@ -80,6 +82,60 @@ class Helpers:
print(f"Import Error: Unable to load {ex.name} module") print(f"Import Error: Unable to load {ex.name} module")
installer.do_install() installer.do_install()
@staticmethod
def find_java_installs():
# If we're windows return oracle java versions,
# otherwise java vers need to be manual.
if os.name == "nt":
# Adapted from LeeKamentsky >>>
# https://github.com/LeeKamentsky/python-javabridge/blob/master/javabridge/locate.py
jdk_key_paths = (
"SOFTWARE\\JavaSoft\\JDK",
"SOFTWARE\\JavaSoft\\Java Development Kit",
)
java_paths = []
for jdk_key_path in jdk_key_paths:
try:
with suppress(OSError), winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE, jdk_key_path
) as kjdk:
for i in itertools.count():
version = winreg.EnumKey(kjdk, i)
kjdk_current = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
jdk_key_path,
)
kjdk_current = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
jdk_key_path + "\\" + version,
)
kjdk_current_values = dict( # pylint: disable=consider-using-dict-comprehension
[
winreg.EnumValue(kjdk_current, i)[:2]
for i in range(winreg.QueryInfoKey(kjdk_current)[1])
]
)
java_paths.append(kjdk_current_values["JavaHome"])
except OSError as e:
if e.errno == 2:
continue
raise
return java_paths
# If we get here we're linux so we will use 'update-alternatives'
# (If distro does not have update-alternatives then manual input.)
try:
paths = subprocess.check_output(
["/usr/bin/update-alternatives", "--list", "java"], encoding="utf8"
)
if re.match("^(/[^/ ]*)+/?$", paths):
return paths.split("\n")
except Exception as e:
print("Java Detect Error: ", e)
logger.error(f"Java Detect Error: {e}")
@staticmethod @staticmethod
def float_to_string(gbs: float): def float_to_string(gbs: float):
s = str(float(gbs) * 1000).rstrip("0").rstrip(".") s = str(float(gbs) * 1000).rstrip("0").rstrip(".")

View File

@ -6,6 +6,7 @@ import typing as t
import json import json
import logging import logging
import threading import threading
import shlex
import bleach import bleach
import libgravatar import libgravatar
import requests import requests
@ -631,6 +632,7 @@ class PanelHandler(BaseHandler):
"/panel/error?error=Unauthorized access Server Config" "/panel/error?error=Unauthorized access Server Config"
) )
return return
page_data["java_versions"] = Helpers.find_java_installs()
if subpage == "files": if subpage == "files":
if ( if (
@ -1361,11 +1363,21 @@ class PanelHandler(BaseHandler):
auto_start = int(float(self.get_argument("auto_start", "0"))) auto_start = int(float(self.get_argument("auto_start", "0")))
crash_detection = int(float(self.get_argument("crash_detection", "0"))) crash_detection = int(float(self.get_argument("crash_detection", "0")))
logs_delete_after = int(float(self.get_argument("logs_delete_after", "0"))) logs_delete_after = int(float(self.get_argument("logs_delete_after", "0")))
java_selection = self.get_argument("java_selection", None)
# subpage = self.get_argument('subpage', None) # subpage = self.get_argument('subpage', None)
server_id = self.check_server_id() server_id = self.check_server_id()
if server_id is None: if server_id is None:
return return
execution_list = shlex.split(execution_command)
if java_selection:
if self.helper.is_os_windows():
execution_list[0] = '"' + java_selection + '/bin/java"'
else:
execution_list[0] = '"' + java_selection + '"'
execution_command = ""
for item in execution_list:
execution_command += item + " "
server_obj: Servers = self.controller.servers.get_server_obj(server_id) server_obj: Servers = self.controller.servers.get_server_obj(server_id)
stale_executable = server_obj.executable stale_executable = server_obj.executable

View File

@ -36,7 +36,7 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-sm-12"> <div class="col-md-6 col-sm-12">
<form class="forms-sample" method="post" action="/panel/server_detail"> <form class="forms-sample" method="post" id="config_form" action="/panel/server_detail">
{% raw xsrf_form_html() %} {% raw xsrf_form_html() %}
<input type="hidden" name="id" value="{{ data['server_stats']['server_id']['server_id'] }}"> <input type="hidden" name="id" value="{{ data['server_stats']['server_id']['server_id'] }}">
<input type="hidden" name="subpage" value="config"> <input type="hidden" name="subpage" value="config">
@ -50,8 +50,8 @@
placeholder="{{ translate('serverConfig', 'serverName', data['lang']) }}" required> placeholder="{{ translate('serverConfig', 'serverName', data['lang']) }}" required>
</div> </div>
<div class="form-group">
{% if data['super_user'] %} {% if data['super_user'] %}
<div class="form-group">
<label for="server_path">{{ translate('serverConfig', 'serverPath', data['lang']) }} <small <label for="server_path">{{ translate('serverConfig', 'serverPath', data['lang']) }} <small
class="text-muted ml-1"> - {{ translate('serverConfig', 'serverPathDesc', data['lang']) }}</small> class="text-muted ml-1"> - {{ translate('serverConfig', 'serverPathDesc', data['lang']) }}</small>
</label> </label>
@ -78,7 +78,20 @@
value="{{ data['server_stats']['server_id']['executable'] }}" value="{{ data['server_stats']['server_id']['executable'] }}"
placeholder="{{ translate('serverConfig', 'serverExecutable', data['lang']) }}" required> placeholder="{{ translate('serverConfig', 'serverExecutable', data['lang']) }}" required>
</div> </div>
{% end %}
<div class="form-group">
<label for="java_selection">{{ translate('serverConfig', 'javaVersion', data['lang']) }} <small
class="text-muted ml-1"> - {{ translate('serverConfig', 'javaVersionDesc', data['lang'])
}}</small> </label>
<select class="form-select form-control form-control-lg select-css" id="java_selection"
name="java_selection" form="config_form">
<option value="">{{ translate('serverConfig', 'javaNoChange', data['lang'])}}</option>
{% for path in data['java_versions'] %}
<option value="{{path}}">{{path}}</option>
{% end %}
</select>
</div>
{% if data['super_user'] %}
<div class="form-group"> <div class="form-group">
<label for="execution_command">{{ translate('serverConfig', 'serverExecutionCommand', data['lang']) }} <label for="execution_command">{{ translate('serverConfig', 'serverExecutionCommand', data['lang']) }}
<small class="text-muted ml-1"> - {{ translate('serverConfig', 'serverExecutionCommandDesc', <small class="text-muted ml-1"> - {{ translate('serverConfig', 'serverExecutionCommandDesc',
@ -86,8 +99,8 @@
<input type="text" class="form-control" name="execution_command" id="execution_command" <input type="text" class="form-control" name="execution_command" id="execution_command"
value="{{ data['server_stats']['server_id']['execution_command'] }}" value="{{ data['server_stats']['server_id']['execution_command'] }}"
placeholder="{{ translate('serverConfig', 'serverExecutionCommand', data['lang']) }}" required> placeholder="{{ translate('serverConfig', 'serverExecutionCommand', data['lang']) }}" required>
{% end %}
</div> </div>
{% end %}
<div class="form-group"> <div class="form-group">
<label for="stop_command">{{ translate('serverConfig', 'serverStopCommand', data['lang']) }} <small <label for="stop_command">{{ translate('serverConfig', 'serverStopCommand', data['lang']) }} <small

View File

@ -301,6 +301,9 @@
"serverCrashDetection": "Server Crash Detection", "serverCrashDetection": "Server Crash Detection",
"serverExecutable": "Server Executable", "serverExecutable": "Server Executable",
"serverExecutableDesc": "The server's executable file", "serverExecutableDesc": "The server's executable file",
"javaVersion": "Override current Java Version",
"javaVersionDesc": "If we've been able to find local java installs. (Windows 'Oracle' only)",
"javaNoChange": "Do Not Override",
"serverExecutionCommand": "Server Execution Command", "serverExecutionCommand": "Server Execution Command",
"serverExecutionCommandDesc": "What will be launched in a hidden terminal", "serverExecutionCommandDesc": "What will be launched in a hidden terminal",
"serverIP": "Server IP", "serverIP": "Server IP",