From e97e06fe52930dcec8bedec2c4204c9b0b7e48a8 Mon Sep 17 00:00:00 2001 From: khaled basbous Date: Mon, 8 Apr 2024 17:03:05 +0200 Subject: [PATCH] logging slf4j logback and install openssl in dockerfile needed for api-server startup --- code/project.clj | 40 +++--- code/src/sixsq/nuvla/server/ring.clj | 36 +++--- code/test-resources/log4j.properties | 7 -- code/test-resources/logback-test.xml | 117 ++++++++++++++++++ .../sixsq/nuvla/server/example.clj | 2 +- code/test/sixsq/nuvla/server/ring_test.clj | 18 ++- container/Dockerfile | 6 +- 7 files changed, 173 insertions(+), 53 deletions(-) delete mode 100644 code/test-resources/log4j.properties create mode 100644 code/test-resources/logback-test.xml rename code/{src => test}/sixsq/nuvla/server/example.clj (90%) diff --git a/code/project.clj b/code/project.clj index b08099a..44e9557 100644 --- a/code/project.clj +++ b/code/project.clj @@ -1,4 +1,6 @@ -(def parent-version "6.7.15") +(def parent-version "6.8.0") +(def slf4j-version "2.0.12") +(def logback-version "1.5.3") (defproject sixsq.nuvla.ring/code "2.1.1-SNAPSHOT" @@ -6,16 +8,14 @@ :url "https://github.com/nuvla/server" - :license {:name "Apache 2.0" - :url "http://www.apache.org/licenses/LICENSE-2.0.txt" + :license {:name "Apache 2.0" + :url "http://www.apache.org/licenses/LICENSE-2.0.txt" :distribution :repo} :plugins [[lein-parent "0.3.9"]] :parent-project {:coords [sixsq.nuvla/parent ~parent-version] :inherit [:plugins - :min-lein-version - :managed-dependencies :repositories :deploy-repositories]} @@ -28,19 +28,21 @@ :aot [sixsq.nuvla.server.ring] :dependencies - [[aleph] - [environ] - [log4j] - [org.clojure/clojure] - [org.clojure/tools.logging] - [org.slf4j/slf4j-api] - [org.slf4j/slf4j-log4j12] - [ring/ring-core] ;; added to force version - ] + [[aleph "0.7.1"] + [environ "1.2.0"] + [ring/ring-core "1.12.1"] + [org.clojure/clojure "1.11.2"] + [org.clojure/tools.logging "1.3.0"] + [org.slf4j/slf4j-api ~slf4j-version] + [org.slf4j/log4j-over-slf4j ~slf4j-version] + [org.slf4j/jul-to-slf4j ~slf4j-version] + [org.slf4j/jcl-over-slf4j ~slf4j-version] + [ch.qos.logback/logback-classic ~logback-version] + [ch.qos.logback/logback-core ~logback-version]] :profiles - {:test {:source-paths ["test"] - :resource-paths ["test-resources"] - :plugins [[lein-test-report-junit-xml "0.2.0"]] - :test-report-junit-xml {:output-dir "test-reports"}} - :dev {:dependencies [[clj-kondo "RELEASE"]]}}) + {:test {:source-paths ["test"] + :resource-paths ["test-resources"] + :plugins [[lein-test-report-junit-xml "0.2.0"]] + :test-report-junit-xml {:output-dir "test-reports"}} + :dev {:dependencies [[clj-kondo "RELEASE"]]}}) diff --git a/code/src/sixsq/nuvla/server/ring.clj b/code/src/sixsq/nuvla/server/ring.clj index dcf8abe..23f1b3e 100644 --- a/code/src/sixsq/nuvla/server/ring.clj +++ b/code/src/sixsq/nuvla/server/ring.clj @@ -2,12 +2,12 @@ "Provides a simple, generic ring application server that can be started either via the static 'main' function (e.g. for running as a daemon) or via the 'start' function (e.g. for testing from the REPL)." - (:require - [clojure.string :as str] - [clojure.tools.logging :as log]) - (:import - [java.io Closeable] - [java.net InetSocketAddress]) + (:require [aleph.http :refer [start-server]] + [clojure.string :as str] + [clojure.tools.logging :as log] + [environ.core :refer [env]]) + (:import [java.io Closeable] + [java.net InetSocketAddress]) (:gen-class)) @@ -60,11 +60,10 @@ (defn- register-shutdown-hook - "Registers a shutdown hook in the JVM to shutdown the application and + "Registers a shutdown hook in the JVM to shut down the application and application container cleanly." [shutdown-fn] - (.. (Runtime/getRuntime) - (addShutdownHook (hook shutdown-fn)))) + (.addShutdownHook (Runtime/getRuntime) (hook shutdown-fn))) (defn- create-shutdown-fn @@ -86,11 +85,9 @@ "Starts the aleph container with the given ring handler and on the given port. Returns the server object that has been started, which can be stopped by calling 'close' on the object." - [handler ^long port host] + [handler ^long port ^String host] (log/info "starting aleph application container on host:port" (str host ":" port)) - (let [start-server (dyn-resolve 'aleph.http/start-server) - server (->> port - (InetSocketAddress. host) + (let [server (->> (InetSocketAddress. host port) (hash-map :socket-address) (start-server handler))] (log/info "started aleph application container on host:port" (str host ":" port)) @@ -98,7 +95,7 @@ (defn- validate-host - "If the argument is the a valid string representation of an IP address or + "If the argument is a valid string representation of an IP address or host, the value is returned. Otherwise, the default host is returned." [host] (if (and (string? host) (not (str/blank? host))) @@ -125,19 +122,19 @@ (defn start "Starts the application and application server. Return a 'stop' function - that will shutdown the application and server when called." + that will shut down the application and server when called." [server-init & [port host]] (let [server-init-fn (dyn-resolve server-init) [handler finalization-fn] (server-init-fn) - port (parse-port port) - host (validate-host host)] + port (parse-port port) + host (validate-host host)] (log/info "starting " server-init "on" host "and port" port) (log/info "java vendor: " (System/getProperty "java.vendor")) (log/info "java version: " (System/getProperty "java.version")) (log/info "java classpath: " (System/getProperty "java.class.path")) - (let [server (start-container handler port host) + (let [server (start-container handler port host) shutdown-fn (create-shutdown-fn server finalization-fn)] (log/info "started" server-init "on" host "and port" port) @@ -157,8 +154,7 @@ function. Starting the server from the REPL will use the values given to the start function." [] - (let [env (dyn-resolve 'environ.core/env) - server-port (env :nuvla-server-port) + (let [server-port (env :nuvla-server-port) server-host (env :nuvla-server-host)] (if-let [server-init (env :nuvla-server-init)] [server-init server-port server-host] diff --git a/code/test-resources/log4j.properties b/code/test-resources/log4j.properties deleted file mode 100644 index e5eec52..0000000 --- a/code/test-resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -log4j.rootLogger=INFO, devnull -log4j.appender.devnull=org.apache.log4j.varia.NullAppender - -#log4j.rootLogger=INFO, console -#log4j.appender.console=org.apache.log4j.ConsoleAppender -#log4j.appender.console.layout=org.apache.log4j.PatternLayout -#log4j.appender.console.layout.ConversionPattern=%-5p %c: %m%n diff --git a/code/test-resources/logback-test.xml b/code/test-resources/logback-test.xml new file mode 100644 index 0000000..dbfe371 --- /dev/null +++ b/code/test-resources/logback-test.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + INFO + + + + + + %date - %-5level - %logger - [%thread] - %msg%n + UTF-8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + diff --git a/code/src/sixsq/nuvla/server/example.clj b/code/test/sixsq/nuvla/server/example.clj similarity index 90% rename from code/src/sixsq/nuvla/server/example.clj rename to code/test/sixsq/nuvla/server/example.clj index 94d559a..f5cd0a6 100644 --- a/code/src/sixsq/nuvla/server/example.clj +++ b/code/test/sixsq/nuvla/server/example.clj @@ -2,7 +2,7 @@ "Provides a simple ring example to ensure that generic ring container works.") -(defn handler [request] +(defn handler [_request] {:status 200 :headers {"Content-Type" "text/plain"} :body "Ring Example Running!\n"}) diff --git a/code/test/sixsq/nuvla/server/ring_test.clj b/code/test/sixsq/nuvla/server/ring_test.clj index b5a1499..ff6718b 100644 --- a/code/test/sixsq/nuvla/server/ring_test.clj +++ b/code/test/sixsq/nuvla/server/ring_test.clj @@ -1,9 +1,7 @@ (ns sixsq.nuvla.server.ring-test - (:require - [clojure.test :refer [are deftest is]] - [sixsq.nuvla.server.ring :as t]) - (:import - (clojure.lang ExceptionInfo))) + (:require [clojure.test :refer [are deftest is]] + [sixsq.nuvla.server.ring :as t]) + (:import (clojure.lang ExceptionInfo))) (deftest check-as-symbol @@ -50,3 +48,13 @@ 10 10 10 "10"))) +(deftest start + (let [shutdown-fn (t/start 'sixsq.nuvla.server.example/init)] + (is (true? (fn? shutdown-fn))) + (shutdown-fn))) + +(deftest register-shutdown-hook + (#'t/register-shutdown-hook prn)) + +(deftest server-cfg + (is (thrown-with-msg? ExceptionInfo #"NUVLA_SERVER_INIT is not defined" (#'t/server-cfg)))) diff --git a/container/Dockerfile b/container/Dockerfile index dcafa1d..c6f7dca 100644 --- a/container/Dockerfile +++ b/container/Dockerfile @@ -18,7 +18,11 @@ COPY target/dependency/lib /opt/nuvla/server/lib COPY src/scripts/log4j.properties /opt/nuvla/server/resources/ COPY src/scripts/start.sh /opt/nuvla/server/bin/ RUN chmod 0755 /opt/nuvla/server/bin/start.sh \ - && mkdir -p /opt/nuvla/server/lib.d/example + && mkdir -p /opt/nuvla/server/lib.d/ + +RUN apk upgrade --update-cache --available && \ + apk add openssl && \ + rm -rf /var/cache/apk/* ENV NUVLA_SERVER_INIT=sixsq.nuvla.server.example/init