Enhance transactional support with savepoints, improve error handling for nested exceptions, and provide better rollback mechanisms. Update tests to validate savepoint behavior and add new test cases for mod errors, task execution, and server configuration. Expand Gradle tasks to generate admin passwords, preserve configs during deployment, and support custom server addresses.
This commit is contained in:
1 parent
26ca1b90a2
commit
67530d35e9
14 files changed
+271
-52
No files matched your search
@@ -3,6 +3,7 @@ package dev.overlord.zomboidforge.gradle;
|
||||
import org.gradle.api.DefaultTask;
|
||||
import org.gradle.api.GradleException;
|
||||
import org.gradle.api.file.DirectoryProperty;
|
||||
import org.gradle.api.file.RegularFileProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.Internal;
|
||||
@@ -15,6 +16,13 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.nio.file.attribute.PosixFileAttributeView;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -37,6 +45,8 @@ public abstract class RunServer extends DefaultTask
|
||||
/** Seconds the server gets to save the world after a stop request. */
|
||||
private static final int STOP_TIMEOUT = 120;
|
||||
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
/** Contains one folder per mod, as in {@code <forge>/mods}. */
|
||||
@Internal
|
||||
public abstract DirectoryProperty getModsDirectory();
|
||||
@@ -50,9 +60,17 @@ public abstract class RunServer extends DefaultTask
|
||||
@Input
|
||||
public abstract Property<String> getDataVolume();
|
||||
|
||||
@Input
|
||||
/** Not an input, so it is not stored in the task history. */
|
||||
@Internal
|
||||
public abstract Property<String> getAdminPassword();
|
||||
|
||||
/** Holds the generated admin password of the volume when {@link #getAdminPassword()} is not set. */
|
||||
@Internal
|
||||
public abstract RegularFileProperty getAdminPasswordFile();
|
||||
|
||||
@Input
|
||||
public abstract Property<String> getServerAddress();
|
||||
|
||||
@Input
|
||||
@Optional
|
||||
public abstract Property<String> getContainerEngine();
|
||||
@@ -60,23 +78,28 @@ public abstract class RunServer extends DefaultTask
|
||||
@TaskAction
|
||||
public void run() throws IOException
|
||||
{
|
||||
String engine = Executables.containerEngine(this.getContainerEngine().getOrNull());
|
||||
String name = this.getContainerName().get();
|
||||
String engine = Executables.containerEngine(this.getContainerEngine().getOrNull());
|
||||
String name = this.getContainerName().get();
|
||||
String address = this.getServerAddress().get();
|
||||
|
||||
// ADMIN_PASSWORD without a value takes the value from the engine's environment, so it is not in the command line.
|
||||
List<String> command = List.of(
|
||||
engine, "run", "--rm",
|
||||
"--name", name,
|
||||
"--publish", "16261:16261/udp",
|
||||
"--publish", "16262:16262/udp",
|
||||
"--env", "ADMIN_PASSWORD=" + this.getAdminPassword().get(),
|
||||
"--publish", address + ":16261:16261/udp",
|
||||
"--publish", address + ":16262:16262/udp",
|
||||
"--env", "ADMIN_PASSWORD",
|
||||
"--volume", this.getDataVolume().get() + ":/data",
|
||||
"--volume", this.getModsDirectory().get().getAsFile().getAbsolutePath() + ":/forge/mods:ro,Z",
|
||||
this.getImage().get()
|
||||
);
|
||||
|
||||
this.getLogger().lifecycle("Starting {} with the mod; connect to localhost:16261. Cancel the build (Ctrl+C) to stop the server.", this.getImage().get());
|
||||
ProcessBuilder builder = new ProcessBuilder(command).redirectErrorStream(true);
|
||||
builder.environment().put("ADMIN_PASSWORD", this.adminPassword());
|
||||
|
||||
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
|
||||
this.getLogger().lifecycle("Starting {} with the mod on {}:16261. Cancel the build (Ctrl+C) to stop the server.", this.getImage().get(), address);
|
||||
|
||||
Process process = builder.start();
|
||||
Thread output = new Thread(() -> RunServer.forward(process), "runServer output");
|
||||
|
||||
output.setDaemon(true);
|
||||
@@ -96,6 +119,42 @@ public abstract class RunServer extends DefaultTask
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The configured admin password, else the one in {@link #getAdminPasswordFile()}; a random one
|
||||
* is generated into the file when it does not exist. The server uses the password only when it
|
||||
* creates the admin account on the volume's first start.
|
||||
*/
|
||||
private String adminPassword() throws IOException
|
||||
{
|
||||
String configured = this.getAdminPassword().getOrNull();
|
||||
if (configured != null) return configured;
|
||||
|
||||
Path file = this.getAdminPasswordFile().get().getAsFile().toPath();
|
||||
|
||||
if (Files.exists(file)) return Files.readString(file, StandardCharsets.UTF_8).strip();
|
||||
|
||||
byte[] random = new byte[18];
|
||||
RunServer.RANDOM.nextBytes(random);
|
||||
|
||||
String password = Base64.getUrlEncoder().withoutPadding().encodeToString(random);
|
||||
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.writeString(file, password, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
|
||||
RunServer.restrictToOwner(file);
|
||||
|
||||
this.getLogger().lifecycle("Generated the admin password '{}' for '{}', kept in '{}'.", password, this.getDataVolume().get(), file);
|
||||
|
||||
return password;
|
||||
}
|
||||
|
||||
/** Makes {@code file} readable and writable by its owner only, where the file system has POSIX permissions. */
|
||||
private static void restrictToOwner(Path file) throws IOException
|
||||
{
|
||||
if (!Files.getFileStore(file).supportsFileAttributeView(PosixFileAttributeView.class)) return;
|
||||
|
||||
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-------"));
|
||||
}
|
||||
|
||||
/** Copies the container's output to the build output, line by line. */
|
||||
private static void forward(Process process)
|
||||
{
|
||||
|
||||
+8
-1
@@ -64,9 +64,16 @@ public abstract class ZomboidForgeExtension
|
||||
/** Image for {@code runServer}. Default: the ZomboidForge image tagged "latest". */
|
||||
public abstract Property<String> getServerImage();
|
||||
|
||||
/** Admin password of the {@code runServer} server. Default: "admin". */
|
||||
/**
|
||||
* Admin password of the {@code runServer} server, used when its volume is created. Default:
|
||||
* a random password, generated once per volume and kept in
|
||||
* {@code <Gradle user home>/zomboidforge/admin-passwords/<volume>}.
|
||||
*/
|
||||
public abstract Property<String> getAdminPassword();
|
||||
|
||||
/** Host address {@code runServer} publishes the game ports on. Default: "127.0.0.1" (this machine only); "0.0.0.0" for every interface. */
|
||||
public abstract Property<String> getServerAddress();
|
||||
|
||||
/** Volume for the {@code runServer} server's /data (world, config). Default: "zomboidforge-<id>". */
|
||||
public abstract Property<String> getDataVolume();
|
||||
|
||||
|
||||
+8
-2
@@ -55,6 +55,7 @@ public final class ZomboidForgeModPlugin implements Plugin<Project>
|
||||
private static final String TOOLS = "dev.overlord.zomboidforge:mod-tools:";
|
||||
private static final String TOOLS_MAIN = "dev.overlord.zomboidforge.tools.ModCheck";
|
||||
private static final String DEFAULT_IMAGE = "git.security-command.org/overlord/zomboidforge:latest";
|
||||
private static final String MOD_CONFIG = "config.toml";
|
||||
private static final int JAVA = 25;
|
||||
|
||||
@Override
|
||||
@@ -72,7 +73,7 @@ public final class ZomboidForgeModPlugin implements Plugin<Project>
|
||||
extension.getApi().convention(version);
|
||||
extension.getSteamBranch().convention("public");
|
||||
extension.getServerImage().convention(ZomboidForgeModPlugin.DEFAULT_IMAGE);
|
||||
extension.getAdminPassword().convention("admin");
|
||||
extension.getServerAddress().convention("127.0.0.1");
|
||||
extension.getDataVolume().convention(extension.getId().map(id -> "zomboidforge-" + id));
|
||||
extension.getGameJar().convention(layout.file(project.getProviders().gradleProperty("zomboidforge.gameJar").map(File::new)));
|
||||
|
||||
@@ -220,6 +221,8 @@ public final class ZomboidForgeModPlugin implements Plugin<Project>
|
||||
project.getTasks().named(LifecycleBasePlugin.ASSEMBLE_TASK_NAME, task -> task.dependsOn(modFolder, modZip));
|
||||
|
||||
// Trying the mod.
|
||||
File passwords = new File(project.getGradle().getGradleUserHomeDir(), "zomboidforge/admin-passwords");
|
||||
|
||||
project.getTasks().register("runServer", RunServer.class, task ->
|
||||
{
|
||||
task.setGroup(ZomboidForgeModPlugin.GROUP);
|
||||
@@ -230,14 +233,17 @@ public final class ZomboidForgeModPlugin implements Plugin<Project>
|
||||
task.getContainerName().set(id.map(name -> "zomboidforge-" + name));
|
||||
task.getDataVolume().set(extension.getDataVolume());
|
||||
task.getAdminPassword().set(extension.getAdminPassword());
|
||||
task.getAdminPasswordFile().set(layout.file(extension.getDataVolume().map(volume -> new File(passwords, volume))));
|
||||
task.getServerAddress().set(extension.getServerAddress());
|
||||
task.getContainerEngine().set(extension.getContainerEngine());
|
||||
});
|
||||
|
||||
project.getTasks().register("deployMod", Sync.class, task ->
|
||||
{
|
||||
task.setGroup(ZomboidForgeModPlugin.GROUP);
|
||||
task.setDescription("Copies the mod folder into zomboidForge.deployDirectory (a server's mods directory).");
|
||||
task.setDescription("Copies the mod folder into zomboidForge.deployDirectory (a server's mods directory); keeps the server's config.toml of the mod.");
|
||||
task.from(modFolder);
|
||||
task.preserve(filter -> filter.include(ZomboidForgeModPlugin.MOD_CONFIG));
|
||||
task.into(extension.getDeployDirectory().dir(id).orElse(layout.getBuildDirectory().dir("deploy-not-configured")));
|
||||
task.onlyIf("zomboidForge.deployDirectory is set", element -> extension.getDeployDirectory().isPresent());
|
||||
});
|
||||
|
||||
+18
@@ -22,6 +22,7 @@ import java.util.jar.JarOutputStream;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.objectweb.asm.Opcodes.ACC_FINAL;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
|
||||
@@ -140,6 +141,23 @@ class ZomboidForgeModPluginTest
|
||||
assertEquals(TaskOutcome.UP_TO_DATE, again.task(":checkMod").getOutcome());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deployReplacesTheModButKeepsTheServerSettings() throws IOException
|
||||
{
|
||||
this.modProject(" deployDirectory = layout.projectDirectory.dir(\"server/mods\")", "");
|
||||
|
||||
Path deployed = this.project.resolve("server/mods/shop");
|
||||
this.write("server/mods/shop/config.toml", "limit = 7\n");
|
||||
this.write("server/mods/shop/lib/old.jar", "");
|
||||
|
||||
this.runner("deployMod").build();
|
||||
|
||||
assertTrue(Files.isRegularFile(deployed.resolve("shop.jar")));
|
||||
assertTrue(Files.isRegularFile(deployed.resolve("lib/util.jar")));
|
||||
assertFalse(Files.exists(deployed.resolve("lib/old.jar")), "files the mod no longer has are removed");
|
||||
assertEquals("limit = 7\n", Files.readString(deployed.resolve("config.toml")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checksAGameModAgainstTheGameJar() throws IOException
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user