         acyclic.utils.pinhole)
   (:require [clj-ssh.ssh :as ssh]
             [taoensso.timbre :as timbre]
-            [ clojure.core.async :as async 
+            [clojure.data.codec.base64 :as b64]
+            [clojure.core.async :as async 
              :refer [<! >! <!! >!! timeout chan alt!! go close!]]))
 (timbre/refer-timbre)
 
+; ami-5cd51634
+
+
+
 (def cred (read-string (slurp "AWS.clj")))
 
 (apply defcredential (map cred [:access-key :secret-key :endpoint]))
 
+(defn s->b64 [s] (String. (b64/encode (.getBytes s))))
 
 
 (def ^{:private true} paths {:zone  [:launch-specification :placement :availability-zone]
-                             :itype  [:launch-specification :instance-type]
+                             :itype [:launch-specification :instance-type]
                              :price [:spot-price]
-                             :n     [:instance-count]})
+                             :n     [:instance-count]
+                             :udata [s->b64 :launch-specification :user-data]})
+
 
 (defmacro go-try [& forms]
   `(go (try (do ~@forms)

cfn or nil, if one of them failed along the way."
  [x & cfns]
  (let [out (chan)]
    (async/go-loop [[f & fs] cfns
                    x        x]
      (if-not f
        (do (debug "Finished" cfns)
            (>! out x))
        (let [y (<! (f x))]
          (if-not y
            (do (debug "Failed at" f)
                (close! out))


(def ag (ssh/ssh-agent {}))

(defn ssh-session [host]
  (let [sess (ssh/session ag host {:strict-host-key-checking :no
                                   :username "ec2-user"})]
    (<!! (async/go-loop [n 10]
           (cond
            (ssh/connected? sess) sess
            (zero? n) (do (debug "Failed to connect to" host) nil)
            :else     (do
                        (ssh/connect sess)
                        (if (ssh/connected? sess)
                          sess
                          (do
                            (<! (timeout 1000))
                            (recur (dec n))))))))))


(defn ssh-sessions [hosts]
  (let [sess (map #()
                  hosts)]
    (loop [n 10]
           (let [o (map #(or (ssh/connected? %) (ssh/connect %)) sess)]
