From 74a4b9a62c27f05b49010b403edc8d3c73a6cfb5 Mon Sep 17 00:00:00 2001 From: TheWhiteDog9487 Date: Thu, 25 Sep 2025 15:57:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BFSystemd=E6=9C=8D=E5=8A=A1=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E5=8D=B8=E8=BD=BD=E9=80=BB=E8=BE=91=E5=9C=A8=E6=95=B4?= =?UTF-8?q?=E4=BD=93=E7=A8=8B=E5=BA=8F=E5=90=AF=E5=8A=A8=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=EF=BC=8C=E4=BB=A5=E8=A7=A3=E5=86=B3=E4=BD=BF?= =?UTF-8?q?=E7=94=A8--install=E6=9B=B4=E6=96=B0=E6=9C=8D=E5=8A=A1=E8=80=8C?= =?UTF-8?q?=E7=8E=B0=E6=9C=89=E6=9C=8D=E5=8A=A1=E6=AD=A3=E5=9C=A8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E5=AF=BC=E8=87=B4Tomcat=E7=BB=91=E5=AE=9A=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E5=A4=B1=E8=B4=A5=E8=80=8C=E6=95=B4=E4=BD=93=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E9=80=80=E5=87=BA=EF=BC=8C=E6=9C=AA=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9C=8D=E5=8A=A1=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E7=9A=84=E6=95=85=E9=9A=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Startup/SystemdInstallerManager.java | 43 +++++++++---------- .../WebAPI/WebApiApplication.java | 6 ++- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/Startup/SystemdInstallerManager.java b/src/main/java/xyz/thewhitedog9487/WebAPI/Startup/SystemdInstallerManager.java index eba57cb..4a071ed 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/Startup/SystemdInstallerManager.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/Startup/SystemdInstallerManager.java @@ -1,12 +1,8 @@ package xyz.thewhitedog9487.WebAPI.Startup; -import discord4j.core.GatewayDiscordClient; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.system.ApplicationHome; -import org.springframework.stereotype.Component; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -16,8 +12,7 @@ import java.util.Arrays; import java.util.List; @Slf4j -@Component -class SystemdInstallerManager implements CommandLineRunner { +public class SystemdInstallerManager{ static String TemplateContent = """ [Unit] Description=WebAPI @@ -35,23 +30,22 @@ class SystemdInstallerManager implements CommandLineRunner { WantedBy=multi-user.target """; - @Autowired ObjectProvider DiscordClientProvider; - ApplicationHome AppHome = new ApplicationHome( SystemdInstallerManager.class ); - String CurrentOS = System.getProperty("os.name"); - String CurrentUser = System.getProperty("user.name"); - Path CurrentExecutableFilePath = GetExecutblePath(); - Path WorkingDirectory = AppHome + static ApplicationHome AppHome = new ApplicationHome( SystemdInstallerManager.class ); + static String CurrentOS = System.getProperty("os.name"); + static String CurrentUser = System.getProperty("user.name"); + static Path CurrentExecutableFilePath = GetExecutblePath(); + static Path WorkingDirectory = AppHome .getDir() .toPath(); - Path ServiceFileDirectory = Path.of("/etc/systemd/system/"); - Path ServiceFileName = Path.of("WebAPI.service"); - String SoftLinkFileName = "Current"; - Path SymbolicLinkPath = WorkingDirectory.resolve(SoftLinkFileName); + static Path ServiceFileDirectory = Path.of("/etc/systemd/system/"); + static Path ServiceFileName = Path.of("WebAPI.service"); + static String SoftLinkFileName = "Current"; + static Path SymbolicLinkPath = WorkingDirectory.resolve(SoftLinkFileName); /** * 检测当前运行环境是否为GraalVM生成的Native Image */ - boolean IsImageCode(){ + static boolean IsImageCode(){ try { return (boolean) Class.forName("org.graalvm.nativeimage.ImageInfo") .getMethod("inImageCode") @@ -66,7 +60,7 @@ class SystemdInstallerManager implements CommandLineRunner { *
* 如果是直接用JRE运行的JAR包,则返回JAR文件的路径 */ - Path GetExecutblePath(){ + static Path GetExecutblePath(){ try { var IsImageCode = IsImageCode(); log.debug("IsImageCode: {}", IsImageCode); @@ -90,15 +84,16 @@ class SystemdInstallerManager implements CommandLineRunner { } catch (IOException ex) { throw new RuntimeException(ex); } } } - @Override - public void run(String... args) throws Exception { - for (String Argument : args) { + @SneakyThrows + public static void ProcessArguments(String[] CommandLineArguments) { + log.debug("CommandLineArguments: {}", Arrays.toString(CommandLineArguments)); + for (String Argument : CommandLineArguments) { if ( List.of("install", "--install").contains(Argument) == true ) { /* 在Native Image / Jar文件旁边生成指向自身的,名为Current的软连接 然后在软连接旁生成systemd服务文件,执行目标指向软连接 */ - List DiscordBotToken = Arrays.stream(args) + List DiscordBotToken = Arrays.stream(CommandLineArguments) .filter(s -> { for (String o : List.of("--Discord_Bot_Token=")) { if (s.startsWith(o) == true) { return true; } } @@ -117,6 +112,8 @@ class SystemdInstallerManager implements CommandLineRunner { log.debug("CurrentExecutableFilePath: {}", CurrentExecutableFilePath); log.debug("WorkingDirectory: {}", WorkingDirectory); log.debug("SymbolicLinkPath: {}", SymbolicLinkPath); + new ProcessBuilder("systemctl", "stop", ServiceFileName.toString()).start().waitFor(); + log.info("已停止systemd服务: {}", ServiceFileName); Files.deleteIfExists(SymbolicLinkPath); Files.createSymbolicLink(SymbolicLinkPath, CurrentExecutableFilePath); log.info("成功创建指向当前程序的软链接: {} -> {}", SymbolicLinkPath, CurrentExecutableFilePath); diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/WebApiApplication.java b/src/main/java/xyz/thewhitedog9487/WebAPI/WebApiApplication.java index c1eaa10..49acc67 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/WebApiApplication.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/WebApiApplication.java @@ -2,13 +2,15 @@ package xyz.thewhitedog9487.WebAPI; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import xyz.thewhitedog9487.WebAPI.Startup.SystemdInstallerManager; @SpringBootApplication public class WebApiApplication { - public static void main(String[] args) { //TODO: 从配置文件或环境变量读取并设置HTTP端口号 - SpringApplication.run(WebApiApplication.class, args); + var SpringApp = new SpringApplication(WebApiApplication.class); + SystemdInstallerManager.ProcessArguments(args); + SpringApp.run(args); } }