git.fiddlerwoaroof.com
Raw Blame History
(ns my-infra.core
  (:use [amazonica.aws.ec2])
  (:gen-class))

(defn mapply [f & args]
  (apply f (apply concat (butlast args) (last args))))

(defn ^:private id-dispatch-fn [type descriptor]
  type)

(defmulti id
  #'id-dispatch-fn)

(defmethod id ::vpc [_ thing]
  (:vpc-id thing))

(defmethod id ::internet-gateway [_ thing]
  (:internet-gateway-id thing))
           
(defmethod id ::subnet [_ thing]
  (:subnet-id thing))
           
(defmethod id ::route-table [_ thing]
  (:route-table-id thing))

(defn ^:private create-dispatch-fn [a & r]
  (::type a))

(defn ^:private params-dispatch-fn [a & r]
  (::type a))

(defmulti params
  #'params-dispatch-fn)

(defmulti create 
  #'create-dispatch-fn)

(defn unwrap [thing]
  (dissoc thing ::type))

(defmethod create ::vpc [thing cred & r]
  (:vpc (mapply create-vpc cred
                (unwrap thing))))

(defmethod create ::subnet [thing cred vpc & r]
  (:subnet (mapply create-subnet cred
                   (assoc (unwrap thing)
                          :vpc-id (id ::vpc vpc)))))

(defmethod create ::route-table [thing cred vpc & r]
  (:route-table (mapply create-route-table cred
                        (assoc (unwrap thing)
                               :vpc-id (id ::vpc vpc)))))

(defmethod create ::route [thing cred route-table & r]
  (:route (mapply create-route cred
                  (assoc (unwrap thing)
                         :route-table-id (id ::route-table route-table)))))

(defmethod create ::internet-gateway [thing cred vpc & r]
  (let [result (:internet-gateway (mapply create-internet-gateway cred
                                          (unwrap thing)))]
    (println (attach-internet-gateway cred
                                      :internet-gateway-id (id ::internet-gateway result)
                                      :vpc-id (id ::vpc vpc)))
    result))

(def cred {:profile "personal"
           :endpoint "us-west-2"})

(defmethod params ::my-vpc [thing & r]
  {:cidr-block "172.16.234.0/24"})

(defmethod params ::my-subnet [thing vpc & r]
  {:vpc-id (id ::vpc vpc)
   :cidr-block "172.16.234.0/25"
   :associate-public-ip-address true})

(defmethod params ::my-route-table [thing vpc & r]
  {:vpc-id (id ::vpc vpc)})

(defmulti route-type identity)
(defmethod route-type ::internet-gateway [_] :gateway-id)

(defn make-routes [route-table cred vpc subnet & routes]
  (clojure.pprint/pprint routes)
  (doall
   (map (fn [[destination target-type target :as first] other]
          (create {::type ::route
                   :destination-cidr-block destination
                   (route-type target-type) (id target-type target)}
                  route-table))
        routes))
  (associate-route-table cred
                         :route-table-id (id ::route-table route-table)
                         :subnet-id (id ::subnet subnet))
  route-table)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; new-api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def vpc (create {::type ::vpc
                  :cidr-block "10.1.0.0/16"}
                 cred))

(def subnet (create {::type ::subnet
                     :cidr-block "10.1.1.0/24"
                     :associate-public-ip-address true}
                    cred
                    vpc))

(def internet-gateway (create {::type ::internet-gateway}
                              cred
                              vpc))

(def route-table (make-routes {::type ::my-route-table}
                              cred vpc subnet
                              ["0.0.0.0/0" ::internet-gateway internet-gateway]))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; old-api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; (derive ::my-vpc ::vpc)
;; (derive ::my-subnet ::subnet)
;; (derive ::my-internet-gateway ::internet-gateway)
;; (derive ::my-route-table ::route-table)
;; 
;; 
;; (def vpc (create ::my-vpc cred))
;; 
;; (def subnet (create ::my-subnet cred vpc))
;; 
;; (def internet-gateway (create ::my-internet-gateway cred vpc))
;; 
;; (def route-table (make-routes ::my-route-table cred vpc subnet
;;                               ["0.0.0.0/0" ::internet-gateway internet-gateway]))
;; 
;; (def init
;;   "#!/bin/bash
;;   cd /home/admin
;;   umask 077
;;   mkdir -p .ssh
;;   cd .ssh
;;   echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB//SW/+2QPkeDh6qvXFymOAyxjL+Cq9QBWgnZrZT/Qx edwlan@srv2' > authorized_keys
;;   ping -c 3 srv2.elangley.org
;;   curl https://srv2.elangley.org/~edwlan/.well-known/ssh/authorized_keys | tee -a authorized_keys
;;   ssh-keygen -t ed25519 -f id_ed25519 -N ''
;;   export DEBIAN_FRONTEND=noninteractive
;;   apt-get install -yq encfs
;;   apt-get install -yq postfix postfix-pgsql
;;   apt-get install -yq dovecot-core dovecot-imapd dovecot-mysql mysql-server dovecot-lmtpd
;;   ")
;; 
;; (def vms
;;   (run-instances cred
;;                  :image-id "ami-71d8820b"
;;                  :instance-type "t2.micro"
;;                  :min-count 1
;;                  :max-count 1
;;                  :user-data (encode init)
;;                  :network-interfaces [{:device-index 0
;;                                        :subnet-id (id ::subnet subnet)
;;                                        :associate-public-ip-address true}]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;