Created
October 7, 2014 08:01
-
-
Save behrica/b09d159734a48bff3c7d to your computer and use it in GitHub Desktop.
Clojure code for freeze / unfreeze of digitalocean droplet
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
(defn destroy-all-images [client-id api-key image-name] | |
(let [imgs (filter #(= (:name %) image-name) (all-images client-id api-key))] | |
(doseq [img imgs] (destroy-image client-id api-key (:id img))))) | |
(defn freeze | |
[client-id api-key droplet-name] | |
(let [droplet-id (:id (droplet-by-name client-id api-key droplet-name))] | |
(if (nil? droplet-id) | |
[:failed (str "Droplet " droplet-name " not existing")] | |
(do | |
(println "Destroy images" droplet-name "...") | |
(destroy-all-images client-id api-key droplet-name) | |
(println "Shutdown droplet" droplet-name "...") | |
(wait-for-event-finish client-id api-key (:event_id (shutdown-droplet client-id api-key droplet-id))) | |
(println "Snapshot droplet" droplet-name "...") | |
(wait-for-event-finish client-id api-key (:event_id (snapshot-droplet client-id api-key droplet-id droplet-name))) | |
(println "Shutdown droplet" droplet-name "...") | |
(wait-for-event-finish client-id api-key (:event_id (shutdown-droplet client-id api-key droplet-id))) | |
(println "Destroy droplet" droplet-name "...") | |
(wait-for-event-finish client-id api-key (:event_id (destroy-droplet client-id api-key droplet-id))) | |
[:ok (str "SUCCESS - Droplet " droplet-name " froozen")])))) | |
(defn unfreeze | |
[client-id api-key image-name] | |
(let [imgs (all-images client-id api-key) | |
image-id (:id (first (filter #(= (:name %) image-name) imgs)))] | |
(if (nil? image-id) | |
[:failed (str "Image " image-name " not existing")] | |
(do (println "Create droplet from image" image-name "...") | |
(let [result-of-new (new-droplet client-id api-key (make-droplet-config image-name image-id))] | |
(if (= "ERROR" (:status result-of-new)) | |
result-of-new | |
(do (wait-for-event-finish client-id api-key | |
(get-in result-of-new | |
[:droplet :event_id])) | |
(println "Destroy images" image-name "...") | |
(destroy-all-images client-id api-key image-name) | |
[:ok (str "SUCCESS - image " image-name " unfroozen")]))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment