Fork me on GitHub
#babashka
<
2022-12-23
>
Ajay Jakate04:12:15

Hey folks! I had a quick question about about epiccastle/bbssh pod if anyone is familiar. I'm using this library to execute a long running process on a remote host with ffmpeg. By default, and in all the example docs you can execute a remote command and have the output sent back to your local machine, but as a future which will block derefing until the command exits. Since it's a command I anticipate taking over an hour, I'm hoping to stream the ffmpeg output continuously. Is there a simple way to do this? I'm getting close with using a :stream output and transforming the input stream manually but I just wanted to double check and make sure there wasn't a much simpler method that I am missing...

Ajay Jakate04:12:53

Here is what I have working but takes a long time to show results:

(let [session (bbssh/ssh "remote_host"
                         {:username "ubuntu"
                          :port 22
                          :identity "~/my_ssh_key"})]
  (-> (bbssh/exec session "ffmpeg -convert super long video" {:out :string})
      deref
      :out))

Ajay Jakate04:12:15

and here is the solution I'm moving towards:

(let [session (bbssh/ssh "remote_host"
                         {:username "ubuntu"
                          :port 22
                          :identity "~/my_ssh_key"})]
  (let [command (bbssh/exec session "ffmpeg -convert super long video" {:out :stream})]
    ;; (println (char(.read (:out command))) returns immediately here, but not print...
    ;; I was going to try to run some kind of long reduce to repeatly grab chars and
    ;; println them with every readline
    ))

Ajay Jakate05:12:36

actually I ended up getting it working using this... not sure if it's the most elegant solution but works ok so far

(let [session (bbssh/ssh "remote_host"
                         {:username "ubuntu"
                          :port 22
                          :identity "~/my_ssh_key"})]
  (let [command (bbssh/exec session "ffmpeg -convert super long video" {:out :stream})
        line (atom "")]
    (loop [c (.read (:out command))]
      (when (not= c -1)
        (if (= \newline (char c))
          (do
            (println @line)
            (reset! line ""))
          (swap! line  #(str % (char c))))
        (recur (.read (:out command)))))))

borkdude08:12:16

yeah this works. you can also use (with-open [stream (io/reader (:out command))] .. do something with .. (line-seq stream))

👍 1
borkdude10:12:33

I just had an idea: A site like https://explainshell.com/explain?cmd=rm+-rf but with links / pointers to the relevant babashka functions / recipes. I probably won't have time to run this myself, but if anyone wants to pick this up as a project, feel free

🤓 5
💯 1