1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#!/bin/sh
#
# Copyright (C) 2012 Mail.RU
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# Tarantool instance deployment script
#
prompt_name=`basename $0`
act_prompt=1
act_status=0
act_debug=0
act_dry=0
act_quiet=0
error() {
echo "$prompt_name error: $*" 1>&2
exit 1
}
log() {
echo "$prompt_name: $*"
}
usage() {
echo "Tarantool deployment script: add more Tarantool instances."
echo "usage: tarantool_deploy.sh [options] <instance>"
echo
echo " --prefix <path> installation path (/usr)"
echo " --prefix_etc <path> installation etc path (/etc)"
echo " --prefix_var <path> installation var path (/var)"
echo
echo " --status display deployment status"
echo " --dry don't create anything, show commands"
echo
echo " --debug show commands"
echo " --yes don't prompt"
echo " --help display this usage"
echo
exit $1
}
rollback_instance() {
id=$1
workdir="${prefix_var}/tarantool_box$id"
config="${prefix_etc}/tarantool/tarantool_box$id.cfg"
rm -rf $workdir
rm -f $config
rm -f "${prefix}/bin/tarantool_box$id.sh"
rm -f "${prefix_etc}/init.d/tarantool_box$id"
}
rollback() {
log ">>> rollback changes"
rollback_instance $deploy_name
exit 1
}
try() {
cmd="$*"
[ $act_debug -gt 0 ] && log "$cmd"
if [ $act_dry -eq 0 ]; then
eval "$cmd"
if [ $? -gt 0 ]; then
rollback
fi
fi
}
deploy() {
id=$1
workdir="${prefix_var}/tarantool_box$id"
config="${prefix_etc}/tarantool/tarantool_box$id.cfg"
log ">>> deploy instance $id"
# setup work environment
try "mkdir -p $workdir/logs"
# setup startup snapshot
try "cp \"${prefix}/share/tarantool/00000000000000000001.snap\" $workdir"
try "chown tarantool:tarantool -R $workdir"
# setup configuration file
try "cp \"${prefix_etc}/tarantool/tarantool.cfg\" $config"
try 'echo work_dir = \"$workdir\" >> $config'
try 'echo username = \"tarantool\" >> $config'
try 'echo logger = \"cat - \>\> logs/tarantool.log\" >> $config'
# setup wrapper
try "ln -s \"${prefix}/bin/tarantool_multi.sh\" \"${prefix}/bin/tarantool_box$id.sh\""
# setup startup script
try "ln -s \"${prefix_etc}/init.d/tarantool_box\" \"${prefix_etc}/init.d/tarantool_box$id\""
# register service
[ -x /sbin/chkconfig ] && try "/sbin/chkconfig --add tarantool_box$id"
}
deploy_check() {
id=$1
# check, if instance is already exist (configuration file, consistent way)
if [ $deploy_exists -eq 1 ]; then
grep "^\(${id}\)$" $deploy_cfg > /dev/null
if [ $? -eq 0 ]; then
[ $act_quiet -eq 0 ] && log "Instance '${id}' is already deployed."
exit 0
fi
fi
# check, if there are any instance-related files exists that could be
# accidently removed or overwritten by setup.
instance_workdir="${prefix_var}/tarantool_box$id"
instance_config="${prefix_etc}/tarantool/tarantool_box$id.cfg"
instance_wrapper="${prefix}/bin/tarantool_box$id.sh"
instance_startup="${prefix_etc}/init.d/tarantool_box$id"
[ -d $instance_workdir ] && error "Instance workdir exists: '$instance_workdir'"
[ -f $instance_config ] && error "Instance configuration file exists: $instance_config"
[ -f $instance_wrapper ] && error "Instance wrapper file exists: $instance_wrapper"
[ -f $instance_startup ] && error "Instance startup file exists: $instance_startup"
}
commit() {
log ">>> updating deployment config"
try "echo $1 >> $deploy_cfg"
}
# process command line arguments
[ $# -eq 0 ] && usage 1
deploy_name_set=0
deploy_name=""
while [ $# -ge 1 ]; do
case $1 in
--yes) act_prompt=0; shift 1 ;;
--quiet) act_quiet=1; shift 1 ;;
--prefix) prefix=$2; shift 2 ;;
--prefix_var) prefix_var=$2; shift 2 ;;
--prefix_etc) prefix_etc=$2; shift 2 ;;
--dry) act_dry=1 ; act_debug=1 ; shift 1 ;;
--debug) act_debug=1; shift 1 ;;
--status) act_status=1; shift 1 ;;
--help) usage 0; shift 1 ;;
*) deploy_name=$1; deploy_name_set=1; break ;;
esac
done
set ${prefix:="/usr"}
set ${prefix_var:="/var"}
set ${prefix_etc:="/etc"}
deploy_cfg="${prefix_etc}/tarantool/tarantool_deploy.cfg"
deploy_exists=0
# check deployment configuration file
[ -f $deploy_cfg ] && deploy_exists=1
# do migration from old deployment (if necessary)
if [ $deploy_exists -eq 0 ]; then
deploy_cfg_old="/usr/local/etc/tarantool_deploy.cfg"
if [ -f $deploy_cfg_old ]; then
mkdir -p /etc/tarantool
cp /usr/local/etc/tarantool* "${prefix_etc}/tarantool/"
deploy_exists=1
fi
fi
# display status
if [ $act_status -ne 0 ]; then
if [ $deploy_exists -eq 0 ]; then
log "No tarantool instances found."
else
log "Current instances:\n`cat $deploy_cfg`"
fi
exit 0
fi
# check that instance name was specified
[ $deploy_name_set -eq 0 ] && usage 1
# validate instance name
echo $deploy_name | grep '^[[:digit:]]\+.\(1\|2\)' > /dev/null
if [ $? -eq 1 ]; then
error "Bad instance name format, should be e.g: 1.1, 1.2, etc."
fi
# check if it consistent to deploy new instance
deploy_check $deploy_name
# ask permission to continue
if [ $act_prompt -eq 1 ]; then
[ $act_dry -ne 0 ] && log "(dry mode)"
log "About to deploy Tarantool instance $deploy_name."
log "Continue? [n/y]"
read answer
case "$answer" in
[Yy]) ;;
*)
log "Abort"
exit 0
;;
esac
fi
deploy $deploy_name
commit $deploy_name
log "done"
# __EOF__
|