You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ma...@apache.org on 2022/04/22 17:56:07 UTC

[camel] branch main updated: CAMEL-18006. camel-jbang package image command (#7488)

This is an automated email from the ASF dual-hosted git repository.

marat 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 b758b72ba8e CAMEL-18006. camel-jbang package image command (#7488)
b758b72ba8e is described below

commit b758b72ba8e862ae2ba0aa641be98872a5d95c71
Author: Marat Gubaidullin <ma...@gmail.com>
AuthorDate: Fri Apr 22 13:55:56 2022 -0400

    CAMEL-18006. camel-jbang package image command (#7488)
    
    * jbang camel package image prototype
    
    * Push
---
 dsl/camel-jbang/camel-jbang-core/pom.xml           |   6 ++
 .../dsl/jbang/core/commands/CamelJBangMain.java    |   3 +-
 .../camel/dsl/jbang/core/commands/Image.java       | 102 +++++++++++++++++++++
 parent/pom.xml                                     |   1 +
 4 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/pom.xml b/dsl/camel-jbang/camel-jbang-core/pom.xml
index 323a490467f..ee15ee567f7 100644
--- a/dsl/camel-jbang/camel-jbang-core/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-core/pom.xml
@@ -89,6 +89,12 @@
             <artifactId>jansi</artifactId>
             <version>${jansi-version}</version>
         </dependency>
+        <!-- Docker generator -->
+        <dependency>
+            <groupId>com.google.cloud.tools</groupId>
+            <artifactId>jib-core</artifactId>
+            <version>${jib-version}</version>
+        </dependency>
         <!-- Code generator -->
         <dependency>
             <groupId>org.apache.camel</groupId>
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index aeb1ea2624f..473bb991c36 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -39,7 +39,8 @@ public class CamelJBangMain implements Callable<Integer> {
                 .addSubcommand("create", new CommandLine(new Create())
                         .addSubcommand("project", new Project()))
                 .addSubcommand("package", new CommandLine(new Package())
-                        .addSubcommand("fat-jar", new FatJar()))
+                        .addSubcommand("fat-jar", new FatJar())
+                        .addSubcommand("image", new Image()))
                 .addSubcommand("generate", new CommandLine(new CodeGenerator())
                         .addSubcommand("rest", new CodeRestGenerator()));
 
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
new file mode 100644
index 00000000000..e9d808c194f
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
@@ -0,0 +1,102 @@
+/*
+ * 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.nio.file.Paths;
+import java.util.Arrays;
+import java.util.concurrent.Callable;
+import java.util.function.Consumer;
+
+import com.google.cloud.tools.jib.api.Containerizer;
+import com.google.cloud.tools.jib.api.DockerDaemonImage;
+import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
+import com.google.cloud.tools.jib.api.Jib;
+import com.google.cloud.tools.jib.api.LogEvent;
+import com.google.cloud.tools.jib.api.RegistryImage;
+import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
+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);
+
+    @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display the help and sub-commands")
+    private boolean helpRequested;
+    @CommandLine.Option(names = {"-f", "--from"}, description = "Base Image", defaultValue = "gcr.io/distroless/java:11")
+    private String from;
+
+    @CommandLine.Option(names = {"-j", "--jar"}, required = true, description = "Jar filename")
+    private String jar;
+
+    @CommandLine.Option(names = {"-t", "--tag"}, description = "Image tag")
+    private String tag;
+
+    @CommandLine.Option(names = {"--push"}, description = "Push to the registry")
+    private boolean push;
+
+    @CommandLine.Option(names = {"-r", "--registry"}, description = "Registry image reference")
+    private String registry;
+    @CommandLine.Option(names = {"-u", "--username"}, description = "Registry username")
+    private String username;
+
+    @CommandLine.Option(names = {"-p", "--password"}, description = "Registry password")
+    private String password;
+
+    @Override
+    public Integer call() throws Exception {
+        File jarFile = Paths.get(jar).toFile();
+        Jib.from(from)
+                .addLayer(Arrays.asList(Paths.get(jar)), "/deployments/")
+                .setWorkingDirectory(AbsoluteUnixPath.get("/deployments"))
+                .setEntrypoint("java", "-jar", jarFile.getName())
+                .containerize(push ? getRegistry() : getDockerImage());
+        return 0;
+    }
+
+    private Containerizer getDockerImage() throws InvalidImageReferenceException {
+        return Containerizer.to(DockerDaemonImage.named(tag)).addEventHandler(LogEvent.class, getEventConsumer());
+    }
+
+    private Containerizer getRegistry() throws InvalidImageReferenceException {
+        return Containerizer.to(
+                        RegistryImage.named(registry).addCredential(username, password))
+                .addEventHandler(LogEvent.class, getEventConsumer());
+    }
+
+    private Consumer<LogEvent> getEventConsumer() {
+        return event -> {
+            switch (event.getLevel()) {
+                case ERROR:
+                    LOG.error(event.getMessage());
+                    break;
+                case WARN:
+                    LOG.warn(event.getMessage());
+                    break;
+                case DEBUG:
+                    LOG.debug(event.getMessage());
+                    break;
+                default:
+                    LOG.info(event.getMessage());
+                    break;
+            }
+        };
+    }
+}
diff --git a/parent/pom.xml b/parent/pom.xml
index c83ac8cba46..0b0db89265f 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -311,6 +311,7 @@
         <jgroups-raft-version>0.5.3.Final</jgroups-raft-version>
         <jgroups-raft-leveldbjni-version>1.8</jgroups-raft-leveldbjni-version>
         <jgroups-raft-mapdb-version>1.0.8</jgroups-raft-mapdb-version>
+        <jib-version>0.21.0</jib-version>
         <jira-guava-version>26.0-jre</jira-guava-version>
         <jira-rest-client-api-version>5.2.2</jira-rest-client-api-version>
         <libthrift-version>0.14.1</libthrift-version>