Skip to content

Commit

Permalink
Advertise all env variables in strongly suggested section and env file
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdenise committed Oct 20, 2023
1 parent a3688e7 commit 0d712f3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 16 deletions.
25 changes: 23 additions & 2 deletions cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.wildfly.glow.OutputFormat.SERVER;
import org.wildfly.glow.ScanArguments.Builder;
import org.wildfly.glow.ScanResults;
import org.wildfly.glow.error.IdentifiedError;
import org.wildfly.glow.maven.MavenResolver;

import picocli.CommandLine;
Expand All @@ -49,6 +50,9 @@
)
public class ScanCommand extends AbstractCommand {

private static final String ADD_ADD_ONS_MSG="@|bold To enable add-ons add the|@ @|fg(yellow) " +
Constants.ADD_ONS_OPTION + "=<list of add-ons>|@ @|bold option to the|@ @|fg(yellow) " +
Constants.SCAN_COMMAND + "|@ @|bold command|@";
@CommandLine.Spec
protected CommandLine.Model.CommandSpec spec;

Expand Down Expand Up @@ -145,13 +149,30 @@ public Integer call() throws Exception {
if (!compact) {
if (suggest.orElse(false)) {
if (!scanResults.getSuggestions().getPossibleAddOns().isEmpty() && addOns.isEmpty()) {
System.out.println(CommandLine.Help.Ansi.AUTO.string("@|bold To enable add-ons add the|@ @|fg(yellow) " + Constants.ADD_ONS_OPTION + "=<list of add-ons>|@ @|bold option to the|@ @|fg(yellow) " + Constants.SCAN_COMMAND + "|@ @|bold command|@"));
System.out.println(CommandLine.Help.Ansi.AUTO.string(ADD_ADD_ONS_MSG));
}
if (!scanResults.getSuggestions().getPossibleProfiles().isEmpty()) {
System.out.println(CommandLine.Help.Ansi.AUTO.string("@|bold To enable the HA profile add the|@ @|fg(yellow) " + Constants.HA_OPTION + "|@ @|bold option to the|@ @|fg(yellow) " + Constants.SCAN_COMMAND + "|@ @|bold command|@"));
}
}
System.out.println(CommandLine.Help.Ansi.AUTO.string("@|bold To provision the WildFly server for your deployment add the|@ @|fg(yellow) " + Constants.PROVISION_OPTION + "=SERVER|@ @|bold option to the|@ @|fg(yellow) " + Constants.SCAN_COMMAND + "|@ @|bold command|@"));
if (scanResults.getErrorSession().hasErrors()) {
if (!suggest.orElse(false)) {
boolean hasAddOn = false;
// Do we have errors and add-ons to set?
for(IdentifiedError err : scanResults.getErrorSession().getErrors()) {
if (!err.getPossibleAddons().isEmpty()) {
hasAddOn = true;
break;
}
}
if (hasAddOn) {
System.out.println(CommandLine.Help.Ansi.AUTO.string(ADD_ADD_ONS_MSG));
}
}
System.out.println(CommandLine.Help.Ansi.AUTO.string("@|bold Some errors have been reported, you should fix them prior to provision a server with the|@ @|fg(yellow) " + Constants.PROVISION_OPTION + "=SERVER|@ @|bold option of the|@ @|fg(yellow) " + Constants.SCAN_COMMAND + "|@ @|bold command|@"));
} else {
System.out.println(CommandLine.Help.Ansi.AUTO.string("@|bold To provision the WildFly server for your deployment add the|@ @|fg(yellow) " + Constants.PROVISION_OPTION + "=SERVER|@ @|bold option to the|@ @|fg(yellow) " + Constants.SCAN_COMMAND + "|@ @|bold command|@"));
}
}
} else {
System.out.print("\n");
Expand Down
26 changes: 22 additions & 4 deletions core/src/main/java/org/wildfly/glow/GlowSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,15 @@ public ScanResults scan() throws Exception {
}
// END cleanup

errorSession.refreshErrors(allBaseLayers, mapping, allEnabledAddOns);
Map<Layer, Set<Env>> stronglySuggestConfigFixes = errorSession.refreshErrors(allBaseLayers, mapping, allEnabledAddOns);
for(Layer l : stronglySuggestConfigFixes.keySet()) {
Set<Env> envs = stronglySuggestedConfigurations.get(l);
if(envs == null) {
envs = new TreeSet<>();
stronglySuggestedConfigurations.put(l, envs);
}
envs.addAll(stronglySuggestConfigFixes.get(l));
}
// Identify the active feature-packs.
ProvisioningConfig activeConfig = buildProvisioningConfig(pConfig, layout,
universeResolver, allBaseLayers, baseLayer, decorators, excludedLayers, fpDependencies, arguments.getConfigName());
Expand Down Expand Up @@ -466,9 +474,16 @@ Path outputConfig(ScanResults scanResults, Path target, String dockerImageName)
if (OutputFormat.DOCKER_IMAGE.equals(arguments.getOutput())) {
// generate docker image
String imageName = dockerImageName == null ? DockerSupport.getImageName(generatedArtifact.getFileName().toString()) : dockerImageName;
ret = DockerSupport.buildApplicationImage(imageName, generatedArtifact, arguments, writer);
Path origDockerFile = DockerSupport.buildApplicationImage(imageName, generatedArtifact, arguments, writer);
IoUtils.recursiveDelete(generatedArtifact);
Files.createDirectories(target);
Path dockerFile = target.resolve("Dockerfile");
Files.copy(origDockerFile, dockerFile);
Files.delete(origDockerFile);
ret = dockerFile;
} else {
ret = generatedArtifact;
}
IoUtils.recursiveDelete(generatedArtifact);
} else {
if (OutputFormat.PROVISIONING_XML.equals(arguments.getOutput())) {
IoUtils.recursiveDelete(target);
Expand All @@ -494,8 +509,11 @@ Path outputConfig(ScanResults scanResults, Path target, String dockerImageName)
getSuggestedConfigurations(), false)).append(System.lineSeparator());
}
if (envFileContent.length() != 0) {
if (!Files.exists(target)) {
Files.createDirectories(target);
}
Path p = target.resolve("configuration.env");
writer.info("The file " + p + " contains the environment variables to set in a " + arguments.getExecutionContext() + " context");
writer.info("The file " + p + " contains the environment variables and the instructions to set them when starting the server.");
Files.write(p, envFileContent.toString().getBytes());
}
return ret;
Expand Down
17 changes: 11 additions & 6 deletions core/src/main/java/org/wildfly/glow/ScanResultsPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -166,12 +167,13 @@ private void detailed(ScanArguments arguments, ScanResults scanResults) throws E
for(Map.Entry<Layer, Set<Env>> entry : scanResults.getSuggestions().getStronglySuggestedConfigurations().entrySet()) {
writer.warn(buildSuggestions(entry.getKey(), entry.getValue()));
}
writer.warn("");
}

writer.info("suggestions");
String suggestedConfigs = buildSuggestions(scanResults.getSuggestions().getSuggestedConfigurations());

if (arguments.isSuggest()) {
writer.info("suggestions");
if (scanResults.getSuggestions().getPossibleAddOns().isEmpty() && scanResults.getSuggestions().getPossibleProfiles().isEmpty() && suggestedConfigs.isEmpty()) {
writer.info("none");
} else {
Expand Down Expand Up @@ -211,9 +213,7 @@ private void detailed(ScanArguments arguments, ScanResults scanResults) throws E
}
} else {
if (!scanResults.getSuggestions().getPossibleAddOns().isEmpty() || !scanResults.getSuggestions().getPossibleAddOns().isEmpty() || !suggestedConfigs.isEmpty()) {
writer.info("some suggestions have been found. You could enable suggestions with --suggest option.");
} else {
writer.info("none");
writer.info("Some suggestions have been found. You could enable suggestions with --suggest option.");
}
}
}
Expand All @@ -229,8 +229,13 @@ private static String buildSuggestions(Map<Layer, Set<Env>> map) throws URISynta
private static String buildSuggestions(Layer layer, Set<Env> envs) throws URISyntaxException, IOException {
StringBuilder suggestedConfigsBuilder = new StringBuilder();
suggestedConfigsBuilder.append("\n").append(layer.getName()).append(":\n");
for (Env e : envs) {
suggestedConfigsBuilder.append(" - ").append(e.getName()).append("=").append(e.getDescription()).append("\n");
Iterator<Env> it = envs.iterator();
while (it.hasNext()) {
Env e = it.next();
suggestedConfigsBuilder.append(" - ").append(e.getName()).append("=").append(e.getDescription());
if (it.hasNext()) {
suggestedConfigsBuilder.append("\n");
}
}
return suggestedConfigsBuilder.toString();
}}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import org.wildfly.glow.Env;

import static org.wildfly.glow.Utils.getAddOnFix;

Expand Down Expand Up @@ -122,9 +123,10 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
}

@Override
public void refreshErrors(Set<Layer> allBaseLayers) throws Exception {
public Map<Layer, Set<Env>> refreshErrors(Set<Layer> allBaseLayers) throws Exception {
Set<IdentifiedError> unboundDatasourcesErrors = errors.get(UNBOUND_DATASOURCES_ERROR);
Set<String> toRemove = new HashSet<>();
Map<Layer, Set<Env>> ret = new HashMap<>();
if (unboundDatasourcesErrors != null) {
for (IdentifiedError error : unboundDatasourcesErrors) {
UnboundDatasourceError uds = (UnboundDatasourceError) error;
Expand All @@ -143,6 +145,14 @@ public void refreshErrors(Set<Layer> allBaseLayers) throws Exception {
if (content != null) {
content = content.replaceAll("##ITEM##", uds.unboundDatasource);
}
if(fix.isEnv()) {
Set<Env> envs = ret.get(l);
if(envs == null) {
envs = new HashSet<>();
ret.put(l, envs);
}
envs.add(new Env(fix.getEnvName(), Fix.getEnvValue(content), false, true));
}
}
String errorMessage = getAddOnFix(l.getAddOn(), content);
error.setFixed(errorMessage);
Expand All @@ -167,6 +177,14 @@ public void refreshErrors(Set<Layer> allBaseLayers) throws Exception {
Fix fix = l.getAddOn().getFixes().get(error.getId());
if (fix != null) {
String content = fix.getContent();
if (fix.isEnv()) {
Set<Env> envs = ret.get(l);
if (envs == null) {
envs = new HashSet<>();
ret.put(l, envs);
}
envs.add(new Env(fix.getEnvName(), Fix.getEnvValue(content), false, true));
}
String errorMessage = getAddOnFix(l.getAddOn(), content);
error.setFixed(errorMessage);
}
Expand All @@ -175,6 +193,7 @@ public void refreshErrors(Set<Layer> allBaseLayers) throws Exception {
}
}
}
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.wildfly.glow.Env;

/**
*
* @author jdenise
*/
public interface ErrorIdentification {
void collectErrors(Path rootPath) throws Exception;
void refreshErrors(Set<Layer> allBaseLayers) throws Exception;
Map<Layer, Set<Env>> refreshErrors(Set<Layer> allBaseLayers) throws Exception;
List<IdentifiedError> getErrors();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.wildfly.glow.Env;

/**
*
Expand Down Expand Up @@ -55,8 +56,8 @@ public void collectEndOfScanErrors(
jndiErrorIdentification.collectErrors(verbose, resourceInjectionInfos, initialContextLookupInfos, allClasses);
}

public void refreshErrors(Set<Layer> allBaseLayers, LayerMapping mapping, Set<AddOn> enabledAddOns) throws Exception {
ds.refreshErrors(allBaseLayers);
public Map<Layer, Set<Env>> refreshErrors(Set<Layer> allBaseLayers, LayerMapping mapping, Set<AddOn> enabledAddOns) throws Exception {
Map<Layer, Set<Env>> stronglySuggested = ds.refreshErrors(allBaseLayers);
// We could have an Enabbled addOn
for (IdentifiedError error : getErrors()) {
if (!error.isFixed()) {
Expand All @@ -66,6 +67,7 @@ public void refreshErrors(Set<Layer> allBaseLayers, LayerMapping mapping, Set<Ad
}
}
}
return stronglySuggested;
}

public List<IdentifiedError> getErrors() {
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/wildfly/glow/error/Fix.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public String getDescription() {
return description;
}

public boolean isEnv() {
return description.endsWith(" env");
}

public String getEnvName() {
String[] split = content.split("=");
return split[0];
}

/**
* @return the content
*/
Expand All @@ -85,4 +94,9 @@ public String getContent() {
public String getForId() {
return forId;
}

public static String getEnvValue(String value) {
String[] split = value.split("=");
return split[1];
}
}

0 comments on commit 0d712f3

Please sign in to comment.