Skip to content

Instantly share code, notes, and snippets.

@chrismyang
Created April 28, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismyang/699837ba1c9508659627 to your computer and use it in GitHub Desktop.
Save chrismyang/699837ba1c9508659627 to your computer and use it in GitHub Desktop.
domino-create-instance.sh
#!/bin/bash
#
# Creates a Domino server instance.
#
set -o nounset -o errexit
function die() { echo >&2 "$@"; exit 1; }
function require_sudo() {
if [ $UID != 0 ]
then
die "This script must be executed as root"
fi
}
function usage() {
local script_name=`basename $0`
cat <<EOF
Usage:
$ sudo bash $script_name
--artifact="" the path of the Domino server .tar.gz file
--directory="" the parent directory under which the instance is to be created (required)
--name="" the name of the instance, e.g. "frontend", "exec-1a" (required)
--port="" the port Domino should listen at (default: $DEFAULT_PORT)
--user="" user that Domino should run as (required)
EOF
exit 1
}
function parse_options() {
ARTIFACT=""
BASE_DIRECTORY=""
INSTANCE_NAME=""
DEFAULT_PORT=9000
PORT=$DEFAULT_PORT
DOMINO_USER=""
for i in "$@"
do
case $i in
--artifact=*)
ARTIFACT="${i#*=}"
shift
;;
--directory=*)
BASE_DIRECTORY="${i#*=}"
shift
;;
--name=*)
INSTANCE_NAME="${i#*=}"
shift
;;
--port=*)
PORT="${i#*=}"
shift
;;
--user=*)
DOMINO_USER="${i#*=}"
shift
;;
*)
usage
;;
esac
done
}
function validate_options() {
[ "$ARTIFACT" == "" ] && usage
[ ! -f "$ARTIFACT" ] && die "File not found: '$ARTIFACT'"
[ "$BASE_DIRECTORY" == "" ] && usage
[ ! -d "$BASE_DIRECTORY" ] && die "Directory not found: '$BASE_DIRECTORY'"
[ "$INSTANCE_NAME" == "" ] && usage
DOMINO_SERVICE_NAME="domino-$INSTANCE_NAME"
INSTANCE_DIR="$BASE_DIRECTORY/$DOMINO_SERVICE_NAME"
[ -d "$INSTANCE_DIR" ] && die "Directory already exists: '$INSTANCE_DIR'"
[ "$PORT" == "" ] && usage
[ "$DOMINO_USER" == "" ] && usage
true
}
function install_and_check_java() {
yum -y install java-1.7.0-openjdk
if [ `java -version 2>&1 | head -1 | awk '{print $3}' | tr -d \"` -ne "1.7.0_76" ]
then
exit 1
fi
}
function monit_config_template() {
cat <<EOF >>/etc/monit.conf
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
allow @monit # allow users of group 'monit' to connect (rw)
allow @users readonly # allow users of group 'users' to connect readonly
EOF
printf "Wrote template to /etc/monit.conf\n"
}
function install_and_start_monit() {
yum -y install monit
monit_config_template
service monit start
}
function start_template() {
cat <<EOF
#!/bin/bash
# Starts the Domino server.
export JAVA_OPTS="\$JAVA_OPTS -Duser.timezone=US/Pacific -Dhttp.port=$PORT"
exec $SERVER_DIR/bin/domino-executord >> $SERVICE_LOG_FILE 2>&1
EOF
}
function stop_template() {
cat <<EOF
#!/bin/bash
# Stops the Domino server.
if [ -f "$PID_FILE" ]
then
PID=\$(cat $PID_FILE)
kill \$PID
for i in {1..15}
do
if [ ! -f "$PID_FILE" ]; then exit 0; fi
sleep 1
done
echo "Process did not exit. Force-killing it."
kill -9 \$PID
else
echo "Domino seems to be already stopped, because $PID_FILE was not found."
fi
EOF
}
function readme_template() {
cat <<EOF
Domino instance $INSTANCE_NAME
The application is automatically launched by Monit. To stop it, run "monit stop $DOMINO_SERVICE_NAME".
- Process ID file: $PID_FILE
- Application configuration: $SERVER_CONFIG_FILE
- Application log file: $SERVER_DIR/logs/application.log
- Service log file: $SERVICE_LOG_FILE
- Monit configuration file: $SERVICE_CONFIGURATION_FILE
- Monit log files: /var/log/monit
EOF
}
function service_template() {
cat <<EOF
check process $DOMINO_SERVICE_NAME with pidfile $PID_FILE
start program = "/bin/su - $DOMINO_USER -c $START_SCRIPT"
stop program = "$STOP_SCRIPT"
EOF
}
function create_instance() {
SERVER_DIR="$INSTANCE_DIR/server"
PID_FILE="$SERVER_DIR/RUNNING_PID"
SERVER_CONFIG_FILE="$SERVER_DIR/serverConfig"
START_SCRIPT="$INSTANCE_DIR/start"
STOP_SCRIPT="$INSTANCE_DIR/stop"
README_FILE="$INSTANCE_DIR/README"
SERVICE_DIR="/service/$DOMINO_SERVICE_NAME"
SERVICE_FILE="$SERVICE_DIR/run"
SERVICE_LOG_FILE="$SERVER_DIR/logs/service.log"
SERVICE_CONFIGURATION_FILE="/etc/monit.d/$DOMINO_SERVICE_NAME"
echo "Creating $DOMINO_SERVICE_NAME instance on $INSTANCE_DIR"
mkdir -p "$INSTANCE_DIR"
mkdir -p "$SERVER_DIR/logs"
mkdir -p "$SERVER_DIR/runs"
mkdir -p "$SERVER_DIR/tmp"
tar zxf "$ARTIFACT" -C "$INSTANCE_DIR"
start_template > "$START_SCRIPT"
chmod +x "$START_SCRIPT"
stop_template > "$STOP_SCRIPT"
chmod +x "$STOP_SCRIPT"
readme_template > "$README_FILE"
chown -R $DOMINO_USER:$DOMINO_USER "$INSTANCE_DIR"
service_template > "$SERVICE_CONFIGURATION_FILE"
echo ""
echo "Instructions are available at $README_FILE"
echo ""
echo "Before starting the instance, please provide a configuration file at $SERVER_CONFIG_FILE"
}
require_sudo
parse_options "$@"
validate_options
create_instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment