Add Java detect for win, and hook up to front end

This commit is contained in:
Zedifus 2022-06-20 23:44:33 +01:00
parent 32b3551108
commit 8e646df58c
4 changed files with 64 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import zipfile
import pathlib import pathlib
import ctypes import ctypes
import subprocess 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
@ -84,6 +85,46 @@ class Helpers:
@staticmethod @staticmethod
def find_java_installs(): 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: try:
paths = subprocess.check_output( paths = subprocess.check_output(
["/usr/bin/update-alternatives", "--list", "java"], encoding="utf8" ["/usr/bin/update-alternatives", "--list", "java"], encoding="utf8"

View File

@ -9,6 +9,7 @@ import threading
import bleach import bleach
import libgravatar import libgravatar
import requests import requests
import shlex
import tornado.web import tornado.web
import tornado.escape import tornado.escape
from tornado import iostream from tornado import iostream
@ -627,6 +628,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,9 +1363,12 @@ class PanelHandler(BaseHandler):
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 = execution_command.split(" ") execution_list = shlex.split(execution_command)
if java_selection: if java_selection:
execution_list[0] = java_selection if self.helper.is_os_windows():
execution_list[0] = '"' + java_selection + '/bin/java"'
else:
execution_list[0] = '"' + java_selection + '"'
execution_command = "" execution_command = ""
for item in execution_list: for item in execution_list:
execution_command += item + " " execution_command += item + " "

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">
@ -79,6 +79,19 @@
placeholder="{{ translate('serverConfig', 'serverExecutable', data['lang']) }}" required> placeholder="{{ translate('serverConfig', 'serverExecutable', data['lang']) }}" required>
</div> </div>
<div class="form-group">
<label for="executable">{{ 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="">None</option>
{% for path in data['java_versions'] %}
<option value="{{path}}">{{path}}</option>
{% end %}
</select>
</div>
<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',

View File

@ -300,6 +300,8 @@
"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)",
"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",