mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(core): make the init hook directory configurable (#5801)
* feat(core): make the init hook directory configurable
Hook directories were fixed at /etc/floci/init/{boot,start,ready,stop,
shutdown}.d, which only the image can provide. floci.init-hooks.dir
(FLOCI_INIT_HOOKS_DIR) now sets that root, defaulting to /etc/floci/init so
the image is unchanged; the LocalStack-compat tree is still read as well.
Hooks also run on Windows, where there is no /bin/sh: .ps1 with
PowerShell, .cmd and .bat with cmd, and .py with python. A .sh hook there
is skipped with a warning unless another shell is configured; on Linux and
macOS the Windows script types are ignored.
Refs #5767
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
* fix(core): match Windows hook extensions in any case and warn about a skip once
Review follow-ups for the configurable hook directory:
- Windows file names are case-insensitive, so SETUP.BAT and Init.PS1 are
now hooks there; elsewhere the extension must stay lower case, as before.
- The warning for a hook Windows cannot run is logged once per file
instead of on every directory listing, and Windows script types are
ignored silently on Linux and macOS.
- The environment variable reference and application.yml list shutdown.d
with the other hook directories.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
* fix(core): always resolve hook directories from the configured root
findMergedScripts called getPrimaryPaths() for the default root and
getPrimaryPaths(hooksRoot) otherwise, though both return the same
directories; the no-argument form only survived because five tests stub
it on a mocked hook. The runner now always passes its root, the overload
is gone, and those tests stub getPrimaryPaths(anyString()).
The runner tests this branch added drop @DisplayName, per AGENTS.md.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
---------
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
43ad214896
commit
050f93e600
11 files changed
+313
-39
No files matched your search
@@ -3509,6 +3509,15 @@ public interface EmulatorConfig {
|
||||
}
|
||||
|
||||
interface InitHooksConfig {
|
||||
/**
|
||||
* Root of the Floci-native hook directories ({@code boot.d}, {@code start.d}, {@code ready.d},
|
||||
* {@code stop.d}, {@code shutdown.d}). The image mounts them under the default; a binary on
|
||||
* the host points this at a directory of its own. The LocalStack-compat tree under
|
||||
* {@code /etc/localstack/init} is always read as well.
|
||||
*/
|
||||
@WithDefault("/etc/floci/init")
|
||||
String dir();
|
||||
|
||||
@WithDefault("/bin/sh")
|
||||
String shellExecutable();
|
||||
|
||||
|
||||
@@ -18,10 +18,16 @@ public class HookScriptExecutor {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(HookScriptExecutor.class);
|
||||
private final EmulatorConfig.InitHooksConfig initHooksConfig;
|
||||
private final boolean windows;
|
||||
|
||||
@Inject
|
||||
public HookScriptExecutor(final EmulatorConfig emulatorConfig) {
|
||||
this(emulatorConfig, System.getProperty("os.name", ""));
|
||||
}
|
||||
|
||||
HookScriptExecutor(final EmulatorConfig emulatorConfig, final String osName) {
|
||||
this.initHooksConfig = emulatorConfig.initHooks();
|
||||
this.windows = HookScripts.isWindows(osName);
|
||||
}
|
||||
|
||||
public void run(final File scriptFile) throws IOException, InterruptedException {
|
||||
@@ -29,11 +35,11 @@ public class HookScriptExecutor {
|
||||
}
|
||||
|
||||
public void run(final File hookDirectory, final String scriptFileName) throws IOException, InterruptedException {
|
||||
final String command = scriptFileName.endsWith(".py") ? "python3" : initHooksConfig.shellExecutable();
|
||||
LOG.debugv("Executing hook script {0} via {1}", scriptFileName, command);
|
||||
final List<String> command = HookScripts.command(scriptFileName, windows, initHooksConfig.shellExecutable());
|
||||
LOG.debugv("Executing hook script {0} via {1}", scriptFileName, command.getFirst());
|
||||
|
||||
// Inherit parent I/O so script output is streamed directly and does not block on unconsumed buffers.
|
||||
final Process process = new ProcessBuilder(command, scriptFileName).directory(hookDirectory).inheritIO().start();
|
||||
final Process process = new ProcessBuilder(command).directory(hookDirectory).inheritIO().start();
|
||||
run(process, scriptFileName);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Which hook scripts can run on this platform, and the command that runs each.
|
||||
*
|
||||
* <p>On Linux and macOS, {@code .sh} runs with the configured shell and {@code .py} with
|
||||
* {@code python3}, as in the image. A native binary on Windows has no {@code /bin/sh}, so there
|
||||
* {@code .ps1} runs with PowerShell, {@code .cmd} and {@code .bat} with {@code cmd}, {@code .py}
|
||||
* with {@code python}, and {@code .sh} only when a shell other than the default
|
||||
* {@code /bin/sh} is configured (Git Bash, for example). Windows file names are case-insensitive, so
|
||||
* there {@code SETUP.BAT} is a {@code .bat} hook; elsewhere the extension must be lower case, as
|
||||
* before.
|
||||
*/
|
||||
final class HookScripts {
|
||||
|
||||
static final String DEFAULT_SHELL = "/bin/sh";
|
||||
|
||||
private HookScripts() {
|
||||
}
|
||||
|
||||
static boolean isWindows(String osName) {
|
||||
return osName != null && osName.toLowerCase(Locale.ROOT).startsWith("windows");
|
||||
}
|
||||
|
||||
/** Whether the file is a hook script at all, on any platform. */
|
||||
static boolean isScript(String fileName, boolean windows) {
|
||||
return extensionOf(fileName, windows) != null;
|
||||
}
|
||||
|
||||
static boolean canRun(String fileName, boolean windows, String shell) {
|
||||
String extension = extensionOf(fileName, windows);
|
||||
if (extension == null) {
|
||||
return false;
|
||||
}
|
||||
return switch (extension) {
|
||||
case ".sh" -> !windows || !DEFAULT_SHELL.equals(shell);
|
||||
case ".py" -> true;
|
||||
case ".ps1", ".cmd", ".bat" -> windows;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
static List<String> command(String fileName, boolean windows, String shell) {
|
||||
String extension = extensionOf(fileName, windows);
|
||||
if (extension == null) {
|
||||
throw new IllegalArgumentException("Not a hook script: " + fileName);
|
||||
}
|
||||
return switch (extension) {
|
||||
case ".py" -> List.of(windows ? "python" : "python3", fileName);
|
||||
case ".ps1" -> List.of("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", fileName);
|
||||
case ".cmd", ".bat" -> List.of("cmd", "/c", fileName);
|
||||
default -> List.of(shell, fileName);
|
||||
};
|
||||
}
|
||||
|
||||
private static String extensionOf(String fileName, boolean windows) {
|
||||
String name = windows ? fileName.toLowerCase(Locale.ROOT) : fileName;
|
||||
for (String extension : List.of(".sh", ".py", ".ps1", ".cmd", ".bat")) {
|
||||
if (name.endsWith(extension)) {
|
||||
return extension;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+19
-19
@@ -1,33 +1,30 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public enum InitializationHook {
|
||||
|
||||
BOOT("boot", "boot",
|
||||
List.of("/etc/floci/init/boot.d"),
|
||||
List.of("/etc/localstack/init/boot.d")),
|
||||
START("startup", "start",
|
||||
List.of("/etc/floci/init/start.d"),
|
||||
List.of("/etc/localstack/init/start.d")),
|
||||
READY("ready", "ready",
|
||||
List.of("/etc/floci/init/ready.d"),
|
||||
List.of("/etc/localstack/init/ready.d")),
|
||||
STOP("shutdown", "shutdown",
|
||||
List.of("/etc/floci/init/stop.d", "/etc/floci/init/shutdown.d"),
|
||||
List.of("/etc/localstack/init/shutdown.d"));
|
||||
BOOT("boot", "boot", List.of("boot.d"), List.of("boot.d")),
|
||||
START("startup", "start", List.of("start.d"), List.of("start.d")),
|
||||
READY("ready", "ready", List.of("ready.d"), List.of("ready.d")),
|
||||
STOP("shutdown", "shutdown", List.of("stop.d", "shutdown.d"), List.of("shutdown.d"));
|
||||
|
||||
/** Default of {@code floci.init-hooks.dir}: where the image expects hook directories to be mounted. */
|
||||
public static final String DEFAULT_ROOT = "/etc/floci/init";
|
||||
static final String COMPAT_ROOT = "/etc/localstack/init";
|
||||
|
||||
private final String name;
|
||||
private final String responseKey;
|
||||
private final List<File> primaryPaths;
|
||||
private final List<String> primarySubdirs;
|
||||
private final List<File> compatPaths;
|
||||
|
||||
InitializationHook(String name, String responseKey, List<String> primaryPaths, List<String> compatPaths) {
|
||||
InitializationHook(String name, String responseKey, List<String> primarySubdirs, List<String> compatSubdirs) {
|
||||
this.name = name;
|
||||
this.responseKey = responseKey;
|
||||
this.primaryPaths = primaryPaths.stream().map(File::new).toList();
|
||||
this.compatPaths = compatPaths.stream().map(File::new).toList();
|
||||
this.primarySubdirs = primarySubdirs;
|
||||
this.compatPaths = compatSubdirs.stream().map(dir -> new File(COMPAT_ROOT, dir)).toList();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -39,9 +36,12 @@ public enum InitializationHook {
|
||||
return responseKey;
|
||||
}
|
||||
|
||||
/** Floci-native directories for this phase. First occurrence of a filename wins. */
|
||||
public List<File> getPrimaryPaths() {
|
||||
return primaryPaths;
|
||||
/**
|
||||
* Floci-native directories for this phase under {@code root} ({@code floci.init-hooks.dir}). First
|
||||
* occurrence of a filename wins.
|
||||
*/
|
||||
public List<File> getPrimaryPaths(String root) {
|
||||
return primarySubdirs.stream().map(dir -> Path.of(root, dir).toFile()).toList();
|
||||
}
|
||||
|
||||
/** LocalStack-compat directories for this phase. Only used when a filename is not already in a primary path. */
|
||||
|
||||
+43
-10
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.lifecycle.InitLifecycleState;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -12,23 +13,55 @@ import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ApplicationScoped
|
||||
public class InitializationHooksRunner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(InitializationHooksRunner.class);
|
||||
|
||||
private static final FilenameFilter SCRIPT_FILE_FILTER =
|
||||
(ignored, name) -> name.endsWith(".sh") || name.endsWith(".py");
|
||||
|
||||
private final HookScriptExecutor hookScriptExecutor;
|
||||
private final InitLifecycleState initLifecycleState;
|
||||
private final String hooksRoot;
|
||||
private final FilenameFilter scriptFileFilter;
|
||||
private final Set<String> warnedSkips = ConcurrentHashMap.newKeySet();
|
||||
|
||||
@Inject
|
||||
public InitializationHooksRunner(final HookScriptExecutor hookScriptExecutor,
|
||||
final InitLifecycleState initLifecycleState) {
|
||||
final InitLifecycleState initLifecycleState,
|
||||
final EmulatorConfig emulatorConfig) {
|
||||
this(hookScriptExecutor, initLifecycleState, emulatorConfig, System.getProperty("os.name", ""));
|
||||
}
|
||||
|
||||
InitializationHooksRunner(final HookScriptExecutor hookScriptExecutor,
|
||||
final InitLifecycleState initLifecycleState,
|
||||
final EmulatorConfig emulatorConfig,
|
||||
final String osName) {
|
||||
this.hookScriptExecutor = hookScriptExecutor;
|
||||
this.initLifecycleState = initLifecycleState;
|
||||
this.hooksRoot = emulatorConfig.initHooks().dir();
|
||||
boolean windows = HookScripts.isWindows(osName);
|
||||
String shell = emulatorConfig.initHooks().shellExecutable();
|
||||
this.scriptFileFilter = (dir, name) -> runnable(dir, name, windows, shell);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a file in a hook directory runs here. Windows script types are ignored without a word
|
||||
* on Linux and macOS; a {@code .sh} hook Windows cannot run is warned about once per file, not
|
||||
* on every directory listing.
|
||||
*/
|
||||
private boolean runnable(File dir, String name, boolean windows, String shell) {
|
||||
if (!HookScripts.isScript(name, windows)) {
|
||||
return false;
|
||||
}
|
||||
if (HookScripts.canRun(name, windows, shell)) {
|
||||
return true;
|
||||
}
|
||||
if (windows && warnedSkips.add(new File(dir, name).getAbsolutePath())) {
|
||||
LOG.warnv("Skipping hook script {0}: it cannot run on this platform (shell {1})", name, shell);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasHooks(final InitializationHook hook) {
|
||||
@@ -85,9 +118,9 @@ public class InitializationHooksRunner {
|
||||
* Merges scripts from all primary (Floci) paths and then compat (LocalStack) paths.
|
||||
* First occurrence of a filename wins; merged list is sorted lexicographically.
|
||||
*/
|
||||
private static List<File> findMergedScripts(final InitializationHook hook) {
|
||||
private List<File> findMergedScripts(final InitializationHook hook) {
|
||||
final Map<String, File> merged = new LinkedHashMap<>();
|
||||
for (final File dir : hook.getPrimaryPaths()) {
|
||||
for (final File dir : hook.getPrimaryPaths(hooksRoot)) {
|
||||
collectScripts(hook.getName(), dir, merged);
|
||||
}
|
||||
for (final File dir : hook.getCompatPaths()) {
|
||||
@@ -99,7 +132,7 @@ public class InitializationHooksRunner {
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static void collectScripts(final String hookName, final File dir,
|
||||
private void collectScripts(final String hookName, final File dir,
|
||||
final Map<String, File> target) {
|
||||
if (!dir.exists()) {
|
||||
LOG.debugv("{0} hook directory does not exist: {1}", hookName, dir.getAbsolutePath());
|
||||
@@ -109,7 +142,7 @@ public class InitializationHooksRunner {
|
||||
LOG.warnv("{0} hook path is not a directory: {1}", hookName, dir.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
final File[] scripts = dir.listFiles(SCRIPT_FILE_FILTER);
|
||||
final File[] scripts = dir.listFiles(scriptFileFilter);
|
||||
if (scripts == null || scripts.length == 0) {
|
||||
LOG.debugv("No {0} hook scripts found in {1}", hookName, dir.getAbsolutePath());
|
||||
return;
|
||||
@@ -123,7 +156,7 @@ public class InitializationHooksRunner {
|
||||
}
|
||||
}
|
||||
|
||||
private static String[] findScriptFileNames(final String hookName, final File hookDirectory) {
|
||||
private String[] findScriptFileNames(final String hookName, final File hookDirectory) {
|
||||
if (!hookDirectory.exists()) {
|
||||
LOG.debugv("{0} hook directory does not exist: {1}", hookName, hookDirectory.getAbsolutePath());
|
||||
return new String[0];
|
||||
@@ -132,7 +165,7 @@ public class InitializationHooksRunner {
|
||||
LOG.warnv("{0} hook path is not a directory: {1}", hookName, hookDirectory.getAbsolutePath());
|
||||
return new String[0];
|
||||
}
|
||||
final String[] scriptFileNames = hookDirectory.list(SCRIPT_FILE_FILTER);
|
||||
final String[] scriptFileNames = hookDirectory.list(scriptFileFilter);
|
||||
if (scriptFileNames == null || scriptFileNames.length == 0) {
|
||||
LOG.debugv("No {0} hook scripts found in {1}", hookName, hookDirectory.getAbsolutePath());
|
||||
return new String[0];
|
||||
|
||||
Reference in new issue
Block a user