Overhaul S3 plugin and update NEWS.

This commit is contained in:
Brit Butler 2013-01-30 23:18:10 -05:00
parent 9a46023d76
commit 7fd7954c5d
2 changed files with 34 additions and 30 deletions

View file

@ -1,6 +1,9 @@
## Changes for 0.9 (2013-xx-xx): ## Changes for 0.9 (2013-xx-xx):
* Add support for analytics via Google. * Add support for analytics via Google.
* Add support for Restructured Text via cl-docutils.
* Add support for deploying to Amazon S3.
* Add a heroku plugin to ease hunchentoot deployments. (thanks @jsmpereira!)
## Changes for 0.8 (2013-01-06): ## Changes for 0.8 (2013-01-06):

View file

@ -1,12 +1,18 @@
(eval-when (:compile-toplevel) (eval-when (:compile-toplevel)
(ql:quickload '(zs3))) (ql:quickload 'zs3))
(defpackage :coleslaw-s3 (defpackage :coleslaw-s3
(:use :cl :coleslaw :zs3)) (:use :cl :zs3)
(:import-from :coleslaw #:deploy)
(:import-from :zs3 #:all-keys
#:etag
#:file-etag
#:put-file)
(:export #:enable))
(in-package :coleslaw-s3) (in-package :coleslaw-s3)
(defparameter *credentials* (get-credentials :s3) (defparameter *credentials* nil
"The credentials to authenticate with Amazon Web Services. "The credentials to authenticate with Amazon Web Services.
Stored in a file with the access key on the first line Stored in a file with the access key on the first line
and the secret key on the second.") and the secret key on the second.")
@ -26,34 +32,29 @@ and the secret key on the second.")
(defun content-type (extension) (defun content-type (extension)
(cdr (assoc extension *content-type-map* :test #'equal))) (cdr (assoc extension *content-type-map* :test #'equal)))
(defun init () (defun stale-keys ()
(unless *credentials* (loop for key being the hash-values in *cache* collecting key))
(set-credentials :s3 (file-credentials "~/.aws"))
(setf *credentials* (get-credentials :s3))))
(defun stale-keys (&key cache) (defun s3-sync (filepath dir)
(loop for key being the hash-values in cache collecting key)) (let ((etag (file-etag filepath))
(key (enough-namestring filepath dir)))
(if (gethash etag *cache*)
(remhash etag *cache*)
(put-file filepath *bucket* key :public t
:content-type (content-type (pathname-type filepath))))))
(defun s3-sync (filepath &key bucket dir public-p cache) (defun dir->s3 (dir)
(flet ((compute-key (namestring) (flet ((upload (file)
(subseq namestring (length (namestring (truename dir)))))) (s3-sync file dir :public-p t)))
(let* ((etag (file-etag filepath)) (cl-fad:walk-directory dir #'upload)))
(namestring (namestring filepath))
(key (compute-key namestring)))
(if (gethash etag cache)
(remhash etag cache)
(put-file filepath bucket key :public public-p
:content-type (content-type (pathname-type filepath)))))))
(defun dir->s3 (dir &key bucket cache public-p) (defmethod deploy :after (staging)
(cl-fad:walk-directory dir (lambda (file) (let ((blog (deploy coleslaw::*config*)))
(s3-sync file :cache cache :dir dir (loop for key across (all-keys *bucket*)
:bucket bucket :public-p public-p)))) do (setf (gethash (etag key) *cache*) key))
(dir->s3 blog)
(delete-objects (stale-keys) *bucket*)))
(defmethod coleslaw::render-site :after () (defun enable (&key auth-file bucket)
(init) (setf *credentials* (file-credentials auth-file)
(let* ((keys (all-keys *bucket*))) *bucket* bucket))
(loop for key across keys do (setf (gethash (etag key) *cache*) key))
(dir->s3 coleslaw::*output-dir* :bucket *bucket* :cache *cache* :public-p t)
(when (stale-keys :cache *cache*)
(delete-objects (stale-keys) *bucket*))))