2 ;;;; Copyright (c) 2009 Zachary Beane, All Rights Reserved
4 ;;;; Redistribution and use in source and binary forms, with or without
5 ;;;; modification, are permitted provided that the following conditions
8 ;;;; * Redistributions of source code must retain the above copyright
9 ;;;; notice, this list of conditions and the following disclaimer.
11 ;;;; * Redistributions in binary form must reproduce the above
12 ;;;; copyright notice, this list of conditions and the following
13 ;;;; disclaimer in the documentation and/or other materials
14 ;;;; provided with the distribution.
16 ;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 (defvar *canonical-bucket-name-suffix
*
35 (defparameter *caller-reference-counter
* 8320208)
37 (defparameter *cloudfront-base-url
*
38 "https://cloudfront.amazonaws.com/2010-08-01/distribution")
42 (defparameter *distribution-specific-errors
*
43 (make-hash-table :test
'equal
)
44 "This table is used to signal the most specific error possible for
45 distribution request error responses.")
47 (defparameter *distribution-error-response-binder
*
53 ("Message" (bind :message
))
55 ("Detail" (bind :detail
))))
56 ("RequestId" (bind :request-id
)))))
58 (define-condition distribution-error
()
61 :accessor distribution-error-type
)
64 :accessor distribution-error-code
)
66 :initarg
:http-status-code
67 :accessor distribution-error-http-status-code
)
69 :initarg
:error-message
70 :accessor distribution-error-message
)
72 :initarg
:error-detail
73 :accessor distribution-error-detail
))
74 (:report
(lambda (condition stream
)
75 (format stream
"~A error ~A: ~A"
76 (distribution-error-type condition
)
77 (distribution-error-code condition
)
78 (distribution-error-message condition
)))))
80 (defmacro define-specific-distribution-error
(error-xml-code error-name
)
82 (setf (gethash ,error-xml-code
*distribution-specific-errors
*)
84 (define-condition ,error-name
(distribution-error) ())))
86 (define-specific-distribution-error "InvalidIfMatchVersion"
87 invalid-if-match-version
)
89 (define-specific-distribution-error "PreconditionFailed"
90 distribution-precondition-failed
)
92 (define-specific-distribution-error "DistributionNotDisabled"
93 distribution-not-disabled
)
95 (define-specific-distribution-error "CNAMEAlreadyExists"
98 (define-specific-distribution-error "TooManyDistributions"
99 too-many-distributions
)
101 (defun maybe-signal-distribution-error (http-status-code content
)
103 (plusp (length content
))
104 (string= (xml-document-element content
) "ErrorResponse"))
105 (let* ((bindings (xml-bind *distribution-error-response-binder
*
107 (condition (gethash (bvalue :code bindings
)
108 *distribution-specific-errors
*
109 'distribution-error
)))
111 :http-status-code http-status-code
112 :error-type
(bvalue :type bindings
)
113 :error-code
(bvalue :code bindings
)
114 :error-message
(bvalue :message bindings
)
115 :error-detail
(bvalue :detail bindings
)))))
118 ;;; Distribution objects
120 (defun canonical-distribution-bucket-name (name)
121 (if (ends-with *canonical-bucket-name-suffix
* name
)
123 (concatenate 'string name
*canonical-bucket-name-suffix
*)))
125 (defun generate-caller-reference ()
128 (incf *caller-reference-counter
*)))
130 (defclass distribution
()
132 :initarg
:origin-bucket
133 :accessor origin-bucket
135 "The S3 bucket that acts as the source of objects for the distribution.")
137 :initarg
:caller-reference
138 :accessor caller-reference
139 :initform
(generate-caller-reference)
141 "A unique value provided by the caller to prevent replays. See
142 http://docs.amazonwebservices.com/AmazonCloudFront/2008-06-30/DeveloperGuide/index.html?AboutCreatingDistributions.html")
148 "Whether this distribution should be enabled at creation time or not.")
160 "Amazon's assigned unique ID.")
162 :initarg
:domain-name
163 :accessor domain-name
165 "Amazon's assigned domain name.")
174 :documentation
"Assigned by Amazon.")
176 :initarg
:last-modified
177 :accessor last-modified
178 :documentation
"Assigned by Amazon.")))
180 (defmethod print-object ((distribution distribution
) stream
)
181 (print-unreadable-object (distribution stream
:type t
)
182 (format stream
"~A for ~S~@[ [~A]~]"
184 (origin-bucket distribution
)
185 (status distribution
))))
187 (defmethod initialize-instance :after
((distribution distribution
)
189 &key
&allow-other-keys
)
190 (declare (ignore initargs
))
191 (setf (origin-bucket distribution
)
192 (canonical-distribution-bucket-name (origin-bucket distribution
))))
195 ;;; Distribution-related requests
197 (defun distribution-document (distribution)
198 (cxml:with-xml-output
(cxml:make-string-sink
)
199 (cxml:with-element
"DistributionConfig"
200 (cxml:attribute
"xmlns" "http://cloudfront.amazonaws.com/doc/2008-06-30/")
201 (cxml:with-element
"Origin"
202 (cxml:text
(origin-bucket distribution
)))
203 (cxml:with-element
"CallerReference"
204 (cxml:text
(caller-reference distribution
)))
205 (dolist (cname (cnames distribution
))
206 (cxml:with-element
"CNAME"
208 (when (comment distribution
)
209 (cxml:with-element
"Comment"
210 (cxml:text
(comment distribution
))))
211 (cxml:with-element
"Enabled"
212 (cxml:text
(if (enabledp distribution
)
216 (defun distribution-request-headers (distribution)
217 (let* ((date (http-date-string))
218 (signature (sign-string (secret-key *credentials
*)
220 (parameters-alist :date date
222 (format nil
"AWS ~A:~A"
223 (access-key *credentials
*)
225 :if-match
(and distribution
(etag distribution
)))))
228 (defun distribution-request (&key distribution
(method :get
)
229 parameters url-suffix content
230 ((:credentials
*credentials
*) *credentials
*))
231 (let ((url (format nil
"~A~@[~A~]" *cloudfront-base-url
* url-suffix
)))
232 (multiple-value-bind (content code headers uri stream must-close-p phrase
)
233 (drakma:http-request url
235 :parameters parameters
239 :content-type
"text/xml"
240 :additional-headers
(distribution-request-headers distribution
)
244 (member method
'(:post
:put
))
245 (distribution-document distribution
))))
246 (declare (ignore uri must-close-p
))
247 (ignore-errors (close stream
))
248 (maybe-signal-distribution-error code content
)
249 (values content headers code phrase
))))
251 (defparameter *distribution-config-form
*
252 '("DistributionConfig"
253 ("Origin" (bind :origin
))
254 ("CallerReference" (bind :caller-reference
))
256 ("CNAME" (bind :cname
)))
257 (optional ("Comment" (bind :comment
)))
258 ("Enabled" (bind :enabled
))))
260 (defparameter *distribution-form
*
263 ("Status" (bind :status
))
264 ("LastModifiedTime" (bind :last-modified-time
))
265 ("InProgressInvalidationBatches" (bind :in-progress-invalidation-batches
))
266 ("DomainName" (bind :domain-name
))
267 ,@*distribution-config-form
*))
269 (defparameter *distribution-config-binder
*
270 (make-binder *distribution-config-form
*))
272 (defparameter *distribution-binder
* (make-binder *distribution-form
*))
274 (defun bindings-distribution (bindings)
275 (let ((timestamp (bvalue :last-modified-time bindings
)))
276 (make-instance 'distribution
277 :id
(bvalue :id bindings
)
278 :status
(bvalue :status bindings
)
279 :caller-reference
(bvalue :caller-reference bindings
)
280 :domain-name
(bvalue :domain-name bindings
)
281 :origin-bucket
(bvalue :origin bindings
)
282 :cnames
(mapcar (lambda (b) (bvalue :cname b
))
283 (bvalue :cnames bindings
))
284 :comment
(bvalue :comment bindings
)
285 :enabledp
(equal (bvalue :enabled bindings
) "true")
286 :last-modified
(and timestamp
287 (parse-amazon-timestamp timestamp
)))))
289 ;;; Distribution queries, creation, and manipulation
291 (defun put-config (distribution)
292 "Post DISTRIBUTION's configuration to AWS. Signals an error and does
293 not retry in the event of an etag match problem."
294 (multiple-value-bind (document headers code
)
295 (distribution-request :distribution distribution
296 :url-suffix
(format nil
"/~A/config"
299 (declare (ignore document headers
))
302 (defun latest-version (distribution)
303 (multiple-value-bind (document headers
)
304 (distribution-request :url-suffix
(format nil
"/~A" (id distribution
)))
305 (let ((new (bindings-distribution (xml-bind *distribution-binder
*
307 (setf (etag new
) (bvalue :etag headers
))
310 (defun merge-into (distribution new
)
311 "Copy slot values from NEW into DISTRIBUTION."
312 (macrolet ((sync (accessor)
313 `(setf (,accessor distribution
) (,accessor new
))))
315 (sync caller-reference
)
322 (sync last-modified
))
325 (defgeneric refresh
(distribution)
327 "Pull down the latest data from AWS for DISTRIBUTION and update its slots.")
328 (:method
((distribution distribution
))
329 (merge-into distribution
(latest-version distribution
))))
331 (defun call-with-latest (fun distribution
)
332 "Call FUN on DISTRIBUTION; if there is an ETag-related error,
333 retries after REFRESHing DISTRIBUTION. FUN should not have side
334 effects on anything but the DISTRIBUTION itself, as it may be re-tried
340 (((or invalid-if-match-version distribution-precondition-failed
)
343 (setf distribution
(refresh distribution
))
345 (return (funcall fun distribution
))))))
347 (defun modify-and-save (fun distribution
)
348 "Call the modification function FUN with DISTRIBUTION as its only
349 argument, and push the modified configuration to Cloudfront. May
350 refresh DISTRIBUTION if needed. FUN should not have side effects on
351 anything but the DISTRIBUTION itself, as it may be re-tried multiple
353 (call-with-latest (lambda (distribution)
354 (multiple-value-prog1
355 (funcall fun distribution
)
356 (put-config distribution
)))
359 (defmacro with-saved-modifications
((var distribution
) &body body
)
360 "Make a series of changes to DISTRIBUTION and push the final result
361 to AWS. BODY should not have side-effects on anything but the
362 DISTRIBUTION itself, as it may be re-tried multiple times."
363 `(modify-and-save (lambda (,var
)
367 (defparameter *distribution-list-binder
*
370 ("Marker" (bind :marker
))
372 ("NextMarker" (bind :next-marker
)))
373 ("MaxItems" (bind :max-items
))
374 ("IsTruncated" (bind :is-truncateD
))
375 (sequence :distributions
376 ("DistributionSummary"
378 ("Status" (bind :status
))
379 ("LastModifiedTime" (bind :last-modified-time
))
380 ("DomainName" (bind :domain-name
))
381 ("Origin" (bind :origin
))
382 (sequence :cnames
("CNAME" (bind :cname
)))
383 (optional ("Comment" (bind :comment
)))
384 ("Enabled" (bind :enabled
)))))))
386 (defun all-distributions (&key
((:credentials
*credentials
*) *credentials
*))
387 (let* ((document (distribution-request))
388 (bindings (xml-bind *distribution-list-binder
* document
)))
390 (bindings-distribution b
))
391 (bvalue :distributions bindings
))))
393 (defun create-distribution (bucket-name &key cnames
(enabled t
) comment
)
394 (unless (listp cnames
)
395 (setf cnames
(list cnames
)))
396 (let ((distribution (make-instance 'distribution
397 :origin-bucket bucket-name
401 (let* ((document (distribution-request :method
:post
402 :distribution distribution
))
403 (bindings (xml-bind *distribution-binder
* document
)))
404 (bindings-distribution bindings
))))
406 (defun %delete-distribution
(distribution)
407 (multiple-value-bind (document headers code
)
408 (distribution-request :url-suffix
(format nil
"/~A" (id distribution
))
409 :distribution distribution
411 (declare (ignore document headers
))
414 (defgeneric delete-distribution
(distribution)
415 (:method
((distribution distribution
))
416 (call-with-latest #'%delete-distribution distribution
)))
418 (defgeneric enable
(distribution)
420 "Mark DISTRIBUTION as enabled. Enabling can take time to take
421 effect; the STATUS of DISTRIBUTION will change from \"InProgress\"
422 to \"Deployed\" when fully enabled.")
423 (:method
((distribution distribution
))
424 (with-saved-modifications (d distribution
)
425 (setf (enabledp d
) t
))))
428 (defgeneric disable
(distribution)
430 "Mark DISTRIBUTION as disabled. Like ENABLE, DISABLE may take some
431 time to take effect.")
432 (:method
((distribution distribution
))
433 (with-saved-modifications (d distribution
)
434 (setf (enabledp d
) nil
)
437 (defgeneric ensure-cname
(distribution cname
)
439 "Add CNAME to DISTRIBUTION's list of CNAMEs, if not already
441 (:method
((distribution distribution
) cname
)
442 (with-saved-modifications (d distribution
)
443 (pushnew cname
(cnames d
)
444 :test
#'string-equal
))))
446 (defgeneric remove-cname
(distribution cname
)
447 (:method
(cname (distribution distribution
))
448 (with-saved-modifications (d distribution
)
450 (remove cname
(cnames distribution
)
451 :test
#'string-equal
)))))
453 (defgeneric set-comment
(distribution comment
)
454 (:method
((distribution distribution
) comment
)
455 (with-saved-modifications (d distribution
)
456 (setf (comment d
) comment
))))
458 (defun distributions-for-bucket (bucket-name)
459 "Return a list of distributions that are associated with BUCKET-NAME."
460 (setf bucket-name
(canonical-distribution-bucket-name bucket-name
))
463 :test-not
#'string-equal
464 :key
#'origin-bucket
))
469 (defclass invalidation
()
474 :documentation
"Amazon's assigned unique ID.")
476 :initarg
:distribution
477 :accessor distribution
480 :initarg
:create-time
482 :accessor create-time
)
486 :initform
"InProgress")
488 :initarg
:caller-reference
489 :initform
(generate-caller-reference)
490 :accessor caller-reference
)
496 (defmethod print-object ((invalidation invalidation
) stream
)
497 (print-unreadable-object (invalidation stream
:type t
)
498 (format stream
"~S [~A]"
500 (status invalidation
))))
502 (defparameter *invalidation-batch-form
*
503 '("InvalidationBatch"
504 (sequence :paths
("Path" (bind :path
)))
505 ("CallerReference" (bind :caller-reference
))))
507 (defparameter *invalidation-form
*
510 ("Status" (bind :status
))
511 ("CreateTime" (bind :create-time
))
512 ,*invalidation-batch-form
*))
514 (defparameter *invalidation-batch-binder
*
515 (make-binder *invalidation-batch-form
*))
517 (defparameter *invalidation-binder
*
518 (make-binder *invalidation-form
*))
520 (defmethod merge-bindings ((invalidation invalidation
) bindings
)
521 (setf (id invalidation
) (bvalue :id bindings
)
522 (status invalidation
) (bvalue :status bindings
)
523 (create-time invalidation
) (parse-amazon-timestamp
524 (bvalue :create-time bindings
))
527 (mapcar (bfun :path
) (bvalue :paths bindings
))))
530 (defgeneric distribution-id
(object)
531 (:method
((invalidation invalidation
))
532 (id (distribution invalidation
))))
534 (defun invalidation-request (invalidation &key
(url-suffix "")
535 (method :get
) content
)
536 (distribution-request :method method
537 :url-suffix
(format nil
"/~A/invalidation~A"
538 (distribution-id invalidation
)
542 (defun invalidation-batch-document (invalidation)
543 (cxml:with-xml-output
(cxml:make-string-sink
)
544 (cxml:with-element
"InvalidationBatch"
545 (cxml:attribute
"xmlns" "http://cloudfront.amazonaws.com/doc/2010-08-01/")
546 (dolist (path (paths invalidation
))
547 (cxml:with-element
"Path"
549 (cxml:with-element
"CallerReference"
550 (cxml:text
(caller-reference invalidation
))))))
553 (defun invalidate-paths (distribution paths
)
554 (let* ((invalidation (make-instance 'invalidation
555 :distribution distribution
558 (invalidation-request invalidation
560 :content
(invalidation-batch-document invalidation
))))
561 (merge-bindings invalidation
(xml-bind *invalidation-binder
* response
))))
564 (defmethod refresh ((invalidation invalidation
))
566 (invalidation-request invalidation
567 :url-suffix
(format nil
"/~A"
568 (id invalidation
)))))
569 (merge-bindings invalidation
(xml-bind *invalidation-binder
* document
))))