build.sh 6.27 KB
Newer Older
Misagh Moayyed's avatar
Misagh Moayyed committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash

function copy() {
	echo -e "Creating configuration directory under /etc/cas"
	mkdir -p /etc/cas/config

	echo -e "Copying configuration files from etc/cas to /etc/cas"
	cp -rfv ./etc/cas/* /etc/cas
}

function help() {
	clear
	echo "******************************************************************"
Misagh Moayyed's avatar
Misagh Moayyed committed
14 15
	tput setaf 2
	echo "Apereo CAS ${casVersion}"
Misagh Moayyed's avatar
Misagh Moayyed committed
16
	echo "Enterprise Single SignOn for all earthlings and beyond"
Misagh Moayyed's avatar
Misagh Moayyed committed
17
	tput sgr 0
Misagh Moayyed's avatar
Misagh Moayyed committed
18 19 20 21
	echo "- https://github.com/apereo/cas"
	echo "- https://apereo.github.io/cas"
	echo "******************************************************************"

Misagh Moayyed's avatar
Misagh Moayyed committed
22
	echo -e "Usage: build.sh [copy|clean|package|run|dependencies|docker|getview|listviews|update|debug|tomcat|gencert]\n"
Misagh Moayyed's avatar
Misagh Moayyed committed
23 24 25 26 27
	echo -e "\tclean: \t\tClean Maven build directory"
	echo -e "\tcli: \t\tRun the CAS command line shell and pass commands"
	echo -e "\tcopy: \t\tCopy config from the project's local etc/cas/config directory to the root /etc/cas/config"
	echo -e "\tdebug: \t\tRun cas.war and listen for Java debugger on port 5000"
	echo -e "\tdependencies: \tGet a report of all dependencies configured in the build"
Misagh Moayyed's avatar
Misagh Moayyed committed
28
	echo -e "\tdocker: \tBuild a Docker image based on the current build and configuration"
Misagh Moayyed's avatar
Misagh Moayyed committed
29 30 31 32 33 34 35 36
	echo -e "\tgencert: \tCreate keystore with SSL certificate in location where CAS looks by default"
	echo -e "\tgetview: \tAsk for a view name to be included in the overlay for customizations"
	echo -e "\tlistviews: \tList all CAS views that ship with the web application and can be customized in the overlay"
	echo -e "\tpackage: \tClean and build CAS war"
	echo -e "\trun: \t\tBuild and run cas.war via Java as an executable war"
	echo -e "\trunalone: \tBuild and run cas.war on its own as a standalone executable"
	echo -e "\ttomcat: \tDeploy the CAS web application to an external Apache Tomcat server"
	echo -e "\tupdate: \tPackage the CAS overlay by force-updating dependencies and SNAPSHOT versions"
Misagh Moayyed's avatar
Misagh Moayyed committed
37 38 39
}

function clean() {
Misagh Moayyed's avatar
Misagh Moayyed committed
40
	./gradlew clean "$@"
Misagh Moayyed's avatar
Misagh Moayyed committed
41 42 43
}

function package() {
Misagh Moayyed's avatar
Misagh Moayyed committed
44
	./gradlew clean build "$@"
Misagh Moayyed's avatar
Misagh Moayyed committed
45 46 47
}

function update() {
Misagh Moayyed's avatar
Misagh Moayyed committed
48
	./gradlew clean build --refresh-dependencies "$@"
Misagh Moayyed's avatar
Misagh Moayyed committed
49 50 51
}

function dependencies() {
Misagh Moayyed's avatar
Misagh Moayyed committed
52
	./gradlew allDependencies
Misagh Moayyed's avatar
Misagh Moayyed committed
53 54 55
}

function tomcat() {
Misagh Moayyed's avatar
Misagh Moayyed committed
56
	./gradlew clean build -Pexternal=true "$@"
Misagh Moayyed's avatar
Misagh Moayyed committed
57 58 59 60 61 62 63 64

	if [ ! -f apache-tomcat.zip ]; then
		wget -O apache-tomcat.zip "http://www-eu.apache.org/dist/tomcat/tomcat-${tomcatVersion}/v${tomcatFullVersion}/bin/apache-tomcat-${tomcatFullVersion}.zip"
	fi
	rm -Rf ./apache-tomcat
	unzip -o apache-tomcat.zip >/dev/null
	mv apache-tomcat-${tomcatFullVersion} apache-tomcat

Misagh Moayyed's avatar
Misagh Moayyed committed
65
	export CATALINA_HOME=./apache-tomcat/
Misagh Moayyed's avatar
Misagh Moayyed committed
66
	chmod +x ./apache-tomcat/bin/*.sh
Misagh Moayyed's avatar
Misagh Moayyed committed
67 68 69
    echo "Attempting to shutdown Apache Tomcat..."
    ./apache-tomcat/bin/shutdown.sh 2>/dev/null

Misagh Moayyed's avatar
Misagh Moayyed committed
70
	cp build/libs/cas.war ../apache-tomcat/webapps/
Misagh Moayyed's avatar
Misagh Moayyed committed
71
	./apache-tomcat/bin/startup.sh
Misagh Moayyed's avatar
Misagh Moayyed committed
72
	tail -F ../apache-tomcat/logs/catalina.out
Misagh Moayyed's avatar
Misagh Moayyed committed
73 74 75
}

function debug() {
Misagh Moayyed's avatar
Misagh Moayyed committed
76
	casWar="build/libs/cas.war"
Misagh Moayyed's avatar
Misagh Moayyed committed
77 78 79 80
	package && java -Xdebug -Xrunjdwp:transport=dt_socket,address=5000,server=y,suspend=n -jar $casWar
}

function run() {
Misagh Moayyed's avatar
Misagh Moayyed committed
81
	casWar="build/libs/cas.war"
Misagh Moayyed's avatar
Misagh Moayyed committed
82 83 84 85
	package && java -XX:TieredStopAtLevel=1 -Xverify:none -jar $casWar
}

function runalone() {
Misagh Moayyed's avatar
Misagh Moayyed committed
86 87
	./gradlew clean build -Pexecutable=true "$@"
	casWar="build/libs/cas.war"
Misagh Moayyed's avatar
Misagh Moayyed committed
88 89 90 91
	chmod +x $casWar
   	$casWar
}

Misagh Moayyed's avatar
Misagh Moayyed committed
92 93 94 95
function jibdocker() {
   ./gradlew clean build jibDockerBuild "$@"
}

Misagh Moayyed's avatar
Misagh Moayyed committed
96
function listviews() {
Misagh Moayyed's avatar
Misagh Moayyed committed
97 98
	explodeApp
	explodedDir=build/cas
Misagh Moayyed's avatar
Misagh Moayyed committed
99 100 101
	find $explodedDir -type f -name "*.html" | xargs -n 1 basename | sort | more
}

Misagh Moayyed's avatar
Misagh Moayyed committed
102 103
function explodeApp() {
	./gradlew explodeWar
Misagh Moayyed's avatar
Misagh Moayyed committed
104 105 106 107
	echo "Exploded the CAS web application file."
}

function getview() {
Misagh Moayyed's avatar
Misagh Moayyed committed
108 109
	explodeApp

Misagh Moayyed's avatar
Misagh Moayyed committed
110
	echo "Searching for view name $@..."
Misagh Moayyed's avatar
Misagh Moayyed committed
111
	explodedDir=build/cas
Misagh Moayyed's avatar
Misagh Moayyed committed
112 113 114 115 116 117 118 119 120 121

	results=`find $explodedDir -type f -name "*.html" | grep -i "$@"`
	count=`wc -w <<< "$results"`

	if [ "$count" -eq 0 ];then
		echo "No views could be found matching $@"
		exit 1
	fi
	echo -e "Found view(s): \n$results"
	if [ "$count" -eq 1 ];then
Misagh Moayyed's avatar
Misagh Moayyed committed
122
		fromFile="build/cas/WEB-INF/classes"
Misagh Moayyed's avatar
Misagh Moayyed committed
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
		toFile="src/main/resources"

		overlayfile=`echo "${results/$fromFile/$toFile}"`
		overlaypath=`dirname "${overlayfile}"`
		# echo "Overlay file is $overlayfile to be created at $overlaypath"
		mkdir -p $overlaypath
		cp $results $overlaypath
		echo "Created view at $overlayfile"
		ls $overlayfile
	else
		echo "More than one view file is found. Narrow down the search query..."
	fi
}

function gencert() {
	if [[ ! -d /etc/cas ]] ; then
		copy
	fi
	which keytool
	if [[ $? -ne 0 ]] ; then
		echo Error: Java JDK \'keytool\' is not installed or is not in the path
		exit 1
	fi
	# override DNAME and CERT_SUBJ_ALT_NAMES before calling or use dummy values
	DNAME="${DNAME:-CN=cas.example.org,OU=Example,OU=Org,C=US}"
	CERT_SUBJ_ALT_NAMES="${CERT_SUBJ_ALT_NAMES:-dns:example.org,dns:localhost,ip:127.0.0.1}"
	echo "Generating keystore for CAS with DN ${DNAME}"
	keytool -genkeypair -alias cas -keyalg RSA -keypass changeit -storepass changeit -keystore /etc/cas/thekeystore -dname ${DNAME} -ext SAN=${CERT_SUBJ_ALT_NAMES}
	keytool -exportcert -alias cas -storepass changeit -keystore /etc/cas/thekeystore -file /etc/cas/cas.cer
}

function cli() {
	rm -f *.log
Misagh Moayyed's avatar
Misagh Moayyed committed
156
	CAS_VERSION=$(./gradlew casVersion --quiet)
Misagh Moayyed's avatar
Misagh Moayyed committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

	echo "CAS version: $CAS_VERSION"
	JAR_FILE_NAME="cas-server-support-shell-${CAS_VERSION}.jar"
	echo "JAR name: $JAR_FILE_NAME"
	JAR_PATH="org/apereo/cas/cas-server-support-shell/${CAS_VERSION}/${JAR_FILE_NAME}"
	echo "JAR path: $JAR_PATH"

	JAR_FILE_LOCAL="$HOME/.m2/repository/$JAR_PATH";
	echo "Local JAR file path: $JAR_FILE_LOCAL";
	if [ -f "$JAR_FILE_LOCAL" ]; then
		echo "Using JAR file locally at $JAR_FILE_LOCAL"
		java -jar $JAR_FILE_LOCAL "$@"
		exit 0;
	fi

Misagh Moayyed's avatar
Misagh Moayyed committed
172
	DOWNLOAD_DIR=./build/libs
Misagh Moayyed's avatar
Misagh Moayyed committed
173 174 175 176 177 178 179 180 181 182 183 184 185

	COMMAND_FILE="${DOWNLOAD_DIR}/${JAR_FILE_NAME}"
	if [ ! -f "$COMMAND_FILE" ]; then
		mkdir -p $DOWNLOAD_DIR
		wget "https://repo1.maven.org/maven2/${JAR_PATH}" -P ./target
	fi

	echo "Running $COMMAND_FILE"
	java -jar $COMMAND_FILE "$@"
	exit 0;

}

Misagh Moayyed's avatar
Misagh Moayyed committed
186
command=$1
Misagh Moayyed's avatar
Misagh Moayyed committed
187 188 189 190 191 192

if [ -z "$command" ]; then
    echo "No commands provided. Defaulting to [run]"
	command="run"
fi

Misagh Moayyed's avatar
Misagh Moayyed committed
193
shift 1
Misagh Moayyed's avatar
Misagh Moayyed committed
194 195 196 197 198 199 200 201

case "$command" in
"copy")
    copy
    ;;
"help")
    help
    ;;
Misagh Moayyed's avatar
Misagh Moayyed committed
202 203 204 205 206 207 208 209 210 211 212 213
"clean")
	clean "$@"
	;;
"package"|"build")
	package "$@"
	;;
"debug")
	debug "$@"
	;;
"run")
	run "$@"
	;;
Misagh Moayyed's avatar
Misagh Moayyed committed
214 215 216
"docker")
	jibdocker "$@"
	;;
Misagh Moayyed's avatar
Misagh Moayyed committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
"gencert")
	gencert "$@"
	;;
"cli")
	cli "$@"
	;;
"update")
	update "$@"
	;;
"dependencies")
	update "$@"
	;;
"runalone")
	runalone "$@"
	;;
"listviews")
	listviews "$@"
	;;
"getview")
	getview "$@"
	;;
"tomcat")
	tomcat
	;;
Misagh Moayyed's avatar
Misagh Moayyed committed
241
esac