Skip to content

Instantly share code, notes, and snippets.

@spudfkc
Created July 29, 2014 18:33
Show Gist options
  • Save spudfkc/df593208979875fea11c to your computer and use it in GitHub Desktop.
Save spudfkc/df593208979875fea11c to your computer and use it in GitHub Desktop.
Script for redeploy, deploy static, upgrade/install of UrbanCode Deploy dev server
#!/bin/bash
#
# author: ncc
#
readonly PROGNAME=$(basename $0)
readonly PROGDIR=$(readlink -m $(dirname $0))
readonly ARGS="$@"
VERSION=dev
###############################################################################
#### CONFIGURE ################################################################
readonly UCD_SERVERS=/opt/udeploy/servers
readonly DB_TYPE=mysql
readonly DB_PREFIX=db_
###############################################################################
# DEBUG
set -x
usage() {
cat <<- EOF
usage: $PROGNAME options
This is used for starting up instances of a UCD server as well as any redeploy or
updades. It can handle database upgrades, strict ibm-ucd.jar redeploy, or static
content redeploy.
OPTIONS:
-n --name Name of the UCD server to act against.
-r --redeploy Redeploy the ibm-ucd.jar to the server.
-i --install Perform an full upgrade or install of a server.
-d --daemon Start a server in the background.
-x --debug debug
--no-resolve skips all attempts to do \`ant resolve\`
-h --help show this help
Examples:
Start a server named ibm_ucd_6014:
$PROGNAME --name ibm_ucd_6014
Redeploy the ibm-ucd.jar
$PROGNAME --name ibm_ucd_6014 --redeploy
Perform an upgrade of the server
$PROGNAME --name ibm_ucd_6014 --upgrade
EOF
exit 0
}
parseargs() {
local arg=
for arg
do
local delim=""
case "$arg" in
--name) args="${args}-n ";;
--redeploy) args="${args}-r ";;
--static) args="${args}-s ";;
--debug) args="${args}-x ";;
--install) args="${args}-i ";;
--daemon) args="${args}-d ";;
--no-resolve) NORESOLVE=1;;
--help) args="${args}-h ";;
*) [[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
eval set -- $args
while getopts "hidrxn:" OPTION
do
case $OPTION in
s)
readonly DO_STATIC=1
;;
x)
readonly DEBUG='-x'
set -x
echo "DEBUG MODE ON"
;;
n)
readonly SERVER_NAME=$OPTARG
readonly SERVERDIR=$UCD_SERVERS/$SERVER_NAME
echo "Server dir is $SERVERDIR"
;;
r)
readonly DO_REDEPLOY=1
;;
i)
readonly DO_INSTALL=1
;;
d)
readonly DAEMON=1
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [[ -z $SERVER_NAME ]]
then
echo "No server name supplied" >&2
return 1
fi
return 0
}
run_jslint() {
local gitdir=$PROGDIR/../.git
local worktree=$PROGDIR/..
git --git-dir $gitdir --work-tree $worktree jslint
}
compile() {
local antargs=("compile-main" "-Dresolve.no=1" "-Denun.no=1" "-Ddojo.build.no=1")
run_ant "${antargs[@]}"
}
redeploy() {
compile
copy_jar
deploy_static
}
copy_jar() {
local jarloc=$PROGDIR/../dist/server/*.jar
local serverjar=$SERVERDIR/lib/
cp $jarloc $serverjar
}
deploy_static() {
clean_old_static
copy_new_static
}
clean_old_static() {
local staticdir=$SERVERDIR/opt/tomcat/webapps/ROOT/static/$VERSION/
local jspdir=$SERVERDIR/opt/tomcat/webapps/ROOT/jsps
local webinfdir=$SERVERDIR/opt/tomcat/webapps/ROOT/WEB-INF/
rm -rf $staticdir/docs/ $staticdir/js/deploy/ $staticdir/css/deploy $staticdir/conf
rm -rf $jspdir $webinfdir/web.xml $SERVERDIR/conf/server/ds-navigation.config
}
copy_new_static() {
local localstatic=$PROGDIR/../src/web/war/
local staticdir=$SERVERDIR/opt/tomcat/webapps/ROOT/static/$VERSION/
local webinfdir=$SERVERDIR/opt/tomcat/webapps/ROOT/WEB-INF
cp -R $localstatic/images/ $staticdir
cp -R $localstatic/docs/ $staticdir
cp -R $localstatic/js/deploy/ $staticdir/js/
cp -R $localstatic/css/ $staticdir
cp -R $localstatic/conf/ $staticdir
cp -R $localstatic/WEB-INF/ $webinfdir
cp $PROGDIR/../conf/server/ds-navigation.config $SERVERDIR/conf/server
}
run_ant() {
local antargs=("$@")
local buildxml=$PROGDIR/../build.xml
local antcmd="ant -f $buildxml ${antargs[@]}"
$antcmd
}
run_server() {
echo "Running server..."
local startscript=$SERVERDIR/bin/server
local runcmd="run"
if [[ 1 == DAEMON ]]
then
runcmd="start"
fi
local shell=/bin/bash
exec $shell $startscript $runcmd
}
copy_mysql_jar() {
local jarsrc=$(readlink -m ~/.dev/jdbc/mysql-connector-java-5.1.28-bin.jar)
local jardest=$(find $(readlink -m $PROGDIR/../dist/install) -type d -maxdepth 1)/lib/ext/
cp $jarsrc $jardest
}
check_mysqldb_exists() {
local dbname=$1
mysql -u $mysqluser --password=$mysqlpass $dbname -e ""
return $?
}
create_mysqldb() {
local dbname=$1
mysql -u $mysqluser --password=$mysqlpass -e "create database $dbname;"
return $?
}
unzip_dist() {
local extractdir=$(readlink -m $PROGDIR/../dist/install/)
local zip=$(find $extractdir -iname "*.zip" -maxdepth 1)
unzip -q -d $extractdir $zip
}
update_install_props() {
local readonly dburl=$1
local readonly dbuser=$2
local readonly dbpass=$3
local installprops=$(find $(readlink -m $PROGDIR/../dist/install/) -name "install.properties")
echo "install.properties located at $installprops"
# default properties
local readonly installServerDir=$SERVERDIR
local readonly version=dev
local readonly dbName=TODO #TODO
local readonly databaseType=$DB_TYPE
local readonly hibernateConnectionDriverClass=com.mysql.jdbc.Driver
local readonly hibernateConnectionUrl=$dburl
local readonly hibernateConnectionPassword=$dbpass
local readonly hibernateConnectionUsername=$dbuser
local readonly hibernateDefaultSchema=
local readonly databaseDerbyPort=11377
local readonly skipDbInstall=false
local readonly checkIndexedFks=false
local readonly installServerWebHost=localhost
local readonly serverExternalWebUrl=
local readonly installServerWebIp=0.0.0.0
local readonly installJavaHome=$JAVA_HOME
local readonly installServerWebPort=8080
local readonly installServerWebAlwaysSecure=Y
local readonly installServerWebHttpsPort=8443
cat <<- EOF > $installprops
#### basic ####
component.name=IBM UrbanCode Deploy
component.directory=ibm-ucd/server
version=${version}
install.server.dir=${installServerDir}
#### database ####
database.type=${databaseType}
hibernate.connection.driver_class=${hibernateConnectionDriverClass}
hibernate.connection.url=${hibernateConnectionUrl}
hibernate.connection.password=${hibernateConnectionPassword}
hibernate.connection.username=${hibernateConnectionUsername}
#### database misc ####
database.derby.port=${databaseDerbyPort}
hibernate.default_schema=${hibernateDefaultSchema}
skip.db.install=${skipDbInstall}
check.indexed.fks=${checkIndexedFks}
#### web ####
install.server.web.host=${installServerWebHost}
server.external.web.url=${serverExternalWebUrl}
server.jms.port=${serverJmsPort}
server.jms.mutualAuth=${serverJmsMutualAuth}
install.server.web.ip=${installServerWebIp}
#### keystore ####
encryption.keystore=${encryptionKeystore}
encryption.keystore.alias=${encryptionKeystoreAlias}
#### other ####
rcl.server.url=${rclServerUrl}
nonInteractive=true
EOF
}
create_dist() {
local antargs=("clean" "dist" "-Ddojo.build.no=1" "-Denun.no=1" "-Dtests.no=1")
if [[ 1 == $NORESOLVE ]]
then
antargs[${#antargs[*]}]="-Dresolve.no=1"
fi
run_ant "${antargs[@]}"
}
run_install() {
local installexec=$(find $(readlink -m $PROGDIR/../dist/install/) -name "install-server.sh")
$installexec
}
install_server() {
echo "installing server..."
local readonly dbname=$DB_PREFIX$SERVER_NAME
### FIXME - this shoudl be config
local readonly dbuser=root
local readonly dbpass=password
local readonly dburl=jdbc:mysql://localhost:3306/$dbname
### END FIXME
if [[ "mysql" == $DB_TYPE ]]
then
local readonly exists=$(check_mysqldb_exists $dbname $mysqluser $mysqlpass)
if [[ 0 != exists ]]
then
create_mysqldb $dbname $mysqluser $mysqlpass
fi
fi
create_dist
unzip_dist
update_install_props $dburl $dbuser $dbpass
run_install
}
find_version() {
local verdir=$(readlink -m $SERVERDIR/opt/tomcat/webapps/ROOT/static)
VERSION=$(basename $(find $verdir -maxdepth 1 -type d | tail -1))
echo "Found version $VERSION"
}
main() {
parseargs $ARGS
find_version
if [[ 1 == $DO_INSTALL ]]
then
install_server
run_server
elif [[ 1 == $DO_REDEPLOY ]]
then
redeploy
run_server
elif [[ 1 == $DO_STATIC ]]
then
deploy_static
else
run_server
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment