Created
February 13, 2015 09:15
-
-
Save benley/cba69919be49b70065e2 to your computer and use it in GitHub Desktop.
mostly literal translation of exhibitor2dns.py into hylang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env hy | |
(import argparse boto logging requests) | |
(def *usage* | |
"exhibitor2dns: Dynamic DNS for Exhibitor-run Zookeeper ensembles.") | |
(defn parse_args [] | |
(setv parser | |
(apply (. argparse ArgumentParser) [] | |
{"description" *usage*})) | |
(setv required | |
(.add_argument_group parser "Required flags")) | |
(apply (. required add_argument) ["--zone"] | |
{"required" true | |
"type" str | |
"help" "DNS zone name (e.g. prod.example.com)"}) | |
(apply (. required add_argument) ["--rr"] | |
{"required" true | |
"type" str | |
"help" "Name of A record to manage. Concatenated with the value of | |
--zone unless it ends in a \".\""}) | |
(apply (. required add_argument) ["--exhibitor_url"] | |
{"required" true | |
"type" str | |
"metavar" "URL" | |
"help" "Base URL to exhibitor http endpoint | |
(e.g. http://exhibitor.prod.example.com/)"}) | |
(apply (. parser add_argument) ["--ttl"] | |
{"default" 300 | |
"type" int | |
"help" "Default record TTL (default: %(default)s)"}) | |
(apply (. parser add_argument) ["--verbosity"] | |
{"default" 20 | |
"type" int | |
"metavar" "N" | |
"help" "Log level (default: %(default)s)"}) | |
(.parse_args parser)) | |
(defn get_zk_servers [exhibitor_url] | |
"Query Exhibitor's REST api and get the current list of servers." | |
(setv url (.format "{}/{}" (.rstrip exhibitor_url "/") | |
"exhibitor/v1/cluster/list")) | |
(.info logging url) | |
(-> url (requests.get) (.json) (get "servers"))) | |
(defmain [&rest args] | |
(setv args (parse_args)) | |
(apply (. logging basicConfig) [] {"level" (. args verbosity)}) | |
(setv r53 (.connect_route53 boto)) | |
(setv zone (.get_zone r53 (. args zone))) | |
(setv target_fqdn | |
(if (.endswith (. args rr) ".") | |
(. args rr) | |
(.format "{}.{}" args.rr args.zone))) | |
(.info logging "Managing route53 record: %s" target_fqdn) | |
(setv exhibitor_list (get_zk_servers (. args exhibitor_url))) | |
(.info logging "Exhibitor cluster: %s" exhibitor_list) | |
(setv existing_record (.get_a zone target_fqdn)) | |
(if existing_record | |
(do (.info logging ("Existing record: %s" (. existing_record | |
resource_records))) | |
(if (!= (sorted exhibitor_list) | |
(sorted (. existing_record resource_records))) | |
(do (.info logging "Updating record to match") | |
(.update_record zone existing_record exhibitor_list)) | |
(.info logging "Up to date."))) | |
(do (.info logging "Creating new record.") | |
(apply (. zone add_a) [target_fqdn exhibitor_list] | |
{"ttl" (. args ttl)}))) | |
(.info logging "Done!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment