   Openvz
   (unmount [this]
     (use-ns)
+    (debug "unmounting" (:vmid spec))
     (try+
       (safe 
         (prox-post (str "/nodes/" node "/openvz/" (:vmid spec) "/status/umount")))

  (use 'com.narkisr.proxmox.provider))

(defmacro safe [f]
  `(try+ (check-task ~f)
         (catch [:status 500] e# (warn  "Container does not exist"))))

(defprotocol Openvz
  (unmount [this]))

(deftype Container [node spec]
  Vm
  (create [this] 
    (use-ns)
    (let [errors (validate spec)]
      (when-not (empty? errors) 
        (throw (ExceptionInfo. "Failed to validate spec" errors)))
      (try+ 
        (check-task (prox-post (str "/nodes/" node "/openvz") spec))
        (catch [:status 500] e 
          (warn "Container already exists" e))
        )))

  (delete [this]
    (use-ns)
    (.unmount this)
    (safe
      (prox-delete (str "/nodes/" node "/openvz/" (:vmid spec)))))

  (start [this]
    (use-ns)
    (safe
      (prox-post (str "/nodes/" node "/openvz/" (:vmid spec) "/status/start"))))

  (stop [this]
    (use-ns)
    (safe 
      (prox-post (str "/nodes/" node "/openvz/" (:vmid spec) "/status/stop"))))


(defn validate [spec]
  (schema/report-errors (schema/validate ct-schema spec)))

(defn task-status [node upid]
  (prox-get (str "/nodes/" node  "/tasks/" upid "/status")))

(defn wait-for [node upid]
  (while (= "running" (:status (task-status node upid)))
    (Thread/sleep 200)
    (debug "Waiting for task" upid "to end")) )

(defn check-task
  "Checking that a proxmox task has succeeded"
  [node upid]
  (wait-for node upid)  
  (let [{:keys [exitstatus] :as res} (task-status node upid)]
    (when (not= exitstatus "OK")
      (throw+ (assoc res :type ::task-failed)))))

