 
 (ns kvm.networking
   (:require 
+    [celestial.provider :refer (wait-for)]
     [slingshot.slingshot :refer [throw+]]
     [taoensso.timbre :as timbre]
     [clojure.core.strint :refer (<<)]

   [c id node]
   (let [[nic mac] (first (macs c id)) uuid (gen-uuid)]
     (execute (<< "arp -i ~{nic}") node :out-fn (collect-log uuid))        
     (if-let [line (first (filter #(.contains % mac) (get-log uuid)))]
       (first (.split line "\\s" ))
       (throw+ {:type ::kvm:networking} "Failed to grab domain Nat IP")
       )))

(def ignore-authenticity "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no")



(defn public-ip
  [c user node id & {:keys [public-nic] :or {public-nic "eth1"}}]
   (wait-for-nat c id node [5 :minute])
   (let [uuid (gen-uuid) nat (nat-ip c id node)
         cmd (<< "ssh ~{ignore-authenticity} ~{user}@~{nat} -C 'ifconfig ~{public-nic}'")]
     (execute cmd node :out-fn (collect-log uuid))
