Navigation Menu

Skip to content

Commit

Permalink
Allow introspection of version number
Browse files Browse the repository at this point in the history
This patchset stores the auto-computed version number for PuppetDB:

* on-disk, inside of a generated JAR file
* in-memory, in `com.puppetlabs.puppetdb.cli.services/version`

This is done by modifying our build file slightly to add a custom metadata
field to build outputs, and adding a func that can parse that field back into
memory.

Signed-off-by: Deepak Giridharagopal <deepak@puppetlabs.com>
  • Loading branch information
grimradical committed Jun 5, 2012
1 parent 72e483a commit 6ec3c01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
37 changes: 20 additions & 17 deletions project.clj
@@ -1,24 +1,26 @@
(require '[clojure.string :as s])
(use '[clojure.java.shell :only (sh)])

(defn version-string
"Determine the version number using 'git describe'"
[]
(let [command ["git" "describe"]
{:keys [exit out err]} (apply sh command)]
(when-not (zero? exit)
(println (format "Non-zero exit status during version check:\n%s\n%s\n%s\n%s"
command exit out err))
(System/exit 1))
(def version-string
(memoize
(fn []
"Determine the version number using 'git describe'"
[]
(let [command ["git" "describe"]
{:keys [exit out err]} (apply sh command)]
(when-not (zero? exit)
(println (format "Non-zero exit status during version check:\n%s\n%s\n%s\n%s"
command exit out err))
(System/exit 1))

;; We just want the first 4 "components" of the version string,
;; joined with dots
(-> out
(s/trim)
(s/replace #"-" ".")
(s/split #"\.")
(#(take 4 %))
(#(s/join "." %)))))
;; We just want the first 4 "components" of the version string,
;; joined with dots
(-> out
(s/trim)
(s/replace #"-" ".")
(s/split #"\.")
(#(take 4 %))
(#(s/join "." %)))))))

(defproject puppetdb (version-string)
:description "Puppet-integrated catalog and fact storage"
Expand Down Expand Up @@ -68,6 +70,7 @@
[ring-mock "0.1.1"]]

:jar-exclusions [#"leiningen/"]
:manifest {"Build-Version" ~(version-string)}

:aot [com.puppetlabs.puppetdb.core]
:main com.puppetlabs.puppetdb.core
Expand Down
4 changes: 4 additions & 0 deletions src/com/puppetlabs/puppetdb/cli/services.clj
Expand Up @@ -68,6 +68,7 @@
;; The following functions setup interaction between the main
;; PuppetDB components.

(def version (pl-utils/get-version-from-manifest))
(def configuration nil)
(def mq-addr "vm://localhost?jms.prefetchPolicy.all=1&create=false")
(def mq-endpoint "com.puppetlabs.puppetdb.commands")
Expand Down Expand Up @@ -202,6 +203,9 @@
:endpoint mq-endpoint}}
ring-app (server/build-app globals)]

(when version
(log/info (format "PuppetDB version %s" version)))

;; Ensure the database is migrated to the latest version
(sql/with-connection db
(migrate!))
Expand Down
25 changes: 23 additions & 2 deletions src/com/puppetlabs/utils.clj
Expand Up @@ -327,18 +327,39 @@
(let [bytes (.getBytes s "UTF-8")]
(digest-func "sha-1" [bytes]))))

;; UUID handling
;; ## UUID handling

(defn uuid
"Generate a random UUID and return its string representation"
[]
(str (java.util.UUID/randomUUID)))

;; System interface
;; ## System interface

(defn num-cpus
"Grabs the number of available CPUs for the local host"
[]
{:post [(pos? %)]}
(-> (Runtime/getRuntime)
(.availableProcessors)))

;; ## META-INF parsing

(defn get-version-from-manifest
"Returns the PuppetDB version number as indicated by a maven POM
file embedded in the current JAR. If we aren't running from within a
a JAR, or if we can't locate a maven artifact on the CLASSPATH, we
return nil.
Yes, this code is kind of inscrutable and impossible to unit test
(usefully) without building actual artifacts. :("
[]
(let [props-path (->> ["META-INF" "maven" "puppetdb" "puppetdb" "pom.properties"]
(apply clojure.java.io/file)
(.getPath))
props-file (clojure.java.io/resource props-path)]
(when props-file
(with-open [rdr (clojure.java.io/reader props-file)]
(let [props (java.util.Properties.)]
(.load props rdr)
(get props "version"))))))

0 comments on commit 6ec3c01

Please sign in to comment.