Skip to content

Commit

Permalink
Introduce env resolver that make it clear that env variables are auto…
Browse files Browse the repository at this point in the history
…matically set
  • Loading branch information
Jean Francois Denise committed Apr 4, 2024
1 parent 47f4cfa commit 4061d29
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 37 deletions.
19 changes: 15 additions & 4 deletions cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@

import static org.wildfly.glow.Arguments.CLOUD_EXECUTION_CONTEXT;
import static org.wildfly.glow.Arguments.COMPACT_PROPERTY;
import org.wildfly.glow.ConfigurationResolver;
import org.wildfly.glow.Env;
import org.wildfly.glow.Layer;
import static org.wildfly.glow.OutputFormat.BOOTABLE_JAR;
import static org.wildfly.glow.OutputFormat.DOCKER_IMAGE;
import static org.wildfly.glow.OutputFormat.OPENSHIFT;
Expand Down Expand Up @@ -311,7 +313,16 @@ public Integer call() throws Exception {
builder.setIsCli(true);
MavenRepoManager directMavenResolver = MavenResolver.newMavenResolver();
ScanResults scanResults = GlowSession.scan(repoManager == null ? directMavenResolver : repoManager, builder.build(), GlowMessageWriter.DEFAULT);
scanResults.outputInformation();
ConfigurationResolver configurationResolver = new ConfigurationResolver() {
@Override
public ResolvedEnvs getResolvedEnvs(Layer layer, Set<Env> input) throws Exception {
if(provision.get().equals(OPENSHIFT)) {
return OpenShiftSupport.getResolvedEnvs(layer, input, disableDeployers);
}
return null;
}
};
scanResults.outputInformation(configurationResolver);
if (provision.isEmpty()) {
if (!compact) {
if (suggest.orElse(false)) {
Expand Down Expand Up @@ -431,13 +442,13 @@ public Integer call() throws Exception {
}
}
if (OutputFormat.OPENSHIFT.equals(provision.get())) {
String name = null;
String name = "";
Path deploymentsDir = target.resolve("deployments");
Files.createDirectories(deploymentsDir);
for (Path p : deployments) {
Files.copy(p, deploymentsDir.resolve(p.getFileName()));
int ext = p.getFileName().toString().indexOf(".");
name = p.getFileName().toString().substring(0, ext);
name += p.getFileName().toString().substring(0, ext);
}
Map<String, String> envMap = new HashMap<>();
for (Set<Env> envs : scanResults.getSuggestions().getStronglySuggestedConfigurations().values()) {
Expand All @@ -446,7 +457,7 @@ public Integer call() throws Exception {
}
}
OpenShiftSupport.deploy(GlowMessageWriter.DEFAULT,
target, name == null ? "app-from-glow" : name.toLowerCase(),
target, name.isEmpty() ? "app-from-glow" : name.toLowerCase(),
envMap,
scanResults.getDiscoveredLayers(),
scanResults.getMetadataOnlyLayers(),
Expand Down
49 changes: 49 additions & 0 deletions core/src/main/java/org/wildfly/glow/ConfigurationResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow;

import java.util.Set;

/**
*
* @author jdenise
*/
public interface ConfigurationResolver {
public class ResolvedEnvs {
private final String name;
private final Set<Env> envs;
public ResolvedEnvs(String name, Set<Env> envs) {
this.name = name;
this.envs = envs;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @return the envs
*/
public Set<Env> getEnvs() {
return envs;
}
}
ResolvedEnvs getResolvedEnvs(Layer layer, Set<Env> input) throws Exception;
}
12 changes: 10 additions & 2 deletions core/src/main/java/org/wildfly/glow/ScanResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ public OutputContent outputConfig(Path target, String dockerImageName) throws Ex
}

public void outputInformation() throws Exception {
outputInformation(GlowMessageWriter.DEFAULT);
outputInformation(GlowMessageWriter.DEFAULT, null);
}

public void outputCompactInformation() throws Exception {
outputCompactInformation(GlowMessageWriter.DEFAULT);
}

public void outputInformation(ConfigurationResolver resolver) throws Exception {
outputInformation(GlowMessageWriter.DEFAULT, resolver);
}

public void outputInformation(GlowMessageWriter writer) throws Exception {
ScanResultsPrinter printer = new ScanResultsPrinter(writer);
outputInformation(writer, null);
}

public void outputInformation(GlowMessageWriter writer, ConfigurationResolver resolver) throws Exception {
ScanResultsPrinter printer = new ScanResultsPrinter(writer, resolver);
glowSession.outputInformation(printer, this);
}

Expand Down
27 changes: 23 additions & 4 deletions core/src/main/java/org/wildfly/glow/ScanResultsPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.wildfly.glow.error.ErrorLevel;
import org.wildfly.glow.error.IdentifiedError;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -31,14 +29,20 @@
import java.util.TreeSet;
import org.jboss.galleon.api.config.GalleonFeaturePackConfig;
import org.jboss.galleon.universe.FeaturePackLocation.FPID;
import org.wildfly.glow.ConfigurationResolver.ResolvedEnvs;

public class ScanResultsPrinter {

private final GlowMessageWriter writer;
private final ConfigurationResolver configResolver;

public ScanResultsPrinter(GlowMessageWriter writer) {
this(writer, null);
}

public ScanResultsPrinter(GlowMessageWriter writer, ConfigurationResolver configResolver) {
this.writer = writer;
this.configResolver = configResolver;
}

void print(ScanArguments arguments, ScanResults scanResults) throws Exception {
Expand Down Expand Up @@ -284,15 +288,15 @@ private void detailed(ScanArguments arguments, ScanResults scanResults) throws E
}
}

private static String buildSuggestions(Map<Layer, Set<Env>> map) throws URISyntaxException, IOException {
private String buildSuggestions(Map<Layer, Set<Env>> map) throws Exception {
StringBuilder suggestedConfigsBuilder = new StringBuilder();
for (Layer l : map.keySet()) {
suggestedConfigsBuilder.append(buildSuggestions(l, map.get(l)));
}
return suggestedConfigsBuilder.toString();
}

private static String buildSuggestions(Layer layer, Set<Env> envs) throws URISyntaxException, IOException {
private String buildSuggestions(Layer layer, Set<Env> envs) throws Exception {
StringBuilder suggestedConfigsBuilder = new StringBuilder();
Set<Env> envVars = new TreeSet<>();
Set<Env> properties = new TreeSet<>();
Expand All @@ -307,6 +311,21 @@ private static String buildSuggestions(Layer layer, Set<Env> envs) throws URISyn
}
if (!envVars.isEmpty()) {
suggestedConfigsBuilder.append("\n").append(layer.getName()).append(" environment variables:\n");
if (configResolver != null) {
ResolvedEnvs resolvedEnvs = configResolver.getResolvedEnvs(layer, envVars);
if (resolvedEnvs != null) {
envVars.removeAll(resolvedEnvs.getEnvs());
if (envVars.isEmpty()) {
suggestedConfigsBuilder.append(" - ").append("Resolver " + resolvedEnvs.getName()).append(" resolved all env variables.");
} else {
suggestedConfigsBuilder.append(" - ").append("Resolver " + resolvedEnvs.getName()).append(" resolved the following env variables:\n");
for (Env env : resolvedEnvs.getEnvs()) {
suggestedConfigsBuilder.append(" - ").append(env.getName() + "\n");

}
}
}
}
Iterator<Env> it2 = envVars.iterator();
while (it2.hasNext()) {
Env e = it2.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.wildfly.glow.Env;
import org.wildfly.glow.GlowMessageWriter;
import org.wildfly.glow.deployment.openshift.api.Deployer;
import org.wildfly.glow.deployment.openshift.api.Utils;
Expand All @@ -56,14 +57,26 @@ public class AMQDeployer implements Deployer {
private static final String AMQ_PASSWORD_ENV = "AMQ_PASSWORD";
private static final String BROKER_AMQ_USERNAME_ENV = "BROKER_AMQ_USERNAME";
private static final String BROKER_AMQ_PASSWORD_ENV = "BROKER_AMQ_PASSWORD";
private static final String MQ_SERVICE_PREFIX_MAPPING_ENV = "MQ_SERVICE_PREFIX_MAPPING";
private static final String TEMPLATE_PASSWORD_ENV = "{PREFIX}_PASSWORD";
private static final String TEMPLATE_USERNAME_ENV = "{PREFIX}_USERNAME";
private static final String TEMPLATE_SERVICE_HOST="{SERVICE-NAME}_AMQ7_TCP_SERVICE_HOST";
private static final String TEMPLATE_SERVICE_PORT="{SERVICE-NAME}_AMQ7_TCP_SERVICE_PORT";

private static final Set<String> RESOLVED_ENVS = new HashSet<>();
static {

RESOLVED_ENVS.add(MQ_SERVICE_PREFIX_MAPPING_ENV);
RESOLVED_ENVS.add(TEMPLATE_PASSWORD_ENV);
RESOLVED_ENVS.add(TEMPLATE_USERNAME_ENV);
RESOLVED_ENVS.add(TEMPLATE_SERVICE_HOST);
RESOLVED_ENVS.add(TEMPLATE_SERVICE_PORT);

REMOTE_BROKER_CONNECTION_MAP.put(AMQ_USER_ENV, REMOTE_BROKER_USER);
REMOTE_BROKER_CONNECTION_MAP.put(AMQ_PASSWORD_ENV, REMOTE_BROKER_PASSWORD);
REMOTE_BROKER_CONNECTION_MAP.put("AMQ_DATA_DIR", "/home/jboss/data");

REMOTE_BROKER_APP_MAP.put("MQ_SERVICE_PREFIX_MAPPING", "broker-amq7=BROKER_AMQ");
REMOTE_BROKER_APP_MAP.put("MQ_SERVICE_PREFIX_MAPPING", "broker-amq7=BROKER_AMQ");
REMOTE_BROKER_APP_MAP.put(MQ_SERVICE_PREFIX_MAPPING_ENV, "broker-amq7=BROKER_AMQ");
REMOTE_BROKER_APP_MAP.put("BROKER_AMQ_TCP_SERVICE_HOST", REMOTE_BROKER_NAME);
REMOTE_BROKER_APP_MAP.put("BROKER_AMQ_TCP_SERVICE_PORT", "61616");
REMOTE_BROKER_APP_MAP.put(BROKER_AMQ_PASSWORD_ENV, REMOTE_BROKER_PASSWORD);
Expand All @@ -73,7 +86,7 @@ public class AMQDeployer implements Deployer {
@Override
public Map<String, String> deploy(GlowMessageWriter writer, Path target, OpenShiftClient osClient,
Map<String, String> env, String appHost, String appName, String matching, Map<String, String> extraEnv) throws Exception {
writer.info("\nDeploying AMQ Messaging Broker");
writer.info("Deploying AMQ Messaging Broker");
Map<String, String> labels = new HashMap<>();
labels.put(LABEL, REMOTE_BROKER_NAME);
ContainerPort port = new ContainerPort();
Expand Down Expand Up @@ -117,8 +130,7 @@ public Map<String, String> deploy(GlowMessageWriter writer, Path target, OpenShi
withTargetPort(v).build()).withType("ClusterIP").withSessionAffinity("None").withSelector(labels).endSpec().build();
osClient.services().resource(service).createOr(NonDeletingOperation::update);
Utils.persistResource(target, service, REMOTE_BROKER_NAME + "-service.yaml");
Map<String, String> ret = new HashMap<>();
ret.putAll(REMOTE_BROKER_APP_MAP);
writer.info("AMQ Messaging Broker has been deployed");
return REMOTE_BROKER_APP_MAP;
}

Expand Down Expand Up @@ -146,6 +158,17 @@ public Map<String, String> disabledDeploy(String appHost, String appName, String
return ret;
}

@Override
public Set<Env> getResolvedEnvs(Set<Env> input) {
Set<Env> envs = new HashSet<>();
for (Env env : input) {
if (RESOLVED_ENVS.contains(env.getName())) {
envs.add(env);
}
}
return envs;
}

@Override
public Set<String> getSupportedLayers() {
Set<String> ret = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Set;
import java.util.TreeMap;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.glow.Env;
import org.wildfly.glow.GlowMessageWriter;

/**
Expand Down Expand Up @@ -103,6 +104,7 @@ private Map<String, String> getExistingEnv(Map<String, String> env) {
@Override
public Map<String, String> deploy(GlowMessageWriter writer, Path target, OpenShiftClient osClient,
Map<String, String> env, String appHost, String appName, String matching, Map<String, String> extraEnv) throws Exception {
writer.info("Deploying " + dbName);
Map<String, String> labels = new HashMap<>();
labels.put(LABEL, dbName);
ContainerPort port = new ContainerPort();
Expand Down Expand Up @@ -142,7 +144,7 @@ public Map<String, String> deploy(GlowMessageWriter writer, Path target, OpenShi
Map<String, String> ret = new HashMap<>();
ret.putAll(getExistingEnv(env));
ret.putAll(APP_MAP);
writer.info("\n" + dbName + " server has been deployed");
writer.info(dbName + " server has been deployed");
return ret;
}

Expand Down Expand Up @@ -174,4 +176,19 @@ public Map<String, String> handleBuildTimeDefault(Set<String> buildEnv, MavenRep
protected String computeBuildTimeValue(String name, MavenRepoManager mvnResolver) throws Exception {
return null;
}

@Override
public Set<Env> getResolvedEnvs(Set<Env> input) {
Set<Env> envs = new HashSet<>();
for(Env env : input) {
if(APP_MAP.containsKey(env.getName())) {
envs.add(env);
} else {
if (env.getName().startsWith(envRadical + "_")) {
envs.add(env);
}
}
}
return envs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.glow.Env;
import org.wildfly.glow.GlowMessageWriter;

/**
Expand All @@ -47,4 +48,6 @@ default Set<String> getSupportedLayers() {
default Map<String, String> handleBuildTimeDefault(Set<String> buildEnv, MavenRepoManager mvnResolver) throws Exception {
return Collections.emptyMap();
}

public Set<Env> getResolvedEnvs(Set<Env> input);
}
Loading

0 comments on commit 4061d29

Please sign in to comment.