You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/09 09:15:14 UTC
[camel] branch main updated: CAMEL-18067: camel-jbang - Do not use log for own commands as logging should be initialized for running Camel
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 23c55596f5d CAMEL-18067: camel-jbang - Do not use log for own commands as logging should be initialized for running Camel
23c55596f5d is described below
commit 23c55596f5df0246c354a4fa02c3afe1a86c9845
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 9 11:14:23 2022 +0200
CAMEL-18067: camel-jbang - Do not use log for own commands as logging should be initialized for running Camel
---
.../jbang/core/commands/AbstractInitKamelet.java | 95 ----------------------
.../camel/dsl/jbang/core/commands/Deploy.java | 32 ++++----
.../camel/dsl/jbang/core/commands/Image.java | 23 +++---
.../camel/dsl/jbang/core/commands/Manifest.java | 10 +--
.../dsl/jbang/core/commands/PropertiesHelper.java | 5 +-
.../apache/camel/dsl/jbang/core/commands/Run.java | 33 +++++++-
.../camel/dsl/jbang/core/commands/Undeploy.java | 19 ++---
.../camel/dsl/jbang/core/common/RuntimeUtil.java | 2 -
8 files changed, 67 insertions(+), 152 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java
deleted file mode 100644
index 23daa6c64a0..00000000000
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.apache.camel.dsl.jbang.core.commands;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.CamelException;
-import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists;
-import org.apache.camel.github.GitHubResourceResolver;
-import org.apache.camel.main.KameletMain;
-import org.apache.camel.spi.Resource;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Deprecated
-public abstract class AbstractInitKamelet {
- private static final Logger LOG = LoggerFactory.getLogger(AbstractInitKamelet.class);
-
- private String resourceLocation;
- private String branch;
-
- protected void setResourceLocation(String baseResourceLocation, String resourcePath) {
- this.resourceLocation = baseResourceLocation + ":" + resourcePath;
- LOG.debug("Resource location is: {}", resourceLocation);
- }
-
- public void setBranch(String branch) {
- this.branch = branch;
- }
-
- protected File resolveResource(File destinationDirectory) throws IOException, CamelException {
- KameletMain main = new KameletMain();
- main.start();
-
- CamelContext context = main.getCamelContext();
-
- try (GitHubResourceResolver resolver = new GitHubResourceResolver()) {
- resolver.setCamelContext(context);
- resolver.setBranch(branch);
-
- Resource resource = resolver.resolve(resourceLocation);
- if (!resource.exists()) {
- throw new CamelException("The resource does not exist");
- }
-
- String fileName = FilenameUtils.getName(resource.getURL().getPath());
- LOG.debug("Destination directory for the downloaded resources: {}", destinationDirectory.getAbsolutePath());
- LOG.debug("Downloaded resource file name: {}", fileName);
- File outputFile = new File(destinationDirectory, fileName);
-
- File parentDir = outputFile.getParentFile();
- if (!parentDir.exists()) {
- if (!parentDir.mkdirs()) {
- LOG.warn("Failed to create the output directory: {}. It may have been created already", parentDir);
- }
- }
-
- if (outputFile.exists()) {
- throw new ResourceAlreadyExists(outputFile);
- } else {
- try (FileOutputStream fo = new FileOutputStream(outputFile)) {
- IOUtils.copy(resource.getInputStream(), fo);
- }
- }
-
- return outputFile;
- }
- }
-
- protected void bootstrap(String branch, String baseResourceLocation, String destination)
- throws IOException, CamelException {
- setBranch(branch);
- setResourceLocation(baseResourceLocation, "camel-kamelets:templates/init-template.properties");
- resolveResource(new File(destination));
- }
-}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
index cc98f4b32b9..3b1bd598ecd 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
@@ -29,15 +29,11 @@ import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import picocli.CommandLine;
@CommandLine.Command(name = "deploy", description = "Deploy resources to Kubernetes, OpenShift, Minikube")
public class Deploy implements Callable<Integer> {
- private static final Logger LOG = LoggerFactory.getLogger(Deploy.class);
-
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
private boolean helpRequested;
@CommandLine.Option(names = { "--namespace" }, required = true, description = "Namespace", defaultValue = "default")
@@ -68,50 +64,50 @@ public class Deploy implements Callable<Integer> {
@Override
public Integer call() throws Exception {
if (minikube) {
- LOG.info("Generating Deployment...");
+ System.out.println("Generating Deployment...");
Deployment deployment = KubernetesHelper.createDeployment(namespace, name, image, version, containerPort, replicas);
- LOG.info("Generating Service...");
+ System.out.println("Generating Service...");
Service service
= KubernetesHelper.createService(namespace, name, version, servicePort, containerPort, minikube, nodePort);
try (KubernetesClient client = new DefaultKubernetesClient()) {
- LOG.info("Creating Deployment in " + (minikube ? "Minikube" : "Kubernetes"));
+ System.out.println("Creating Deployment in " + (minikube ? "Minikube" : "Kubernetes"));
client.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
client.services().inNamespace(namespace).delete(service);
- LOG.info("Creating Service in " + (minikube ? "Minikube" : "Kubernetes"));
+ System.out.println("Creating Service in " + (minikube ? "Minikube" : "Kubernetes"));
client.services().inNamespace(namespace).createOrReplace(service);
} catch (Exception ex) {
- LOG.error("Error", ex.getMessage());
+ System.out.println("ERROR: " + ex.getMessage());
}
} else if (openshift) {
if (!image.startsWith("image-registry.openshift-image-registry.svc:5000") && image.split("/").length != 3) {
image = "image-registry.openshift-image-registry.svc:5000/" + image;
}
- LOG.info("Generating Deployment...");
+ System.out.println("Generating Deployment...");
Deployment deployment = KubernetesHelper.createDeployment(namespace, name, image, version, containerPort, replicas);
- LOG.info("Generating Service...");
+ System.out.println("Generating Service...");
Service service
= KubernetesHelper.createService(namespace, name, version, servicePort, containerPort, minikube, nodePort);
- LOG.info("Generating Route...");
+ System.out.println("Generating Route...");
Route route = KubernetesHelper.createRoute(namespace, name, version, containerPort);
OpenShiftConfig config
= new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withTrustCerts(true).build();
try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
- LOG.info("Creating Deployment in Openshift");
+ System.out.println("Creating Deployment in Openshift");
client.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
client.services().inNamespace(namespace).delete(service);
- LOG.info("Creating Service in Openshift");
+ System.out.println("Creating Service in Openshift");
client.services().inNamespace(namespace).createOrReplace(service);
- LOG.info("Creating Route in Openshift");
+ System.out.println("Creating Route in Openshift");
client.routes().inNamespace(namespace).createOrReplace(route);
} catch (KubernetesClientException ex) {
Status status = ex.getStatus();
if (status != null) {
- LOG.error("Error: [%d %s] [%s] %s", status.getCode(), status.getStatus(), status.getReason(),
- status.getMessage());
+ System.out.println("ERROR: " + status.getCode() + " " + status.getStatus() + " " + status.getReason() + " "
+ + ex.getMessage());
} else {
- LOG.error(ex.getMessage());
+ System.out.println("ERROR " + ex.getMessage());
}
}
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
index d8611ebaafa..1652a7e5919 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
@@ -43,14 +43,11 @@ import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import picocli.CommandLine;
@CommandLine.Command(name = "image", description = "Create Docker and OCI container images")
public class Image implements Callable<Integer> {
- private static final Logger LOG = LoggerFactory.getLogger(Image.class);
private static final int LOG_TAIL_SIZE = 10;
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
@@ -93,7 +90,7 @@ public class Image implements Callable<Integer> {
public Integer call() throws Exception {
File jarFile = Paths.get(jar).toFile();
if (openshift) {
- LOG.info("Generating resources...");
+ System.out.println("Generating resources...");
OpenShiftConfig config
= new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withNamespace(namespace)
.withTrustCerts(true).build();
@@ -101,11 +98,11 @@ public class Image implements Callable<Integer> {
ImageStream imageStream = KubernetesHelper.createImageStream(namespace, name, version);
BuildConfig buildConfig
= KubernetesHelper.createBuildConfig(namespace, name, version, jarFile.getName(), sourceImage);
- LOG.info("Creating ImageStream...");
+ System.out.println("Creating ImageStream...");
client.imageStreams().createOrReplace(imageStream);
- LOG.info("Creating BuildConfig...");
+ System.out.println("Creating BuildConfig...");
client.buildConfigs().createOrReplace(buildConfig);
- LOG.info("Creating Build...");
+ System.out.println("Creating Build...");
Build build = client.buildConfigs()
.inNamespace(namespace)
.withName(buildConfig.getMetadata().getName())
@@ -129,7 +126,7 @@ public class Image implements Callable<Integer> {
} catch (IOException e) {
// This may happen if the LogWatch is closed while we are still reading.
// We shouldn't let the build fail, so let's log a warning and display last few lines of the log
- LOG.warn("Log stream closed, redisplaying last " + LOG_TAIL_SIZE + " entries:");
+ System.out.println("Log stream closed, redisplaying last " + LOG_TAIL_SIZE + " entries:");
try {
display(client.builds().withName(buildName).tailingLines(LOG_TAIL_SIZE)
.getLogReader());
@@ -183,16 +180,16 @@ public class Image implements Callable<Integer> {
return event -> {
switch (event.getLevel()) {
case ERROR:
- LOG.error(event.getMessage());
+ System.out.println("ERROR: " + event.getMessage());
break;
case WARN:
- LOG.warn(event.getMessage());
+ System.out.println("WARN: " + event.getMessage());
break;
case DEBUG:
- LOG.debug(event.getMessage());
+ System.out.println("DEBUG: " + event.getMessage());
break;
default:
- LOG.info(event.getMessage());
+ System.out.println(event.getMessage());
break;
}
};
@@ -201,7 +198,7 @@ public class Image implements Callable<Integer> {
private static void display(Reader logReader) throws IOException {
BufferedReader reader = new BufferedReader(logReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
- LOG.info(line);
+ System.out.println(line);
}
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Manifest.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Manifest.java
index 61ffa8ecc0f..e7efd0195e1 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Manifest.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Manifest.java
@@ -30,13 +30,10 @@ import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.Route;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import picocli.CommandLine;
@CommandLine.Command(name = "manifests", description = "Create Kubernetes resources")
public class Manifest implements Callable<Integer> {
- private static final Logger LOG = LoggerFactory.getLogger(Manifest.class);
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
private boolean helpRequested;
@@ -71,7 +68,7 @@ public class Manifest implements Callable<Integer> {
@Override
public Integer call() throws Exception {
try {
- LOG.info("Generating resources...");
+ System.out.println("Generating resources...");
if (minikube) {
Deployment deployment
= KubernetesHelper.createDeployment(namespace, name, image, version, containerPort, replicas);
@@ -96,7 +93,6 @@ public class Manifest implements Callable<Integer> {
write(buildConfig, "build-config.yaml");
}
} catch (Exception ex) {
- LOG.error("Error", ex.getMessage());
}
return 0;
}
@@ -104,10 +100,10 @@ public class Manifest implements Callable<Integer> {
private void write(Object object, String filename) throws IOException {
Path output = Paths.get(path != null ? path : System.getProperty("user.dir"));
if (!Files.exists(output)) {
- LOG.info("Creating output folder " + output);
+ System.out.println("Creating output folder " + output);
Files.createDirectories(output);
}
- LOG.info("Writing {}...", filename);
+ System.out.println("Writing " + filename);
Files.write(Paths.get(output.toString(), filename),
Serialization.asYaml(object).getBytes(StandardCharsets.UTF_8));
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java
index 0a1f355648d..22206d51e8a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java
@@ -27,12 +27,9 @@ import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import picocli.CommandLine;
public final class PropertiesHelper {
- private static final Logger LOG = LoggerFactory.getLogger(CamelJBangMain.class);
private static final String APPLICATION_PROPERTIES_FILE = "application.properties";
private static final String PROPERTY_PREFIX = "camel.jbang";
private static final String COMMAND_PREFIX = "CamelJBang";
@@ -104,7 +101,7 @@ public final class PropertiesHelper {
try (FileInputStream fis = new FileInputStream(defaultsFile)) {
properties.load(fis);
} catch (IOException e) {
- LOG.error("Error reading defaults file: {}", e.getMessage(), e);
+ // ignore
}
}
return properties;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 2561c234f77..884538bcc85 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -255,7 +255,7 @@ class Run implements Callable<Integer> {
private int run() throws Exception {
File work = new File(WORK_DIR);
- FileUtil.removeDir(work);
+ removeDir(work);
work.mkdirs();
// generate open-api early
@@ -592,7 +592,7 @@ class Run implements Callable<Integer> {
return main;
}
- private void configureLogging() {
+ private void configureLogging() throws Exception {
if (silentRun) {
RuntimeUtil.configureLog("off", false, false);
} else if (logging) {
@@ -711,4 +711,33 @@ class Run implements Callable<Integer> {
}
}
+ private static void removeDir(File d) {
+ String[] list = d.list();
+ if (list == null) {
+ list = new String[0];
+ }
+ for (String s : list) {
+ File f = new File(d, s);
+ if (f.isDirectory()) {
+ removeDir(f);
+ } else {
+ delete(f);
+ }
+ }
+ delete(d);
+ }
+
+ private static void delete(File f) {
+ if (!f.delete()) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException ex) {
+ // Ignore Exception
+ }
+ if (!f.delete()) {
+ f.deleteOnExit();
+ }
+ }
+ }
+
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Undeploy.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Undeploy.java
index c5718327f65..03ad0a82203 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Undeploy.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Undeploy.java
@@ -25,13 +25,10 @@ import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import picocli.CommandLine;
@CommandLine.Command(name = "undeploy", description = "Undeploy resources from Kubernetes, OpenShift, Minikube")
public class Undeploy implements Callable<Integer> {
- private static final Logger LOG = LoggerFactory.getLogger(Undeploy.class);
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
private boolean helpRequested;
@@ -55,25 +52,25 @@ public class Undeploy implements Callable<Integer> {
OpenShiftConfig config
= new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withTrustCerts(true).build();
try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
- LOG.info("Deleting Routes...");
+ System.out.println("Deleting Routes...");
client.routes().inNamespace(namespace).withLabels(labels).delete();
- LOG.info("Deleting Service...");
+ System.out.println("Deleting Service...");
client.services().inNamespace(namespace).withLabels(labels).delete();
- LOG.info("Deleting Deployment...");
+ System.out.println("Deleting Deployment...");
client.apps().deployments().inNamespace(namespace).withLabels(labels).delete();
- LOG.info("Deleting ImageStream...");
+ System.out.println("Deleting ImageStream...");
client.imageStreams().inNamespace(namespace).withLabels(labels).delete();
- LOG.info("Deleting BuildConfig...");
+ System.out.println("Deleting BuildConfig...");
client.buildConfigs().inNamespace(namespace).withLabels(labels).delete();
}
} else {
try (KubernetesClient client = new DefaultKubernetesClient()) {
- LOG.info("Deleting Service...");
+ System.out.println("Deleting Service...");
client.services().inNamespace(namespace).withLabels(labels).delete();
- LOG.info("Deleting Deployment...");
+ System.out.println("Deleting Deployment...");
client.apps().deployments().inNamespace(namespace).withLabels(labels).delete();
} catch (Exception ex) {
- LOG.error("Error", ex.getMessage());
+ System.out.println("Error Undeploying " + ex.getMessage());
}
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
index 5b6a956ae67..4203e9e7c3d 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
@@ -20,7 +20,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
-import org.slf4j.LoggerFactory;
public final class RuntimeUtil {
@@ -66,7 +65,6 @@ public final class RuntimeUtil {
break;
default: {
Configurator.setRootLevel(Level.INFO);
- LoggerFactory.getLogger(RuntimeUtil.class).warn("Invalid logging level: {}", level);
}
}
}