PageRenderTime 70ms CodeModel.GetById 29ms app.highlight 36ms RepoModel.GetById 2ms app.codeStats 0ms

/src/clj/backtype/storm/crate/zookeeper.clj

http://github.com/nathanmarz/storm-deploy
Clojure | 231 lines | 214 code | 17 blank | 0 comment | 11 complexity | e948250362836e73ca252ea63a59a874 MD5 | raw file
  1(ns backtype.storm.crate.zookeeper
  2  (:require
  3   [pallet.action.directory :as directory]
  4   [pallet.action.file :as file]
  5   [pallet.action.remote-directory :as remote-directory]
  6   [pallet.action.remote-file :as remote-file]
  7   [pallet.action.service :as service]
  8   [pallet.action.user :as user]
  9   [pallet.argument :as argument]
 10   [pallet.compute :as compute]
 11   [pallet.parameter :as parameter]
 12   [pallet.session :as session]
 13   [pallet.stevedore :as stevedore]
 14   [clojure.string :as string]
 15   [pallet.resource.package :as package]
 16   [pallet.action.exec-script :as exec-script]
 17   [pallet.crate.crontab :as crontab]
 18  )
 19  (:use
 20   pallet.thread-expr))
 21
 22(def install-path "/usr/local/zookeeper")
 23(def log-path "/var/log/zookeeper")
 24(def tx-log-path "/mnt/zookeeper")
 25(def config-path "/etc/zookeeper")
 26(def data-path "/var/zookeeper")
 27(def zookeeper-home install-path)
 28(def zookeeper-user "zookeeper")
 29(def zookeeper-group "zookeeper")
 30(def default-config
 31  {:dataDir data-path
 32   :tickTime 2000
 33   :clientPort 2181
 34   :initLimit 10
 35   :syncLimit 5
 36   :dataLogDir tx-log-path})
 37
 38(defn url "Download url"
 39  [version]
 40  (format
 41   "http://www.apache.org/dist/zookeeper/zookeeper-%s/zookeeper-%s.tar.gz"
 42   version version))
 43
 44(defn install
 45  "Install Zookeeper"
 46  [session & {:keys [user group version home]
 47              :or {user zookeeper-user
 48                   group zookeeper-group
 49                   version "3.3.6"}
 50              :as options}]
 51  (let [url (url version)
 52        home (or home (format "%s-%s" install-path version))]
 53    (->
 54     session
 55     (package/package "daemontools")
 56
 57     (parameter/assoc-for
 58      [:zookeeper :home] home
 59      [:zookeeper :owner] user
 60      [:zookeeper :group] group)
 61     (user/group group :system true)
 62     (user/user user :system true :group group)
 63     (remote-directory/remote-directory
 64      home
 65      :url url :md5-url (str url ".md5")
 66      :unpack :tar :tar-options "xz"
 67      :owner user :group group)
 68     (directory/directory log-path :owner user :group group :mode "0755")
 69     (directory/directory tx-log-path :owner user :group group :mode "0755")
 70     (directory/directory config-path :owner user :group group :mode "0755")
 71     (directory/directory data-path :owner user :group group :mode "0755")
 72     (directory/directory (format "/home/%s" user) :owner user :group group :mode "0755")
 73     (directory/directory (format "%s/supervise" home) :owner user :group group :mode "0755")
 74     (remote-file/remote-file
 75      (format "%s/purge" home)
 76      :content
 77(format
 78"#!/bin/bash
 79cd %s && export ZOOBINDIR=\"bin\" && . bin/zkEnv.sh && echo $CLASSPATH && java -cp $CLASSPATH org.apache.zookeeper.server.PurgeTxnLog %s %s -n 3
 80"
 81home
 82tx-log-path
 83data-path
 84)
 85      :overwrite-changes true
 86      :literal true
 87      :mode 755)
 88     (remote-file/remote-file
 89      (format "%s/run" home)
 90      :content
 91"#!/bin/bash
 92
 93export ZOOBINDIR=\".\"
 94      
 95if [ \"x$JMXLOCALONLY\" = \"x\" ]
 96then
 97    JMXLOCALONLY=false
 98fi
 99
100if [ \"x$JMXDISABLE\" = \"x\" ]
101then
102    echo \"JMX enabled by default\"
103    # for some reason these two options are necessary on jdk6 on Ubuntu
104    #   accord to the docs they are not necessary, but otw jconsole cannot
105    #   do a local attach
106    ZOOMAIN=\"-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain\"
107else
108    echo \"JMX disabled by user request\"
109    ZOOMAIN=\"org.apache.zookeeper.server.quorum.QuorumPeerMain\"
110fi
111
112if [ \"x$2\" != \"x\" ]
113then
114    ZOOCFG=\"$ZOOCFGDIR/$2\"
115fi
116      
117      
118cd bin && . ./zkEnv.sh && java  \"-Dzookeeper.log.dir=${ZOO_LOG_DIR}\" \"-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\"
119               "
120      :overwrite-changes true
121      :literal true
122      :mode 755)
123
124     (remote-file/remote-file
125      (format "%s/log4j.properties" config-path)
126      :remote-file (format "%s/conf/log4j.properties" home)
127      :owner user :group group :mode "0644")
128     (file/sed
129       (format "%s/bin/zkServer.sh" home)
130       {"# chkconfig:.*" ""
131        "# description:.*" ""
132        "# by default we allow local JMX connections"
133        "# by default we allow local JMX connections\\n# chkconfig: 2345 20 80\\n# description: zookeeper"})
134     (file/sed
135      (format "%s/log4j.properties" config-path)
136      {"log4j.rootLogger=INFO, CONSOLE"
137       "log4j.rootLogger=INFO, ROLLINGFILE"
138       "log4j.appender.ROLLINGFILE.File=zookeeper.log"
139       (format "log4j.appender.ROLLINGFILE.File=%s/zookeeper.log" log-path)}
140      :seperator "|")
141      )))
142
143(defn init [session]
144  (-> session
145    (exec-script/exec-script
146        (cd ~(parameter/get-for session [:zookeeper :home]))
147        "sudo -u " ~(parameter/get-for session [:zookeeper :owner]) " nohup supervise . &")
148    (crontab/crontab "root"
149      :content (format "@daily sh %s/purge" (parameter/get-for session [:zookeeper :home])))
150    ))
151
152(defn config-files
153  "Create a zookeeper configuration file.  We sort by name to preserve sequence
154   across invocations."
155  [session]
156  (let [target-name (session/target-name session)
157        target-ip (session/target-ip session)
158        nodes (sort-by compute/hostname (session/nodes-in-group session))
159        configs (parameter/get-for
160                 session
161                 [:zookeper (keyword (session/group-name session))])
162        config (configs (keyword target-name))
163        owner (parameter/get-for session [:zookeeper :owner])
164        group (parameter/get-for session [:zookeeper :group])]
165    (->
166     session
167     (remote-file/remote-file
168      (format "%s/zoo.cfg" config-path)
169      :content (str (string/join
170                     \newline
171                     (map #(format "%s=%s" (name (first %)) (second %))
172                          (merge
173                           default-config
174                           (dissoc config :electionPort :quorumPort))))
175                    \newline
176                    (when (> (count nodes) 1)
177                      (string/join
178                       \newline
179                       (map #(let [config (configs
180                                           (keyword (compute/hostname %1)))]
181                               (format "server.%s=%s:%s:%s"
182                                       %2
183                                       (compute/private-ip %1)
184                                       (:quorumPort config 2888)
185                                       (:electionPort config 3888)))
186                            nodes
187                            (range 1 (inc (count nodes)))))))
188      :owner owner :group group :mode "0644")
189
190     (remote-file/remote-file
191      (format "%s/myid" data-path)
192      :content (str (some #(and (= target-ip (second %)) (first %))
193                          (map #(vector %1 (compute/primary-ip %2))
194                               (range 1 (inc (count nodes)))
195                               nodes)))
196      :owner owner :group group :mode "0644"))))
197
198(defn store-configuration
199  "Capture zookeeper configuration"
200  [session options]
201  (parameter/update-for
202   session
203   [:zookeper (keyword (session/group-name session))]
204   (fn [m]
205     (assoc m (session/target-name session) options))))
206
207(defn configure
208  "Configure zookeeper instance"
209  [session & {:keys [dataDir tickTime clientPort initLimit syncLimit dataLogDir
210                     electionPort quorumPort]
211              :or {client-port 2181 quorumPort 2888 electionPort 3888}
212              :as options}]
213  (->
214   session
215   (store-configuration
216    (assoc options :quorumPort quorumPort :electionPort electionPort))
217   (config-files)))
218
219#_
220(pallet.core/defnode zk
221  {}
222  :bootstrap (pallet.action/phase
223              (pallet.crate.automated-admin-user/automated-admin-user))
224  :configure (pallet.action/phase
225              (pallet.crate.java/java :openjdk :jdk)
226              (pallet.crate.zookeeper/install)
227              (pallet.crate.zookeeper/configure)
228              (pallet.crate.zookeeper/init))
229  :restart-zookeeper (pallet.action/phase
230                      (pallet.action.service/service
231                       "zookeeper" :action :restart)))