/src/clj/backtype/storm/provision.clj

http://github.com/nathanmarz/storm-deploy · Clojure · 161 lines · 133 code · 22 blank · 6 comment · 3 complexity · 6eb7cffe6e0a9b156d4cb76cfa311843 MD5 · raw file

  1. (ns backtype.storm.provision
  2. (:import [java.io File])
  3. (:use [clojure.contrib.command-line]
  4. [pallet.compute :exclude [admin-user]]
  5. [backtype.storm security]
  6. [pallet.core]
  7. [org.jclouds.compute2 :only [nodes-in-group]]
  8. [backtype.storm.util :only [with-var-roots]]
  9. [clojure.tools.logging]
  10. )
  11. (:require [pallet.configure])
  12. (:require [pallet.compute.jclouds])
  13. (:require [backtype.storm.node :as node])
  14. (:require [backtype.storm.crate.storm :as storm])
  15. (:require [backtype.storm.deploy-util :as util]))
  16. (log-capture! "java.logging")
  17. ;; memoize this
  18. (defn my-region []
  19. (-> (pallet.configure/pallet-config) :services :default :jclouds.regions)
  20. )
  21. (defn jclouds-group [& group-pieces]
  22. (str "jclouds#" (apply str group-pieces)))
  23. (defn- print-ips-for-tag! [aws tag-str]
  24. (let [running-node (filter running? (map (partial pallet.compute.jclouds/jclouds-node->node aws) (nodes-in-group aws tag-str)))]
  25. (info (str "TAG: " tag-str))
  26. (info (str "PUBLIC: " (clojure.string/join ", " (map primary-ip running-node))))
  27. (info (str "PRIVATE: " (clojure.string/join ", " (map private-ip running-node))))))
  28. (defn print-all-ips! [aws name]
  29. (let [all-tags [(str "zookeeper-" name) (str "nimbus-" name) (str "supervisor-" name)]]
  30. (doseq [tag all-tags]
  31. (print-ips-for-tag! aws tag))))
  32. (defn converge! [name branch commit aws sn zn nn]
  33. (converge {(node/nimbus name branch commit) nn
  34. (node/supervisor name branch commit) sn
  35. (node/zookeeper name) zn
  36. }
  37. :compute aws))
  38. (defn sync-storm-conf-dir [aws name]
  39. (let [conf-dir (str (System/getProperty "user.home") "/.storm")
  40. storm-yaml (storm/mk-storm-yaml name node/storm-yaml-path aws)
  41. supervisor-yaml (storm/mk-supervisor-yaml aws name)]
  42. (.mkdirs (File. conf-dir))
  43. (spit (str conf-dir "/storm.yaml") storm-yaml)
  44. (spit (str conf-dir "/supervisor.yaml") supervisor-yaml)))
  45. (defn attach! [aws name]
  46. (info "Attaching to Available Cluster...")
  47. (sync-storm-conf-dir aws name)
  48. (authorizeme aws (jclouds-group "nimbus-" name) 80 (my-region))
  49. (authorizeme aws (jclouds-group "nimbus-" name) (node/storm-conf "nimbus.thrift.port") (my-region))
  50. (authorizeme aws (jclouds-group "nimbus-" name) (node/storm-conf "ui.port") (my-region))
  51. (authorizeme aws (jclouds-group "nimbus-" name) (node/storm-conf "drpc.port") (my-region))
  52. (info "Attaching Complete."))
  53. (defn start-with-nodes! [aws name nimbus supervisor zookeeper]
  54. (let [nimbus (node/nimbus* name nimbus)
  55. supervisor (node/supervisor* name supervisor)
  56. zookeeper (node/zookeeper name zookeeper)
  57. sn (int (node/clusters-conf "supervisor.count" 1))
  58. zn (int (node/clusters-conf "zookeeper.count" 1))]
  59. (info (format "Provisioning nodes [nn=1, sn=%d, zn=%d]" sn zn))
  60. (converge {nimbus 1
  61. supervisor sn
  62. zookeeper zn
  63. }
  64. :compute aws
  65. )
  66. (debug "Finished converge")
  67. (authorize-group aws (my-region) (jclouds-group "nimbus-" name) (jclouds-group "supervisor-" name))
  68. (authorize-group aws (my-region) (jclouds-group "supervisor-" name) (jclouds-group "nimbus-" name))
  69. (debug "Finished authorizing groups")
  70. (lift nimbus :compute aws :phase [:post-configure :exec])
  71. (lift supervisor :compute aws :phase [:post-configure :exec])
  72. (debug "Finished post-configure and exec phases")
  73. (attach! aws name)
  74. (info "Provisioning Complete.")
  75. (print-all-ips! aws name)))
  76. (defn start! [aws name branch commit]
  77. (println "Starting cluster with storm branch " branch (if (empty? commit) "" (str " and commit " commit)))
  78. (start-with-nodes! aws name (node/nimbus-server-spec name branch commit) (node/supervisor-server-spec name branch commit) (node/zookeeper-server-spec))
  79. )
  80. (defn upgrade-with-nodes! [aws name nimbus supervisor zookeeper]
  81. (let [nimbus (node/nimbus* name nimbus)
  82. supervisor (node/supervisor* name supervisor)
  83. zookeeper (node/zookeeper name zookeeper)]
  84. ; (authorize-group aws (my-region) (jclouds-group "nimbus-" name) (jclouds-group "supervisor-" name))
  85. ; (authorize-group aws (my-region) (jclouds-group "supervisor-" name) (jclouds-group "nimbus-" name))
  86. ; (lift zookeeper :compute aws :phase [:configure])
  87. ; (lift nimbus :compute aws :phase [:configure :post-configure :exec])
  88. (lift supervisor :compute aws :phase [:configure :post-configure :exec])
  89. (println "Upgrade Complete.")))
  90. (defn upgrade! [aws name branch commit]
  91. (println "Upgrading cluster with storm branch " branch (if (empty? commit) "" (str " and commit " commit)))
  92. (upgrade-with-nodes! aws name (node/nimbus-server-spec name branch commit) (node/supervisor-server-spec name branch commit) (node/zookeeper-server-spec))
  93. )
  94. (defn stop! [aws name]
  95. (println "Shutting Down nodes...")
  96. (converge! name nil nil aws 0 0 0)
  97. (println "Shutdown Finished."))
  98. (defn mk-aws []
  99. (let [storm-conf (-> (storm/storm-config "default")
  100. (update-in [:environment :user] util/resolve-keypaths))]
  101. (compute-service-from-map storm-conf)))
  102. (defn -main [& args]
  103. (let [aws (mk-aws)
  104. user (-> (storm/storm-config "default")
  105. :environment
  106. :user
  107. (util/resolve-keypaths))
  108. ]
  109. (System/setProperty "jna.nosys" "true")
  110. (with-var-roots [node/*USER* user]
  111. (with-command-line args
  112. "Provisioning tool for Storm Clusters"
  113. [[start? "Start Cluster?"]
  114. [stop? "Shutdown Cluster?"]
  115. [attach? "Attach to Cluster?"]
  116. [upgrade? "Upgrade existing cluster"]
  117. [ips? "Print Cluster IP Addresses?"]
  118. [name "Cluster name" "dev"]
  119. [branch "Branch" "master"]; default branch is master.
  120. [commit "Commit SHA1" nil]]; default is not to pass a commit
  121. (cond
  122. stop? (stop! aws name)
  123. start? (start! aws name branch commit)
  124. upgrade? (upgrade! aws name branch commit)
  125. attach? (attach! aws name)
  126. ips? (print-all-ips! aws name)
  127. :else (println "Must pass --start, --stop , upgrade, --attach or --ips")))))
  128. (shutdown-agents)
  129. (println "Done.")
  130. (System/exit 0))
  131. ;; DEBUGGING
  132. (comment
  133. (use 'backtype.storm.provision)
  134. (ns backtype.storm.provision)
  135. (def aws (mk-aws))
  136. (lift (node/supervisor "test" nil) :compute aws :phase [:post-configure] )
  137. (sync-storm-conf-dir aws)
  138. (print-all-ips! aws)
  139. )