|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.transport; |
| 11 | + |
| 12 | +import org.gradle.api.file.DirectoryProperty; |
| 13 | +import org.gradle.api.services.BuildService; |
| 14 | +import org.gradle.api.services.BuildServiceParameters; |
| 15 | +import org.gradle.process.ExecOperations; |
| 16 | +import org.gradle.process.ExecResult; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | +import java.util.function.BiFunction; |
| 32 | + |
| 33 | +import javax.inject.Inject; |
| 34 | + |
| 35 | +/** |
| 36 | + * An encapsulation of operations on transport version resources. |
| 37 | + * |
| 38 | + * <p>These are resource files to describe transport versions that will be loaded at Elasticsearch runtime. They exist |
| 39 | + * as jar resource files at runtime, and as a directory of resources at build time. |
| 40 | + * |
| 41 | + * <p>The layout of the transport version resources are as follows: |
| 42 | + * <ul> |
| 43 | + * <li><b>/transport/definitions/named/</b> |
| 44 | + * - Definitions that can be looked up by name. The name is the filename before the .csv suffix.</li> |
| 45 | + * <li><b>/transport/definitions/unreferenced/</b> |
| 46 | + * - Definitions which contain ids that are known at runtime, but cannot be looked up by name.</li> |
| 47 | + * <li><b>/transport/latest/</b> |
| 48 | + * - The latest transport version definition for each release branch.</li> |
| 49 | + * </ul> |
| 50 | + */ |
| 51 | +public abstract class TransportVersionResourcesService implements BuildService<TransportVersionResourcesService.Parameters> { |
| 52 | + |
| 53 | + public interface Parameters extends BuildServiceParameters { |
| 54 | + DirectoryProperty getTransportResourcesDirectory(); |
| 55 | + |
| 56 | + DirectoryProperty getRootDirectory(); |
| 57 | + } |
| 58 | + |
| 59 | + @Inject |
| 60 | + public abstract ExecOperations getExecOperations(); |
| 61 | + |
| 62 | + private static final Path DEFINITIONS_DIR = Path.of("definitions"); |
| 63 | + private static final Path NAMED_DIR = DEFINITIONS_DIR.resolve("named"); |
| 64 | + private static final Path UNREFERENCED_DIR = DEFINITIONS_DIR.resolve("unreferenced"); |
| 65 | + private static final Path LATEST_DIR = Path.of("latest"); |
| 66 | + |
| 67 | + private final Path transportResourcesDir; |
| 68 | + private final Path rootDir; |
| 69 | + private final AtomicReference<Set<String>> mainResources = new AtomicReference<>(null); |
| 70 | + private final AtomicReference<Set<String>> changedResources = new AtomicReference<>(null); |
| 71 | + |
| 72 | + @Inject |
| 73 | + public TransportVersionResourcesService(Parameters params) { |
| 74 | + this.transportResourcesDir = params.getTransportResourcesDirectory().get().getAsFile().toPath(); |
| 75 | + this.rootDir = params.getRootDirectory().get().getAsFile().toPath(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Return the directory for this repository which contains transport version resources. |
| 80 | + * This should be an input to any tasks reading resources from this service. |
| 81 | + */ |
| 82 | + Path getTransportResourcesDir() { |
| 83 | + return transportResourcesDir; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return the transport version definitions directory for this repository. |
| 88 | + * This should be an input to any tasks that only read definitions from this service. |
| 89 | + */ |
| 90 | + Path getDefinitionsDir() { |
| 91 | + return transportResourcesDir.resolve(DEFINITIONS_DIR); |
| 92 | + } |
| 93 | + |
| 94 | + // return the path, relative to the resources dir, of a named definition |
| 95 | + private Path getNamedDefinitionRelativePath(String name) { |
| 96 | + return NAMED_DIR.resolve(name + ".csv"); |
| 97 | + } |
| 98 | + |
| 99 | + /** Return all named definitions, mapped by their name. */ |
| 100 | + Map<String, TransportVersionDefinition> getNamedDefinitions() throws IOException { |
| 101 | + Map<String, TransportVersionDefinition> definitions = new HashMap<>(); |
| 102 | + // temporarily include unreferenced in named until validation understands the distinction |
| 103 | + for (var dir : List.of(NAMED_DIR, UNREFERENCED_DIR)) { |
| 104 | + Path path = transportResourcesDir.resolve(dir); |
| 105 | + if (Files.isDirectory(path) == false) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + try (var definitionsStream = Files.list(path)) { |
| 109 | + for (var definitionFile : definitionsStream.toList()) { |
| 110 | + String contents = Files.readString(definitionFile, StandardCharsets.UTF_8).strip(); |
| 111 | + var definition = TransportVersionDefinition.fromString(definitionFile.getFileName().toString(), contents); |
| 112 | + definitions.put(definition.name(), definition); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return definitions; |
| 117 | + } |
| 118 | + |
| 119 | + /** Test whether the given named definition exists */ |
| 120 | + TransportVersionDefinition getNamedDefinitionFromMain(String name) { |
| 121 | + String resourcePath = getNamedDefinitionRelativePath(name).toString(); |
| 122 | + return getMainFile(resourcePath, TransportVersionDefinition::fromString); |
| 123 | + } |
| 124 | + |
| 125 | + /** Test whether the given named definition exists */ |
| 126 | + boolean namedDefinitionExists(String name) { |
| 127 | + return Files.exists(transportResourcesDir.resolve(getNamedDefinitionRelativePath(name))); |
| 128 | + } |
| 129 | + |
| 130 | + /** Return the path within the repository of the given named definition */ |
| 131 | + Path getRepositoryPath(TransportVersionDefinition definition) { |
| 132 | + return rootDir.relativize(transportResourcesDir.resolve(getNamedDefinitionRelativePath(definition.name()))); |
| 133 | + } |
| 134 | + |
| 135 | + /** Read all latest files and return them mapped by their release branch */ |
| 136 | + Map<String, TransportVersionLatest> getLatestByReleaseBranch() throws IOException { |
| 137 | + Map<String, TransportVersionLatest> latests = new HashMap<>(); |
| 138 | + try (var stream = Files.list(transportResourcesDir.resolve(LATEST_DIR))) { |
| 139 | + for (var latestFile : stream.toList()) { |
| 140 | + String contents = Files.readString(latestFile, StandardCharsets.UTF_8).strip(); |
| 141 | + var latest = TransportVersionLatest.fromString(latestFile.getFileName().toString(), contents); |
| 142 | + latests.put(latest.name(), latest); |
| 143 | + } |
| 144 | + } |
| 145 | + return latests; |
| 146 | + } |
| 147 | + |
| 148 | + /** Retrieve the latest transport version for the given release branch on main */ |
| 149 | + TransportVersionLatest getLatestFromMain(String releaseBranch) { |
| 150 | + String resourcePath = getLatestRelativePath(releaseBranch).toString(); |
| 151 | + return getMainFile(resourcePath, TransportVersionLatest::fromString); |
| 152 | + } |
| 153 | + |
| 154 | + /** Return the path within the repository of the given latest */ |
| 155 | + Path getRepositoryPath(TransportVersionLatest latest) { |
| 156 | + return rootDir.relativize(transportResourcesDir.resolve(getLatestRelativePath(latest.branch()))); |
| 157 | + } |
| 158 | + |
| 159 | + private Path getLatestRelativePath(String releaseBranch) { |
| 160 | + return LATEST_DIR.resolve(releaseBranch + ".csv"); |
| 161 | + } |
| 162 | + |
| 163 | + // Return the transport version resources paths that exist in main |
| 164 | + private Set<String> getMainResources() { |
| 165 | + if (mainResources.get() == null) { |
| 166 | + synchronized (mainResources) { |
| 167 | + String output = gitCommand("ls-tree", "--name-only", "-r", "main", "."); |
| 168 | + |
| 169 | + HashSet<String> resources = new HashSet<>(); |
| 170 | + Collections.addAll(resources, output.split(System.lineSeparator())); |
| 171 | + mainResources.set(resources); |
| 172 | + } |
| 173 | + } |
| 174 | + return mainResources.get(); |
| 175 | + } |
| 176 | + |
| 177 | + // Return the transport version resources paths that have been changed relative to main |
| 178 | + private Set<String> getChangedResources() { |
| 179 | + if (changedResources.get() == null) { |
| 180 | + synchronized (changedResources) { |
| 181 | + String output = gitCommand("diff", "--name-only", "main", "."); |
| 182 | + |
| 183 | + HashSet<String> resources = new HashSet<>(); |
| 184 | + Collections.addAll(resources, output.split(System.lineSeparator())); |
| 185 | + changedResources.set(resources); |
| 186 | + } |
| 187 | + } |
| 188 | + return changedResources.get(); |
| 189 | + } |
| 190 | + |
| 191 | + // Read a transport version resource from the main branch, or return null if it doesn't exist on main |
| 192 | + private <T> T getMainFile(String resourcePath, BiFunction<String, String, T> parser) { |
| 193 | + if (getMainResources().contains(resourcePath) == false) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + String content = gitCommand("show", "main:./" + resourcePath).strip(); |
| 197 | + return parser.apply(resourcePath, content); |
| 198 | + } |
| 199 | + |
| 200 | + // run a git command, relative to the transport version resources directory |
| 201 | + private String gitCommand(String... args) { |
| 202 | + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); |
| 203 | + |
| 204 | + List<String> command = new ArrayList<>(); |
| 205 | + Collections.addAll(command, "git", "-C", getTransportResourcesDir().toString()); |
| 206 | + Collections.addAll(command, args); |
| 207 | + |
| 208 | + ExecResult result = getExecOperations().exec(spec -> { |
| 209 | + spec.setCommandLine(command); |
| 210 | + spec.setStandardOutput(stdout); |
| 211 | + spec.setErrorOutput(stdout); |
| 212 | + spec.setIgnoreExitValue(true); |
| 213 | + }); |
| 214 | + |
| 215 | + if (result.getExitValue() != 0) { |
| 216 | + throw new RuntimeException( |
| 217 | + "git command failed with exit code " |
| 218 | + + result.getExitValue() |
| 219 | + + System.lineSeparator() |
| 220 | + + "command: " |
| 221 | + + String.join(" ", command) |
| 222 | + + System.lineSeparator() |
| 223 | + + "output:" |
| 224 | + + System.lineSeparator() |
| 225 | + + stdout.toString(StandardCharsets.UTF_8) |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | + return stdout.toString(StandardCharsets.UTF_8); |
| 230 | + } |
| 231 | +} |
0 commit comments