Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active December 15, 2018 03:10
Show Gist options
  • Save thiagoa/878317d19b128af6a3489b8b7bc148d1 to your computer and use it in GitHub Desktop.
Save thiagoa/878317d19b128af6a3489b8b7bc148d1 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/sbcl --script
(load (merge-pathnames ".sbclrc" (user-homedir-pathname)))
(ql:quickload "cl-json" :silent t)
(require "inferior-shell")
(defparameter *ssh-user* "ec2-user")
(defparameter *ssh-key-path* "~/.ssh/us-east-1-platform-key.pem")
(defun abort-program (message)
(princ (format nil "ERROR: ~a~%" message) *error-output*)
(sb-ext:quit :unix-status 1))
(defun get-raw-aws-json (instance-name)
(inferior-shell:run/s
(concatenate 'string
"aws ec2 describe-instances"
" --region us-east-1"
" --filters 'Name=tag:Name,Values=" instance-name "'"
" 'Name=instance-state-name,Values=running'")))
(defun get-aws-attr (instance-name attr-name)
(let* ((raw-json (get-raw-aws-json instance-name))
(parsed-json (json:decode-json-from-string raw-json))
(aws-attrs (cadar (cdadar parsed-json))))
(cdr (assoc attr-name aws-attrs))))
(defun run-ssh (instance-name)
(unless instance-name
(error "No instance name given"))
(let ((dns (get-aws-attr instance-name :*private-dns-name)))
(unless dns
(error "No DNS name found. Is the instance name correct?"))
(inferior-shell:run/interactive
(concatenate 'string
"ssh -i "
*ssh-key-path* " "
*ssh-user* "@" dns))))
(let ((instance-name (cadr sb-ext:*posix-argv*)))
(handler-case (run-ssh instance-name)
(error (c)
(abort-program c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment