Add configuration.

This commit is contained in:
Michael Lipp 2023-07-27 18:47:00 +02:00
parent 48d8e63c25
commit 7fb1cc95af
3 changed files with 89 additions and 3 deletions

View file

@ -0,0 +1,5 @@
# The values in comments are the defaults.
"/Operator":
"/Controller":
test: yes

View file

@ -19,14 +19,20 @@
package org.jdrupes.vmoperator.manager; package org.jdrupes.vmoperator.manager;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import org.jgrapes.core.Channel; import org.jgrapes.core.Channel;
import org.jgrapes.core.Component; import org.jgrapes.core.Component;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.util.events.ConfigurationUpdate;
import org.jgrapes.util.events.InitialConfiguration;
/** /**
* The application class. * The application class.
*/ */
public class Controller extends Component { public class Controller extends Component {
// private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
/** /**
* Instantiates a new manager. * Instantiates a new manager.
* *
@ -38,4 +44,49 @@ public class Controller extends Component {
attach(new VmWatcher(channel())); attach(new VmWatcher(channel()));
attach(new Reconciler(channel())); attach(new Reconciler(channel()));
} }
/**
* On configuration update.
*
* @param event the event
*/
@Handler
public void onConfigurationUpdate(ConfigurationUpdate event) {
event.structured(componentPath()).ifPresent(c -> {
if (event instanceof InitialConfiguration) {
processInitialConfiguration(c);
}
});
}
private void processInitialConfiguration(
Map<String, Object> runnerConfiguration) {
// try {
// config = mapper.convertValue(runnerConfiguration,
// Configuration.class);
// if (!config.check()) {
// // Invalid configuration, not used, problems already logged.
// config = null;
// }
//
// // Prepare firmware files and add to config
// setFirmwarePaths();
//
// // Obtain more context data from template
// var tplData = dataFromTemplate();
// swtpmDefinition = Optional.ofNullable(tplData.get("swtpm"))
// .map(d -> new CommandDefinition("swtpm", d)).orElse(null);
// qemuDefinition = Optional.ofNullable(tplData.get("qemu"))
// .map(d -> new CommandDefinition("qemu", d)).orElse(null);
//
// // Forward some values to child components
// qemuMonitor.configure(config.monitorSocket,
// config.vm.powerdownTimeout);
// } catch (IllegalArgumentException | IOException | TemplateException e) {
// logger.log(Level.SEVERE, e, () -> "Invalid configuration: "
// + e.getMessage());
// // Don't use default configuration
// config = null;
// }
}
} }

View file

@ -18,10 +18,17 @@
package org.jdrupes.vmoperator.manager; package org.jdrupes.vmoperator.manager;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.logging.LogManager; import java.util.logging.LogManager;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_NAME;
import org.jdrupes.vmoperator.util.FsdUtils; import org.jdrupes.vmoperator.util.FsdUtils;
import org.jgrapes.core.Channel; import org.jgrapes.core.Channel;
import org.jgrapes.core.Component; import org.jgrapes.core.Component;
@ -29,6 +36,9 @@ import org.jgrapes.core.Components;
import org.jgrapes.core.annotation.Handler; import org.jgrapes.core.annotation.Handler;
import org.jgrapes.core.events.Stop; import org.jgrapes.core.events.Stop;
import org.jgrapes.io.NioDispatcher; import org.jgrapes.io.NioDispatcher;
import org.jgrapes.util.FileSystemWatcher;
import org.jgrapes.util.YamlConfigurationStore;
import org.jgrapes.util.events.WatchFile;
/** /**
* The application class. * The application class.
@ -39,13 +49,26 @@ public class Operator extends Component {
/** /**
* Instantiates a new manager. * Instantiates a new manager.
* @param cmdLine
* *
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
public Operator() throws IOException { public Operator(CommandLine cmdLine) throws IOException {
// Prepare component tree // Prepare component tree
attach(new NioDispatcher()); attach(new NioDispatcher());
attach(new FileSystemWatcher(channel()));
attach(new Controller(channel())); attach(new Controller(channel()));
// Configuration store with file in /etc/opt (default)
File config = new File(cmdLine.getOptionValue('c',
"/etc/opt/" + VM_OP_NAME + "/config.yaml"));
// Don't rely on night config to produce a good exception
// for this simple case
if (!Files.isReadable(config.toPath())) {
throw new IOException("Cannot read configuration file " + config);
}
attach(new YamlConfigurationStore(channel(), config, false));
fire(new WatchFile(config.toPath()));
} }
/** /**
@ -66,7 +89,8 @@ public class Operator extends Component {
if (path.isPresent()) { if (path.isPresent()) {
props = Files.newInputStream(path.get()); props = Files.newInputStream(path.get());
} else { } else {
props = Operator.class.getResourceAsStream("logging.properties"); props
= Operator.class.getResourceAsStream("logging.properties");
} }
LogManager.getLogManager().readConfiguration(props); LogManager.getLogManager().readConfiguration(props);
} catch (IOException e) { } catch (IOException e) {
@ -82,8 +106,14 @@ public class Operator extends Component {
*/ */
@SuppressWarnings("PMD.SignatureDeclareThrowsException") @SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
// parse the command line arguments
final Options options = new Options();
options.addOption(new Option("c", "config", true, "The configu"
+ "ration file (defaults to /etc/opt/vmoperator/config.yaml)."));
CommandLine cmd = parser.parse(options, args);
// The Operator is the root component // The Operator is the root component
app = new Operator(); app = new Operator(cmd);
// Prepare Stop // Prepare Stop
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {