Normalised comments, whitespace and line lengths. Updated user and password to provider-identity and provider-credential

This commit is contained in:
Hugo Duncan 2010-09-12 11:41:11 -04:00
parent 198eeec3d9
commit da5c0d03f8
1 changed files with 62 additions and 49 deletions

View File

@ -21,8 +21,8 @@
"A clojure binding to the jclouds ComputeService. "A clojure binding to the jclouds ComputeService.
Current supported services are: Current supported services are:
[ec2, rimuhosting, cloudservers, trmk-ecloud, trmk-vcloudexpress, vcloud, bluelock, [ec2, rimuhosting, cloudservers, trmk-ecloud, trmk-vcloudexpress, vcloud,
eucalyptus, slicehost] bluelock, eucalyptus, slicehost]
Here's an example of getting some compute configuration from rackspace: Here's an example of getting some compute configuration from rackspace:
@ -30,12 +30,12 @@ Here's an example of getting some compute configuration from rackspace:
(use 'clojure.contrib.pprint) (use 'clojure.contrib.pprint)
(def provider \"cloudservers\") (def provider \"cloudservers\")
(def user \"username\") (def provider-identity \"username\")
(def password \"password\") (def provider-credential \"password\")
; create a compute service ;; create a compute service
(def compute (def compute
(compute-service provider user password)) (compute-service provider provider-identity provider-credential))
(with-compute-service [compute] (with-compute-service [compute]
(pprint (locations)) (pprint (locations))
@ -43,11 +43,13 @@ Here's an example of getting some compute configuration from rackspace:
(pprint (nodes)) (pprint (nodes))
(pprint (hardware-profiles))) (pprint (hardware-profiles)))
Here's an example of creating and running a small linux node with the tag webserver: Here's an example of creating and running a small linux node with the tag
webserver:
; create a compute service using ssh and log4j extensions
(def compute ;; create a compute service using ssh and log4j extensions
(compute-service provider user password :ssh :log4j)) (def compute
(compute-service
provider provider-identity provider-credential :ssh :log4j))
(run-node \"webserver\" compute) (run-node \"webserver\" compute)
@ -60,8 +62,8 @@ See http://code.google.com/p/jclouds for details."
[org.jclouds.compute [org.jclouds.compute
ComputeService ComputeServiceContext ComputeServiceContextFactory] ComputeService ComputeServiceContext ComputeServiceContextFactory]
[org.jclouds.compute.domain [org.jclouds.compute.domain
Template TemplateBuilder ComputeMetadata NodeMetadata Hardware OsFamily Template TemplateBuilder ComputeMetadata NodeMetadata Hardware
Image] OsFamily Image]
[org.jclouds.compute.options TemplateOptions] [org.jclouds.compute.options TemplateOptions]
[org.jclouds.compute.predicates [org.jclouds.compute.predicates
NodePredicates] NodePredicates]
@ -76,13 +78,14 @@ See http://code.google.com/p/jclouds for details."
(defn compute-service (defn compute-service
"Create a logged in context." "Create a logged in context."
([#^String service #^String principal #^String credential & options] ([#^String provider #^String provider-identity #^String provider-credential
& options]
(let [module-keys (set (keys module-lookup)) (let [module-keys (set (keys module-lookup))
ext-modules (filter #(module-keys %) options) ext-modules (filter #(module-keys %) options)
opts (apply hash-map (filter #(not (module-keys %)) options))] opts (apply hash-map (filter #(not (module-keys %)) options))]
(.. (ComputeServiceContextFactory.) (.. (ComputeServiceContextFactory.)
(createContext (createContext
service principal credential provider provider-identity provider-credential
(apply modules (concat ext-modules (opts :extensions))) (apply modules (concat ext-modules (opts :extensions)))
(reduce #(do (.put %1 (name (first %2)) (second %2)) %1) (reduce #(do (.put %1 (name (first %2)) (second %2)) %1)
(Properties.) (dissoc opts :extensions))) (Properties.) (dissoc opts :extensions)))
@ -140,7 +143,7 @@ See http://code.google.com/p/jclouds for details."
([tag] (nodes-with-tag tag *compute*)) ([tag] (nodes-with-tag tag *compute*))
([#^String tag #^ComputeService compute] ([#^String tag #^ComputeService compute]
(filter #(= (.getTag %) tag) (nodes-with-details compute)))) (filter #(= (.getTag %) tag) (nodes-with-details compute))))
(defn images (defn images
"Retrieve the available images for the compute context." "Retrieve the available images for the compute context."
([] (images *compute*)) ([] (images *compute*))
@ -166,24 +169,27 @@ See http://code.google.com/p/jclouds for details."
"Create the specified number of nodes using the default or specified "Create the specified number of nodes using the default or specified
template. template.
; simplest way to add 2 small linux nodes to the group webserver is to run ;; Simplest way to add 2 small linux nodes to the group webserver is to run
(run-nodes \"webserver\" 2 compute) (run-nodes \"webserver\" 2 compute)
; which is the same as wrapping the run-nodes command with an implicit compute service ;; which is the same as wrapping the run-nodes command with an implicit
; note that this will actually add another 2 nodes to the set called \"webserver\" ;; compute service.
;; Note that this will actually add another 2 nodes to the set called
;; \"webserver\"
(with-compute-service [compute] (with-compute-service [compute]
(run-nodes \"webserver\" 2 )) (run-nodes \"webserver\" 2 ))
; which is the same as specifying the default template ;; which is the same as specifying the default template
(with-compute-service [compute] (with-compute-service [compute]
(run-nodes \"webserver\" 2 (default-template))) (run-nodes \"webserver\" 2 (default-template)))
; which, on gogrid, is the same as constructing the smallest centos template that has no layered software ;; which, on gogrid, is the same as constructing the smallest centos template
;; that has no layered software
(with-compute-service [compute] (with-compute-service [compute]
(run-nodes \"webserver\" 2 (run-nodes \"webserver\" 2
(build-template service :centos :smallest :image-name-matches \".*w/ None.*\"))) (build-template
service :centos :smallest :image-name-matches \".*w/ None.*\")))"
"
([tag count] ([tag count]
(run-nodes tag count (default-template *compute*) *compute*)) (run-nodes tag count (default-template *compute*) *compute*))
([tag count compute-or-template] ([tag count compute-or-template]
@ -198,20 +204,22 @@ See http://code.google.com/p/jclouds for details."
(defn run-node (defn run-node
"Create a node using the default or specified template. "Create a node using the default or specified template.
; simplest way to add a small linux node to the group webserver is to run ;; simplest way to add a small linux node to the group webserver is to run
(run-node \"webserver\" compute) (run-node \"webserver\" compute)
; which is the same as wrapping the run-node command with an implicit compute service ;; which is the same as wrapping the run-node command with an implicit compute
; note that this will actually add another node to the set called \"webserver\" ;; service.
;; Note that this will actually add another node to the set called
;; \"webserver\"
(with-compute-service [compute] (with-compute-service [compute]
(run-node \"webserver\" )) (run-node \"webserver\" ))"
"
([tag] ([tag]
(first (run-nodes tag 1 (default-template *compute*) *compute*))) (first (run-nodes tag 1 (default-template *compute*) *compute*)))
([tag compute-or-template] ([tag compute-or-template]
(if (compute-service? compute-or-template) (if (compute-service? compute-or-template)
(first (run-nodes tag 1 (default-template compute-or-template) compute-or-template)) (first
(run-nodes
tag 1 (default-template compute-or-template) compute-or-template))
(first (run-nodes tag 1 compute-or-template *compute*)))) (first (run-nodes tag 1 compute-or-template *compute*))))
([tag template compute] ([tag template compute]
(first (run-nodes tag 1 template compute)))) (first (run-nodes tag 1 template compute))))
@ -316,8 +324,10 @@ See http://code.google.com/p/jclouds for details."
(define-accessors NodeMetadata "node" credentials hardware state tag) (define-accessors NodeMetadata "node" credentials hardware state tag)
(defn builder-options [builder] (defn builder-options [builder]
(or (get-field org.jclouds.compute.domain.internal.TemplateBuilderImpl :options builder) (or
(TemplateOptions.))) (get-field
org.jclouds.compute.domain.internal.TemplateBuilderImpl :options builder)
(TemplateOptions.)))
(defmacro option-option-fn-0arg [key] (defmacro option-option-fn-0arg [key]
`(fn [builder#] `(fn [builder#]
@ -333,19 +343,23 @@ See http://code.google.com/p/jclouds for details."
(defmacro option-option-fn-1arg [key] (defmacro option-option-fn-1arg [key]
`(fn [builder# value#] `(fn [builder# value#]
(let [options# (builder-options builder#)] (let [options# (builder-options builder#)]
(~(symbol (str "." (camelize-mixed (name key)))) options# (seq-to-array value#)) (~(symbol (str "." (camelize-mixed (name key))))
options# (seq-to-array value#))
(.options builder# options#)))) (.options builder# options#))))
(def option-1arg-map (def option-1arg-map
(apply array-map (apply array-map
(concat (concat
(make-option-map option-fn-1arg (make-option-map
[:os-family :location-id :architecture :image-id :hardware-id option-fn-1arg
:os-name-matches :os-version-matches :os-description-matches [:os-family :location-id :architecture :image-id :hardware-id
:os-64-bit :image-version-matches :image-name-matches :os-name-matches :os-version-matches :os-description-matches
:image-description-matches :min-cores :min-ram]) :os-64-bit :image-version-matches :image-name-matches
(make-option-map option-option-fn-1arg :image-description-matches :min-cores :min-ram])
[:run-script :install-private-key :authorize-public-key :inbound-ports])))) (make-option-map
option-option-fn-1arg
[:run-script :install-private-key :authorize-public-key
:inbound-ports]))))
(def option-0arg-map (def option-0arg-map
(apply hash-map (apply hash-map
(concat (concat
@ -384,11 +398,10 @@ See http://code.google.com/p/jclouds for details."
(println "Unknown option" option)))) (println "Unknown option" option))))
;; TODO look at clojure-datalog ;; TODO look at clojure-datalog
(defn build-template (defn build-template
"Creates a template that can be used to run nodes. "Creates a template that can be used to run nodes.
There are many options to use for the default template There are many options to use for the default template"
"
[#^ComputeService compute option & options] [#^ComputeService compute option & options]
(let [builder (.. compute (templateBuilder))] (let [builder (.. compute (templateBuilder))]
(loop [option option (loop [option option