|
1 | 1 | (ns clj-libssh2.channel
|
2 | 2 | "Functions for manipulating channels within an SSH session."
|
3 | 3 | (:refer-clojure :exclude [read])
|
4 |
| - (:require [net.n01se.clojure-jna :as jna] |
5 |
| - [clj-libssh2.error :refer [handle-errors]] |
| 4 | + (:require [clojure.tools.logging :as log] |
| 5 | + [net.n01se.clojure-jna :as jna] |
| 6 | + [clj-libssh2.error :as error :refer [handle-errors]] |
6 | 7 | [clj-libssh2.libssh2 :as libssh2]
|
7 | 8 | [clj-libssh2.libssh2.channel :as libssh2-channel]
|
8 | 9 | [clj-libssh2.libssh2.scp :as libssh2-scp]
|
|
23 | 24 |
|
24 | 25 | 0 on success. An exception will be thrown if an error occurs."
|
25 | 26 | [session channel]
|
| 27 | + (log/info "Closing channel.") |
26 | 28 | (block session
|
27 | 29 | (handle-errors session (libssh2-channel/close channel))))
|
28 | 30 |
|
|
41 | 43 |
|
42 | 44 | 0 on success. An exception will be thrown if an error occurs."
|
43 | 45 | [session channel commandline]
|
| 46 | + (log/info "Executing a command on the remote host.") |
44 | 47 | (block session
|
45 | 48 | (handle-errors session (libssh2-channel/exec channel commandline))))
|
46 | 49 |
|
|
63 | 66 | :err-msg The error message.
|
64 | 67 | :lang-tag The language tag, if provided."
|
65 | 68 | [session channel]
|
| 69 | + (log/info "Collecting the exit signal data from the remote host.") |
66 | 70 | (let [->str (fn [string-ref length-ref]
|
67 | 71 | (let [string (.getValue string-ref)
|
68 | 72 | length (.getValue length-ref)]
|
|
97 | 101 |
|
98 | 102 | The numeric exit code from the remote process."
|
99 | 103 | [channel]
|
| 104 | + (log/info "Collecting the exit code from the remote process.") |
100 | 105 | (libssh2-channel/get-exit-status channel))
|
101 | 106 |
|
102 | 107 | (defn free
|
|
125 | 130 |
|
126 | 131 | A newly-allocated channel object, or throws exception on failure."
|
127 | 132 | [session]
|
| 133 | + (log/info "Opening a new channel.") |
128 | 134 | (block-return session (libssh2-channel/open-session (:session session))))
|
129 | 135 |
|
130 | 136 | (defn open-scp-recv
|
|
141 | 147 | the file information as reported by the remote host. Throws exception on
|
142 | 148 | failure."
|
143 | 149 | [session remote-path]
|
| 150 | + (log/info "Opening a new channel to receive a file using SCP.") |
144 | 151 | (let [fileinfo (Stat/newInstance)]
|
145 | 152 | {:channel (block-return session
|
146 | 153 | (libssh2-scp/recv (:session session) remote-path fileinfo))
|
|
164 | 171 |
|
165 | 172 | A newly-allocated channel object for sending a file via SCP."
|
166 | 173 | [session remote-path {:keys [atime mode mtime size] :as props}]
|
| 174 | + (log/info "Opening a new channel to send a file using SCP.") |
167 | 175 | (let [mode (or mode 0644)
|
168 | 176 | mtime (or mtime 0)
|
169 | 177 | atime (or atime 0)]
|
|
182 | 190 |
|
183 | 191 | 0 on success, throws an exception on failure."
|
184 | 192 | [session channel]
|
| 193 | + (log/info "Notifying the remote host of EOF") |
185 | 194 | (block session (handle-errors session (libssh2-channel/send-eof channel))))
|
186 | 195 |
|
187 | 196 | (defn setenv
|
|
204 | 213 |
|
205 | 214 | 0 on success. Errors will result in exceptions."
|
206 | 215 | [session channel env]
|
| 216 | + (log/info "Setting environment variables on the remote host.") |
207 | 217 | (let [->str (fn [v]
|
208 | 218 | (cond (nil? v) ""
|
209 | 219 | (keyword? v) (name v)
|
210 | 220 | :else (str v)))
|
211 | 221 | fail-if-forbidden (fn [ret]
|
212 | 222 | (if (= libssh2/ERROR_CHANNEL_REQUEST_DENIED ret)
|
213 |
| - (throw (ex-info "Setting environment variables is not permitted." |
214 |
| - {:session session})) |
| 223 | + (error/raise "Setting environment variables is not permitted." |
| 224 | + {:session session}) |
215 | 225 | ret))]
|
216 | 226 | (doseq [[k v] env]
|
217 | 227 | (block session
|
|
394 | 404 | (when (and (= pump-fn pull)
|
395 | 405 | (= :eagain new-status)
|
396 | 406 | (< (-> session :options :read-timeout) (- now last-read-time)))
|
397 |
| - (throw (ex-info "Read timeout on a channel." |
398 |
| - {:direction (-> stream :direction name) |
399 |
| - :id (-> stream :id) |
400 |
| - :timeout (-> session :options :read-timeout) |
401 |
| - :session session}))) |
| 407 | + (error/raise "Read timeout on a channel." |
| 408 | + {:direction (-> stream :direction name) |
| 409 | + :id (-> stream :id) |
| 410 | + :timeout (-> session :options :read-timeout) |
| 411 | + :session session})) |
402 | 412 | (assoc stream :status new-status :last-read-time now))
|
403 | 413 | stream))
|
404 | 414 |
|
|
427 | 437 | :status Always :eof
|
428 | 438 | :stream The OutputStream object connected to the SSH stream."
|
429 | 439 | [session channel input-streams output-streams]
|
| 440 | + (log/info "Pumping all the streams on a channel.") |
430 | 441 | (let [now (System/currentTimeMillis)
|
431 | 442 | write-size (-> session :options :write-chunk-size)
|
432 | 443 | streams (concat (->> input-streams
|
|
0 commit comments