diff options
author | toby <smorgrav@yahoo-inc.com> | 2018-04-17 15:01:45 +0200 |
---|---|---|
committer | toby <smorgrav@yahoo-inc.com> | 2018-04-18 14:14:42 +0200 |
commit | 3176670169311418a858294b8e5d6dee3d062419 (patch) | |
tree | 97a2933df37cf065d0593264d387d87645a1ceea /node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java | |
parent | 6f501a0670afea481eae3ef648af67079a4306cf (diff) |
Implement general iptables editor support to enable redirect to live side by side with dokcer dns rules
Diffstat (limited to 'node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java index 83954551905..3d4dbcca0e6 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java @@ -9,6 +9,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; import java.util.logging.Logger; import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; @@ -24,20 +26,36 @@ public class Editor { private static int maxLength = 300; - private final Path path; + private final Supplier<List<String>> supplier; + private final Consumer<List<String>> consumer; + private final String name; private final LineEditor editor; public Editor(Path path, LineEditor editor) { - this.path = path; - this.editor = editor; + this(path.toString(), + () -> uncheck(() -> Files.readAllLines(path, ENCODING)), + (newLines) -> uncheck(() -> Files.write(path, newLines, ENCODING)), + editor); } /** - * Read the file which must be encoded in UTF-8, use the LineEditor to edit it, - * and any modifications were done write it back and return true. + * @param name The name of what is being edited - used in logging + * @param supplier Supplies the editor with a list of lines to edit + * @param consumer Consumes the lines to presist if any changes is detected + * @param editor The line operations to execute on the lines supplied */ - public boolean converge(TaskContext context) { - List<String> lines = uncheck(() -> Files.readAllLines(path, ENCODING)); + public Editor(String name, + Supplier<List<String>> supplier, + Consumer<List<String>> consumer, + LineEditor editor) { + this.supplier = supplier; + this.consumer = consumer; + this.name = name; + this.editor = editor; + } + + public boolean edit(Consumer<String> logConsumer) { + List<String> lines = supplier.get(); List<String> newLines = new ArrayList<>(); StringBuilder diff = new StringBuilder(); boolean modified = false; @@ -80,11 +98,19 @@ public class Editor { } String diffDescription = diffTooLarge(diff) ? "" : ":\n" + diff.toString(); - context.recordSystemModification(logger, "Patching file " + path + diffDescription); - uncheck(() -> Files.write(path, newLines, ENCODING)); + logConsumer.accept("Patching " + name + diffDescription); + consumer.accept(newLines); return true; } + /** + * Read the file which must be encoded in UTF-8, use the LineEditor to edit it, + * and any modifications were done write it back and return true. + */ + public boolean converge(TaskContext context) { + return this.edit(line -> context.recordSystemModification(logger, line)); + } + private static void maybeAdd(StringBuilder diff, List<String> lines) { for (String line : lines) { if (!diffTooLarge(diff)) { |