<commit_before>(ns circle.backend.ec2
  (:use [circle.aws-credentials :only (aws-credentials)])
  (:use [clojure.tools.logging :only (infof)])
  (:import com.amazonaws.services.ec2.AmazonEC2Client
           (com.amazonaws.services.ec2.model DescribeInstancesResult
                                             TerminateInstancesRequest
                                             DeleteSecurityGroupRequest
                                             DeleteKeyPairRequest)))

(defmacro with-ec2-client
  [client & body]
  `(let [~client (AmazonEC2Client. aws-credentials)]
     ~@body))

(defn availability-zones []
  (-> (AmazonEC2Client. aws-credentials)
      (.describeAvailabilityZones)
      (.getAvailabilityZones)
      (->>
       (map (fn [az]
              {:zone (.getZoneName az)
               :region (.getRegionName az)
               :status (.getState az)})))))

(defn reservations
  "returns all ec2 reservations"
  []
  (with-ec2-client client
    (-> client
        (.describeInstances)
        (.getReservations)
        (->> (map bean)))))

(defn instances []
  (->>
   (reservations)
   (mapcat :instances)
   (map bean)))
  
(defn all-instance-ids []
  (map :instanceId (instances)))

(defn instance [instance-id]
  (first (filter #(= instance-id (:instanceId %)) (instances))))

(defn get-availability-zone [instance-id]
  (-> instance-id
      (instance)
      :placement
      (.getAvailabilityZone)))

(defn terminate-instances [instance-ids]
  (with-ec2-client client
    (-> client
        (.terminateInstances (TerminateInstancesRequest. instance-ids))
        (bean))))

(defn terminate-all-instances []
  (terminate-instances (all-instance-ids)))

(defn security-groups
  []
  (with-ec2-client client
    (-> client
        (.describeSecurityGroups)
        (.getSecurityGroups)
        (->>
         (map bean)))))

(defn delete-group [group-name]
  (with-ec2-client client
    (-> client
        (.deleteSecurityGroup (DeleteSecurityGroupRequest. group-name)))))

(defn delete-groups-matching
  "delete all ec2 security groups matching the regex. Mainly used as repl workaround for jclouds bugs"
  [regex]
  (doseq [group (filter #(re-find regex %) (map :groupName (security-groups)))]
    (println "deleting" group)
    (delete-group group)))

(defn keypairs []
  (with-ec2-client client
    (-> client
        (.describeKeyPairs)
        (.getKeyPairs)
        (->>
         (map bean)))))

(defn delete-keypair
  [name]
  (println "deleting keypair %s" name)
  (with-ec2-client client
    (-> client
        (.deleteKeyPair (DeleteKeyPairRequest. name)))))

(defn delete-keypairs-matching [re]
  (doseq [kp (filter #(re-find re (:keyName %)) (keypairs))]
    (delete-keypair (-> kp :keyName))))<commit_msg>Add delete keypair fns<commit_after>(ns circle.backend.ec2
  (:use [circle.aws-credentials :only (aws-credentials)])
  (:use [clojure.tools.logging :only (infof)])
  (:use [clojure.core.incubator :only (-?>)])
  (:import com.amazonaws.services.ec2.AmazonEC2Client
           (com.amazonaws.services.ec2.model DescribeInstancesResult
                                             TerminateInstancesRequest
                                             DeleteSecurityGroupRequest
                                             DeleteKeyPairRequest)))

(defmacro with-ec2-client
  [client & body]
  `(let [~client (AmazonEC2Client. aws-credentials)]
     ~@body))

(defn availability-zones []
  (-> (AmazonEC2Client. aws-credentials)
      (.describeAvailabilityZones)
      (.getAvailabilityZones)
      (->>
       (map (fn [az]
              {:zone (.getZoneName az)
               :region (.getRegionName az)
               :status (.getState az)})))))

(defn reservations
  "returns all ec2 reservations"
  []
  (with-ec2-client client
    (-> client
        (.describeInstances)
        (.getReservations)
        (->> (map bean)))))

(defn instances []
  (->>
   (reservations)
   (mapcat :instances)
   (map bean)
   (filter #(not= :terminated (-?> % :state (bean) :name (keyword))))))
  
(defn all-instance-ids []
  (map :instanceId (instances)))

(defn instance [instance-id]
  (first (filter #(= instance-id (:instanceId %)) (instances))))

(defn get-availability-zone [instance-id]
  (-> instance-id
      (instance)
      :placement
      (.getAvailabilityZone)))

(defn terminate-instances [instance-ids]
  (with-ec2-client client
    (-> client
        (.terminateInstances (TerminateInstancesRequest. instance-ids))
        (bean))))

(defn terminate-all-instances []
  (terminate-instances (all-instance-ids)))

(defn security-groups
  []
  (with-ec2-client client
    (-> client
        (.describeSecurityGroups)
        (.getSecurityGroups)
        (->>
         (map bean)))))

(defn delete-group [group-name]
  (with-ec2-client client
    (-> client
        (.deleteSecurityGroup (DeleteSecurityGroupRequest. group-name)))))

(defn delete-groups-matching
  "delete all ec2 security groups matching the regex. Mainly used as repl workaround for jclouds bugs"
  [regex]
  (doseq [group (filter #(re-find regex %) (map :groupName (security-groups)))]
    (println "deleting" group)
    (delete-group group)))

(defn keypairs []
  (with-ec2-client client
    (-> client
        (.describeKeyPairs)
        (.getKeyPairs)
        (->>
         (map bean)))))

(defn delete-keypair
  [name]
  (println "deleting keypair" name)
  (with-ec2-client client
    (-> client
        (.deleteKeyPair (DeleteKeyPairRequest. name)))))

(defn delete-keypairs-matching [re]
  (doseq [kp (filter #(re-find re (:keyName %)) (keypairs))]
    (delete-keypair (-> kp :keyName))))

(defn delete-unused-jclouds-keypairs
  []
  (let [keep (into #{} (map :keyName (instances)))]
    (doseq [k (->> (keypairs)
                   (filter #(re-find #"^jclouds" (:keyName %)))
                   (filter #(not (contains? keep (:keyName %)))))]
      (delete-keypair (:keyName k)))))
{'fraction_non_alphanumeric': 0.14831726703872822, 'fraction_numerical': 0.0050735667174023336, 'mean_word_length': 2.537081339712919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2730606', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1861, 'n_words': 402}