Skip to content

Commit

Permalink
add command-line client for agent
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Jan 15, 2016
1 parent 7ef85bc commit 19e1ef6
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 26 deletions.
3 changes: 3 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[org.clojure/clojurescript "1.7.170"]
[org.clojure/core.async "0.2.374"]
[org.clojure/tools.logging "0.3.1"]
[org.clojure/tools.cli "0.3.3"]
[environ "1.0.1"]
[clj-logging-config "1.9.12"]
[binaryage/chromex "0.2.0"]
Expand All @@ -30,6 +31,8 @@
[lein-shell "0.4.2"]
[lein-environ "1.0.1"]]

:main dirac.agent-cli

:figwheel
{:server-port 7100
:server-logfile ".figwheel_server.log"
Expand Down
10 changes: 6 additions & 4 deletions src/agent/dirac/agent.clj
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@
The problem with `lein repl` :init config is that it is evaluated before nREPL fully starts.
Actually it waits for this init code to fully evaluate before starting nREPL server."
[& [config]]
(if-not (or (:skip-logging-setup config) (utils/env-val :dirac-agent-skip-logging-setup))
(logging/setup-logging!))
(log/info "Booting Dirac Agent...")
(future (boot-now! config))
(let [effective-config (config/get-effective-config config)]
(if-not (:skip-logging-setup effective-config)
(logging/setup-logging! effective-config))
(log/info "Booting Dirac Agent...")
(log/debug "effective config: " effective-config)
(future (boot-now! config)))
true)
14 changes: 5 additions & 9 deletions src/agent/dirac/agent/config.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns dirac.agent.config
(require [clojure.tools.logging :as log]
[dirac.lib.utils :refer [assoc-env-val]]))
(require [dirac.lib.utils :refer [assoc-env-val]]))

(def nrepl-server-default-config
{:host "localhost"
Expand All @@ -11,7 +10,8 @@
:port 8231})

(def default-config
{:max-boot-trials 10
{:log-level "WARN" ; OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL
:max-boot-trials 10
:initial-boot-delay 1000
:delay-between-boot-trials 500
:skip-logging-setup false
Expand All @@ -22,10 +22,11 @@

(defn get-environ-config []
(-> {}
(assoc-env-val [:log-level] :dirac-agent-log-level)
(assoc-env-val [:skip-logging-setup] :dirac-agent-skip-logging-setup :bool)
(assoc-env-val [:max-boot-trials] :dirac-agent-max-boot-trials :int)
(assoc-env-val [:initial-boot-delay] :dirac-agent-initial-boot-delay :int)
(assoc-env-val [:delay-between-boot-trials] :dirac-agent-delay-between-boot-trials :int)
(assoc-env-val [:skip-logging-setup] :dirac-agent-skip-logging-setup :bool)
(assoc-env-val [:nrepl-server :host] :dirac-nrepl-server-host)
(assoc-env-val [:nrepl-server :port] :dirac-nrepl-server-port :int)
(assoc-env-val [:nrepl-tunnel :host] :dirac-nrepl-tunnel-host)
Expand All @@ -42,12 +43,7 @@

(defn get-effective-config* [& [config]]
(let [environ-config (get-environ-config)]
(log/debug "default config:" default-config)
(log/debug "environ config:" environ-config)
(log/debug "ad-hoc config:" config)
(log/debug "---------------")
(let [effective-config (deep-merge default-config environ-config (or config {}))]
(log/debug "effective config: " effective-config)
effective-config)))

(def ^:dynamic get-effective-config (memoize get-effective-config*)) ; assuming environ-config is constant
21 changes: 14 additions & 7 deletions src/agent/dirac/agent/logging.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns dirac.agent.logging
(require [clj-logging-config.log4j :as config]
[dirac.lib.logging :as logging]
[dirac.lib.utils :as utils]))
[dirac.lib.utils :as utils])
(:import (org.apache.log4j Level)))

(def base-options
{:level :info
Expand All @@ -10,11 +11,17 @@
(defn make-options [& [options]]
(merge base-options options))

(defn config->options [config]
(if-let [log-level (:log-level config)]
(let [level (Level/toLevel log-level Level/INFO)]
{:level level})))

; -- our default setup ------------------------------------------------------------------------------------------------------

(defn setup-logging! []
(logging/setup-logging!)
(config/set-loggers!
"dirac.agent" (make-options)
"dirac.agent.logging" (make-options)
"dirac.agent.config" (make-options)))
(defn setup-logging! [config]
(let [options (config->options config)]
(logging/setup-logging! options)
(config/set-loggers!
"dirac.agent" (make-options options)
"dirac.agent.logging" (make-options options)
"dirac.agent.config" (make-options options))))
96 changes: 96 additions & 0 deletions src/agent/dirac/agent_cli.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
(ns dirac.agent-cli
(:require [clojure.string :as string]
[clojure.tools.cli :refer [parse-opts]]
[dirac.agent :as agent])
(:gen-class))

(def port-validator [#(< 0 % 0x10000) "Must be a number between 0 and 65536"])
(def port-parser #(Integer/parseInt %))

; -- options ----------------------------------------------------------------------------------------------------------------

(def cli-options
[["-p" "--nrepl-tunnel-port PORT" "nREPL tunnel port number (listen)"
:parse-fn port-parser
:validate port-validator]
["-H" "--nrepl-tunnel-host HOST" "nREPL tunnel host (bind)"
:default-desc "localhost"]
["-q" "--nrepl-server-port PORT" "nREPL server port number"
:parse-fn port-parser
:validate port-validator]
["-I" "--nrepl-server-host HOST" "nREPL server host"
:default-desc "localhost"]
["-v" nil "Verbosity level; may be specified multiple times to increase value"
:id :verbosity
:default 0
:assoc-fn (fn [m k _] (update-in m [k] inc))]
["-h" "--help"]])

; -- helpers ----------------------------------------------------------------------------------------------------------------

(defn usage [options-summary]
(->> [(str "Dirac Agent connects to an existing nREPL server "
"and acts as a proxy which provides nREPL connections"
"to Dirac DevTools instances in the browser.")
""
"Usage: dirac-agent [options] action"
""
"Options:"
options-summary
""
"Actions:"
" start Start a new Dirac Agent server"
""]
(string/join \newline)))

(defn error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(string/join \newline errors)))

(defn exit [status msg]
(println msg)
(System/exit status))

; -- config conversion ------------------------------------------------------------------------------------------------------

(defn verbosity->log-level [verbosity]
(case verbosity
0 nil
1 "INFO"
2 "DEBUG"
3 "TRACE"
"ALL"))

(def config-mapping
[[:nrepl-tunnel-port [:nrepl-tunnel :port]]
[:nrepl-tunnel-host [:nrepl-tunnel :host]]
[:nrepl-server-port [:nrepl-server :port]]
[:nrepl-server-host [:nrepl-server :host]]
[:verbosity [:log-level]]])

(defn apply-config-option [options config mapping]
(let [[option config-path] mapping]
(if-let [option-value (option options)]
(assoc-in config config-path option-value)
config)))

(defn map-config-options [options mapping]
(reduce (partial apply-config-option options) {} mapping))

(defn options->config [options]
(-> options
(update :verbosity verbosity->log-level)
(map-config-options config-mapping)))

; -- main entry point -------------------------------------------------------------------------------------------------------

(defn -main [& args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
(cond
(:help options) (exit 0 (usage summary))
(not= (count arguments) 1) (exit 1 (usage summary))
errors (exit 1 (error-msg errors)))
(let [config (options->config options)]
(case (first arguments)
"start" (agent/boot! config)
(exit 1 (usage summary))))))
12 changes: 6 additions & 6 deletions src/lib/dirac/lib/logging.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

; -- our default setup ------------------------------------------------------------------------------------------------------

(defn setup-logging! []
(defn setup-logging! [options]
(config/set-loggers!
"dirac.lib.nrepl-client" (make-options)
"dirac.lib.nrepl-tunnel" (make-options)
"dirac.lib.nrepl-tunnel-server" (make-options)
"dirac.lib.weasel-server" (make-options)
"dirac.lib.ws-server" (make-options)))
"dirac.lib.nrepl-client" (make-options options)
"dirac.lib.nrepl-tunnel" (make-options options)
"dirac.lib.nrepl-tunnel-server" (make-options options)
"dirac.lib.weasel-server" (make-options options)
"dirac.lib.ws-server" (make-options options)))

0 comments on commit 19e1ef6

Please sign in to comment.