Permalink
Browse files

changed ssh-exec interface

  • Loading branch information...
1 parent d8c69aa commit 361b36598734cb4486776aab1a34ac5f5d02f83c @exceedhl committed Aug 25, 2012
Showing with 51 additions and 52 deletions.
  1. +12 −17 spec/clabric/dsl_spec.clj
  2. +12 −14 spec/clabric/ssh_spec.clj
  3. +6 −4 spec/clabric/task_spec.clj
  4. +1 −1 src/clabric/dsl.clj
  5. +16 −15 src/clabric/ssh.clj
  6. +4 −1 src/clabric/task.clj
View
@@ -7,11 +7,8 @@
[clabric.task]
[clojure.contrib.mock]))
-(defn mock-ssh-session [opt]
- opt)
-
-(defn mock-ssh-exec [session command]
- (merge session {:command command}))
+(defn mock-ssh-exec [command options]
+ (merge options {:command command}))
(describe "DSL"
@@ -20,23 +17,21 @@
(it "should pass arguments to ssh"
(deftask t1 ["host1"] "task t1"
(run "ls"))
- (with-redefs [ssh-session mock-ssh-session
- ssh-exec mock-ssh-exec]
+ (with-redefs [ssh-exec mock-ssh-exec]
(should= {:host "host1" :opt1 "opt1" :command "ls"}
(first (execute t1 :opt1 "opt1")))))
(it "should be able to override option"
(deftask t1 ["host1"] "task t1"
(run "ls" :opt1 "new opt1" :opt2 "opt2"))
- (with-redefs [ssh-session mock-ssh-session
- ssh-exec mock-ssh-exec]
+ (with-redefs [ssh-exec mock-ssh-exec]
(should= {:host "host1" :opt1 "new opt1" :opt2 "opt2" :command "ls"}
(first (execute t1 :opt1 "opt1"))))))
(context "cmd command"
(it "should be able to execute commands with options"
- (defn cmd-test [command options expected-exit-code expected-output expected-error]
+ (defn cmd-exec-test [command options expected-exit-code expected-output expected-error]
(let [result (cmd-exec command options)]
(if (fn? expected-exit-code)
(expected-exit-code (:exit result))
@@ -48,12 +43,12 @@
(expected-error (:err result))
(should= expected-error (:err result)))))
- (cmd-test "ls -la" {} 0 #(should (re-find #"drwxr-xr-x" %1)) "")
- (cmd-test "whoami" {} 0 (str (current-user) "\n") "")
- (cmd-test "pwd" {:dir "/usr"} 0 "/usr\n" "")
- (cmd-test "wrong-command" {} #(should-not= 0 %1) "" #(should (re-find #"No such file or directory" %1)))
- (cmd-test "find /usr -name bin -d 1" {} 0 "/usr/bin\n" "")
- (cmd-test "pwd | wc -c | sed -e s/^[[:space:]]*//" {:dir "/usr"} 0 "5\n" "")
+ (cmd-exec-test "ls -la" {} 0 #(should (re-find #"drwxr-xr-x" %1)) "")
+ (cmd-exec-test "whoami" {} 0 (str (current-user) "\n") "")
+ (cmd-exec-test "pwd" {:dir "/usr"} 0 "/usr\n" "")
+ (cmd-exec-test "wrong-command" {} #(should-not= 0 %1) "" #(should (re-find #"No such file or directory" %1)))
+ (cmd-exec-test "find /usr -name bin -d 1" {} 0 "/usr/bin\n" "")
+ (cmd-exec-test "pwd | wc -c | sed -e s/^[[:space:]]*//" {:dir "/usr"} 0 "5\n" "")
(should-throw (cmd-exec "" {}))
(should-throw (cmd-exec "pwd | " {})))
@@ -69,4 +64,4 @@
(cmd "pwd" :dir "/usr"))
(let [result (execute t1 :dir "/tmp")]
(should= 0 (:exit result))
- (should= "/usr\n" (:out result))))))
+ (should= "/usr\n" (:out result))))))
View
@@ -11,22 +11,19 @@
(with port 22)
(with user "vagrant")
(with private_key_path (str (current-user-home) "/.ssh/vagrant_private_key"))
- (with session-params {:host @host :port @port :user @user
- :private_key_path @private_key_path})
- (with session
- (ssh-session @session-params))
+ (with ssh-exec-default #(ssh-exec %1 {:host @host :user @user :port @port :private_key_path @private_key_path}))
(it "should be able to execute simple command successfully"
- (let [result (ssh-exec @session "whoami")
+ (let [result (@ssh-exec-default "whoami")
exit (:exit result)
output (:out result)
error (:err result)]
(should= 0 exit)
(should= (str @user "\n") output)
(should= "" error)))
- (it "should be able to execute complex commands successfully"
- (let [result (ssh-exec @session "cd /tmp; pwd | wc -c")
+ (it "should be able to execute commands successfully"
+ (let [result (@ssh-exec-default "cd /tmp; pwd | wc -c")
exit (:exit result)
output (:out result)
error (:err result)]
@@ -35,7 +32,7 @@
(should= "" error)))
(it "should return error if command execution failed"
- (let [result (ssh-exec @session "wrong-command")
+ (let [result (@ssh-exec-default "wrong-command")
exit (:exit result)
output (:out result)
error (:err result)]
@@ -48,29 +45,30 @@
(context "with not enough parameters"
(with session-params {:host @host})
(it "should use default value"
- (should= (current-user) (.getUserName @session))
- (should= 22 (.getPort @session))))
+ (let [session (ssh-session @session-params)]
+ (should= (current-user) (.getUserName session))
+ (should= 22 (.getPort session)))))
(context "with wrong key"
(with private_key_path "non-existing key")
(it "should throw exception"
(should-throw JSchException "java.io.FileNotFoundException: non-existing key (No such file or directory)"
- (ssh-exec @session "ls"))))
+ (@ssh-exec-default "ls"))))
(context "with non-existing host"
(with host "non-existing host")
(it "should throw exception"
(should-throw JSchException "java.net.UnknownHostException: non-existing host"
- (ssh-exec @session "ls"))))
+ (@ssh-exec-default "ls"))))
(context "with wrong port"
(with port 55555)
(it "should throw exception"
(should-throw JSchException "java.net.ConnectException: Connection refused"
- (ssh-exec @session "ls"))))
+ (@ssh-exec-default "ls"))))
(context "with wrong user"
(with user "non-existing-user")
(it "should throw exception"
(should-throw JSchException "Auth fail"
- (ssh-exec @session "ls")))))
+ (@ssh-exec-default "ls")))))
View
@@ -1,7 +1,7 @@
(ns clabric.task-spec
(:use
- [speclj.core]
- [clabric.task]))
+ [speclj.core]
+ [clabric.task]))
(defn distributed-action []
(distribute (fn [option]
@@ -83,6 +83,8 @@
(deftask t1 ["host1" "host2"] "task t1")
(should= ["host1" "host2"] (:hosts t1)))
- (it "should merge duplicate hosts while define with duplicate host entries")))
+ (it "should merge duplicate hosts while define with duplicate host entries"
+ (deftask t1 ["host1" "host1" "host2"] "task t1")
+ (should= ["host1" "host2"] (:hosts t1)))))
-(run-specs)
+(run-specs)
View
@@ -7,7 +7,7 @@
(defn run [command & options]
(distribute (fn [option]
(let [option (merge option (apply array-map options))]
- (ssh-exec (ssh-session option) command)))))
+ (ssh-exec command option)))))
(defn- split-and-filter-cmds [cmds]
(map #(vec (filter (complement empty?) (split %1 (re-pattern "\\s+"))))
View
@@ -26,18 +26,19 @@
~@body
(finally (.disconnect ~ch))))
-(defn ssh-exec [session command]
- (with-connected-session session
- (let [out (ByteArrayOutputStream.)
- err (ByteArrayOutputStream.)
- ^ChannelExec exec (.openChannel session "exec")]
- (doto exec
- (.setOutputStream out)
- (.setErrStream err)
- (.setCommand command))
- (with-connected-channel exec
- (while (.isConnected exec)
- (Thread/sleep 100))
- ;; (if (> (.size out) 0) (info (.toString out)))
- ;; (if (> (.size err) 0) (error (.toString err)))
- {:exit (.getExitStatus exec) :out (.toString out) :err (.toString err)}))))
+(defn ssh-exec [command options]
+ (let [session (ssh-session options)]
+ (with-connected-session session
+ (let [out (ByteArrayOutputStream.)
+ err (ByteArrayOutputStream.)
+ ^ChannelExec exec (.openChannel session "exec")]
+ (doto exec
+ (.setOutputStream out)
+ (.setErrStream err)
+ (.setCommand command))
+ (with-connected-channel exec
+ (while (.isConnected exec)
+ (Thread/sleep 100))
+ ;; (if (> (.size out) 0) (info (.toString out)))
+ ;; (if (> (.size err) 0) (error (.toString err)))
+ {:exit (.getExitStatus exec) :out (.toString out) :err (.toString err)})))))
View
@@ -5,9 +5,12 @@
(defrecord Task [^String name hosts ^String description body])
+(defn- merge-hosts [hosts]
+ (vec (set hosts)))
+
(defmacro deftask
[name hosts description & body]
- `(def ~name (Task. (name '~name) ~hosts ~description (fn [] ~@body))))
+ `(def ~name (Task. (name '~name) ~(merge-hosts hosts) ~description (fn [] ~@body))))
(defn execute [task & options]
(binding [*hosts* (:hosts task)

0 comments on commit 361b365

Please sign in to comment.