Permalink
Browse files

Initial BDSM Redis extension.

  • Loading branch information...
1 parent 307e3a1 commit 0bc989f49a80586e33a1848c510f7c5089906258 @wayneeseguin wayneeseguin committed Mar 24, 2011
View
55 redis/README.md
@@ -0,0 +1,55 @@
+# Redis BDSM Extension
+
+## Usage
+
+ redis <options> <action>
+
+## Options
+
+Path to redis.conf file
+
+ redis --conf /etc/redis/redis.conf
+
+Path to redis pid file
+
+ redis --pid /var/run/redis.pid
+
+Path to redis server binary
+
+ redis --bin /usr/bin
+
+Username to invoke redis as
+
+ redis --user redis
+
+Use to debugging the control script
+
+ redis --trace <action>
+
+
+## Actions
+
+Start Redis
+
+ redis start
+
+Stop Redis
+
+ redis stop
+
+Restart Redis
+
+ redis restart
+
+Display Redis INFO output for the server
+
+ redis info
+
+Show if Redis is running and on what port if so.
+
+ redis status
+
+Display the CLI usage information
+
+ redis help
+
View
0 redis/VERSION
No changes.
View
51 redis/bin/help
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+
+description()
+{
+ printf "Extension for managing redis (http://wiki.redis.org/Main)."
+}
+
+actions()
+{
+ printf "
+ install - Install redis on the system.
+ setup - Setup redis configuration.
+ start - Startup redis.
+ stop - Stop redis.
+ restart - Restart redis.
+ server <project_name> - Add and enable a new server(s)
+ enable <project_name> - Enable project server config.
+ disable <project_name> - Disable project server config.
+ help - Display this help text.
+ "
+}
+
+usage()
+{
+ printf "
+Description
+
+ BDSM $(description)
+
+Usage
+
+ bdsm redis [action]
+
+Actions
+ $(actions)
+"
+}
+
+command="usage" # Default
+while [[ $# -gt 0 ]] ; do
+ token="$1" ; shift
+ case "$token" in
+ description) command="description" ;;
+ actions) command="actions" ;;
+ *) command="usage" ;;
+ esac
+done
+
+$command
+
+exit $?
View
124 redis/bin/install
@@ -0,0 +1,124 @@
+#!/usr/bin/env bash
+
+if echo "$*" awk '/--trace/' ; then set -x ; fi
+
+log() { printf "$*\n" ; return $? ; }
+fail() { log "ERROR: $*" ; exit 1 ; }
+
+setup_redis_user()
+{
+ if [[ $UID -eq 0 ]] ; then
+ useradd -r redis # TODO: Make this more robust...
+ fi
+ return 0
+}
+
+create_redis_directories()
+{
+ log "Creating directories ${package} ${version}...\n"
+ for directory in "${directories[@]}" ; do
+ [[ ! -d "$directory" ]] && mkdir -p "$directory"
+ chown -R $user "$directory"
+ done
+ return 0
+}
+
+install_init_script()
+{
+ cp -f "$extension_templates_path/redis.init.template" \
+ "${init_scripts_path}/redis"
+
+ [[ -s "${init_scripts_path}/redis" && ! -x "${init_scripts_path}/redis" ]] &&
+ chmod +x "${init_scripts_path}/redis"
+ return 0
+}
+
+cleanup()
+{
+ log "Cleaning up ${package} ${version}...\n"
+ if ! cd "$src_path" ; then
+ fail "Failed changing into the $src_path source path."
+ fi
+
+ for entity in $(find "$src_path/" -mindepth 1 -maxdepth 1 -iname "${package}-${version}*") ; do
+ rm -rf "$entity"
+ done
+ return 0
+}
+
+download_redis()
+{
+ [[ ! -d "$src_path" ]] && mkdir -p $src_path
+
+ if ! cd $src_path ; then
+ fail " Failed to create and cd to $src_path"
+ fi
+
+ log "Downloading ${package_file}...\n"
+ if ! curl -O -L "${redis_url}" ; then
+ fail "${package_file} failed to download from '${redis_url}'."
+ fi
+ return $?
+}
+
+extract_redis()
+{
+ log "Extracting ${package} ${version}...\n"
+ if ! tar zxf ${package_file} ; then
+ fail "${package_file} failed to extract."
+ fi
+
+ return 0
+}
+
+build_redis()
+{
+ cd ${package}-${version}
+ if [[ $? -gt 0]] ; then
+ fail "Could not enter extracted ${package}-${version} source directory."
+ fi
+
+ # Nothing to configure, proceeding straight to make.
+
+ log "#compiling ${package} ${version}...\n"
+ if ! make 2>&1 > "${redis_src_path}/make.log" ; then
+ fail "Compiling ${package}-${version} failed, see ${redis_src_path}/make.log"
+ fi
+ return $?
+}
+
+install_redis()
+{
+ log "#installing ${package} ${version}...\n"
+
+ for file in redis-server redis-cli ; do
+ if ! install -b -d -S "${redis_src_path}/${file}" "${redis_bin_path}/${file}" ; then
+ fail "${package} ${version} failed to install (specifically ${file}).\n"
+ fi
+ done
+ return 0
+}
+
+source "${extension_scripts_path}/initialize"
+
+while [[ $# -gt 0 ]] ; do
+ token="$1" ; shift
+ case "$token" in
+ --trace) set -o xtrace ;;
+ *) fail "Unknown command line argument '$token' " ;;
+ esac
+done
+
+download_redis
+
+build_redis
+
+install_redis
+
+setup_redis_user
+
+create_redis_directories
+
+install_init_script
+
+exit $result
View
0 redis/bin/restart
No changes.
View
0 redis/bin/setup
No changes.
View
0 redis/bin/start
No changes.
View
0 redis/bin/stop
No changes.
View
72 redis/scripts/initialize
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+
+set -o errtrace
+export PS4='+[${BASH_SOURCE}] : ${LINENO} : ${FUNCNAME[0]:+${FUNCNAME[0]}() $ }'
+
+log() { printf "$*\n" ; return $? ; }
+fail() { log "ERROR: $*" ; exit 1 ; }
+
+if [[ ${debug_flag:-0} -eq 1 ]] ; then
+ set -o verbose
+fi
+
+if [[ ${trace_flag:-0} -eq 1 ]] ; then
+ set -o xtrace
+fi
+
+files=( "/etc/.bdsmrc" "$HOME/.bdsmrc" )
+for file in "${files[@]}" ; do
+ [[ -s "$file" ]] && source "$file"
+done
+
+export PATH="/sbin:/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local:sbin:$PATH"
+
+if [[ $UID -eq 0 ]] ; then
+ if [[ -d /etc/init.d ]] ; then
+ init_scripts_path=/etc/init.d
+ elif [[ -d /etc/rc.d ]] ; then
+ init_scripts_path=/etc/rc.d
+ else
+ printf "ERROR: Unknown init scripts directory (neither /etc/init.d nor /etc/rc.d were found).\n"
+ exit 1
+ fi
+else
+ true ${init_scripts_path:="$HOME/bin"}
+fi
+
+package="redis"
+true ${version:="1.2.2"}
+true ${archive_format:="tar.gz"}
+package_file="${package}-${version}.${archive_format}"
+true ${redis_url:="http://redis.googlecode.com/files/${package_file}"}
+result=0
+
+if [[ $UID -eq 0 ]] ; then
+ prefix="${prefix:-/usr/local}"
+ directories=(/etc/redis /var/log/redis /var/run/redis /data/redis)
+ user="redis"
+else
+ prefix="${prefix:-$HOME}"
+ redis_install_path="$prefix"
+ directories=("$HOME/.redis/log" "$HOME/.redis/data" "$HOME/.redis/pids")
+ user="$USER"
+fi
+true ${src_path:="$prefix/src"}
+
+# Redis Configuration.
+true ${redis_package_path="${src_path}/${package}-${version}"}
+true ${redis_src_path="${redis_package_path}/src"}
+true ${redis_install_path:="$prefix"}
+true ${redis_bin_path:="${redis_install_path}/bin"}
+true ${redis_config_path:="/etc/redis"}
+true ${redis_config_file:="${redis_config_file:-"${redis_config_path}/redis.conf"}"}
+true ${redis_servers_path:="$redis_etc_path/servers"}
+true ${redis_server_binary:="${redis_bin_path}/redis-server"}
+true ${redis_cli_binary:="${redis_bin_path}/redis-cli"}
+true ${redis_pid_file:="/var/run/redis.pid"}
+true ${redis_lock_file:="/var/lock/redis.lock"}
+true ${redis_spool_path:="/var/spool/redis"}
+true ${redis_log_path:="/var/log/redis"}
+
+timestamp=$(date +%m.%d.%YT%H:%M:%S)
+redis_master_pid="$(ps auxww | awk "/[r]edis-server $redis_config_file/{print \$2}" | head -1)"
View
0 redis/templates/redis.conf.template
No changes.
View
136 redis/templates/redis.init.template
@@ -0,0 +1,136 @@
+#!/usr/bin/env bash
+
+log() { printf "$*\n" ; return $? ; }
+fail() { log "ERROR: $*" ; exit 1 ; }
+
+initialize()
+{
+ if [[ $UID -eq 0 ]] ; then
+ [[ -s /etc/conf.d/redis ]] && source /etc/conf.d/redis
+ else
+ redis_user="$USER"
+ fi
+
+ true ${redis_user:="redis"}
+ true ${redis_port:="6379"}
+ true ${redis_bin_path:="/usr/local/bin"}
+ true ${redis_server_binary:="${redis_bin_path}/redis-server"}
+ true ${redis_pid_file:="/var/run/redis.pid"}
+ true ${redis_config_file:="/etc/redis/redis.conf"}
+
+ if [[ -s "$pidfile" ]] ; then
+ pid=$(cat "$pidfile")
+ else
+ rm -f "$pidfile"
+ fi
+
+ return 0
+}
+
+redis_help()
+{
+ printf "
+
+Usage:
+
+ $0 <options> <action>
+
+Options:
+
+ --conf - Path to redis.conf file
+ --pid - Path to redis pid file
+ --bin - Path to redis-server binary
+ --user - Username to invoke redis as
+ --trace - Use to debugging this control script
+
+Description:
+
+ start - Start Redis
+ stop - Stop Redis
+ restart - Restart Redis
+ info - Display Redis INFO output for the server
+ status - Show if Redis is running and on what port if so.
+ help - Display this usage information
+
+"
+ return 0
+}
+
+redis_start()
+{
+ if [[ -s "$pidfile" ]] ; then
+ fail "$pidfile exists, redis-server is either already running or crashed.\n"
+ else
+ log "#starting Redis as $redis_user using $redis_config..."
+
+ if [[ "$redis_user" != "$USER" ]] ; then
+ su "$redis_user" -c "\"$redis_server_binary\" \"$redis_config\""
+ else
+ "$redis_server_binary" "$redis_config"
+ fi
+ fi
+ return $?
+}
+
+redis_stop()
+{
+ if [[ -s "$pidfile" ]] ; then
+ log "#stopping Redis as $redis_user using $redis_config..."
+
+ "${redis_server_binary/%server/cli}" shutdown
+
+ while [[ -x /proc/${pid} ]] ; do
+ echo "Waiting for the redis server to shutdown ..."
+ sleep 1
+ done
+ rm -f $pidfile
+ log "redis server stopped."
+ sleep 1
+ else
+ fail "$pidfile does not exist, Redis is not running"
+ fi
+}
+
+redis_restart()
+{
+ log "#restarting Redis server..."
+ if [[ -s "$pidfile" ]] ; then
+ redis_stop
+ else
+ log "Redis is not running, proceeding."
+ fi
+ redis_start
+
+ return $?
+}
+
+redis_info()
+{
+ if [[ -s "$pidfile" ]] ; then
+ "${redis_server_binary/%server/cli}" INFO
+ else
+ fail "$pidfile does not exist, Redis is not running"
+ exit 1
+ fi
+ return $?
+}
+
+initialize
+
+while [[ $# -gt 0 ]] ; do
+ token="$1" ; shift
+ case "$token" in
+ --trace) set -o xtrace ;;
+ --conf) redis_config_file="$1" ; shift ;;
+ --pid) redis_pid_file="$1" ; shift ;;
+ --bin) redis_bin_path="$1" ; shift ;;
+ start) redis_start ;;
+ stop) redis_stop ;;
+ restart) redis_restart ;;
+ info) redis_info ;;
+ help) redis_help ;;
+ *) redis_help ; exit 1 ;;
+ esac
+done
+
+exit $?

0 comments on commit 0bc989f

Please sign in to comment.