Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active July 31, 2018 16:33
Show Gist options
  • Select an option

  • Save alandipert/d2cb38ee869448182c4b to your computer and use it in GitHub Desktop.

Select an option

Save alandipert/d2cb38ee869448182c4b to your computer and use it in GitHub Desktop.

Revisions

  1. alandipert revised this gist Dec 30, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ec2query.boot
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    :endpoint "us-east-1"})

    (defn paths
    "Enumerate set of paths, with values at each path, for a nested map/vector."
    "Enumerate set of paths in a nested map/vector."
    ([root]
    (when (or (map? root) (vector? root))
    (paths [] root)))
  2. alandipert revised this gist Dec 30, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ec2query.boot
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,6 @@
    "Print out instance ids and public DNS names (if available) of instances in a particular state."
    [s state STATE str "State of the instances to search for."
    t tags KEY=VALUE {str str} "Tags to filter by (optional). For example: ./ec2query.boot -s running -t System=Reporting"]
    (let [state (or state (do (info "No state supplied, defaulting to 'running'") "running"))]
    (let [state (or state (do (info "No state supplied, defaulting to 'running'\n") "running"))]
    (doseq [[id dns] (get-instances (make-db @instances) state tags)]
    (println id dns))))
    (println id dns))))
  3. alandipert created this gist Dec 30, 2015.
    76 changes: 76 additions & 0 deletions ec2query.boot
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/usr/bin/env boot
    ;; or `BOOT_FILE=ec2query.boot boot repl' for interactive use

    (set-env! :dependencies '[[amazonica "0.3.23"]
    [com.datomic/datomic-free "0.9.5344"]])

    (require '[amazonica.aws.ec2 :as ec2]
    '[amazonica.core :refer [defcredential]]
    '[boot.cli :refer [defclifn]]
    '[boot.util :refer [info]]
    '[datomic.api :refer [db q] :as d])

    (defcredential
    {:access-key (System/getenv "AWS_ACCESS_KEY")
    :secret-key (System/getenv "AWS_SECRET_KEY")
    :endpoint "us-east-1"})

    (defn paths
    "Enumerate set of paths, with values at each path, for a nested map/vector."
    ([root]
    (when (or (map? root) (vector? root))
    (paths [] root)))
    ([parent x]
    (cond (map? x)
    (mapcat (fn [[k v]] (paths (conj parent k) v)) x)
    (vector? x)
    (mapcat #(paths (conj parent %1) %2) (range) x)
    :else [parent])))

    (defn map->tuples
    "Returns every path and value in the map as a set of tuples, each prefixed
    with the supplied id."
    [id m]
    (mapv #(conj % (get-in {id m} %)) (paths {id m})))

    (def instances
    (reify clojure.lang.IDeref
    (deref [_]
    (->> (ec2/describe-instances)
    :reservations
    (mapcat :instances)))))

    (defn make-db [maps]
    (mapcat #(map->tuples %1 %2) (range) maps))

    (def +instance-states+
    #{"pending" "running" "stopping" "stopped" "shutting-down" "terminated" "rebooting"})

    (defn tags->tuples [id tag-map]
    (when (seq tag-map)
    (reduce-kv
    #(let [e (gensym "?e")]
    (into %1 [[id :tags e :key %2]
    [id :tags e :value %3]]))
    []
    tag-map)))

    (defn get-instances [db state tag-map]
    {:pre [(contains? +instance-states+ state)]}
    (q (concat '[:find ?instance-id ?public-dns
    :in $ ?state
    :where
    [?id :state :name ?state]
    [?id :instance-id ?instance-id]
    [?id :public-dns-name ?public-dns]]
    (tags->tuples '?id tag-map))
    db
    state))

    (defclifn -main
    "Print out instance ids and public DNS names (if available) of instances in a particular state."
    [s state STATE str "State of the instances to search for."
    t tags KEY=VALUE {str str} "Tags to filter by (optional). For example: ./ec2query.boot -s running -t System=Reporting"]
    (let [state (or state (do (info "No state supplied, defaulting to 'running'") "running"))]
    (doseq [[id dns] (get-instances (make-db @instances) state tags)]
    (println id dns))))