Skip to content
This repository
Browse code

Update to use defmulti-os and friends

Version specific information is now more localised and is open coded.
  • Loading branch information...
commit 5e48694daa5997c40444b467f1fe1ab2315a289a 1 parent 47f59f8
Hugo Duncan hugoduncan authored
8 pom.xml
@@ -14,12 +14,4 @@
14 14 <developerConnection>scm:git:ssh://git@github.com/pallet/postgres-crate.git</developerConnection>
15 15 <url>https://github.com/pallet/postgres-crate</url>
16 16 </scm>
17   -
18   - <!-- make sure we can find the parent pom -->
19   - <repositories>
20   - <repository>
21   - <id>sonatype</id>
22   - <url>http://oss.sonatype.org/content/repositories/releases</url>
23   - </repository>
24   - </repositories>
25 17 </project>
331 src/pallet/crate/postgres.clj
... ... @@ -1,41 +1,41 @@
1 1 (ns pallet.crate.postgres
2 2 "Install and configure PostgreSQL.
3 3
4   - # Settings
  4 +# Settings
5 5
6   - Settings are constructed with the settings-map function.
  6 +Settings are constructed with the settings-map function.
7 7
8   - The settings map has a :permissions, :options, and :recovery keys
9   - used to specify the default database hba.conf, postgresal.conf and
10   - recovery.conf files. Paths in the default options should contain a
11   - \"%s\" for the database cluster name (n.b. a cluster is a group
12   - of databases running under the same postmaster process).
  8 +The settings map has a :permissions, :options, and :recovery keys
  9 +used to specify the default database hba.conf, postgresal.conf and
  10 +recovery.conf files. Paths in the default options should contain a
  11 +\"%s\" for the database cluster name (n.b. a cluster is a group
  12 +of databases running under the same postmaster process).
13 13
14   - The settings-map should be passed to the `settings` crate function
15   - which fills out the target specific paths, etc. Paths passed to `settings`
16   - should contain a \"%s\" to receive the database name.
  14 +The settings-map should be passed to the `settings` crate function
  15 +which fills out the target specific paths, etc. Paths passed to `settings`
  16 +should contain a \"%s\" to receive the database name.
17 17
18   - For each database cluster, `database-settings` must be called to set up the
19   - database specific settings from the default settings passed to the `settings`
20   - function. Database specific settings may be set up by passing a settings map
21   - to the `database-settings` function. Paths passed to database-settings should
22   - be complete amd not contain \"%s\" placeholders.
  18 +For each database cluster, `database-settings` must be called to set up the
  19 +database specific settings from the default settings passed to the `settings`
  20 +function. Database specific settings may be set up by passing a settings map
  21 +to the `database-settings` function. Paths passed to database-settings should
  22 +be complete amd not contain \"%s\" placeholders.
23 23
24   - ## Settings map details
  24 +## Settings map details
25 25
26   - These keys can also appear under the :dbs key for database cluster specific
27   - settings, in which case the paths should be plain strings.
  26 +These keys can also appear under the :dbs key for database cluster specific
  27 +settings, in which case the paths should be plain strings.
28 28
29   - For example:
  29 +For example:
30 30
31   - {:options {:port 5432
32   - :data_directory \"/var/lib/pgsql/%s\"}
33   - :clusters {:db1
34   - {:options {:port :5433
35   - :data_directory \"/var/lib/pgsql/db1\"}}}}
  31 + {:options {:port 5432
  32 + :data_directory \"/var/lib/pgsql/%s\"}
  33 + :clusters {:db1
  34 + {:options {:port :5433
  35 + :data_directory \"/var/lib/pgsql/db1\"}}}}
36 36
37   - Links:
38   - - http://blog.2ndquadrant.com/en/2010/05/install-multiple-postgresql-servers-redhat-linux.html
  37 +Links:
  38 +- http://blog.2ndquadrant.com/en/2010/05/install-multiple-postgresql-servers-redhat-linux.html
39 39 "
40 40 (:require
41 41 [pallet.action :as action]
@@ -56,13 +56,15 @@
56 56 [clojure.string :as string])
57 57 (:use
58 58 pallet.thread-expr
59   - [pallet.script :only [defscript]]))
60   -
61   -;; slingshot version compatibility
62   -(try
63   - (use '[slingshot.slingshot :only [throw+]])
64   - (catch Exception _
65   - (use '[slingshot.core :only [throw+]])))
  59 + [pallet.action.package :only [package package-manager package-source]]
  60 + [pallet.action.package.debian-backports :only [debian-backports-repository]]
  61 + [pallet.script :only [defscript]]
  62 + [pallet.version-dispatch
  63 + :only [defmulti-version-crate defmulti-version defmulti-os-crate
  64 + multi-version-session-method multi-version-method
  65 + multi-os-session-method]]
  66 + [pallet.versions :only [as-version-vector version-string]]
  67 + [slingshot.slingshot :only [throw+]]))
66 68
67 69 (def ^{:private true} pallet-cfg-preamble
68 70 "# This file was auto-generated by Pallet. Do not edit it manually unless you
@@ -73,9 +75,151 @@
73 75 (def ^{:doc "Flag for recognising changes to configuration"}
74 76 postgresql-config-changed-flag "postgresql-config")
75 77
  78 +;;; Default Postgres package version
  79 +(defmulti-os-crate postgres-package-version [session])
  80 +
  81 +(multi-os-session-method
  82 + postgres-package-version {:os :linux}
  83 + [os os-version session]
  84 + [8])
  85 +
  86 +(multi-os-session-method
  87 + postgres-package-version {:os :ubuntu :os-version [12]}
  88 + [os os-version session]
  89 + [9 1])
  90 +
  91 +;;; Install strategy
  92 +(defmulti-version-crate install-strategy [version session settings])
  93 +
  94 +(def ^{:dynamic true} *pgdg-repo-versions*
  95 + {"9.0" "9.0-5"
  96 + "9.1" "9.1-4"
  97 + "9.2" "9.2-5"})
  98 +
  99 +(defn pgdg-url
  100 + [version os-family]
  101 + (format
  102 + "http://yum.pgrpms.org/reporpms/%s/pgdg-%s%s-%s.noarch.rpm"
  103 + version
  104 + (name os-family)
  105 + (string/replace version "." "")
  106 + (*pgdg-repo-versions* version)))
  107 +
  108 +(multi-version-session-method
  109 + install-strategy {:os :rh-base}
  110 + [os os-version version session settings]
  111 + (->
  112 + (cond
  113 + (:strategy settings) settings
  114 + (:package-source settings) (assoc settings :strategy :package-source)
  115 + :else (let [default-version (postgres-package-version session)
  116 + target-version (:version settings)]
  117 + (if (= (version-string default-version) target-version)
  118 + (assoc settings
  119 + :strategy :packages
  120 + :packages (map
  121 + #(str "postgresql-" (name %))
  122 + (:components settings #{:server :libs}))
  123 + :layout :rh-base)
  124 + (assoc session
  125 + :strategy :package-source
  126 + :rpm {:name "pgdg.rpm" :url (pgdg-url version os)}
  127 + :packages (map
  128 + #(str "postgresql"
  129 + (string/replace target-version "." "")
  130 + "-" (name %))
  131 + (:components settings #{:server :libs}))
  132 + :layout :pgdg))))))
  133 +
  134 +(multi-version-session-method
  135 + install-strategy {:os :debian}
  136 + [os os-version version session settings]
  137 + (->
  138 + (cond
  139 + (:strategy settings) settings
  140 + (:package-source settings) (assoc settings :strategy :package-source)
  141 + :else (let [default-version (postgres-package-version session)
  142 + target-version (:version settings)]
  143 + (if (= (version-string default-version) target-version)
  144 + (assoc settings
  145 + :strategy :packages
  146 + :packages ["postgresql"]
  147 + :layout :debian-base)
  148 + (assoc settings
  149 + :strategy :package-source
  150 + :package-source (debian-backports-repository)
  151 + :packages ["libpq5" (str "postgresql-" target-version)]
  152 + :layout :debian-base))))))
  153 +
  154 +(multi-version-session-method
  155 + install-strategy {:os :ubuntu}
  156 + [os os-version version session settings]
  157 + (->
  158 + (cond
  159 + (:strategy settings) settings
  160 + (:package-source settings) (assoc settings :strategy :package-source)
  161 + :else (let [default-version (postgres-package-version session)
  162 + target-version (:version settings)]
  163 + (if (= (version-string default-version) target-version)
  164 + (assoc settings
  165 + :strategy :packages
  166 + :packages ["postgresql"]
  167 + :layout :debian-base)
  168 + (assoc settings
  169 + :strategy :package-source
  170 + :package-source {:name "Martin Pitt backports"
  171 + :aptitude {:url "ppa:pitti/postgresql"}
  172 + :apt {:url "ppa:pitti/postgresql"}}
  173 + :packages [(str "postgresql-" target-version)]
  174 + :layout :debian-base))))))
  175 +
  176 +(multi-version-session-method
  177 + install-strategy {:os :arch}
  178 + [os os-version version session settings]
  179 + (->
  180 + (cond
  181 + (:strategy settings) settings
  182 + (:package-source settings) (assoc settings :strategy :package-source)
  183 + :else (let [default-version (postgres-package-version session)
  184 + target-version (:version settings)]
  185 + (if (= (version-string default-version) target-version)
  186 + (assoc settings
  187 + :strategy :packages
  188 + :packages ["postgresql"]
  189 + :layout :arch)
  190 + (throw+
  191 + {:reason :no-install-strategy}
  192 + "No install strategy for postgres %s on %s %s"
  193 + version os os-version))))))
  194 +
  195 +;;; Dispatch to install strategy
  196 +(defmulti install-method (fn [session settings] (:strategy settings)))
  197 +
  198 +(defmethod install-method :packages [session settings]
  199 + (reduce package session (:packages settings)))
  200 +
  201 +(defmethod install-method :package-source
  202 + [session {:keys [package-source packages] :as settings}]
  203 + (->
  204 + session
  205 + (apply-map-> package/package-source (:name package-source) package-source)
  206 + (package/package-manager :update)
  207 + (when-> (= package-source :debian-backports)
  208 + (debian-backports/add-debian-backports)
  209 + (package/package-manager :update))
  210 + (for-> [pkg packages] (package pkg))))
  211 +
  212 +(defmethod install-method :rpm
  213 + [session {:keys [rpm] :as settings}]
  214 + (->
  215 + session
  216 + (action/with-precedence {:always-before `package/package}
  217 + (apply-map-> package/add-rpm (:name rpm) rpm))))
  218 +
  219 +
  220 +;;; Default settings
76 221 (def default-settings-map
77   - {:version "9.0"
78   - :components #{:server :contrib}
  222 + {:components #{:server :contrib}
79 223 :owner "postgres"
80 224 :options {:port 5432
81 225 :max_connections 100
@@ -110,26 +254,10 @@
110 254 [:permissions]))))
111 255 settings))
112 256
113   -;;; default values, which are distribution and package dependent
114   -
115   -(defn package-source
116   - "Decide where to get the packages from"
117   - [session version]
118   - (let [os-family (session/os-family session)]
119   - (cond
120   - (and (= :debian os-family) (.startsWith version "9.0")) :debian-backports
121   - (and (= :ubuntu os-family) (.startsWith version "9.0")) :martin-pitt-backports
122   - (and (= :centos os-family) (.startsWith version "9.0")) :pgdg
123   - (and (= :fedora os-family) (.startsWith version "9.0")) :pgdg
124   - :else :native)))
125   -
126   -(def pgdg-repo-versions
127   - {"9.0" "9.0-5"})
128   -
129 257 (defmulti default-settings
130 258 "Determine the default settings for the specified "
131   - (fn [session os-family package-source settings]
132   - [os-family package-source]))
  259 + (fn [session os-family layout settings]
  260 + layout))
133 261
134 262 (defn base-settings [session]
135 263 {:service "postgresql"
@@ -138,13 +266,12 @@
138 266 :options {:external_pid_file (str (stevedore/script (~lib/pid-root))
139 267 "/postgresql.pid")}})
140 268
141   -(defmethod default-settings [:debian :native]
142   - [session os-family package-source settings]
  269 +(defmethod default-settings :debian-base
  270 + [session os-family layout settings]
143 271 (let [version (:version settings)]
144 272 (merge-settings
145 273 (base-settings session)
146   - {:packages ["postgresql"]
147   - :default-cluster-name "main"
  274 + {:default-cluster-name "main"
148 275 :bin (format "/usr/lib/postgresql/%s/bin/" version)
149 276 :share (format "/usr/lib/postgresql/%s/share/" version)
150 277 :wal_directory (format "/var/lib/postgresql/%s/%%s/archive" version)
@@ -159,8 +286,8 @@
159 286 :external_pid_file (format "/var/run/postgresql/%s-%%s.pid" version)
160 287 :unix_socket_directory "/var/run/postgresql"}})))
161 288
162   -(defmethod default-settings [:rh :native]
163   - [session os-family package-source settings]
  289 +(defmethod default-settings :rh-base
  290 + [session os-family layout settings]
164 291 (let [version (:version settings)]
165 292 (merge-settings
166 293 (base-settings session)
@@ -176,17 +303,13 @@
176 303 :ident_file (format "/var/lib/pgsql/%s/%%s/pg_ident.conf" version)
177 304 :external_pid_file (format "/var/run/postmaster-%s-%%s.pid" version)}})))
178 305
179   -(defmethod default-settings [:rh :pgdg]
  306 +(defmethod default-settings :pgdg
180 307 [session os-family package-source settings]
181 308 (let [version (:version settings)]
182 309 (merge-settings
183 310 (base-settings session)
184   - (default-settings session :rh :native settings)
185   - {:packages (map
186   - #(str "postgresql" (string/replace version "." "")
187   - "-" (name %))
188   - (:components settings))
189   - :bin (format "/usr/pgsql-%s/bin/" version)
  311 + (default-settings session os-family :rh-base settings)
  312 + {:bin (format "/usr/pgsql-%s/bin/" version)
190 313 :share (format "/usr/pgsql-%s/share/" version)
191 314 :default-cluster-name "data"
192 315 :service (str "postgresql-" version "-%s")
@@ -199,7 +322,7 @@
199 322 :hba_file (format "/var/lib/pgsql/%s/%%s/pg_hba.conf" version)
200 323 :ident_file (format "/var/lib/pgsql/%s/%%s/pg_ident.conf" version)}})))
201 324
202   -(defmethod default-settings [:arch :native]
  325 +(defmethod default-settings :arch
203 326 [session os-family package-source settings]
204 327 (let [version (:version settings)]
205 328 (merge-settings
@@ -215,26 +338,6 @@
215 338 :hba_file "/var/lib/postgres/%%s/pg_hba.conf"
216 339 :ident_file "/var/lib/postgres/%%s/pg_ident.conf"}})))
217 340
218   -(defmethod default-settings [:debian :debian-backports]
219   - [session os-family package-source settings]
220   - (let [version (:version settings)]
221   - (merge-settings
222   - (default-settings session :debian :native settings)
223   - {:packages [(str "postgresql-" version)]})))
224   -
225   -(defmethod default-settings [:debian :martin-pitt-backports]
226   - [session os-family package-source settings]
227   - (default-settings session :debian :debian-backports settings))
228   -
229   -(defn pgdg-url
230   - [version os-family]
231   - (format
232   - "http://yum.pgrpms.org/reporpms/%s/pgdg-%s%s-%s.noarch.rpm"
233   - version
234   - (name os-family)
235   - (string/replace version "." "")
236   - (pgdg-repo-versions version)))
237   -
238 341 ;;; pg_hba.conf
239 342
240 343 (def ^{:private true}
@@ -539,17 +642,21 @@
539 642 assoc-in [:clusters (keyword cluster-name)]
540 643 settings)))
541 644
542   -(defn settings
  645 +(defn postgres-settings
543 646 "Add postgresql settings to the session map."
544   - [session settings-map & {:keys [instance]}]
545   - (let [package-source (get settings-map :package-source
546   - (package-source session (:version settings-map)))
  647 + [session
  648 + {:keys [version]
  649 + :or {version (version-string (postgres-package-version session))}
  650 + :as settings}
  651 + & {:keys [instance]}]
  652 + (let [settings (assoc settings :version version)
  653 + settings (install-strategy session version settings)
547 654 settings (merge-settings
548   - {:package-source package-source}
549 655 (default-settings
550   - session (session/base-distribution session)
551   - package-source settings-map)
552   - settings-map)
  656 + session
  657 + (session/base-distribution session)
  658 + (:layout settings) settings)
  659 + settings)
553 660 old-settings (parameter/get-target-settings
554 661 session :postgresql instance nil)]
555 662 (logging/debugf "Postgresql Settings %s" settings)
@@ -562,7 +669,8 @@
562 669 [cluster-name (:default-cluster-name settings)]
563 670 (cluster-settings cluster-name {} :instance instance)))))
564 671
565   -(defn postgres
  672 +
  673 +(defn install-postgres
566 674 "Install postgres."
567 675 [session & {:keys [instance]}]
568 676 (let [os-family (session/os-family session)
@@ -572,35 +680,8 @@
572 680 version (:version settings)]
573 681 (logging/debugf
574 682 "postgresql %s from %s packages [%s]"
575   - version (name package-source) (string/join ", " packages))
576   - (->
577   - session
578   - (when-> (= package-source :martin-pitt-backports)
579   - (package/package-source
580   - "Martin Pitt backports"
581   - :aptitude {:url "ppa:pitti/postgresql"})
582   - (package/package-manager :update))
583   - (when-> (= package-source :debian-backports)
584   - (debian-backports/add-debian-backports)
585   - (package/package-manager :update)
586   - (package/package
587   - "libpq5"
588   - :enable (str
589   - (stevedore/script (~lib/os-version-name))
590   - "-backports")))
591   - (when->
592   - (= package-source :pgdg)
593   - (action/with-precedence {:action-id ::add-pgdg-rpm
594   - :always-before `package/package}
595   - (package/add-rpm "pgdg.rpm" :url (pgdg-url version os-family)))
596   - (action/with-precedence {:action-id ::pgdg-update
597   - :always-before `package/package
598   - :always-after ::add-pgdg-rpm}
599   - (package/package-manager :update)))
600   - ;; install packages
601   - (arg-> [session]
602   - (for-> [package (:packages settings)]
603   - (package/package package))))))
  683 + version (:name package-source) (string/join ", " packages))
  684 + (install-method session settings)))
604 685
605 686 (defn hba-conf
606 687 "Generates a pg_hba.conf file from the arguments. Each record is either a
22 test/pallet/crate/postgres_test.clj
@@ -37,7 +37,7 @@
37 37 [:ubuntu :aptitude]
38 38 (postgres/default-settings
39 39 {:server {:image {:os-family :ubuntu} :node-id :id}}
40   - :debian :martin-pitt-backports (postgres/settings-map {}))))
  40 + :debian :debian-base (postgres/settings-map {}))))
41 41 :options :data_directory)))
42 42
43 43 (deftest settings-test
@@ -47,20 +47,20 @@
47 47 :pallet.stevedore.bash/bash
48 48 (pallet.script/with-script-context
49 49 [:ubuntu :aptitude]
50   - (pallet.crate.postgres/settings
  50 + (pallet.crate.postgres/postgres-settings
51 51 {:server {:image {:os-family :ubuntu} :node-id :id}}
52   - (pallet.crate.postgres/settings-map {}))))
  52 + (pallet.crate.postgres/settings-map {:layout :debian-base}))))
53 53 :parameters :host :id :postgresql :default :options :data_directory )))
54 54
55 55 (deftest postgres-test
56 56 (is ; just check for compile errors for now
57 57 (build-actions/build-actions
58 58 {}
59   - (postgres/settings (postgres/settings-map {:version "8.0"}))
60   - (postgres/postgres)
61   - (postgres/settings (postgres/settings-map {:version "9.0"}))
  59 + (postgres/postgres-settings (postgres/settings-map {:version "8.0"}))
  60 + (postgres/install-postgres)
  61 + (postgres/postgres-settings (postgres/settings-map {:version "9.0"}))
62 62 (postgres/cluster-settings "db1" {})
63   - (postgres/postgres)
  63 + (postgres/install-postgres)
64 64 (postgres/hba-conf)
65 65 (postgres/postgresql-script :content "some script")
66 66 (postgres/create-database "db")
@@ -70,13 +70,13 @@
70 70 (let [settings
71 71 (second (build-actions/build-actions
72 72 {}
73   - (postgres/settings
  73 + (postgres/postgres-settings
74 74 (postgres/settings-map
75 75 {:version "9.0"
76 76 :wal_directory "/var/lib/postgres/%s/archive/"}))
77 77 (postgres/cluster-settings "db1" {})
78 78 (postgres/cluster-settings "db2" {})
79   - (postgres/settings (postgres/settings-map {:version "9.0"}))))
  79 + (postgres/postgres-settings (postgres/settings-map {:version "9.0"}))))
80 80 pg-settings (-> settings :parameters :host :id :postgresql :default)]
81 81 (is (-> pg-settings :clusters :db1))
82 82 (is (-> pg-settings :clusters :db2))
@@ -104,10 +104,10 @@
104 104 (package/package-manager :update)
105 105 (automated-admin-user/automated-admin-user))
106 106 :settings (phase/phase-fn
107   - (postgres/settings (postgres/settings-map {}))
  107 + (postgres/postgres-settings (postgres/settings-map {}))
108 108 (postgres/cluster-settings "db1" {:options {:port 5433}}))
109 109 :configure (phase/phase-fn
110   - (postgres/postgres))
  110 + (postgres/install-postgres))
111 111 :verify (phase/phase-fn
112 112 (postgres/log-settings)
113 113 (postgres/initdb)

0 comments on commit 5e48694

Please sign in to comment.
Something went wrong with that request. Please try again.