Skip to content

Commit

Permalink
stabilize pg cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
niquola committed Jul 4, 2016
1 parent 81adc26 commit fb585a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ankusha/config.clj
Expand Up @@ -30,7 +30,7 @@
:primary_conninfo (to-props {:host (:host master-cfg)
:port (:port master-cfg)
:user (get-in master-cfg [:user :name])
:password (get-in master-cfg [:user :name])})
:password (get-in master-cfg [:user :password])})
:restore_command (str "cp /tmp/wallogs/pg_xlog/%f %p")
:archive_cleanup_command "rm -f %r"}))

Expand Down
40 changes: 27 additions & 13 deletions src/ankusha/pg_cluster.clj
Expand Up @@ -11,6 +11,15 @@

(defn bin [util] (str (:bin @config) "/" util))

(defn sh! [& args]
(log/info args)
(let [res (apply sh/sh args)]
(if (= 0 (:exit res))
res
(do
(log/error res)
(throw (Exception. (str res)))))))

(defn node-config [opts]
(assert (:data-dir opts))
(let [data-dir (:data-dir opts)]
Expand Down Expand Up @@ -119,25 +128,27 @@
"-D" (pg-data-dir cfg)
:env {"PGPASSFILE" pgpass-path}]]

(sh/sh "mkdir" "-p" (pg-data-dir cfg))
(sh! "mkdir" "-p" (:data-dir cfg))
(sh! "chmod" "0700" (:data-dir cfg))
(spit pgpass-path
(let [{{u :name pwd :password} :user h :host p :port} parent-cfg]
(str h ":" p ":*:" u ":" pwd "\n")))
(log/info "chmod .pgpass" (sh/sh "chmod" "0600" pgpass-path))
(log/info "chmod .pgpass")
(sh! "chmod" "0600" pgpass-path)
(log/info args)
(let [res (apply sh/sh args)]
(if (= 0 (:exit res))
(log/info "basebackup done")
(throw (Exception. (str "Basebackup failed: " res)))))
(sh/sh "rm" "-f" pgpass-path)
(sh! "rm" "-f" pgpass-path)
(pg-config/update-config cfg)
(pg-config/update-hba cfg)
(pg-config/update-recovery parent-cfg cfg)
(state/assoc-in [:pg] cfg)
(let [res (start)]
(when-not (= 0 (:exit res))
(throw (Exception. (str res)))))
(wait-pg cfg 10)))
(wait-pg cfg 600)))

(defn clean-up []
(sh/sh "rm" "-rf" "/tmp/wallogs")
Expand All @@ -158,7 +169,7 @@

(def cfg
{:name "node-1"
:host "192.168.0.108"
:host "127.0.0.1"
:port 5434
:data-dir "/tmp/node-1"})

Expand All @@ -170,21 +181,24 @@
(start)
(stop)

(sh/sh "rm" "-rf" "/tmp/node-2")
(sh! "rm" "-rf" "/tmp/node-2")

(state/with-node "node-2"
(replica master-cfg
{:name "node-2"
:host "192.168.0.108"
:host "127.0.0.1"
:port 5435
:data-dir "/tmp/node-2"}))

(start)
(state/with-node "node-2"
(stop))
(state/with-node "node-2" (stop))


(sh/sh "env" :env {"UPS" "HELLO"})
(replica master-cfg
{:name "node-3"
:host "127.0.0.1"
:port 5436
:data-dir "/tmp/node-3"})

(replica "node-1" {:name "node-3" :port 5436})
(start "node-3")
(stop "node-3"))
(state/with-node "node-3" (stop))
)

0 comments on commit fb585a0

Please sign in to comment.