Skip to content

Instantly share code, notes, and snippets.

@narkisr
Created Jun 23, 2012
Embed
What would you like to do?
pallet redis crate
(ns pallet.crate.redis
"Simple redis crate for pallet,
with a DSL for configuration"
(:require [pallet.parameter :as parameter]
[pallet.action.package :as pkg]
[pallet.action.user :as user]
[pallet.action.directory :as dir]
[pallet.action.remote-file :as file]
[pallet.action.service :as service]))
(def default-config
{:daemonize "yes"
:pidfile "/var/run/redis.pid"
:port 6379
:bind "127.0.0.1"
:timeout 3000
:loglevel "verbose"
:logfile "/var/log/redis/redis-server.log"
:databases 16
:dir "/var/lib/redis"
:dbfilename "dump.rdb"
:rdbcompression "yes"
:save ["900 1" "300 10" "60 10000"]
:appendfsync "everysec"
:vm-enabled "no"
:glueoutputbuf "yes"
:hash-max-zipmap-entries 64
:hash-max-zipmap-value 512
:activerehashing "yes"})
(defn- generate-line
[[directive args]]
(format "%-8s %s\n"
(name directive)
(str args)))
(defn- explode-lines
[[directive args]]
(if (sequential? args)
(for [arg args] [directive arg])
[[directive args]]))
(defn- generate-config
[config]
(->> config
(map explode-lines)
(reduce concat [])
(map generate-line)
(reduce str)))
(defn- apply-defaults
[config data-dir]
(-> default-config
(assoc :dir data-dir)
(merge config)))
(defn install-package
[session]
(-> session
(pkg/package-source "dotdeb" :aptitude
{:url "http://packages.dotdeb.org"
:scopes ["all"]
:key-url "http://www.dotdeb.org/dotdeb.gpg"
:release "stable"})
(pkg/package-manager :update)
(pkg/package "redis-server")))
(defn settings
[session & {:keys [config data-dir user group]
:or {data-dir "/var/lib/redis"}}]
(parameter/assoc-target-settings
session :redis :main
{:config (generate-config (apply-defaults config data-dir))}))
(defn configure
[session]
(let [{:keys [config user group data-dir]}
(parameter/get-target-settings session :redis :main)]
(-> session
(user/group group :system true)
(user/user user :system true :group group)
(dir/directory data-dir :owner user :group group :mode "0755")
(file/remote-file "/etc/redis/redis.conf"
:content config
:literal true))))
(defn restart
[session]
(-> session
(service/service "redis" :action :restart)))
(defn start
[session]
(-> session
(service/service "redis" :action :start)))
(defn stop
[session]
(-> session
(service/service "redis" :action :stop)))
#_
(pallet.core/defnode redis
{}
:bootstrap (pallet.phase/phase-fn
(pallet.crate.automated-admin-user/automated-admin-user))
:configure (pallet.phase/phase-fn
(pallet.crate.redis/install-package)
(pallet.crate.redis/configure :config {:maxmemory 1073741824}
(pallet.crate.redis/configure))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment