 
 (import-logging)
 
-(defn creds [] (hypervisor :aws))
+(defn creds [] (dissoc (hypervisor :aws) :ostemplates))
 
 (defn wait-for-status [instance req-stat timeout]
   "Waiting for ec2 machine status timeout is in mili"

        {:type ::aws:ebs-volume-availability :message "Failed to wait for ebs volume to become available"})
     (with-ctx ebs/delete-volume (ebs :volume-id))))

(defconstrainedrecord Instance [endpoint spec user]
  "An Ec2 instance"
  [(provider-validation spec) (-> endpoint nil? not)]
  Vm
  (create [this] 
    (let [{:keys [aws]} spec instance-id (-> (with-ctx ec2/run-instances (dissoc aws :volumes)) :instances first :id)]
       (s/partial-system (spec :system-id) {:aws {:instance-id instance-id}})
       (debug "created" instance-id)
       (handle-volumes aws endpoint instance-id)    
       (when-let [ip (get-in spec [:machine :ip])] 
         (debug (<<  "Associating existing ip ~{ip} to instance-id"))
          (with-ctx eip/assoc-pub-ip instance-id ip))

      (s/partial-system (spec :system-id) 
        {:machine {:ip (pubdns-to-ip ec2-host)}}))))

(defn set-hostname [{:keys [aws machine] :as spec} endpoint instance-id user]
  "Uses a generic method of setting hostname in Linux (see http://www.debianadmin.com/manpages/sysctlmanpage.txt)
   Note that in ec2 both Centos and Ubuntu use sudo!"
  (let [{:keys [hostname domain os]} machine 
        remote {:host (pub-dns endpoint instance-id) :user user}]
    (execute (<< "echo kernel.hostname=~{hostname} | sudo tee -a /etc/sysctl.conf") remote )
    (execute (<< "echo kernel.domainname=\"~{hostname}.~{domain}\" | sudo tee -a /etc/sysctl.conf") remote )
    (execute (<< "echo 127.0.1.1 ~{hostname}.~{domain} ~{hostname} | sudo tee -a /etc/hosts") remote )
    (case (hypervisor :aws :ostemplates os :flavor)
      :debian  (execute (<< "echo ~{hostname} | sudo tee /etc/hostname") remote )
      :redhat  (execute (<< "/bin/sed -i 's/HOSTNAME=.*/HOSTNAME=~{hostname}.~{domain}/g") remote )
      (throw+ ::no-matching-flavor :msg (<< "no os flavor found for ~{os}"))
      )
    (execute "sudo sysctl -e -p" remote) 
    (with-ctx ec2/create-tags [(instance-desc endpoint instance-id :id)] {:Name hostname})
    ))
